1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-10 12:29:03 -08:00

remember modified values (add serialization)

This commit is contained in:
Connor Olding 2015-05-01 08:20:04 -07:00
parent ee5b7122ff
commit 46b42a0421
2 changed files with 79 additions and 7 deletions

View file

@ -1,5 +1,6 @@
require "boilerplate" require "boilerplate"
require "addrs.init" require "addrs.init"
require "serialize"
local blocknames = { local blocknames = {
'R ', 'RS', 'RO', 'RP', 'R ', 'RS', 'RO', 'RP',
@ -28,11 +29,8 @@ function ShortMonitor:init(name, a)
self.once = false self.once = false
self.old_bytes = {} self.old_bytes = {}
local modified = {} self.modified = {}
for i=0, self.len/2 do self.dirty = false
modified[i] = false
end
self.modified = modified
end end
function ShortMonitor:diff() function ShortMonitor:diff()
@ -54,8 +52,9 @@ end
function ShortMonitor:mark(i, x, x1) function ShortMonitor:mark(i, x, x1)
local ih = math.floor(i/2) local ih = math.floor(i/2)
if self.modified[ih] == false then if not self.modified[ih] then
self.modified[ih] = true self.modified[ih] = true
self.dirty = true
local block, page, row = butts(ih) local block, page, row = butts(ih)
printf('%2s Page %1i Row %3i', blocknames[block+1], page+1, row+1) printf('%2s Page %1i Row %3i', blocknames[block+1], page+1, row+1)
end end
@ -69,7 +68,7 @@ function ShortMonitor:dump()
local mod = self.modified[ih] local mod = self.modified[ih]
local value = R2(self.begin + ih) local value = R2(self.begin + ih)
local vs = mod and '[variable]' or ('%04X'):format(value) 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 buff = buff..s
end end
print(buff) print(buff)
@ -79,7 +78,13 @@ end
-- = 5568 bytes (0x15C0) -- = 5568 bytes (0x15C0)
me = ShortMonitor('me', A(0x210A24, 0x15C0)) me = ShortMonitor('me', A(0x210A24, 0x15C0))
me.modified = deserialize('_ootmemod.lua') or {}
while true do while true do
me:diff() me:diff()
if me.dirty then
serialize(me.modified, ('_ootmemod.lua'))
me.dirty = false
end
emu.frameadvance() emu.frameadvance()
end end

67
Lua/serialize.lua Normal file
View file

@ -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