|
1 |
|
2 classes_editable = setmetatable({}, {__mode = "v", __index = function (t, k) return GetEditableClass(k) or {}; end, __newindex = function (t, k, v) return SaveEditableClass(k, v); end }); |
|
3 moobox = moobox or {}; |
|
4 |
|
5 --Load it from file |
|
6 function GetEditableClass(name) |
|
7 local subenv = { classes = { } } |
|
8 local classdefinition = loadfile("objects/"..param..".lua"); |
|
9 setfenv(classdefinition, subenv); |
|
10 local success, message = pcall(classdefinition); |
|
11 if not success then return message or "Unkown error reading class" |
|
12 else return subenv.class; end |
|
13 end |
|
14 |
|
15 --Save it to file |
|
16 function SaveEditableClass(name, t) |
|
17 function writetable(filename, t) |
|
18 local f = io.open(filename, "w+"); |
|
19 if not f then return nil; end |
|
20 for k, v in pairs(t) do |
|
21 if type(v) == "string" then |
|
22 f:write(string.format("class[\"%s\"] = \"%s\";\n", k, v)); |
|
23 end |
|
24 end |
|
25 end |
|
26 |
|
27 local success, message = pcall(writetable, string.format("editor/%s.dat", name), t); |
|
28 if not success then return message; end |
|
29 return nil; |
|
30 end |
|
31 |
|
32 --Compile (and the compiled class goes to classes[name] |
|
33 function CompileEditableClass(t) |
|
34 if not t then return "Error compiling... no (or invalid) class specified"; end |
|
35 local compiled = {}; |
|
36 for k, v in pairs(t) do |
|
37 local success, compiledverb = pcall(loadstring, v); |
|
38 if not success then return "Error compiling verb '"..k.."': "..(compiledverb or "Unkown error"); end |
|
39 compiled[k] = (compiledverb and setfenv(compiledverb, verbbox)) or nil; |
|
40 end |
|
41 return compiled; |
|
42 end |