plugins/cheat: Overwrite existing hotkeys if all hotkeys are cleared (fixes GitHub #10387).

This commit is contained in:
Vas Crabb 2022-10-04 14:19:15 +11:00
parent 6befb00cbf
commit bdfdbd8796

View File

@ -154,9 +154,10 @@ function cheat.startplugin()
end end
end end
end end
local path = emu.subst_env(manager.machine.options.entries.cheatpath:value():match("([^;]+)"))
local filepath = path .. "/" .. cheatname .. "_hotkeys.json"
if #hotkeys > 0 then if #hotkeys > 0 then
local json = require("json") local json = require("json")
local path = emu.subst_env(manager.machine.options.entries.cheatpath:value():match("([^;]+)"))
local attr = lfs.attributes(path) local attr = lfs.attributes(path)
if not attr then if not attr then
lfs.mkdir(path) lfs.mkdir(path)
@ -165,7 +166,7 @@ function cheat.startplugin()
end end
if cheatname:find("/", 1, true) then if cheatname:find("/", 1, true) then
local softpath = path .. "/" .. cheatname:match("([^/]+)") local softpath = path .. "/" .. cheatname:match("([^/]+)")
local attr = lfs.attributes(softpath) attr = lfs.attributes(softpath)
if not attr then if not attr then
lfs.mkdir(softpath) lfs.mkdir(softpath)
elseif attr.mode ~= "directory" then -- uhhh? elseif attr.mode ~= "directory" then -- uhhh?
@ -173,11 +174,21 @@ function cheat.startplugin()
end end
end end
local file = io.open(path .. "/" .. cheatname .. "_hotkeys.json", "w+") local file = io.open(filepath, "w+")
if file then if file then
file:write(json.stringify(hotkeys, {indent = true})) file:write(json.stringify(hotkeys, {indent = true}))
file:close() file:close()
end end
else
local attr = lfs.attributes(filepath)
if attr and (attr.mode == "file") then
local json = require("json")
local file = io.open(filepath, "w+")
if file then
file:write(json.stringify(hotkeys, {indent = true}))
file:close()
end
end
end end
end end