Autofire plugin: Save/load fixes (#5093)

* Fixed bugs related to reloading roms

Soft resets would reload autofire settings without saving them first, causing the settings to be lost. This commit adds a check to only reload from the settings file if loading a different rom than before.

Hard resets would leave bad references lying around, causing MAME to crash under certain circumstances (i.e. resetting while in the edit menu and entering the menu again). This commit makes sure to properly clean up and reinitialize menu and button states when resetting.

* Used set_folder to avoid hardcoding plugin name in settings path

* Bumped autofire plugin version
This commit is contained in:
Jack Li 2019-05-21 15:44:44 -07:00 committed by ajrhacker
parent 20d691db79
commit 27f66693c5
4 changed files with 45 additions and 6 deletions

View File

@ -265,6 +265,14 @@ local function handle_button_menu(index, event)
return false
end
function lib:init_menu(buttons)
header_height = 0
content_height = 0
menu_stack = { MENU_TYPES.MAIN }
current_button = {}
inputs = {}
end
function lib:populate_menu(buttons)
local current_menu = menu_stack[#menu_stack]
if current_menu == MENU_TYPES.MAIN then

View File

@ -1,7 +1,9 @@
local lib = {}
local plugin_path = ''
local function get_settings_path()
return lfs.env_replace(manager:machine():options().entries.pluginspath:value():match('([^;]+)')) .. '/autofire/cfg/'
return plugin_path .. '/cfg/'
end
local function get_settings_filename()
@ -45,6 +47,10 @@ local function serialize_settings(button_list)
return settings
end
function lib:set_plugin_path(path)
plugin_path = path
end
function lib:load_settings()
local buttons = {}
local json = require('json')

View File

@ -2,13 +2,20 @@
-- copyright-holders:Jack Li
local exports = {}
exports.name = 'autofire'
exports.version = '0.0.1'
exports.version = '0.0.2'
exports.description = 'Autofire plugin'
exports.license = 'The BSD 3-Clause License'
exports.author = { name = 'Jack Li' }
local autofire = exports
function autofire.set_folder(path)
local loader = require('autofire/autofire_save')
if loader then
loader:set_plugin_path(path)
end
end
function autofire.startplugin()
-- List of autofire buttons, each being a table with keys:
@ -21,6 +28,8 @@ function autofire.startplugin()
-- 'counter' - position in autofire cycle
local buttons = {}
local current_rom = nil
local function process_button(button)
local pressed = manager:machine():input():code_pressed(button.key)
if pressed then
@ -54,10 +63,26 @@ function autofire.startplugin()
end
end
local function reinit_buttons()
for i, button in ipairs(buttons) do
button.counter = 0
button.button = manager:machine():ioport().ports[button.port].fields[button.field]
end
end
local function load_settings()
local loader = require('autofire/autofire_save')
if loader then
buttons = loader:load_settings()
if current_rom == emu.romname() then
reinit_buttons()
else
local loader = require('autofire/autofire_save')
if loader then
buttons = loader:load_settings()
end
end
current_rom = emu.romname()
local menu_handler = require('autofire/autofire_menu')
if menu_handler then
menu_handler:init_menu(buttons)
end
end

View File

@ -2,7 +2,7 @@
"plugin": {
"name": "autofire",
"description": "Autofire plugin",
"version": "0.0.1",
"version": "0.0.2",
"author": "Jack Li",
"type": "plugin",
"start": "false"