diff options
author | cracyc <cracyc@users.noreply.github.com> | 2018-03-12 22:49:26 -0500 |
---|---|---|
committer | cracyc <cracyc@users.noreply.github.com> | 2018-03-12 22:49:26 -0500 |
commit | d035e3b546659c1718b2767ea3310fc6dc1df471 (patch) | |
tree | 299228263b8a49da42709012f1f1f493d0c57372 | |
parent | 53c4580f6940dd57495d78f6a1b9a813a85d8233 (diff) |
plugins/portname: change to address objections, also as suggested converted the controls.ini in repo https://github.com/cracyc/portname Removed the import but will still load parent if available (nw)
-rw-r--r-- | plugins/portname/init.lua | 96 |
1 files changed, 26 insertions, 70 deletions
diff --git a/plugins/portname/init.lua b/plugins/portname/init.lua index 959d89c5b84..f90978a34ee 100644 --- a/plugins/portname/init.lua +++ b/plugins/portname/init.lua @@ -14,75 +14,43 @@ exports.author = { name = "Carl" } local portname = exports function portname.startplugin() + local json = require("json") 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" + filename = emu.romname() .. "_" .. emu.softname() .. ".json" else - filename = emu.romname() .. ".po" + filename = emu.romname() .. ".json" end return filename end emu.register_start(function() - local reclevel = 0 - local function parse_names(names) - local orig, rep - local setfound = true - reclevel = reclevel + 1 - if reclevel == 3 then - emu.print_verbose("portname: too many import levels\n") - return - end - names:gsub("[^\n\r]*", function (line) - if line:find("^#import") then - local impfile = emu.file(ctrlrpath .. "/portname", "r") - local ret = impfile:open(line:match("^#import (.*)")) - if ret then - emu.print_verbose("portname: import file not found\n") - else - parse_names(impfile:read(impfile:size())) - end - elseif line:find("^#set") then - local sets = line:match("^#set (.*)") - setfound = false - for s in sets:gmatch("[^,]*") do - if s == emu.romname() then - setfound = true - break - end - end - elseif line:find("^msgid") then - orig = line:match("^msgid \"(.+)\"") - elseif line:find("^msgstr") then - rep = line:match("^msgstr \"(.+)\"") - if rep and rep ~= "" and setfound 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 - return line - end) - reclevel = reclevel - 1 - end local file = emu.file(ctrlrpath .. "/portname", "r") local ret = file:open(get_filename()) if ret then ret = file:open(get_filename(true)) if ret then - ret = file:open(manager:machine():system().parent .. ".po") + ret = file:open(manager:machine():system().parent .. ".json") if ret then return end end end - parse_names(file:read(file:size())) + local ctable = json.parse(file:read(file:size())) + for pname, port in pairs(ctable.ports) do + local ioport = manager:machine():ioport().ports[pname] + if ioport then + for mask, label in pairs(port.labels) do + for num3, field in pairs(ioport.fields) do + if tonumber(mask) == field.mask and label.player == field.player then + field.live.name = label.name + end + end + end + end + end end) local function menu_populate() @@ -91,20 +59,14 @@ function portname.startplugin() local function menu_callback(index, event) if event == "select" then - local fields = {} + local ports = {} for pname, port in pairs(manager:machine():ioport().ports) do + local labels = {} + ports[pname] = { labels = labels } for fname, field in pairs(port.fields) do - local dname = field.default_name - if not fields[dname] then - fields[dname] = {} - fields[dname].name = "" - end - if fname ~= dname then - fields[dname].name = fname + if not labels[field.mask] then + labels[field.mask] = { name = fname, player = field.player } end - fields[dname].port = pname - fields[dname].mask = field.mask - fields[dname].default = dname end end local function check_path(path) @@ -137,18 +99,12 @@ function portname.startplugin() file:close() return false end - local sfields = {} - for def, field in pairs(fields) do - sfields[#sfields + 1] = field - end - table.sort(sfields, function(a, b) if(a.port == b.port) then return a.mask < b.mask end return a.port < b.port end) file = io.open(ctrlrpath .. "/portname/" .. filename, "w") - for n, field in ipairs(sfields) do - def = field.default:gsub("[\\\"]", function (s) return "\\" .. s end) - custom = field.name:gsub("[\\\"]", function (s) return "\\" .. s end) - file:write(string.format("# port %s mask %08x\n", field.port, field.mask)) - file:write("msgid \"" .. def .."\"\nmsgstr \"" .. custom .. "\"\n\n") + local ctable = { romname = emu.romname(), ports = ports } + if emu.softname() ~= "" then + ctable.softname = emu.softname() end + file:write(json.stringify(ctable, { indent = true })) file:close() manager:machine():popmessage(string.format(_("Input port name file saved to %s"), ctrlrpath .. "/portname/" .. filename)) end |