mirror of
https://github.com/holub/mame
synced 2025-06-30 16:00:01 +03:00
plugin/cheat: flesh out the exported interface a bit more
This commit is contained in:
parent
520e0cd0e7
commit
c70bc6be75
@ -193,8 +193,6 @@ function cheat.startplugin()
|
|||||||
return
|
return
|
||||||
end
|
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)
|
local function run_if(cheat, func)
|
||||||
if func then
|
if func then
|
||||||
local stat, err = pcall(func)
|
local stat, err = pcall(func)
|
||||||
@ -293,7 +291,7 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function bpset(cheat, dev, addr, func)
|
local function bpset(cheat, dev, addr, func)
|
||||||
if is_oneshot(cheat) then
|
if cheat:is_oneshot() then
|
||||||
error("bpset not permitted in oneshot cheat")
|
error("bpset not permitted in oneshot cheat")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -302,7 +300,7 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function wpset(cheat, dev, space, wptype, addr, len, func)
|
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")
|
error("wpset not permitted in oneshot cheat")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -333,7 +331,7 @@ function cheat.startplugin()
|
|||||||
local function input_trans(list)
|
local function input_trans(list)
|
||||||
local xlate = { start = {}, stop = {}, last = 0 }
|
local xlate = { start = {}, stop = {}, last = 0 }
|
||||||
local function errout(port, field)
|
local function errout(port, field)
|
||||||
cheat.enabled = false
|
cheat:set_enabled(false)
|
||||||
error(port .. field .. " not found")
|
error(port .. field .. " not found")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -369,7 +367,7 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function input_run(cheat, list)
|
local function input_run(cheat, list)
|
||||||
if not is_oneshot(cheat) then
|
if not cheat:is_oneshot() then
|
||||||
cheat.enabled = false
|
cheat.enabled = false
|
||||||
error("input_run only allowed in one shot cheats")
|
error("input_run only allowed in one shot cheats")
|
||||||
return
|
return
|
||||||
@ -379,6 +377,72 @@ function cheat.startplugin()
|
|||||||
inputs[#inputs + 1] = list
|
inputs[#inputs + 1] = list
|
||||||
end
|
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)
|
local function parse_cheat(cheat)
|
||||||
cheat.cheat_env = { draw_text = draw_text,
|
cheat.cheat_env = { draw_text = draw_text,
|
||||||
draw_line = draw_line,
|
draw_line = draw_line,
|
||||||
@ -396,6 +460,9 @@ function cheat.startplugin()
|
|||||||
{ insert = table.insert,
|
{ insert = table.insert,
|
||||||
remove = table.remove } }
|
remove = table.remove } }
|
||||||
cheat.enabled = false
|
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
|
-- verify scripts are valid first
|
||||||
if not cheat.script then
|
if not cheat.script then
|
||||||
@ -500,6 +567,20 @@ function cheat.startplugin()
|
|||||||
if not param then
|
if not param then
|
||||||
return
|
return
|
||||||
end
|
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.min = tonumber(param.min) or 0
|
||||||
param.max = tonumber(param.max) or #param.item
|
param.max = tonumber(param.max) or #param.item
|
||||||
param.step = tonumber(param.step) or 1
|
param.step = tonumber(param.step) or 1
|
||||||
@ -508,7 +589,7 @@ function cheat.startplugin()
|
|||||||
if not item.value then
|
if not item.value then
|
||||||
item.value = (count * param.step) + param.min
|
item.value = (count * param.step) + param.min
|
||||||
else
|
else
|
||||||
item.value = tonumber(item.value)
|
item.value = tonumber(item.value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
param.last = #param.item
|
param.last = #param.item
|
||||||
@ -579,7 +660,7 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
menu[num][2] = ""
|
menu[num][2] = ""
|
||||||
menu[num][3] = "off"
|
menu[num][3] = "off"
|
||||||
elseif is_oneshot(cheat) then
|
elseif cheat:is_oneshot() then
|
||||||
menu[num][2] = _("Set")
|
menu[num][2] = _("Set")
|
||||||
menu[num][3] = 0
|
menu[num][3] = 0
|
||||||
else
|
else
|
||||||
@ -593,7 +674,7 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
if cheat.parameter.index == 0 then
|
if cheat.parameter.index == 0 then
|
||||||
if is_oneshot(cheat) then
|
if cheat:is_oneshot() then
|
||||||
menu[num][2] = _("Set")
|
menu[num][2] = _("Set")
|
||||||
else
|
else
|
||||||
menu[num][2] = _("Off")
|
menu[num][2] = _("Off")
|
||||||
@ -640,22 +721,12 @@ function cheat.startplugin()
|
|||||||
hotkeymenu = true
|
hotkeymenu = true
|
||||||
elseif index == 3 then
|
elseif index == 3 then
|
||||||
for num, cheat in pairs(cheats) do
|
for num, cheat in pairs(cheats) do
|
||||||
if cheat.enabled then
|
cheat:set_enabled(false)
|
||||||
run_if(cheat, cheat.script.off)
|
cheat:set_index(0)
|
||||||
bwpclr(cheat)
|
|
||||||
end
|
|
||||||
cheat.enabled = false
|
|
||||||
if cheat.parameter then
|
|
||||||
cheat.parameter.value = cheat.parameter.min
|
|
||||||
cheat.parameter.index = 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
elseif index == 4 then
|
elseif index == 4 then
|
||||||
for num, cheat in pairs(cheats) do
|
for num, cheat in pairs(cheats) do
|
||||||
if cheat.enabled then
|
cheat:set_enabled(false)
|
||||||
run_if(cheat, cheat.script.off)
|
|
||||||
bwpclr(cheat)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
cheats = load_cheats()
|
cheats = load_cheats()
|
||||||
for num, cheat in pairs(cheats) do
|
for num, cheat in pairs(cheats) do
|
||||||
@ -666,20 +737,6 @@ function cheat.startplugin()
|
|||||||
return true
|
return true
|
||||||
end
|
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]
|
local cheat = cheats[index]
|
||||||
if not cheat then
|
if not cheat then
|
||||||
return false
|
return false
|
||||||
@ -690,72 +747,31 @@ function cheat.startplugin()
|
|||||||
end
|
end
|
||||||
elseif event == "left" then
|
elseif event == "left" then
|
||||||
if cheat.parameter then
|
if cheat.parameter then
|
||||||
local param = cheat.parameter
|
local idx, chg = cheat:set_index(cheat:get_index() - 1)
|
||||||
if param.index == 1 then
|
return chg
|
||||||
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
|
|
||||||
else
|
else
|
||||||
if cheat.enabled and not is_oneshot(cheat) then
|
if not cheat:is_oneshot() then
|
||||||
cheat.enabled = false
|
return cheat:set_enabled(false)
|
||||||
run_if(cheat, cheat.script.off)
|
|
||||||
bwpclr(cheat)
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
elseif event == "right" then
|
elseif event == "right" then
|
||||||
if cheat.parameter then
|
if cheat.parameter then
|
||||||
local param = cheat.parameter
|
local idx, chd = cheat:set_index(cheat:get_index() + 1)
|
||||||
if param.index == 0 then
|
return chd
|
||||||
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
|
|
||||||
else
|
else
|
||||||
if not cheat.enabled and not is_oneshot(cheat) then
|
if not cheat:is_oneshot() then
|
||||||
cheat.enabled = true
|
local state, chg = cheat:set_enabled(true)
|
||||||
run_if(cheat, cheat.script.on)
|
return chg
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
elseif event == "select" then
|
elseif event == "select" then
|
||||||
if is_oneshot(cheat) then
|
if cheat:is_oneshot() then
|
||||||
if cheat.parameter and cheat.script.change and cheat.parameter.index ~= 0 then
|
cheat:set_enabled(true)
|
||||||
param_calc(cheat.parameter)
|
if cheat.parameter and cheat.script.change and cheat:get_index() ~= 0 then
|
||||||
cheat.cheat_env.param = cheat.parameter.value
|
manager:machine():popmessage(string.format(_("Activated: %s = %s"), cheat.desc, cheat:get_text()))
|
||||||
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))
|
|
||||||
elseif not cheat.parameter and cheat.script.on then
|
elseif not cheat.parameter and cheat.script.on then
|
||||||
cheat.script.on()
|
|
||||||
manager:machine():popmessage(string.format(_("Activated: %s"), cheat.desc))
|
manager:machine():popmessage(string.format(_("Activated: %s"), cheat.desc))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -810,7 +826,7 @@ function cheat.startplugin()
|
|||||||
if cheat.hotkeys and cheat.hotkeys.keys then
|
if cheat.hotkeys and cheat.hotkeys.keys then
|
||||||
if manager:machine():input():seq_pressed(cheat.hotkeys.keys) then
|
if manager:machine():input():seq_pressed(cheat.hotkeys.keys) then
|
||||||
if not cheat.hotkeys.pressed 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
|
if not run_if(cheat, cheat.script.change) then
|
||||||
run_if(cheat, cheat.script.on)
|
run_if(cheat, cheat.script.on)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user