diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs index 85bdb4fdf1..1f9c76379b 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs @@ -9,18 +9,26 @@ namespace BizHawk.Client.Common { private readonly LuaFunctionList _luaFunctions = new LuaFunctionList(); - public EventLuaLibrary(Action logOutputCallback) + public EventLuaLibrary(Action logOutputCallback) { LogOutputCallback = logOutputCallback; } public override string Name { get { return "event"; } } - public Action LogOutputCallback { get; set; } + public Action LogOutputCallback { get; set; } public Lua CurrentThread { get; set; } #region Events Library Helpers + private void Log(string message) + { + if (LogOutputCallback != null) + { + LogOutputCallback(message); + } + } + public LuaFunctionList RegisteredFunctions { get { return _luaFunctions; } } public void CallSaveStateEvent(string name) @@ -37,7 +45,7 @@ namespace BizHawk.Client.Common } catch (SystemException e) { - LogOutputCallback( + Log( "error running function attached by lua function event.onsavestate" + "\nError message: " + e.Message); @@ -59,7 +67,7 @@ namespace BizHawk.Client.Common } catch (SystemException e) { - LogOutputCallback( + Log( "error running function attached by lua function event.onloadstate" + "\nError message: " + e.Message); @@ -81,7 +89,7 @@ namespace BizHawk.Client.Common } catch (SystemException e) { - LogOutputCallback( + Log( "error running function attached by lua function event.onframestart" + "\nError message: " + e.Message); @@ -103,7 +111,7 @@ namespace BizHawk.Client.Common } catch (SystemException e) { - LogOutputCallback( + Log( "error running function attached by lua function event.onframeend" + "\nError message: " + e.Message); diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs index e9660ffb60..55e65ed9b7 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs @@ -15,9 +15,18 @@ namespace BizHawk.Client.Common } public override string Name { get { return "mainmemory"; } } + public Action LogOutputCallback { get; set; } #region Main Memory Library Helpers + private void Log(object message) + { + if (LogOutputCallback != null) + { + LogOutputCallback(message.ToString()); + } + } + private static int U2S(uint u, int size) { var s = (int)u; @@ -121,12 +130,21 @@ namespace BizHawk.Client.Common { var lastAddr = length + addr; var table = _lua.NewTable(); - for (var i = addr; i <= lastAddr; i++) + if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size) { - var a = string.Format("{0:X2}", i); - var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i); - var vs = string.Format("{0:X2}", (int)v); - table[a] = vs; + for (var i = addr; i <= lastAddr; i++) + { + var a = string.Format("{0:X2}", i); + var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i); + var vs = string.Format("{0:X2}", (int)v); + table[a] = vs; + } + } + else + { + Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " + + Global.Emulator.MemoryDomains.MainMemory.Size + + " in mainmemory.readbyterange()"); } return table; @@ -160,9 +178,19 @@ namespace BizHawk.Client.Common { foreach (var address in memoryblock.Keys) { - Global.Emulator.MemoryDomains.MainMemory.PokeByte( - LuaInt(address), - (byte)LuaInt(memoryblock[address])); + var addr = LuaInt(address); + if (addr < Global.Emulator.MemoryDomains.MainMemory.Size) + { + Global.Emulator.MemoryDomains.MainMemory.PokeByte( + addr, + (byte)LuaInt(memoryblock[address])); + } + else + { + Log("Warning: Attempted read " + addr + " outside memory domain size of " + + Global.Emulator.MemoryDomains.MainMemory.Size + + " in mainmemory.writebyterange()"); + } } } diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs index 6b23c8ec47..5a9e2fe2eb 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs @@ -16,9 +16,18 @@ namespace BizHawk.Client.Common } public override string Name { get { return "memory"; } } + public Action LogOutputCallback { get; set; } #region Memory Library Helpers + private void Log(object message) + { + if (LogOutputCallback != null) + { + LogOutputCallback(message.ToString()); + } + } + private static int U2S(uint u, int size) { var s = (int)u; @@ -120,12 +129,22 @@ namespace BizHawk.Client.Common { var lastAddr = length + addr; var table = _lua.NewTable(); - for (var i = addr; i <= lastAddr; i++) + + if (lastAddr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size) { - var a = string.Format("{0:X2}", i); - var v = Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekByte(i); - var vs = string.Format("{0:X2}", (int)v); - table[a] = vs; + for (var i = addr; i <= lastAddr; i++) + { + var a = string.Format("{0:X2}", i); + var v = Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekByte(i); + var vs = string.Format("{0:X2}", (int)v); + table[a] = vs; + } + } + else + { + Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " + + Global.Emulator.MemoryDomains[_currentMemoryDomain].Size + + " in memory.readbyterange()"); } return table; @@ -139,9 +158,19 @@ namespace BizHawk.Client.Common { foreach (var address in memoryblock.Keys) { - Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeByte( - LuaInt(address), - (byte)LuaInt(memoryblock[address])); + var addr = LuaInt(address); + if (addr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size) + { + Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeByte( + addr, + (byte)LuaInt(memoryblock[address])); + } + else + { + Log("Warning: Attempted read " + addr + " outside memory domain size of " + + Global.Emulator.MemoryDomains[_currentMemoryDomain].Size + + " in memory.writebyterange()"); + } } } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index d61a845c83..233b9974f1 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -15,7 +15,7 @@ namespace BizHawk.Client.EmuHawk { 4, "Scanlines" }, }; - public EmuHawkLuaLibrary(Action logOutputCallback) + public EmuHawkLuaLibrary(Action logOutputCallback) : this() { LogOutputCallback = logOutputCallback; @@ -24,7 +24,7 @@ namespace BizHawk.Client.EmuHawk public EmuHawkLuaLibrary() { } public override string Name { get { return "client"; } } - public Action LogOutputCallback { get; set; } + public Action LogOutputCallback { get; set; } private void Log(string message) { diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Console.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Console.cs index a3e7dc2c09..e8270185ea 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Console.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Console.cs @@ -44,10 +44,10 @@ namespace BizHawk.Client.EmuHawk LogWithSeparator("\t", "\n", outputs); } - // Single param version is used by logOutputCallback of some libraries. - public static void Log(string output) + //// Single param version is used by logOutputCallback of some libraries. + public static void LogOutput(object output) { - Log((object)output); + Log(output); } [LuaMethodAttributes( diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index a316bdfa90..b3bf2093b9 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -9,7 +9,7 @@ namespace BizHawk.Client.EmuHawk public class EmuLuaLibrary { private readonly FormsLuaLibrary _formsLibrary = new FormsLuaLibrary(); - private readonly EventLuaLibrary _eventLibrary = new EventLuaLibrary(ConsoleLuaLibrary.Log); + private readonly EventLuaLibrary _eventLibrary = new EventLuaLibrary(ConsoleLuaLibrary.LogOutput); private readonly GuiLuaLibrary _guiLibrary = new GuiLuaLibrary(); private readonly LuaConsole _caller; @@ -82,7 +82,7 @@ namespace BizHawk.Client.EmuHawk lua.RegisterFunction("print", this, GetType().GetMethod("Print")); new BitLuaLibrary().LuaRegister(lua, Docs); - new EmuHawkLuaLibrary(ConsoleLuaLibrary.Log).LuaRegister(lua, Docs); + new EmuHawkLuaLibrary(ConsoleLuaLibrary.LogOutput).LuaRegister(lua, Docs); new ConsoleLuaLibrary().LuaRegister(lua, Docs); new EmulatorLuaLibrary( @@ -95,14 +95,26 @@ namespace BizHawk.Client.EmuHawk _guiLibrary.LuaRegister(lua, Docs); new InputLuaLibrary(_lua).LuaRegister(lua, Docs); new JoypadLuaLibrary(_lua).LuaRegister(lua, Docs); - new MemoryLuaLibrary(_lua).LuaRegister(lua, Docs); - new MainMemoryLuaLibrary(_lua).LuaRegister(lua, Docs); + + var memory = new MemoryLuaLibrary(_lua) + { + LogOutputCallback = ConsoleLuaLibrary.LogOutput + }; + memory.LuaRegister(lua, Docs); + + var mainmemory = new MainMemoryLuaLibrary(_lua) + { + LogOutputCallback = ConsoleLuaLibrary.LogOutput + }; + mainmemory.LuaRegister(lua, Docs); + new MovieLuaLibrary(_lua).LuaRegister(lua, Docs); new NesLuaLibrary().LuaRegister(lua, Docs); new SavestateLuaLibrary().LuaRegister(lua, Docs); new SnesLuaLibrary().LuaRegister(lua, Docs); new StringLuaLibrary(_lua).LuaRegister(lua, Docs); new GameInfoLuaLibrary(_lua).LuaRegister(lua, Docs); + Docs.Sort(); }