MAME Lua插件:替换弃用函数emu.register_start/register_stop遇阻求助
MAME Lua插件:替换废弃启动/停止回调函数后无法运行的修复方案
问题描述
我开发了一款用于MAME的Lua插件,通过两个PowerShell脚本实现物理屏幕在横竖方向间切换。原脚本使用emu.register_start()和emu.register_stop()能稳定运行,但这两个函数已被废弃,官方推荐使用emu.add_machine_reset_notifier和emu.add_machine_stop_notifier替代。我替换后代码无法正常工作,寻求修复方案。
原代码
local exports = {} exports.name = "screenrotator" exports.version = "0.0.1" exports.description = "Rotate Display" exports.license = "The BSD 3-Clause License" exports.author = { name = "Daren" } -- Define file locations for portrait and landscape scripts local portraitScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\portrait.ps1" local landscapeScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\landscape.ps1" -- Flag to track if the plugin start function has been called local startCalled = false -- Define the plugin functions function exports.startplugin() -- Check if the start function has already been called if not startCalled then -- Set flag to true to indicate that the start function has been called startCalled = true emu.register_start(function() local screenrotation = manager.machine.system.orientation print(screenrotation) if screenrotation == "rot270" or screenrotation == "rot90" then print("Vertical game. Rotating...") os.execute("powershell.exe -file " .. portraitScript) else print("Horizontal game. Not Rotating...") end end) emu.register_stop(function() local screenrotation = manager.machine.system.orientation print(screenrotation) if screenrotation == "rot270" or screenrotation == "rot90" then print("Vertical game. Rotating back...") os.execute("powershell.exe -file " .. landscapeScript) else print("Horizontal game. No need to rotate back.") end end) else print("Start function already called. Skipping...") end end -- Call the plugin initialization function exports.startplugin() return exports
修复后的代码
local exports = {} exports.name = "screenrotator" exports.version = "0.0.1" exports.description = "Rotate Display" exports.license = "The BSD 3-Clause License" exports.author = { name = "Daren" } -- Define file locations for portrait and landscape scripts local portraitScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\portrait.ps1" local landscapeScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\landscape.ps1" -- Flag to track if the plugin start function has been called local startCalled = false -- Define the plugin functions function exports.startplugin() if not startCalled then startCalled = true -- 替换废弃的emu.register_start,使用机器重置通知(启动时触发) emu.add_machine_reset_notifier(function() local screenrotation = manager.machine.system.orientation print(screenrotation) if screenrotation == "rot270" or screenrotation == "rot90" then print("Vertical game. Rotating...") os.execute("powershell.exe -file " .. portraitScript) else print("Horizontal game. Not Rotating...") end end) -- 替换废弃的emu.register_stop,使用机器停止通知 emu.add_machine_stop_notifier(function(machine) -- 直接使用回调传入的machine实例获取方向,比manager.machine更可靠 local screenrotation = machine.system.orientation print(screenrotation) if screenrotation == "rot270" or screenrotation == "rot90" then print("Vertical game. Rotating back...") os.execute("powershell.exe -file " .. landscapeScript) else print("Horizontal game. No need to rotate back.") end end) else print("Start function already called. Skipping...") end end exports.startplugin() return exports
关键修复点
- 启动回调替换:
emu.register_start()被emu.add_machine_reset_notifier()替代,该函数会在机器启动或重置时触发,与原函数的触发时机完全匹配。 - 停止回调替换:
emu.register_stop()被emu.add_machine_stop_notifier()替代,回调函数会接收当前机器实例作为参数,推荐直接使用该参数获取系统方向,避免依赖全局的manager.machine。 - 逻辑保留:原有的屏幕方向判断、PowerShell脚本执行逻辑完全保留,确保功能不变。
验证注意事项
- 确认你的MAME版本(0.246及以上)支持这两个新函数(你使用的0.263版本完全兼容)。
- 检查PowerShell脚本路径是否正确,确保MAME有权限执行PowerShell命令。
内容的提问来源于stack exchange,提问作者Daren Page
相关产品推荐
相关产品推荐

