Skip to content

Commit

Permalink
Luacontroller: Add configurable size limit for mem table, 100kBytes b…
Browse files Browse the repository at this point in the history
…y default, fixes #240

If size limit is exceeded, the luacontroller overheats. Overheating will cause the memory
to be erased.
  • Loading branch information
Jeija committed Oct 18, 2015
1 parent 97f7b57 commit b89fe52
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions mesecons_luacontroller/init.lua
Expand Up @@ -163,19 +163,18 @@ end
-----------------
-- Overheating --
-----------------

local function overheat_off(pos)
mesecon.receptor_off(pos, mesecon.rules.flat)
local function burn_controller(pos)
local node = minetest.get_node(pos)
node.name = BASENAME.."_burnt"
minetest.swap_node(pos, node)
minetest.get_meta(pos):set_string("lc_memory", "");
-- Wait for pending operations
minetest.after(0.2, mesecon.receptor_off, pos, mesecon.rules.flat)
end


local function overheat(pos, meta)
if mesecon.do_overheat(pos) then -- If too hot
local node = minetest.get_node(pos)
node.name = BASENAME.."_burnt"
minetest.swap_node(pos, node)
-- Wait for pending operations
minetest.after(0.2, overheat_off, pos)
burn_controller(pos)
return true
end
end
Expand Down Expand Up @@ -379,12 +378,17 @@ local function load_memory(meta)
end


local function save_memory(meta, mem)
meta:set_string("lc_memory",
minetest.serialize(
remove_functions(mem)
)
)
local function save_memory(pos, meta, mem)
local memstring = minetest.serialize(remove_functions(mem))
local memsize_max = mesecon.setting("luacontroller_memsize", 100000)

if (#memstring <= memsize_max) then
meta:set_string("lc_memory", memstring)
else
print("Error: Luacontroller memory overflow. "..memsize_max.." bytes available, "
..#memstring.." required. Controller overheats.")
burn_controller(pos)
end
end


Expand Down Expand Up @@ -412,15 +416,17 @@ local function run(pos, event)
return "Ports set are invalid."
end

save_memory(meta, env.mem)

-- Actually set the ports
set_port_states(pos, env.port)

-- Save memory. This may burn the luacontroller if a memory overflow occurs.
save_memory(pos, meta, env.mem)
end

mesecon.queue:add_function("lc_interrupt", function (pos, luac_id, iid)
-- There is no luacontroller anymore / it has been reprogrammed / replaced
-- There is no luacontroller anymore / it has been reprogrammed / replaced / burnt
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
if (minetest.registered_nodes[minetest.get_node(pos).name].is_burnt) then return end
run(pos, {type="interrupt", iid = iid})
end)

Expand Down Expand Up @@ -595,6 +601,7 @@ minetest.register_node(BASENAME .. "_burnt", {
"jeija_microcontroller_sides.png"
},
inventory_image = "jeija_luacontroller_burnt_top.png",
is_burnt = true,
paramtype = "light",
groups = {dig_immediate=2, not_in_creative_inventory=1},
drop = BASENAME.."0000",
Expand Down

0 comments on commit b89fe52

Please sign in to comment.