summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/autofire/autofire_save.lua
diff options
context:
space:
mode:
author Jack Li <jackrjli@berkeley.edu>2019-05-14 07:26:27 -0700
committer ajrhacker <ajrhacker@users.noreply.github.com>2019-05-14 10:26:27 -0400
commit0040650300e4d60406e3ad27d7aee914195dd69e (patch)
tree4a91198dd84e2252a01211830e80d47fcb51f7be /plugins/autofire/autofire_save.lua
parente24a0a19308457e5a23cd84a35fffea53701eea5 (diff)
Add plugin for autofire (#5050)
* Hardcoded autofire plugin * Changed register_frame to register_frame_done, removed pause check * Added support for multiple buttons loaded from a file * Implemented saving settings on quit * Fixed multiple keybindings for same button from overwriting each other * Replaced double-quotes with single-quotes * Refactored saving/loading into a separate module * Changed button format to use input_code rather than string token Settings format is unchanged (still saved as string token). * Rewrote table initialization in save/load * Implemented menus * Added helper messages for "on frames"/"off frames"
Diffstat (limited to 'plugins/autofire/autofire_save.lua')
-rw-r--r--plugins/autofire/autofire_save.lua86
1 files changed, 86 insertions, 0 deletions
diff --git a/plugins/autofire/autofire_save.lua b/plugins/autofire/autofire_save.lua
new file mode 100644
index 00000000000..e7d28029cd5
--- /dev/null
+++ b/plugins/autofire/autofire_save.lua
@@ -0,0 +1,86 @@
+local lib = {}
+
+local function get_settings_path()
+ return lfs.env_replace(manager:machine():options().entries.pluginspath:value():match('([^;]+)')) .. '/autofire/cfg/'
+end
+
+local function get_settings_filename()
+ return emu.romname() .. '.cfg'
+end
+
+local function initialize_button(settings)
+ if settings.port and settings.field and settings.key and settings.on_frames and settings.off_frames then
+ local new_button = {
+ port = settings.port,
+ field = settings.field,
+ key = manager:machine():input():code_from_token(settings.key),
+ on_frames = settings.on_frames,
+ off_frames = settings.off_frames,
+ counter = 0
+ }
+ local port = manager:machine():ioport().ports[settings.port]
+ if port then
+ local field = port.fields[settings.field]
+ if field then
+ new_button.button = field
+ return new_button
+ end
+ end
+ end
+ return nil
+end
+
+local function serialize_settings(button_list)
+ local settings = {}
+ for index, button in ipairs(button_list) do
+ setting = {
+ port = button.port,
+ field = button.field,
+ key = manager:machine():input():code_to_token(button.key),
+ on_frames = button.on_frames,
+ off_frames = button.off_frames
+ }
+ settings[#settings + 1] = setting
+ end
+ return settings
+end
+
+function lib:load_settings()
+ local buttons = {}
+ local json = require('json')
+ local file = io.open(get_settings_path() .. get_settings_filename(), 'r')
+ if not file then
+ return buttons
+ end
+ local loaded_settings = json.parse(file:read('a'))
+ file:close()
+ if not loaded_settings then
+ return buttons
+ end
+ for index, button_settings in ipairs(loaded_settings) do
+ local new_button = initialize_button(button_settings)
+ if new_button then
+ buttons[#buttons + 1] = new_button
+ end
+ end
+ return buttons
+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
+ return
+ end
+ local json = require('json')
+ local settings = serialize_settings(buttons)
+ local file = io.open(path .. get_settings_filename(), 'w')
+ if file then
+ file:write(json.stringify(settings, {indent = true}))
+ file:close()
+ end
+end
+
+return lib