read/write byte range functions - range check, and log warnings if user attempts to access outside the domain range, rather than throw a vague lua exception and crash

This commit is contained in:
adelikat 2014-05-19 01:42:41 +00:00
parent e1a5123dcf
commit 4ee4088c4c
6 changed files with 108 additions and 31 deletions

View File

@ -9,18 +9,26 @@ namespace BizHawk.Client.Common
{
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList();
public EventLuaLibrary(Action<string> logOutputCallback)
public EventLuaLibrary(Action<object> logOutputCallback)
{
LogOutputCallback = logOutputCallback;
}
public override string Name { get { return "event"; } }
public Action<string> LogOutputCallback { get; set; }
public Action<object> 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);

View File

@ -15,9 +15,18 @@ namespace BizHawk.Client.Common
}
public override string Name { get { return "mainmemory"; } }
public Action<string> 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()");
}
}
}

View File

@ -16,9 +16,18 @@ namespace BizHawk.Client.Common
}
public override string Name { get { return "memory"; } }
public Action<string> 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()");
}
}
}

View File

@ -15,7 +15,7 @@ namespace BizHawk.Client.EmuHawk
{ 4, "Scanlines" },
};
public EmuHawkLuaLibrary(Action<string> logOutputCallback)
public EmuHawkLuaLibrary(Action<object> logOutputCallback)
: this()
{
LogOutputCallback = logOutputCallback;
@ -24,7 +24,7 @@ namespace BizHawk.Client.EmuHawk
public EmuHawkLuaLibrary() { }
public override string Name { get { return "client"; } }
public Action<string> LogOutputCallback { get; set; }
public Action<object> LogOutputCallback { get; set; }
private void Log(string message)
{

View File

@ -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(

View File

@ -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();
}