From 699ef428bfaffe495f7f4ffed9886ecd962a1058 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 3 Jan 2023 23:46:02 +1000 Subject: [PATCH] Fix `mainmemory` lib keeping reference to unloaded core's memdomain --- .../lua/LuaHelperLibs/MainMemoryLuaLibrary.cs | 6 ++++++ src/BizHawk.Client.Common/lua/LuaLibraryBase.cs | 5 +++++ src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs | 1 + 3 files changed, 12 insertions(+) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs index 2881224264..5c012bd8bf 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs @@ -25,6 +25,12 @@ namespace BizHawk.Client.Common private string MainMemName => _mainMemName ??= APIs.Memory.MainMemoryName; + public override void Restarted() + { + _mainMemDomain = null; + _mainMemName = null; + } + [LuaMethodExample("local stmaiget = mainmemory.getname( );")] [LuaMethod("getname", "returns the name of the domain defined as main memory for the given core")] public string GetName() diff --git a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs index 6a7c819d5f..40e52e5fc0 100644 --- a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -1,6 +1,8 @@ using System; using System.Threading; +using BizHawk.Emulation.Common; + namespace BizHawk.Client.Common { public abstract class LuaLibraryBase @@ -40,6 +42,9 @@ namespace BizHawk.Client.Common } } + /// for implementors to reset any fields whose value depends on or a service + public virtual void Restarted() {} + /// attempted to have Lua running in two host threads at once public static void SetCurrentThread(LuaFile luaFile) { diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs index 6ac1344fae..440b7cfbe1 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs @@ -171,6 +171,7 @@ namespace BizHawk.Client.EmuHawk { lib.APIs = _apiContainer; ServiceInjector.UpdateServices(newServiceProvider, lib); + lib.Restarted(); } }