Skip to content

Commit c1d6693

Browse files
committed
Added textutils
1 parent 5195949 commit c1d6693

File tree

4 files changed

+321
-39
lines changed

4 files changed

+321
-39
lines changed

src/ccapi.lua

+48-36
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
along with CCAPI. If not, see <http://www.gnu.org/licenses/>.
1717
]]
1818

19+
local lfs = require("lfs")
20+
21+
local modname = ...
22+
1923
local M = {
2024
util={},
21-
version="CraftOS 1.63"
25+
ccversion="CraftOS 1.63",
26+
version="CCAPI 0.0.0",
2227
}
2328

2429
if ccapi_DebugKernel then
@@ -67,15 +72,21 @@ do
6772
end
6873
end
6974

70-
-- Prepare a _G clone
75+
-- Prepare basic env
7176
M.prepareEnv = function(ccEnv, eventQueue)
7277
local select,type,error = select,type,error
7378
local setfenv,getfenv = setfenv,getfenv
7479
local cyield = coroutine.yield
7580

7681
local luapath = package.path
77-
local dirsep = package.config:match("^([^\n]*)") -- 1st line
78-
local pathsep = package.config:match("^[^\n]*\n([^\n]*)") -- 2nd line
82+
local pc = {} -- pc = "package config"
83+
-- ds = "directory separator"
84+
-- ps = "path separator"
85+
-- np = "name point"
86+
-- ed = "executable directory"
87+
-- im = "ignore mark"
88+
pc.ds,pc.ps,pc.np,pc.ed,pc.im = package.config:match("^([^\n]*)\n([^\n]*)\n([^\n]*)\n([^\n]*)\n([^\n]*)")
89+
7990

8091
-- make it so every new function has ccEnv as the env
8192
setfenv(1,ccEnv)
@@ -98,16 +109,6 @@ M.prepareEnv = function(ccEnv, eventQueue)
98109
end
99110
end
100111

101-
ccEnv.getfenv = function(f)
102-
if getfenv(f) == getfenv(0) then
103-
return ccEnv
104-
elseif type(f) == "number" then
105-
return getfenv(f+1)
106-
else
107-
return getfenv(f)
108-
end
109-
end
110-
111112
if not ccEnv.os then
112113
ccEnv.os = {}
113114
end
@@ -121,6 +122,7 @@ M.prepareEnv = function(ccEnv, eventQueue)
121122

122123
do
123124
-- No I'm not gonna take 5 parameters like a fucking PEASANT!
125+
-- TAKE THAT, COMPUTERCRAFT!
124126
local function parseEvent(evt, ...)
125127
if evt == "terminate" then
126128
error("Terminated")
@@ -132,37 +134,47 @@ M.prepareEnv = function(ccEnv, eventQueue)
132134
end
133135
end
134136

135-
for x in luapath:gmatch("([^" .. pathsep .. "]+)") do
136-
local path = x
137-
path = path:gsub("?","ccapi"):gsub("ccapi.lua","kernel.lua")
138-
local f,e = loadfile(path)
139-
if f then
140-
local s,e = pcall(f, M, path)
141-
if s then
142-
break
137+
setfenv(1,getfenv(0))
138+
139+
return ccEnv, eventQueue,function()
140+
setfenv(1,ccEnv)
141+
142+
-- TODO properly
143+
for x in luapath:gmatch("([^" .. pc.ps .. "]+)") do
144+
local path = x:gsub("%" .. pc.np, (modname:gsub("%.", pc.ds))):gsub("^%./", assert(lfs.currentdir()):gsub("%%","%%%%") .. "/")
145+
local f,e = io.open(path,"r")
146+
if f then
147+
f:close()
148+
local p1, p2 = path:match("(.*)" .. pc.ds .. "(.*)$")
149+
path = p1 .. p2:gsub(modname:match("%.?([^%.]*)$"),"kernel")
150+
local f,e = loadfile(path)
151+
if f then
152+
local s,e = pcall(f, M, path)
153+
if s then
154+
break
155+
end
156+
if M.debug and (e or not s) then
157+
print(s,e)
158+
end
159+
end
160+
if M.debug and (e or not f) then
161+
print(f,e)
162+
end
143163
end
144-
if M.debug and (e or not s) then
145-
print(s,e)
164+
if M.debug and (e or not f) then
165+
print(f,e)
146166
end
147167
end
148-
if M.debug and (e or not f) then
149-
print(f,e)
150-
end
151-
end
152168

153-
setfenv(1,getfenv(0))
154-
return ccEnv, eventQueue
169+
setfenv(1,getfenv(0))
170+
end
155171
end
156172

157173
function M.runCC(func, env, eventQueue, id)
158-
if env then
159-
setfenv(func, env)
160-
env, eventQueue = M.prepareEnv(env or M.simplecopy(_G), eventQueue or {})
161-
end
162-
if not eventQueue then eventQueue = {} end
174+
local env, eventQueue, loadkernel = M.prepareEnv(env or M.simplecopy(_G), eventQueue or {})
163175
-- main loop
164176
while true do
165-
177+
-- TODO
166178
end
167179
end
168180

src/demo.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
ccapi_DebugKernel = true -- enable CCAPI debug mode
2020
local ccapi = require("ccapi")
2121

22-
local ccEnv, eventQueue = ccapi.prepareEnv(ccapi.simplecopy(_G), {})
22+
local ccEnv, eventQueue, loadkernel = ccapi.prepareEnv(ccapi.simplecopy(_G), {})
23+
loadkernel()

src/kernel.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
along with CCAPI. If not, see <http://www.gnu.org/licenses/>.
1717
]]
1818

19+
-- This file is like CC's bios.lua but better...
20+
1921
local M, path = ...
2022

2123
if not M then print("Use ccapi") return end
@@ -25,15 +27,17 @@ print("Loading CCAPI kernel!")
2527
if M.debug then
2628
local debugstring = [[
2729
28-
CCAPI Version: ${CC};
30+
CC Version: ${CC};
31+
CCAPI Version: ${API};
2932
Lua Version: ${LUA};
3033
-- TODO add more info
3134
]]
3235
print((debugstring:gsub(
3336
"${(.-)}",
3437
{
3538
LUA = _VERSION,
36-
CC = M.version,
39+
CC = M.ccversion,
40+
API = M.version,
3741
}
3842
)))
3943
end

0 commit comments

Comments
 (0)