diff options
Diffstat (limited to 'mesecons_luacontroller')
| -rw-r--r-- | mesecons_luacontroller/init.lua | 70 | 
1 files changed, 49 insertions, 21 deletions
diff --git a/mesecons_luacontroller/init.lua b/mesecons_luacontroller/init.lua index 3016014..0884244 100644 --- a/mesecons_luacontroller/init.lua +++ b/mesecons_luacontroller/init.lua @@ -100,17 +100,17 @@ local heat = function (meta) -- warm up  	end  end -local cool = function (meta) -- cool down after a while -	h = meta:get_int("heat") -	if h ~= nil then -		meta:set_int("heat", h - 1) -	end -end +--local cool = function (meta) -- cool down after a while +--	h = meta:get_int("heat") +--	if h ~= nil then +--		meta:set_int("heat", h - 1) +--	end +--end  local overheat = function (meta) -- determine if too hot  	h = meta:get_int("heat")  	if h == nil then return true end -- if nil then overheat -	if h > 20 then  +	if h > 40 then   		return true  	else   		return false  @@ -127,7 +127,7 @@ end  local code_prohibited = function(code)  	-- Clean code -	local prohibited = {"while", "for", "repeat", "until", "function"} +	local prohibited = {"while", "for", "repeat", "until", "function", "goto"}  	for _, p in ipairs(prohibited) do  		if string.find(code, p) then  			return "Prohibited command: "..p @@ -135,10 +135,34 @@ local code_prohibited = function(code)  	end  end -local safeprint = function(param) +local safe_print = function(param)  	print(dump(param))  end +deep_copy = function(original, visited) --deep copy that removes functions +	visited = visited or {} +	if visited[original] ~= nil then --already visited this node +		return visited[original] +	end +	if type(original) == 'table' then --nested table +		local copy = {} +		visited[original] = copy +		for key, value in next, original, nil do +			copy[deep_copy(key, visited)] = deep_copy(value, visited) +		end +		setmetatable(copy, deep_copy(getmetatable(original), visited)) +		return copy +	elseif type(original) == 'function' then --ignore functions +		return nil +	else --by-value type +		return original +	end +end + +local safe_serialize = function(value) +	return minetest.serialize(deep_copy(value)) +end +  local interrupt = function(params)  	lc_update(params.pos, {type="interrupt", iid = params.iid})  end @@ -150,15 +174,16 @@ local getinterrupt = function(pos)  		local meta = minetest.env:get_meta(pos)  		local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}  		local found = false +		local search = safe_serialize(iid)  		for _, i in ipairs(interrupts) do -			if minetest.serialize(i) == minetest.serialize(iid) then +			if safe_serialize(i) == search then  				found = true  				break  			end  		end  		if not found then  			table.insert(interrupts, iid) -			meta:set_string("lc_interrupts", minetest.serialize(interrupts)) +			meta:set_string("lc_interrupts", safe_serialize(interrupts))  		end  		minetest.after(time, interrupt, {pos=pos, iid = iid})  	end @@ -181,7 +206,7 @@ local create_environment = function(pos, mem, event)  	local rports = get_real_portstates(pos)  	return { -			print = safeprint, +			print = safe_print,  			pin = merge_portstates(vports, rports),  			port = vports,  			interrupt = getinterrupt(pos), @@ -258,7 +283,7 @@ end  local do_overheat = function (pos, meta)  	-- Overheat protection  	heat(meta) -	minetest.after(0.5, cool, meta) +	--minetest.after(0.5, cool, meta)  	if overheat(meta) then  		mesecon:swap_node(pos, BASENAME.."_burnt")  		minetest.env:get_meta(pos):set_string("lc_interrupts", "") @@ -272,15 +297,16 @@ local load_memory = function(meta)  end  local save_memory = function(meta, mem) -	meta:set_string("lc_memory", minetest.serialize(mem)) +	meta:set_string("lc_memory", safe_serialize(mem))  end  local interrupt_allow = function (meta, event)  	if event.type ~= "interrupt" then return true end  	local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {} +	local search = safe_serialize(event.iid)  	for _, i in ipairs(interrupts) do -		if minetest.serialize(i) == minetest.serialize(event.iid) then +		if safe_serialize(i) == search then  			return true  		end  	end @@ -354,10 +380,12 @@ end  --        ______  --       | --- |   | | --- |___| |        __       ___  _   __         _  _ --- |     |       |  | |\ |  |  |_| |  | |  |  |_ |_| --- |     |______ |__| | \|  |  | \ |__| |_ |_ |_ |\ +--       | +--       |        __       ___  _   __         _  _ +-- |   | |       |  | |\ |  |  |_| |  | |  |  |_ |_| +-- |___| |______ |__| | \|  |  | \ |__| |_ |_ |_ |\ +-- | +-- |  --  ----------------------- @@ -412,9 +440,9 @@ if d == 1 then  end  if a + b + c + d ~= 0 then -	groups = {dig_immediate=2, not_in_creative_inventory=1} +	groups = {dig_immediate=2, not_in_creative_inventory=1, overheat = 1}  else -	groups = {dig_immediate=2} +	groups = {dig_immediate=2, overheat = 1}  end  output_rules[cid] = {}  | 
