diff -r 000000000000 -r cc66ad6b0d75 editing.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/editing.lua Sat Mar 27 17:43:08 2010 +0000 @@ -0,0 +1,42 @@ + +classes_editable = setmetatable({}, {__mode = "v", __index = function (t, k) return GetEditableClass(k) or {}; end, __newindex = function (t, k, v) return SaveEditableClass(k, v); end }); +moobox = moobox or {}; + +--Load it from file +function GetEditableClass(name) + local subenv = { classes = { } } + local classdefinition = loadfile("objects/"..param..".lua"); + setfenv(classdefinition, subenv); + local success, message = pcall(classdefinition); + if not success then return message or "Unkown error reading class" + else return subenv.class; end +end + +--Save it to file +function SaveEditableClass(name, t) + function writetable(filename, t) + local f = io.open(filename, "w+"); + if not f then return nil; end + for k, v in pairs(t) do + if type(v) == "string" then + f:write(string.format("class[\"%s\"] = \"%s\";\n", k, v)); + end + end + end + + local success, message = pcall(writetable, string.format("editor/%s.dat", name), t); + if not success then return message; end + return nil; +end + +--Compile (and the compiled class goes to classes[name] +function CompileEditableClass(t) + if not t then return "Error compiling... no (or invalid) class specified"; end + local compiled = {}; + for k, v in pairs(t) do + local success, compiledverb = pcall(loadstring, v); + if not success then return "Error compiling verb '"..k.."': "..(compiledverb or "Unkown error"); end + compiled[k] = (compiledverb and setfenv(compiledverb, verbbox)) or nil; + end + return compiled; +end \ No newline at end of file