mame/plugins/timer/init.lua
Vas Crabb e6588480c4
Lua engine improvements (#9453)
Made auto-boot script errors and plugin bootstrap errors fatal.

Run auto-boot scripts in a sandbox.  Globals can be accessed, but not
set.  The sandbox is cleared on hard reset, but not on soft reset.

Added (hopefully) useful to string metafunctions to device_t and
address space that show short names and tags.

Fixed issues in plugins that surface when strict type checking is
enabled, as this means numbers and nil are not automatically converted
to strings.  Plugins should be tested with debug builds to check for
this.

Made save item read_block raise an error on invalid arguments rather
than returning an empty string, and made it use luaL_buffer directly
rather than using the helper wrapper.

Changed some more function bindings to use set_function to avoid issues
related to ThePhD/sol2#608, and got rid of some unnecessary lambda
captures.
2022-03-23 20:27:30 +11:00

77 lines
2.1 KiB
Lua

-- license:BSD-3-Clause
-- copyright-holders:Vas Crabb
local exports = {
name = 'timer',
version = '0.0.3',
description = 'Game play timer',
license = 'BSD-3-Clause',
author = { name = 'Vas Crabb' } }
local timer = exports
function timer.startplugin()
local total_time = 0
local start_time = 0
local play_count = 0
local emu_total = emu.attotime()
local reference = 0
local lastupdate
local highlight -- hacky - workaround for the menu not remembering the selected item if its ref is nullptr
local function sectohms(time)
local hrs = time // 3600
local min = (time % 3600) // 60
local sec = time % 60
return string.format(_p('plugin-timer', '%03d:%02d:%02d'), hrs, min, sec)
end
local function menu_populate()
lastupdate = os.time()
local refname = (reference == 0) and _p('plugin-timer', 'Wall clock') or _p('plugin-timer', 'Emulated time')
local time = (reference == 0) and (lastupdate - start_time) or manager.machine.time.seconds
local total = (reference == 0) and (total_time + time) or (manager.machine.time + emu_total).seconds
return
{
{ _p("plugin-timer", "Reference"), refname, (reference == 0) and 'r' or 'l' },
{ '---', '', '' },
{ _p("plugin-timer", "Current time"), sectohms(time), "off" },
{ _p("plugin-timer", "Total time"), sectohms(total), "off" },
{ _p("plugin-timer", "Play Count"), tostring(play_count), "off" } },
highlight,
"idle"
end
local function menu_callback(index, event)
if (index == 1) and ((event == 'left') or (event == 'right') or (event == 'select')) then
reference = reference ~ 1
return true
end
highlight = index
return os.time() > lastupdate
end
emu.register_start(
function()
if emu.romname() ~= '___empty' then
start_time = os.time()
local persister = require('timer/timer_persist')
total_time, play_count, emu_total = persister:start_session()
end
end)
emu.register_stop(
function()
if emu.romname() ~= '___empty' then
local persister = require('timer/timer_persist')
persister:update_totals(start_time)
end
end)
emu.register_menu(menu_callback, menu_populate, _p("plugin-timer", "Timer"))
end
return exports