diff options
Diffstat (limited to 'plugins/autofire/autofire_save.lua')
-rw-r--r-- | plugins/autofire/autofire_save.lua | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/plugins/autofire/autofire_save.lua b/plugins/autofire/autofire_save.lua index 3692540cd5b..f863b003f4d 100644 --- a/plugins/autofire/autofire_save.lua +++ b/plugins/autofire/autofire_save.lua @@ -1,7 +1,7 @@ local lib = {} local function get_settings_path() - return emu.subst_env(manager.machine.options.entries.homepath:value():match('([^;]+)')) .. '/autofire/' + return manager.machine.options.entries.homepath:value():match('([^;]+)') .. '/autofire' end local function get_settings_filename() @@ -10,23 +10,25 @@ end local function initialize_button(settings) if settings.port and settings.mask and settings.type and settings.key and settings.on_frames and settings.off_frames then + local ioport = manager.machine.ioport local new_button = { port = settings.port, + mask = settings.mask, + type = ioport:token_to_input_type(settings.type), key = manager.machine.input:seq_from_tokens(settings.key), + key_cfg = settings.key, on_frames = settings.on_frames, off_frames = settings.off_frames, counter = 0 } - local ioport = manager.machine.ioport local port = ioport.ports[settings.port] if port then local field = port:field(settings.mask) - if field and (field.type == ioport:token_to_input_type(settings.type)) then - new_button.field = field.name + if field and (field.type == new_button.type) then new_button.button = field - return new_button end end + return new_button end return nil end @@ -34,15 +36,15 @@ end local function serialize_settings(button_list) local settings = {} for index, button in ipairs(button_list) do - setting = { + local setting = { port = button.port, - mask = button.button.mask, - type = manager.machine.ioport:input_type_to_token(button.button.type), - key = manager.machine.input:seq_to_tokens(button.key), + mask = button.mask, + type = manager.machine.ioport:input_type_to_token(button.type), + key = button.key_cfg, on_frames = button.on_frames, off_frames = button.off_frames } - settings[#settings + 1] = setting + table.insert(settings, setting) end return settings end @@ -50,7 +52,7 @@ end function lib:load_settings() local buttons = {} local json = require('json') - local filename = get_settings_path() .. get_settings_filename() + local filename = get_settings_path() .. '/' .. get_settings_filename() local file = io.open(filename, 'r') if not file then return buttons @@ -58,7 +60,7 @@ function lib:load_settings() local loaded_settings = json.parse(file:read('a')) file:close() if not loaded_settings then - emu.print_error(string.format('Error loading autofire settings: error parsing file "%s" as JSON\n', filename)) + emu.print_error(string.format('Error loading autofire settings: error parsing file "%s" as JSON', filename)) return buttons end for index, button_settings in ipairs(loaded_settings) do @@ -73,23 +75,23 @@ end function lib:save_settings(buttons) local path = get_settings_path() local attr = lfs.attributes(path) - if not attr then - lfs.mkdir(path) - elseif attr.mode ~= 'directory' then - emu.print_error(string.format('Error autofire settings macros: "%s" is not a directory\n', path)) + if attr and (attr.mode ~= 'directory') then + emu.print_error(string.format('Error saving autofire settings: "%s" is not a directory', path)) return end + local filename = path .. '/' .. get_settings_filename() if #buttons == 0 then - os.remove(path .. get_settings_filename()) + os.remove(filename) return + elseif not attr then + lfs.mkdir(path) end local json = require('json') local settings = serialize_settings(buttons) local data = json.stringify(settings, {indent = true}) - local filename = path .. get_settings_filename() local file = io.open(filename, 'w') if not file then - emu.print_error(string.format('Error saving autofire settings: error opening file "%s" for writing\n', filename)) + emu.print_error(string.format('Error saving autofire settings: error opening file "%s" for writing', filename)) return end file:write(data) |