From 978ab32a11e663b2571c9f72ec43114e2eabf2c5 Mon Sep 17 00:00:00 2001 From: cracyc Date: Fri, 8 Apr 2016 11:59:32 -0500 Subject: [PATCH] plugins/cheatfind: start lua cheat finder library [Carl] --- plugins/cheatfind/init.lua | 98 +++++++++++++++++++++++++++++++++++ plugins/cheatfind/plugin.json | 10 ++++ 2 files changed, 108 insertions(+) create mode 100644 plugins/cheatfind/init.lua create mode 100644 plugins/cheatfind/plugin.json diff --git a/plugins/cheatfind/init.lua b/plugins/cheatfind/init.lua new file mode 100644 index 00000000000..a4334b67aba --- /dev/null +++ b/plugins/cheatfind/init.lua @@ -0,0 +1,98 @@ +-- license:BSD-3-Clause +-- copyright-holders:Carl +-- This is a library of functions to be used at the Lua console as cf.getspaces() etc... +local exports = {} +exports.name = "cheatfind" +exports.version = "0.0.1" +exports.description = "Cheat finder helper library" +exports.license = "The BSD 3-Clause License" +exports.author = { name = "Carl" } + +local cheatfind = exports + +function cheatfind.startplugin() + local cheat = {} + + -- return table of devices and spaces + function cheat.getspaces() + local spaces = {} + for tag, device in pairs(manager:machine().devices) do + if device.spaces then + spaces[tag] = {} + for name, space in pairs(device.spaces) do + spaces[tag][name] = space + end + end + end + return spaces + end + + -- return table of ram devices + function cheat.getram() + local ram = {} + for tag, device in pairs(manager:machine().devices) do + if device:shortname() == "ram" then + ram[tag] = device + end + end + end + + -- save data block + function cheat.save(space, start, size) + local data = { block = "", dev = space, start = start, size = size, space = space } + if space.shortname then + if space:shortname() == "ram" then + data.block = emu.item(device.items["0/m_pointer"]):read_block(start, size) + if not data.block then + return nil + end + end + else + local block = "" + for i = start, start + size do + block = block .. string.pack("B", space:read_u8(i)) + end + data.block = block + end + return data + end + + -- compare a data block to the current state + function cheat.comp(olddata, oper, val) + local newdata = cheat.save(olddata.dev, olddata.start, olddata.size, olddata.space) + local ret = {} + if not val then + val = 0 + end + if oper == "+" or oper == "inc" then + for i = 1, olddata.size do + local old = string.unpack("B", olddata.block, i) + local new = string.unpack("B", newdata.block, i) + if old < new then + if (val > 0 and (old + val) == new) or val == 0 then + ret[#ret + 1] = { addr = olddata.start + i, + oldval = old, + newval = new} + end + end + end + elseif oper == "-" or oper == "dec" then + for i = 1, olddata.size do + local old = string.unpack("B", olddata.block, i) + local new = string.unpack("B", newdata.block, i) + if old > new then + if (val > 0 and (old - val) == new) or val == 0 then + ret[#ret + 1] = { addr = olddata.start + i, + oldval = old, + newval = new} + end + end + end + end + return ret + end + + _G.cf = cheat +end + +return exports diff --git a/plugins/cheatfind/plugin.json b/plugins/cheatfind/plugin.json new file mode 100644 index 00000000000..8316cb5f334 --- /dev/null +++ b/plugins/cheatfind/plugin.json @@ -0,0 +1,10 @@ +{ + "plugin": { + "name": "cheatfind", + "description": "Cheat finder helper library", + "version": "0.0.1", + "author": "Carl", + "type": "plugin", + "start": "false" + } +}