16
16
along with CCAPI. If not, see <http://www.gnu.org/licenses/>.
17
17
]]
18
18
19
+ local lfs = require (" lfs" )
20
+
21
+ local modname = ...
22
+
19
23
local M = {
20
24
util = {},
21
- version = " CraftOS 1.63"
25
+ ccversion = " CraftOS 1.63" ,
26
+ version = " CCAPI 0.0.0" ,
22
27
}
23
28
24
29
if ccapi_DebugKernel then
67
72
end
68
73
end
69
74
70
- -- Prepare a _G clone
75
+ -- Prepare basic env
71
76
M .prepareEnv = function (ccEnv , eventQueue )
72
77
local select ,type ,error = select ,type ,error
73
78
local setfenv ,getfenv = setfenv ,getfenv
74
79
local cyield = coroutine.yield
75
80
76
81
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
+
79
90
80
91
-- make it so every new function has ccEnv as the env
81
92
setfenv (1 ,ccEnv )
@@ -98,16 +109,6 @@ M.prepareEnv = function(ccEnv, eventQueue)
98
109
end
99
110
end
100
111
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
-
111
112
if not ccEnv .os then
112
113
ccEnv .os = {}
113
114
end
@@ -121,6 +122,7 @@ M.prepareEnv = function(ccEnv, eventQueue)
121
122
122
123
do
123
124
-- No I'm not gonna take 5 parameters like a fucking PEASANT!
125
+ -- TAKE THAT, COMPUTERCRAFT!
124
126
local function parseEvent (evt , ...)
125
127
if evt == " terminate" then
126
128
error (" Terminated" )
@@ -132,37 +134,47 @@ M.prepareEnv = function(ccEnv, eventQueue)
132
134
end
133
135
end
134
136
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
143
163
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 )
146
166
end
147
167
end
148
- if M .debug and (e or not f ) then
149
- print (f ,e )
150
- end
151
- end
152
168
153
- setfenv (1 ,getfenv (0 ))
154
- return ccEnv , eventQueue
169
+ setfenv (1 ,getfenv (0 ))
170
+ end
155
171
end
156
172
157
173
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 {})
163
175
-- main loop
164
176
while true do
165
-
177
+ -- TODO
166
178
end
167
179
end
168
180
0 commit comments