mame/plugins/timer/init.lua
Vas Crabb d64ea5331b
-frontend: Refactored menu event handling and fixed a number of issues. (#8777)
* Moved common code for drawing about box, info viewer, and other text box menus to a base class; removed the last of the info viewer logic and the multi-line item hack from the base menu class.
* Added previous/next group navigation for general inputs and plugin input selection menus.
* Moved message catalog logic to lib/util, allowing osd and emu to use localised messages.
* Made the base menu class use the UI manager’s feature for holding session state rather than a static map and mutex.
* Improved menu event handling model, and fixed many issues, particularly with menus behaving badly when hidden/shown.
* Added better support for menus that don’t participate in the usual menu stack, like the menuless sliders and the save/load state menus.
* Made a number of menus refresh state when being shown after being hidden (fixes MT08121 among other issues).
* Fixed indication of mounted slot option in the slot option details menu.
* Improved appearance of background menus when emulation isn't running - draw all menus in the stack, and darken the background menus to make the edges of the active menu clearer.
* Fixed locale issues in -listxml.

-debugger: Made GUI debuggers more uniform.
* Added new memory view features to Win32 debugger.
* Fixed spelling of hexadecimal in Cocoa debugger and added decimal address option.
* Fixed duplicate keyboard shortcut in Cocoa debugger (Shift-Cmd-D was both new device window and 64-bit float format).
* Made keyboard shortcuts slightly more consistent across debuggers.

-plugins: Moved input selection menu and sequence polling code to a common library.  Fixed the issue that prevented keyboard inputs being mapped with -steadykey on.

-docs: Started adding some documentation for MAME's internal UI, and updated the list of example front-ends.

-Regenerated message catalog sources.  For translators, the new strings are mostly:
* The names of the inputs provided by the OS-dependent layer for things like fullscreen and video features. These show up in the user interface inputs menu.
* The names for automatically generated views. These show up in the video options menu - test with a system with a lot of screens to see more variants.
* The input macro plugin UI.
* A few format strings for analog input assignments.
* A few strings for the about box header.
2021-10-31 12:31:16 +11:00

113 lines
2.9 KiB
Lua

-- license:BSD-3-Clause
-- copyright-holders:Carl
require('lfs')
local sqlite3 = require('lsqlite3')
local exports = {}
exports.name = "timer"
exports.version = "0.0.2"
exports.description = "Game play timer"
exports.license = "BSD-3-Clause"
exports.author = { name = "Carl" }
local timer = exports
function timer.startplugin()
local dir = emu.subst_env(manager.options.entries.homepath:value())
local timer_db = dir .. "/timer/timer.db"
local timer_started = false
local total_time = 0
local start_time = 0
local play_count = 0
local function save()
total_time = total_time + (os.time() - start_time)
local db = assert(sqlite3.open(timer_db))
local insert_stmt = assert( db:prepare("INSERT OR IGNORE INTO timer VALUES (?, ?, 0, 0)") )
insert_stmt:bind_values(emu.romname(), emu.softname())
insert_stmt:step()
insert_stmt:reset()
local update_stmt = assert( db:prepare("UPDATE timer SET total_time=?, play_count=? WHERE driver=? AND software=?") )
update_stmt:bind_values(total_time, play_count,emu.romname(), emu.softname())
update_stmt:step()
update_stmt:reset()
assert(db:close() == sqlite3.OK)
end
emu.register_start(function()
local file
if timer_started then
save()
end
timer_started = true
lfs.mkdir(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)
if not found then
db:exec[[ CREATE TABLE timer (
driver VARCHAR(32) PRIMARY KEY,
software VARCHAR(40),
total_time INTEGER NOT NULL,
play_count INTEGER NOT NULL
); ]]
end
local stmt, row
stmt = db:prepare("SELECT total_time, play_count FROM timer WHERE driver = ? AND software = ?")
stmt:bind_values(emu.romname(), emu.softname())
if (stmt:step() == sqlite3.ROW) then
row = stmt:get_named_values()
play_count = row.play_count
total_time = row.total_time
else
play_count = 0
total_time = 0
end
assert(db:close() == sqlite3.OK)
start_time = os.time()
play_count = play_count + 1
end)
emu.register_stop(function()
timer_started = false
save()
total_time = 0
play_count = 0
end)
local function sectohms(time)
local hrs = math.floor(time / 3600)
local min = math.floor((time % 3600) / 60)
local sec = time % 60
return string.format(_p("plugin-timer", "%03d:%02d:%02d"), hrs, min, sec)
end
local lastupdate
local function menu_populate()
lastupdate = os.time()
local time = lastupdate - start_time
return
{{ _p("plugin-timer", "Current time"), sectohms(time), "off" },
{ _p("plugin-timer", "Total time"), sectohms(total_time + time), "off" },
{ _p("plugin-timer", "Play Count"), play_count, "off" }},
nil,
"idle"
end
local function menu_callback(index, event)
return os.time() > lastupdate
end
emu.register_menu(menu_callback, menu_populate, _p("plugin-timer", "Timer"))
end
return exports