move emu library to its own object, remove emu_onsnoop (should use event library instead), moved many multiclient only functions into the client library instead (todo: document those)

This commit is contained in:
adelikat 2013-10-31 18:09:40 +00:00
parent 1cd62c2df1
commit 4bb60ec8c9
4 changed files with 164 additions and 168 deletions

View File

@ -21,7 +21,10 @@ namespace BizHawk.MultiClient
return new [] return new []
{ {
"closerom", "closerom",
"enablerewind",
"frameskip",
"getwindowsize", "getwindowsize",
"ispaused",
"opencheats", "opencheats",
"openhexeditor", "openhexeditor",
"openramwatch", "openramwatch",
@ -30,6 +33,7 @@ namespace BizHawk.MultiClient
"opentasstudio", "opentasstudio",
"opentoolbox", "opentoolbox",
"opentracelogger", "opentracelogger",
"pause",
"pause_av", "pause_av",
"reboot_core", "reboot_core",
"screenheight", "screenheight",
@ -38,6 +42,9 @@ namespace BizHawk.MultiClient
"screenwidth", "screenwidth",
"setscreenshotosd", "setscreenshotosd",
"setwindowsize", "setwindowsize",
"speedmode",
"togglepause",
"unpause",
"unpause_av", "unpause_av",
"xpos", "xpos",
"ypos", "ypos",
@ -52,6 +59,51 @@ namespace BizHawk.MultiClient
GlobalWinF.MainForm.CloseROM(); GlobalWinF.MainForm.CloseROM();
} }
public static void client_enablerewind(object boolean)
{
string temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp))
{
if (temp == "0" || temp.ToLower() == "false")
{
GlobalWinF.MainForm.RewindActive = false;
GlobalWinF.OSD.AddMessage("Rewind suspended");
}
else
{
GlobalWinF.MainForm.RewindActive = true;
GlobalWinF.OSD.AddMessage("Rewind enabled");
}
}
}
public void client_frameskip(object num_frames)
{
try
{
string temp = num_frames.ToString();
int frames = Convert.ToInt32(temp);
if (frames > 0)
{
Global.Config.FrameSkip = frames;
GlobalWinF.MainForm.FrameSkipMessage();
}
else
{
ConsoleLuaLibrary.console_log("Invalid frame skip value");
}
}
catch
{
ConsoleLuaLibrary.console_log("Invalid frame skip value");
}
}
public static bool client_ispaused()
{
return GlobalWinF.MainForm.EmulatorPaused;
}
public static int client_getwindowsize() public static int client_getwindowsize()
{ {
return Global.Config.TargetZoomFactor; return Global.Config.TargetZoomFactor;
@ -97,6 +149,11 @@ namespace BizHawk.MultiClient
GlobalWinF.MainForm.LoadTraceLogger(); GlobalWinF.MainForm.LoadTraceLogger();
} }
public static void client_pause()
{
GlobalWinF.MainForm.PauseEmulator();
}
public static void client_pause_av() public static void client_pause_av()
{ {
GlobalWinF.MainForm.PauseAVI = true; GlobalWinF.MainForm.PauseAVI = true;
@ -163,7 +220,36 @@ namespace BizHawk.MultiClient
{ {
LogOutputCallback("Invalid window size"); LogOutputCallback("Invalid window size");
} }
}
public void client_speedmode(object percent)
{
try
{
int speed = Convert.ToInt32(percent.ToString());
if (speed > 0 && speed < 1600) //arbituarily capping it at 1600%
{
GlobalWinF.MainForm.ClickSpeedItem(speed);
}
else
{
ConsoleLuaLibrary.console_log("Invalid speed value");
}
}
catch
{
ConsoleLuaLibrary.console_log("Invalid speed value");
}
}
public static void client_togglepause()
{
GlobalWinF.MainForm.TogglePause();
}
public static void client_unpause()
{
GlobalWinF.MainForm.UnpauseEmulator();
} }
public static void client_unpause_av() public static void client_unpause_av()

View File

@ -5,12 +5,40 @@ using BizHawk.Emulation.Consoles.Nintendo;
namespace BizHawk.MultiClient namespace BizHawk.MultiClient
{ {
public partial class EmuLuaLibrary public partial class EmulatorLuaLibrary : LuaLibraryBase
{ {
#region Emu Library Helpers public EmulatorLuaLibrary(Action frameAdvanceCallback, Action yieldCallback)
: base()
{
_frameAdvanceCallback = frameAdvanceCallback;
_yieldCallback = yieldCallback;
}
// TODO: error handling for argument count mismatch public override string Name { get { return "emu"; } }
private void emu_setrenderplanes_do(object[] lua_p) public override string[] Functions
{
get
{
return new[]
{
"displayvsync",
"frameadvance",
"framecount",
"getsystemid",
"islagged",
"lagcount",
"limitframerate",
"minimizeframeskip",
"setrenderplanes",
"yield",
};
}
}
private Action _frameAdvanceCallback;
private Action _yieldCallback;
private static void emu_setrenderplanes_do(object[] lua_p)
{ {
if (Global.Emulator is NES) if (Global.Emulator is NES)
{ {
@ -34,9 +62,7 @@ namespace BizHawk.MultiClient
} }
} }
#endregion public static void emu_displayvsync(object boolean)
public void emu_displayvsync(object boolean)
{ {
string temp = boolean.ToString(); string temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
@ -53,78 +79,32 @@ namespace BizHawk.MultiClient
} }
} }
public void emu_enablerewind(object boolean)
{
string temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp))
{
if (temp == "0" || temp.ToLower() == "false")
{
GlobalWinF.MainForm.RewindActive = false;
GlobalWinF.OSD.AddMessage("Rewind suspended");
}
else
{
GlobalWinF.MainForm.RewindActive = true;
GlobalWinF.OSD.AddMessage("Rewind enabled");
}
}
}
public void emu_frameadvance() public void emu_frameadvance()
{ {
FrameAdvanceRequested = true; _frameAdvanceCallback();
currThread.Yield(0);
} }
public int emu_framecount() public static int emu_framecount()
{ {
return Global.Emulator.Frame; return Global.Emulator.Frame;
} }
public void emu_frameskip(object num_frames) public static string emu_getsystemid()
{
try
{
string temp = num_frames.ToString();
int frames = Convert.ToInt32(temp);
if (frames > 0)
{
Global.Config.FrameSkip = frames;
GlobalWinF.MainForm.FrameSkipMessage();
}
else
{
ConsoleLuaLibrary.console_log("Invalid frame skip value");
}
}
catch
{
ConsoleLuaLibrary.console_log("Invalid frame skip value");
}
}
public string emu_getsystemid()
{ {
return Global.Emulator.SystemId; return Global.Emulator.SystemId;
} }
public bool emu_islagged() public static bool emu_islagged()
{ {
return Global.Emulator.IsLagFrame; return Global.Emulator.IsLagFrame;
} }
public bool emu_ispaused() public static int emu_lagcount()
{
return GlobalWinF.MainForm.EmulatorPaused;
}
public int emu_lagcount()
{ {
return Global.Emulator.LagCount; return Global.Emulator.LagCount;
} }
public void emu_limitframerate(object boolean) public static void emu_limitframerate(object boolean)
{ {
string temp = boolean.ToString(); string temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
@ -141,7 +121,7 @@ namespace BizHawk.MultiClient
} }
} }
public void emu_minimizeframeskip(object boolean) public static void emu_minimizeframeskip(object boolean)
{ {
string temp = boolean.ToString(); string temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
@ -158,75 +138,16 @@ namespace BizHawk.MultiClient
} }
} }
public void emu_on_snoop(LuaFunction luaf) public static void emu_setrenderplanes( // For now, it accepts arguments up to 5.
{
if (luaf != null)
{
Global.Emulator.CoreComm.InputCallback = delegate
{
try
{
luaf.Call();
}
catch (SystemException e)
{
GlobalWinF.MainForm.LuaConsole1.WriteToOutputWindow(
"error running function attached by lua function emu.on_snoop" +
"\nError message: " + e.Message);
}
};
}
else
Global.Emulator.CoreComm.InputCallback = null;
}
public void emu_pause()
{
GlobalWinF.MainForm.PauseEmulator();
}
public void emu_setrenderplanes( // For now, it accepts arguments up to 5.
object lua_p0, object lua_p1 = null, object lua_p2 = null, object lua_p0, object lua_p1 = null, object lua_p2 = null,
object lua_p3 = null, object lua_p4 = null) object lua_p3 = null, object lua_p4 = null)
{ {
emu_setrenderplanes_do(LuaVarArgs(lua_p0, lua_p1, lua_p2, lua_p3, lua_p4)); emu_setrenderplanes_do(LuaVarArgs(lua_p0, lua_p1, lua_p2, lua_p3, lua_p4));
} }
public void emu_speedmode(object percent)
{
try
{
string temp = percent.ToString();
int speed = Convert.ToInt32(temp);
if (speed > 0 && speed < 1000) //arbituarily capping it at 1000%
{
GlobalWinF.MainForm.ClickSpeedItem(speed);
}
else
{
ConsoleLuaLibrary.console_log("Invalid speed value");
}
}
catch
{
ConsoleLuaLibrary.console_log("Invalid speed value");
}
}
public void emu_togglepause()
{
GlobalWinF.MainForm.TogglePause();
}
public void emu_unpause()
{
GlobalWinF.MainForm.UnpauseEmulator();
}
public void emu_yield() public void emu_yield()
{ {
GlobalWinF.DisplayManager.NeedsToPaint = true; _yieldCallback();
currThread.Yield(0);
} }
} }
} }

View File

@ -86,28 +86,6 @@ namespace BizHawk.MultiClient
"text", "text",
}; };
public static string[] EmuFunctions = new[]
{
"displayvsync",
"enablerewind",
"frameadvance",
"framecount",
"frameskip",
"getsystemid",
"islagged",
"ispaused",
"lagcount",
"limitframerate",
"minimizeframeskip",
"on_snoop",
"pause",
"setrenderplanes",
"speedmode",
"togglepause",
"unpause",
"yield",
};
public void LuaRegister(Lua lua) public void LuaRegister(Lua lua)
{ {
lua.RegisterFunction("print", this, GetType().GetMethod("print")); lua.RegisterFunction("print", this, GetType().GetMethod("print"));
@ -115,6 +93,12 @@ namespace BizHawk.MultiClient
new BitLuaLibrary().LuaRegister(lua, Docs); new BitLuaLibrary().LuaRegister(lua, Docs);
new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs); new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs);
new ConsoleLuaLibrary().LuaRegister(lua, Docs); new ConsoleLuaLibrary().LuaRegister(lua, Docs);
new EmulatorLuaLibrary(
new Action(Frameadvance),
new Action(EmuYield)
).LuaRegister(lua, Docs);
_eventLibrary.LuaRegister(lua, Docs); _eventLibrary.LuaRegister(lua, Docs);
_formsLibrary.LuaRegister(lua, Docs); _formsLibrary.LuaRegister(lua, Docs);
new InputLuaLibrary(_lua).LuaRegister(lua, Docs); new InputLuaLibrary(_lua).LuaRegister(lua, Docs);
@ -133,13 +117,6 @@ namespace BizHawk.MultiClient
Docs.Add("gui", t, GetType().GetMethod("gui_" + t)); Docs.Add("gui", t, GetType().GetMethod("gui_" + t));
} }
lua.NewTable("emu");
foreach (string t in EmuFunctions)
{
lua.RegisterFunction("emu." + t, this, GetType().GetMethod("emu_" + t));
Docs.Add("emu", t, GetType().GetMethod("emu_" + t));
}
Docs.Sort(); Docs.Sort();
} }
@ -156,24 +133,6 @@ namespace BizHawk.MultiClient
return t; return t;
} }
/// <summary>
/// LuaInterface requires the exact match of parameter count, except optional parameters.
/// So, if you want to support variable arguments, declare them as optional and pass
/// them to this method.
/// </summary>
/// <param name="lua_args"></param>
/// <returns></returns>
private object[] LuaVarArgs(params object[] lua_args)
{
int n = lua_args.Length;
int trim = 0;
for (int i = n - 1; i >= 0; --i)
if (lua_args[i] == null) ++trim;
object[] lua_result = new object[n - trim];
Array.Copy(lua_args, lua_result, n - trim);
return lua_result;
}
public class ResumeResult public class ResumeResult
{ {
public bool WaitForFrame; public bool WaitForFrame;
@ -205,6 +164,18 @@ namespace BizHawk.MultiClient
_caller.AddText(s); _caller.AddText(s);
} }
private void Frameadvance()
{
FrameAdvanceRequested = true;
currThread.Yield(0);
}
private void EmuYield()
{
GlobalWinF.DisplayManager.NeedsToPaint = true;
currThread.Yield(0);
}
#endregion #endregion
} }
} }

View File

@ -39,5 +39,23 @@ namespace BizHawk.MultiClient
{ {
return Convert.ToUInt32((double)lua_arg); return Convert.ToUInt32((double)lua_arg);
} }
/// <summary>
/// LuaInterface requires the exact match of parameter count, except optional parameters.
/// So, if you want to support variable arguments, declare them as optional and pass
/// them to this method.
/// </summary>
/// <param name="lua_args"></param>
/// <returns></returns>
protected static object[] LuaVarArgs(params object[] lua_args)
{
int n = lua_args.Length;
int trim = 0;
for (int i = n - 1; i >= 0; --i)
if (lua_args[i] == null) ++trim;
object[] lua_result = new object[n - trim];
Array.Copy(lua_args, lua_result, n - trim);
return lua_result;
}
} }
} }