editing.lua

Sat, 27 Mar 2010 17:43:08 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 27 Mar 2010 17:43:08 +0000
changeset 0
cc66ad6b0d75
permissions
-rw-r--r--

Initial commit (importing from old SVN repo which got lost)


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

mercurial