diff --git a/Lua/oot memory editor monitor.lua b/Lua/oot memory editor monitor.lua index 18dc369..29ce6ee 100644 --- a/Lua/oot memory editor monitor.lua +++ b/Lua/oot memory editor monitor.lua @@ -1,5 +1,6 @@ require "boilerplate" require "addrs.init" +require "serialize" local blocknames = { 'R ', 'RS', 'RO', 'RP', @@ -28,11 +29,8 @@ function ShortMonitor:init(name, a) self.once = false self.old_bytes = {} - local modified = {} - for i=0, self.len/2 do - modified[i] = false - end - self.modified = modified + self.modified = {} + self.dirty = false end function ShortMonitor:diff() @@ -54,8 +52,9 @@ end function ShortMonitor:mark(i, x, x1) local ih = math.floor(i/2) - if self.modified[ih] == false then + if not self.modified[ih] then self.modified[ih] = true + self.dirty = true local block, page, row = butts(ih) printf('%2s Page %1i Row %3i', blocknames[block+1], page+1, row+1) end @@ -69,7 +68,7 @@ function ShortMonitor:dump() local mod = self.modified[ih] local value = R2(self.begin + ih) local vs = mod and '[variable]' or ('%04X'):format(value) - local s = ('%2X\t%2i\t%i\t%s\n'):format(block, page+1, row+1, vs) + local s = ('%02X\t%i\t%i\t%s\n'):format(block, page+1, row+1, vs) buff = buff..s end print(buff) @@ -79,7 +78,13 @@ end -- = 5568 bytes (0x15C0) me = ShortMonitor('me', A(0x210A24, 0x15C0)) +me.modified = deserialize('_ootmemod.lua') or {} + while true do me:diff() + if me.dirty then + serialize(me.modified, ('_ootmemod.lua')) + me.dirty = false + end emu.frameadvance() end diff --git a/Lua/serialize.lua b/Lua/serialize.lua new file mode 100644 index 0000000..1603d06 --- /dev/null +++ b/Lua/serialize.lua @@ -0,0 +1,67 @@ +-- it's simple, dumb, unsafe, incomplete, and it gets the damn job done + +local type = type +local pairs = pairs +local tostring = tostring +local open = io.open +local strfmt = string.format +local strrep = string.rep + +function kill_bom(s) + if #s >= 3 and s:byte(1)==0xEF and s:byte(2)==0xBB and s:byte(3)==0xBF then + return s:sub(4) + end + return s +end + +function sanitize(v) + return type(v) == 'string' and strfmt('%q', v) or tostring(v) +end + +function _serialize(value, writer, level) + level = level or 1 + if type(value) == 'table' then + local indent = strrep('\t', level) + writer('{\n') + for key,value in pairs(value) do + local sane = sanitize(key) + local keyval = sane == '"'..key..'"' and key or '['..sane..']' + writer(indent..keyval..' = ') + _serialize(value, writer, level + 1) + writer(',\n') + end + writer(strrep('\t', level - 1)..'}') + else + writer(sanitize(value)) + end +end + +function _deserialize(script) + local f = loadstring(kill_bom(script)) + if f ~= nil then + return f() + else + print('WARNING: no function deserialize with') + return nil + end +end + +function serialize(value, path) + local file = open(path, 'w') + if not file then return end + file:write("return ") + _serialize(value, function(...) + file:write(...) + end) + file:write("\n") + file:close() +end + +function deserialize(path) + local file = open(path, 'r') + if not file then return end + local script = file:read('*a') + local value = _deserialize(script) + file:close() + return value +end