plugin/cheat: flesh out the exported interface a bit more

This commit is contained in:
cracyc 2020-09-13 16:37:53 -05:00
parent 520e0cd0e7
commit c70bc6be75

View File

@ -193,8 +193,6 @@ function cheat.startplugin()
return
end
local function is_oneshot(cheat) return cheat.script and not cheat.script.run and not cheat.script.off end
local function run_if(cheat, func)
if func then
local stat, err = pcall(func)
@ -293,7 +291,7 @@ function cheat.startplugin()
end
local function bpset(cheat, dev, addr, func)
if is_oneshot(cheat) then
if cheat:is_oneshot() then
error("bpset not permitted in oneshot cheat")
return
end
@ -302,7 +300,7 @@ function cheat.startplugin()
end
local function wpset(cheat, dev, space, wptype, addr, len, func)
if is_oneshot(cheat) then
if cheat:is_oneshot() then
error("wpset not permitted in oneshot cheat")
return
end
@ -333,7 +331,7 @@ function cheat.startplugin()
local function input_trans(list)
local xlate = { start = {}, stop = {}, last = 0 }
local function errout(port, field)
cheat.enabled = false
cheat:set_enabled(false)
error(port .. field .. " not found")
return
end
@ -369,7 +367,7 @@ function cheat.startplugin()
end
local function input_run(cheat, list)
if not is_oneshot(cheat) then
if not cheat:is_oneshot() then
cheat.enabled = false
error("input_run only allowed in one shot cheats")
return
@ -379,6 +377,72 @@ function cheat.startplugin()
inputs[#inputs + 1] = list
end
local function param_calc(param)
if param.item then
if not param.item[param.index] then -- uh oh
param.index = 1
end
param.value = param.item[param.index].value
return
end
param.value = param.min + (param.step * (param.index - 1))
if param.value > param.max then
param.value = param.max
end
end
-- return is current state, ui change
local function set_enabled(cheat, state)
if cheat:is_oneshot() then
if state then
if cheat.parameter and cheat.script.change and cheat.parameter.index ~= 0 then
param_calc(cheat.parameter)
cheat.cheat_env.param = cheat.parameter.value
cheat.script.change()
elseif not cheat.parameter and cheat.script.on then
cheat.script.on()
end
end
return false, false
end
if cheat.enabled == state then
return state, false
end
if not state then
cheat.enabled = false
run_if(cheat, cheat.script.off)
bwpclr(cheat)
else
cheat.enabled = true
run_if(cheat, cheat.script.on)
end
return state, true
end
-- return is current index, ui change
local function set_index(cheat, index)
local param = cheat.parameter
local oldindex = param.index
if (param.index < 0) or (param.index >= param.last) or (param.index == index) then
return param.index, false
end
param.index = index
if index == 0 then
cheat.cheat_env.param = param.min
cheat:set_enabled(false)
else
if oldindex == 0 then
cheat:set_enabled(true)
end
param_calc(param)
cheat.cheat_env.param = param.value
if not cheat:is_oneshot() then
run_if(cheat, cheat.script.change)
end
end
return index, true
end
local function parse_cheat(cheat)
cheat.cheat_env = { draw_text = draw_text,
draw_line = draw_line,
@ -396,6 +460,9 @@ function cheat.startplugin()
{ insert = table.insert,
remove = table.remove } }
cheat.enabled = false
cheat.set_enabled = set_enabled;
cheat.get_enabled = function(cheat) return cheat.enabled end
cheat.is_oneshot = function(cheat) return cheat.script and not cheat.script.run and not cheat.script.off end
-- verify scripts are valid first
if not cheat.script then
@ -500,6 +567,20 @@ function cheat.startplugin()
if not param then
return
end
cheat.set_index = set_index;
cheat.set_value = function(cheat, value)
local idx, chg = cheat:set_index((value / cheat.parameter.step) + 1)
return cheat.parameter.value, chg
end
cheat.get_index = function(cheat) return cheat.parameter.index end
cheat.get_value = function(cheat) return cheat.parameter.value end
cheat.get_text = function(cheat)
if cheat.parameter.item then
return cheat.parameter.item[cheat.parameter.index]
else
return cheat.parameter.value
end
end
param.min = tonumber(param.min) or 0
param.max = tonumber(param.max) or #param.item
param.step = tonumber(param.step) or 1
@ -508,7 +589,7 @@ function cheat.startplugin()
if not item.value then
item.value = (count * param.step) + param.min
else
item.value = tonumber(item.value)
item.value = tonumber(item.value)
end
end
param.last = #param.item
@ -579,7 +660,7 @@ function cheat.startplugin()
end
menu[num][2] = ""
menu[num][3] = "off"
elseif is_oneshot(cheat) then
elseif cheat:is_oneshot() then
menu[num][2] = _("Set")
menu[num][3] = 0
else
@ -593,7 +674,7 @@ function cheat.startplugin()
end
else
if cheat.parameter.index == 0 then
if is_oneshot(cheat) then
if cheat:is_oneshot() then
menu[num][2] = _("Set")
else
menu[num][2] = _("Off")
@ -640,22 +721,12 @@ function cheat.startplugin()
hotkeymenu = true
elseif index == 3 then
for num, cheat in pairs(cheats) do
if cheat.enabled then
run_if(cheat, cheat.script.off)
bwpclr(cheat)
end
cheat.enabled = false
if cheat.parameter then
cheat.parameter.value = cheat.parameter.min
cheat.parameter.index = 0
end
cheat:set_enabled(false)
cheat:set_index(0)
end
elseif index == 4 then
for num, cheat in pairs(cheats) do
if cheat.enabled then
run_if(cheat, cheat.script.off)
bwpclr(cheat)
end
cheat:set_enabled(false)
end
cheats = load_cheats()
for num, cheat in pairs(cheats) do
@ -666,20 +737,6 @@ function cheat.startplugin()
return true
end
local function param_calc(param)
if param.item then
if not param.item[param.index] then -- uh oh
param.index = 1
end
param.value = param.item[param.index].value
return
end
param.value = param.min + (param.step * (param.index - 1))
if param.value > param.max then
param.value = param.max
end
end
local cheat = cheats[index]
if not cheat then
return false
@ -690,72 +747,31 @@ function cheat.startplugin()
end
elseif event == "left" then
if cheat.parameter then
local param = cheat.parameter
if param.index == 1 then
param.index = 0
cheat.enabled = false
cheat.cheat_env.param = param.min
run_if(cheat, cheat.script.off)
bwpclr(cheat)
return true
elseif param.index == 0 then
return false
end
param.index = param.index - 1
param_calc(param)
cheat.cheat_env.param = param.value
if not is_oneshot(cheat) then
run_if(cheat, cheat.script.change)
end
return true
local idx, chg = cheat:set_index(cheat:get_index() - 1)
return chg
else
if cheat.enabled and not is_oneshot(cheat) then
cheat.enabled = false
run_if(cheat, cheat.script.off)
bwpclr(cheat)
return true
if not cheat:is_oneshot() then
return cheat:set_enabled(false)
end
return false
end
elseif event == "right" then
if cheat.parameter then
local param = cheat.parameter
if param.index == 0 then
cheat.enabled = true
run_if(cheat, cheat.script.on)
elseif param.index == param.last then
return false
end
param.index = param.index + 1
param_calc(param)
cheat.cheat_env.param = param.value
if not is_oneshot(cheat) then
run_if(cheat, cheat.script.change)
end
return true
local idx, chd = cheat:set_index(cheat:get_index() + 1)
return chd
else
if not cheat.enabled and not is_oneshot(cheat) then
cheat.enabled = true
run_if(cheat, cheat.script.on)
return true
if not cheat:is_oneshot() then
local state, chg = cheat:set_enabled(true)
return chg
end
return false
end
elseif event == "select" then
if is_oneshot(cheat) then
if cheat.parameter and cheat.script.change and cheat.parameter.index ~= 0 then
param_calc(cheat.parameter)
cheat.cheat_env.param = cheat.parameter.value
cheat.script.change()
local subtext
if cheat.parameter.item then
subtext = cheat.parameter.item[cheat.parameter.index]
else
subtext = cheat.parameter.value
end
manager:machine():popmessage(string.format(_("Activated: %s = %s"), cheat.desc, subtext))
if cheat:is_oneshot() then
cheat:set_enabled(true)
if cheat.parameter and cheat.script.change and cheat:get_index() ~= 0 then
manager:machine():popmessage(string.format(_("Activated: %s = %s"), cheat.desc, cheat:get_text()))
elseif not cheat.parameter and cheat.script.on then
cheat.script.on()
manager:machine():popmessage(string.format(_("Activated: %s"), cheat.desc))
end
end
@ -810,7 +826,7 @@ function cheat.startplugin()
if cheat.hotkeys and cheat.hotkeys.keys then
if manager:machine():input():seq_pressed(cheat.hotkeys.keys) then
if not cheat.hotkeys.pressed then
if is_oneshot(cheat) then
if cheat:is_oneshot() then
if not run_if(cheat, cheat.script.change) then
run_if(cheat, cheat.script.on)
end