Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to pass in the memory access object to plugins #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions plugins/solo-z3r-multiworld.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local plugin = {}

plugin.memory = {}

plugin.name = "Solo Z3R Multiworld"
plugin.author = "authorblues"

Expand Down Expand Up @@ -44,7 +46,7 @@ local CLEAR_DELAY_FRAMES = 1
local prev_sram_data = nil

local function get_game_mode()
return mainmemory.read_u8(0x0010)
return plugin.memory.read_u8(0x0010)
end

local function is_normal_gameplay()
Expand All @@ -57,7 +59,7 @@ end
local function read_BCD_to_delimiter(addr, stop)
local result = 0
for i = 1,20 do
local value = memory.read_u8(addr, "CARTROM")
local value = plugin.memory.read_u8(addr, "CARTROM")
if value == stop then break end
result = (result * 10) + (value - 0x30)
addr = addr + 1
Expand All @@ -67,7 +69,7 @@ local function read_BCD_to_delimiter(addr, stop)
end

local function get_sram_data()
return mainmemory.readbyterange(SRAM_DATA_START, SRAM_DATA_SIZE)
return plugin.memory.readbyterange(SRAM_DATA_START, SRAM_DATA_SIZE)
end

-- returns sram changes as a consistent, serialized string
Expand Down Expand Up @@ -147,17 +149,17 @@ end
function plugin.on_game_load(data, settings)
-- a handful of checks to make sure this is a SNES game first
local has_cartrom = false
for i,domain in ipairs(memory.getmemorydomainlist()) do
for i,domain in ipairs(plugin.memory.getmemorydomainlist()) do
if domain == "CARTROM" then has_cartrom = true end
end

-- if the cartrom isn't present, or it is too small, bail
if not has_cartrom then return end
if memory.getmemorydomainsize("CARTROM") < 0x200000 then return end
if plugin.memory.getmemorydomainsize("CARTROM") < 0x200000 then return end

-- if the rom name does not seem to be from a valid Z3R rom, ignore it
for i,val in ipairs(ROM_NAME_PATTERN) do
local mem = memory.read_u8(ROM_NAME_ADDR + i - 1, "CARTROM")
local mem = plugin.memory.read_u8(ROM_NAME_ADDR + i - 1, "CARTROM")
if val ~= nil and mem ~= val then return end
end

Expand Down Expand Up @@ -191,8 +193,8 @@ function plugin.on_frame(data, settings)
local sram_data = get_sram_data()

if is_normal_gameplay() then
player_id = mainmemory.read_u8(OUTGOING_PLAYER_ADDR)
item_id = mainmemory.read_u8(OUTGOING_ITEM_ADDR)
player_id = plugin.memory.read_u8(OUTGOING_PLAYER_ADDR)
item_id = plugin.memory.read_u8(OUTGOING_ITEM_ADDR)

local prev_player = data.prev_player or 0
data.prev_player = player_id
Expand All @@ -204,23 +206,23 @@ function plugin.on_frame(data, settings)
end

local queue_len = #meta.itemqueues[this_player_id]
local recv_count = mainmemory.read_u16_le(RECV_COUNT_ADDR)
local recv_count = plugin.memory.read_u16_le(RECV_COUNT_ADDR)
if recv_count > queue_len then
mainmemory.write_u16_le(RECV_COUNT_ADDR, 0)
plugin.memory.write_u16_le(RECV_COUNT_ADDR, 0)
recv_count = 0
end

if recv_count < queue_len and mainmemory.read_u8(INCOMING_ITEM_ADDR) == 0 then
if recv_count < queue_len and plugin.memory.read_u8(INCOMING_ITEM_ADDR) == 0 then
local obj = meta.itemqueues[this_player_id][recv_count+1]
local item = obj.item

if settings.progressive then
item = adjust_progressive(obj.item)
end

mainmemory.write_u8(INCOMING_ITEM_ADDR, item)
mainmemory.write_u8(INCOMING_PLAYER_ADDR, obj.src)
mainmemory.write_u16_le(RECV_COUNT_ADDR, recv_count+1)
plugin.memory.write_u8(INCOMING_ITEM_ADDR, item)
plugin.memory.write_u8(INCOMING_PLAYER_ADDR, obj.src)
plugin.memory.write_u16_le(RECV_COUNT_ADDR, recv_count+1)
end
elseif get_game_mode() == 0x00 then
-- if we somehow got to the title screen (reset?) with items queued to
Expand All @@ -232,8 +234,8 @@ function plugin.on_frame(data, settings)
-- clear the outgoing items addresses no matter the gamemode
if (meta.cleardelay[this_player_id] or 0) > 0 then
if meta.cleardelay[this_player_id] == 0 then
mainmemory.write_u8(OUTGOING_PLAYER_ADDR, 0)
mainmemory.write_u8(OUTGOING_ITEM_ADDR, 0)
plugin.memory.write_u8(OUTGOING_PLAYER_ADDR, 0)
plugin.memory.write_u8(OUTGOING_ITEM_ADDR, 0)
data.prev_player = 0
else
meta.cleardelay[this_player_id] = meta.cleardelay[this_player_id] - 1
Expand Down
2 changes: 2 additions & 0 deletions shuffler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ function complete_setup()
if config.plugins ~= nil then
for pmodpath,pdata in pairs(config.plugins) do
local pmodule = require(PLUGINS_FOLDER .. '.' .. pmodpath)
pmodule.memory = memory
if checkversion(pmodule.minversion) then
print('Plugin loaded: ' .. pmodule.name)
else
Expand Down Expand Up @@ -467,6 +468,7 @@ if emu.getsystemid() ~= "NULL" then
for pmodpath,pdata in pairs(config.plugins) do
local pmodule = require(PLUGINS_FOLDER .. '.' .. pmodpath)
pmodule._module = pmodpath
pmodule.memory = memory
if pmodule ~= nil then table.insert(plugins, pmodule) end
end
end
Expand Down