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
This commit is contained in:
Szunti 2021-05-04 02:40:10 +02:00 committed by GitHub
parent a90f1c885d
commit 25137717c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 81 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

55
plugins/util.lua Normal file
View File

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