summaryrefslogtreecommitdiffstatshomepage
path: root/plugins
diff options
context:
space:
mode:
author Szunti <Szunti@users.noreply.github.com>2021-05-04 02:40:10 +0200
committer GitHub <noreply@github.com>2021-05-03 20:40:10 -0400
commit25137717c9392d142650fcd679b09c400a2f5c4a (patch)
tree3d0f917bd4ef51a36cb45f78e3b8d33dcf375ffe /plugins
parenta90f1c885d8cbb7105cf6af2e1e2661799b5c529 (diff)
Create console history file in homepath (#8026)
* Fix console history path, homepath is a core option * Create missing directories recursively in lua plugins. * Add lfs to global environment in a less magical way. require normally doesn't bind the name globally just returns the module, mame sets a preloader that does bind lfs globally, but maybe it's less surprising to do it explicitly
Diffstat (limited to 'plugins')
-rw-r--r--plugins/autofire/autofire_save.lua3
-rw-r--r--plugins/boot.lua2
-rw-r--r--plugins/cheat/init.lua5
-rw-r--r--plugins/console/init.lua9
-rw-r--r--plugins/data/data_hiscore.lua4
-rw-r--r--plugins/data/database.lua6
-rw-r--r--plugins/hiscore/init.lua3
-rw-r--r--plugins/portname/init.lua3
-rw-r--r--plugins/timer/init.lua4
-rw-r--r--plugins/util.lua55
10 files changed, 81 insertions, 13 deletions
diff --git a/plugins/autofire/autofire_save.lua b/plugins/autofire/autofire_save.lua
index 0576a95582d..3c6f177ff13 100644
--- a/plugins/autofire/autofire_save.lua
+++ b/plugins/autofire/autofire_save.lua
@@ -1,3 +1,4 @@
+local util = require("util")
local lib = {}
local function get_settings_path()
@@ -70,7 +71,7 @@ function lib:save_settings(buttons)
local path = get_settings_path()
local attr = lfs.attributes(path)
if not attr then
- lfs.mkdir(path)
+ util.mkdir_recursive(path)
elseif attr.mode ~= 'directory' then
return
end
diff --git a/plugins/boot.lua b/plugins/boot.lua
index 8ae3e069755..0bc32e91b27 100644
--- a/plugins/boot.lua
+++ b/plugins/boot.lua
@@ -1,6 +1,6 @@
-- license:BSD-3-Clause
-- copyright-holders:Miodrag Milanovic
-require('lfs')
+_G.lfs = require("lfs")
_G._ = emu.lang_translate
_G.emu.plugin = {} -- table to contain plugin interfaces
diff --git a/plugins/cheat/init.lua b/plugins/cheat/init.lua
index 208efc3585e..412bc261030 100644
--- a/plugins/cheat/init.lua
+++ b/plugins/cheat/init.lua
@@ -72,6 +72,7 @@ exports.license = "The BSD 3-Clause License"
exports.author = { name = "Carl" }
local cheat = exports
+local util = require("util")
function cheat.set_folder(path)
cheat.path = path
@@ -159,7 +160,7 @@ function cheat.startplugin()
local path = emu.subst_env(manager.machine.options.entries.cheatpath:value():match("([^;]+)"))
local attr = lfs.attributes(path)
if not attr then
- lfs.mkdir(path)
+ util.mkdir_recursive(path)
elseif attr.mode ~= "directory" then -- uhhh?
return
end
@@ -167,7 +168,7 @@ function cheat.startplugin()
local softpath = path .. "/" .. cheatname:match("([^/]+)")
local attr = lfs.attributes(softpath)
if not attr then
- lfs.mkdir(softpath)
+ util.mkdir_recursive(softpath)
elseif attr.mode ~= "directory" then -- uhhh?
return
end
diff --git a/plugins/console/init.lua b/plugins/console/init.lua
index 95218d67389..c9811ba2467 100644
--- a/plugins/console/init.lua
+++ b/plugins/console/init.lua
@@ -13,6 +13,8 @@ local history_file = "console_history"
local history_fullpath = nil
+local util = require("util")
+
function console.startplugin()
local conth = emu.thread()
local ln_started = false
@@ -239,8 +241,8 @@ function console.startplugin()
end
if (not started) then
-- options are not available in startplugin, so we load the history here
- local homepath = emu.subst_env(manager.ui.options.entries.homepath:value():match("([^;]+)"))
- history_fullpath = homepath .. '/console_history'
+ local homepath = emu.subst_env(manager.options.entries.homepath:value():match("([^;]+)"))
+ history_fullpath = homepath .. '/' .. history_file
ln.loadhistory(history_fullpath)
started = true
end
@@ -298,7 +300,8 @@ end
setmetatable(console, {
__gc = function ()
if history_fullpath then
- ln = require("linenoise")
+ util.create_parent_dirs(history_fullpath)
+ local ln = require("linenoise")
ln.savehistory(history_fullpath)
end
end})
diff --git a/plugins/data/data_hiscore.lua b/plugins/data/data_hiscore.lua
index 3bf5467fe9e..e6d7d75fdc0 100644
--- a/plugins/data/data_hiscore.lua
+++ b/plugins/data/data_hiscore.lua
@@ -1,6 +1,8 @@
-- to use this get the package from http://greatstone.free.fr/hi2txt/
-- extract the hi2txt.zip and place it in your history path
+local util = require("util")
+
local dat = {}
local env = {}
local output
@@ -1206,7 +1208,7 @@ function dat.check(set, softlist)
local scrpath = path:match("[^;]*") .. "/"
local scrfile = io.open(scrpath .. set .. ".lua", "w+")
if not scrfile then
- lfs.mkdir(scrpath)
+ util.mkdir_recursive(scrpath)
scrfile = io.open(scrpath .. set .. ".lua", "w+")
end
if scrfile then
diff --git a/plugins/data/database.lua b/plugins/data/database.lua
index b868173ce08..bd7de2d665d 100644
--- a/plugins/data/database.lua
+++ b/plugins/data/database.lua
@@ -1,6 +1,7 @@
local sql = require("lsqlite3")
local datfile = {}
local db
+local util = require("util")
local function check_db(msg)
if db:errcode() > sql.OK then
@@ -12,7 +13,10 @@ do
local dbpath = emu.subst_env(mame_manager.ui.options.entries.historypath:value():match("([^;]+)"))
db = sql.open(dbpath .. "/history.db")
if not db then
- lfs.mkdir(dbpath)
+ local success, err = util.mkdir_recursive(dbpath)
+ if not success then
+ emu.print_error("Unable to create parent directories for history database: " .. err)
+ end
db = sql.open(dbpath .. "/history.db")
if not db then
emu.print_error("Unable to create history.db\n")
diff --git a/plugins/hiscore/init.lua b/plugins/hiscore/init.lua
index 2a9687d1547..093737a9eec 100644
--- a/plugins/hiscore/init.lua
+++ b/plugins/hiscore/init.lua
@@ -12,6 +12,7 @@ exports.description = "Hiscore"
exports.license = "WTFPL license"
exports.author = { name = "borgar@borgar.net" }
local hiscore = exports
+local util = require("util")
local hiscore_plugin_path = ""
@@ -171,7 +172,7 @@ function hiscore.startplugin()
local output = io.open(get_file_name(), "wb");
if not output then
-- attempt to create the directory, and try again
- lfs.mkdir( hiscore_path );
+ util.mkdir_recursive( hiscore_path );
output = io.open(get_file_name(), "wb");
end
emu.print_verbose("hiscore: write_scores output")
diff --git a/plugins/portname/init.lua b/plugins/portname/init.lua
index 24cdc50facf..926bf442a45 100644
--- a/plugins/portname/init.lua
+++ b/plugins/portname/init.lua
@@ -25,6 +25,7 @@ exports.license = "The BSD 3-Clause License"
exports.author = { name = "Carl" }
local portname = exports
+local util = require("util")
function portname.startplugin()
local json = require("json")
@@ -129,7 +130,7 @@ function portname.startplugin()
local function check_path(path)
local attr = lfs.attributes(path)
if not attr then
- lfs.mkdir(path)
+ util.mkdir_recursive(path)
if not lfs.attributes(path) then
manager.machine:popmessage(_("Failed to save input name file"))
emu.print_verbose("portname: unable to create path " .. path .. "\n")
diff --git a/plugins/timer/init.lua b/plugins/timer/init.lua
index f0ddabbcf50..562f3c6a8b1 100644
--- a/plugins/timer/init.lua
+++ b/plugins/timer/init.lua
@@ -1,6 +1,6 @@
-- license:BSD-3-Clause
-- copyright-holders:Carl
-require('lfs')
+local util = require('util')
local sqlite3 = require('lsqlite3')
local exports = {}
exports.name = "timer"
@@ -44,7 +44,7 @@ function timer.startplugin()
save()
end
timer_started = true
- lfs.mkdir(dir .. '/timer')
+ util.mkdir_recursive(dir .. '/timer')
local db = assert(sqlite3.open(timer_db))
local found=false
db:exec([[select * from sqlite_master where name='timer';]], function(...) found=true return 0 end)
diff --git a/plugins/util.lua b/plugins/util.lua
new file mode 100644
index 00000000000..6b2d7f438bf
--- /dev/null
+++ b/plugins/util.lua
@@ -0,0 +1,55 @@
+local lfs = require("lfs")
+
+-- Returns true if dirname is an existing directory, false if not a directory,
+-- or nil, an error message and a system dependent error code on error.
+local function is_dir(dirname)
+ local ret, err, code = lfs.attributes(dirname, "mode")
+ if ret == nil then
+ return ret, err, code
+ else
+ return ret == "directory"
+ end
+end
+
+-- Get the directory name for the file.
+local function dirname(filename)
+ if filename == "/" then
+ return "/"
+ end
+ local parent = filename:match("(.*)/.")
+ if parent then
+ if parent == "" then
+ parent = "/"
+ end
+ return parent
+ end
+ return "."
+end
+
+-- Create dir and parents for dir if needed. Returns true on success,
+-- nil, an error message and a system dependent error code on error.
+local function mkdir_recursive(dir)
+ local ret, err, code = is_dir(dir)
+ if ret == true then
+ return true
+ end
+ local parent = dirname(dir)
+ local ret, err, code = mkdir_recursive(parent)
+ if not ret then
+ return ret, err, code
+ end
+ return lfs.mkdir(dir)
+end
+
+-- Create the parents of the file recursively if needed, returns true on success,
+-- or nil, an error message and a system dependent error code on error.
+local function create_parent_dirs(filename)
+ local parent = dirname(filename)
+ return mkdir_recursive(parent)
+end
+
+return {
+ dirname = dirname,
+ mkdir_recursive = mkdir_recursive,
+ create_parent_dirs = create_parent_dirs
+}