plugins/portname: new wip plugin for loading translations or alt names for input port fields [Carl]

This commit is contained in:
cracyc 2017-12-30 16:02:00 -06:00
parent c3c8df4cce
commit d3e7be82a8
3 changed files with 139 additions and 0 deletions

108
plugins/portname/init.lua Normal file
View File

@ -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

View File

@ -0,0 +1,10 @@
{
"plugin": {
"name": "portname",
"description": "IOPort name/translation plugin",
"version": "0.0.1",
"author": "Carl",
"type": "plugin",
"start": "false"
}
}

View File

@ -1496,11 +1496,22 @@ void lua_engine::initialize()
"field", &ioport_port::field,
"fields", sol::property([this](ioport_port &p){
sol::table f_table = sol().create_table();
// parse twice for custom and default names, default has priority
for(ioport_field &field : p.fields())
{
if (field.type_class() != INPUT_CLASS_INTERNAL)
f_table[field.name()] = &field;
}
for(ioport_field &field : p.fields())
{
if (field.type_class() != INPUT_CLASS_INTERNAL)
{
if(field.specific_name())
f_table[field.specific_name()] = &field;
else
f_table[field.manager().type_name(field.type(), field.player())] = &field;
}
}
return f_table;
}));
@ -1511,6 +1522,9 @@ void lua_engine::initialize()
"set_value", &ioport_field::set_value,
"device", sol::property(&ioport_field::device),
"name", sol::property(&ioport_field::name),
"default_name", sol::property([](ioport_field &f) {
return f.specific_name() ? f.specific_name() : f.manager().type_name(f.type(), f.player());
}),
"player", sol::property(&ioport_field::player, &ioport_field::set_player),
"mask", sol::property(&ioport_field::mask),
"defvalue", sol::property(&ioport_field::defvalue),
@ -1529,9 +1543,16 @@ void lua_engine::initialize()
"analog_invert", sol::property(&ioport_field::analog_invert),
"impulse", sol::property(&ioport_field::impulse),
"type", sol::property(&ioport_field::type),
"live", sol::property(&ioport_field::live),
"crosshair_scale", sol::property(&ioport_field::crosshair_scale, &ioport_field::set_crosshair_scale),
"crosshair_offset", sol::property(&ioport_field::crosshair_offset, &ioport_field::set_crosshair_offset));
/* field.live
*/
sol().registry().new_usertype<ioport_field_live>("ioport_field_live", "new", sol::no_constructor,
"name", &ioport_field_live::name);
/* machine:parameters()
* parameter:add(tag, val) - add tag = val parameter
* parameter:lookup(tag) - get val for tag