diff options
author | cracyc <cracyc@users.noreply.github.com> | 2017-12-30 16:02:00 -0600 |
---|---|---|
committer | cracyc <cracyc@users.noreply.github.com> | 2017-12-30 16:02:56 -0600 |
commit | d3e7be82a8308f773d43487b43ea28e6426dcd03 (patch) | |
tree | 2e82b2fba6ce06e0c4fbab4ce2a52fa89f94676a /plugins | |
parent | c3c8df4cce5a88e1cf860c64002f79aec73ca079 (diff) |
plugins/portname: new wip plugin for loading translations or alt names for input port fields [Carl]
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/portname/init.lua | 108 | ||||
-rw-r--r-- | plugins/portname/plugin.json | 10 |
2 files changed, 118 insertions, 0 deletions
diff --git a/plugins/portname/init.lua b/plugins/portname/init.lua new file mode 100644 index 00000000000..b30763aee66 --- /dev/null +++ b/plugins/portname/init.lua @@ -0,0 +1,108 @@ +-- license:BSD-3-Clause +-- copyright-holders:Carl +-- use plugin options to save the input port list to a gettext formatted file +-- the file is saved in the ctrlrpath dir +local exports = {} +exports.name = "portname" +exports.version = "0.0.1" +exports.description = "IOPort name/translation plugin" +exports.license = "The BSD 3-Clause License" +exports.author = { name = "Carl" } + +local portname = exports + +function portname.startplugin() + local ctrlrpath = lfs.env_replace(manager:options().entries.ctrlrpath:value():match("([^;]+)")) + local function get_filename(nosoft) + local filename + if emu.softname() ~= "" and not nosoft then + filename = emu.romname() .. "_" .. emu.softname() .. ".po" + else + filename = emu.romname() .. ".po" + end + return filename + end + + emu.register_start(function() + local file = io.open(ctrlrpath .. "/" .. get_filename(), "r") + if not file then + file = io.open(ctrlrpath .. "/" .. get_filename(true), "r") + if not file then + return + end + end + local orig, rep + for line in file:lines() do + if line:find("^msgid") then + orig = line:match("^msgid \"(.+)\"") + elseif line:find("^msgstr") then + rep = line:match("^msgstr \"(.+)\"") + if rep and rep ~= "" then + rep = rep:gsub("\\(.)", "%1") + orig = orig:gsub("\\(.)", "%1") + for pname, port in pairs(manager:machine():ioport().ports) do + if port.fields[orig] then + port.fields[orig].live.name = rep + end + end + end + end + end + file:close() + end) + + local function menu_populate() + return {{ _("Save input names to file"), "", 0 }} + end + + local function menu_callback(index, event) + if event == "select" then + local fields = {} + for pname, port in pairs(manager:machine():ioport().ports) do + for fname, field in pairs(port.fields) do + local dname = field.default_name + if not fields[dname] then + fields[dname] = "" + end + if fname ~= dname then + fields[dname] = fname + end + end + end + local attr = lfs.attributes(ctrlrpath) + if not attr then + lfs.mkdir(ctrlrpath) + if not lfs.attributes(ctrlrpath) then + manager:machine():popmessage(_("Failed to save input name file")) + emu.print_verbose("portname: unable to create ctrlrpath " .. ctrlrpath .. "\n") + return false + end + elseif attr.mode ~= "directory" then + manager:machine():popmessage(_("Failed to save input name file")) + emu.print_verbose("portname: ctrlrpath exists but isn't directory " .. ctrlrpath .. "\n") + return false + end + local filename = get_filename() + local file = io.open(ctrlrpath .. "/" .. filename, "r") + if file then + emu.print_verbose("portname: input name file exists " .. filename .. "\n") + manager:machine():popmessage(_("Failed to save input name file")) + file:close() + return false + end + file = io.open(ctrlrpath .. "/" .. filename, "w") + for def, custom in pairs(fields) do + def = def:gsub("[\\\"]", function (s) return "\\" .. s end) + custom = custom:gsub("[\\\"]", function (s) return "\\" .. s end) + file:write("msgid \"" .. def .."\"\nmsgstr \"" .. custom .. "\"\n\n") + end + file:close() + manager:machine():popmessage(_("Input port name file saved to ") .. ctrlrpath .. "/" .. filename) + end + return false + end + + emu.register_menu(menu_callback, menu_populate, _("Input ports")) +end + +return exports diff --git a/plugins/portname/plugin.json b/plugins/portname/plugin.json new file mode 100644 index 00000000000..4680340840d --- /dev/null +++ b/plugins/portname/plugin.json @@ -0,0 +1,10 @@ +{ + "plugin": { + "name": "portname", + "description": "IOPort name/translation plugin", + "version": "0.0.1", + "author": "Carl", + "type": "plugin", + "start": "false" + } +} |