2011-02-20 19:18:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2012-01-22 22:20:09 +00:00
|
|
|
|
using System.IO;
|
2011-02-20 19:18:27 +00:00
|
|
|
|
using LuaInterface;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using BizHawk.MultiClient.tools;
|
2012-01-22 23:44:53 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2011-02-20 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
namespace BizHawk.MultiClient
|
|
|
|
|
{
|
2012-01-22 22:20:09 +00:00
|
|
|
|
public class LuaImplementation
|
2011-06-26 02:09:06 +00:00
|
|
|
|
{
|
2012-01-28 21:43:55 +00:00
|
|
|
|
Lua lua = new Lua();
|
2011-06-26 02:09:06 +00:00
|
|
|
|
LuaConsole Caller;
|
2012-01-22 03:14:31 +00:00
|
|
|
|
public String LuaLibraryList = "";
|
2012-01-28 21:43:55 +00:00
|
|
|
|
public EventWaitHandle LuaWait;
|
|
|
|
|
public bool isRunning;
|
|
|
|
|
private Thread LuaThread;
|
2012-01-22 22:42:40 +00:00
|
|
|
|
private int CurrentMemoryDomain = 0; //Main memory by default
|
2012-01-21 20:05:53 +00:00
|
|
|
|
|
|
|
|
|
public LuaImplementation(LuaConsole passed)
|
|
|
|
|
{
|
2012-01-28 21:43:55 +00:00
|
|
|
|
LuaWait = new AutoResetEvent(false);
|
2012-01-22 03:14:31 +00:00
|
|
|
|
LuaLibraryList = "";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
Caller = passed.get();
|
2012-01-28 21:43:55 +00:00
|
|
|
|
LuaRegister(lua);
|
|
|
|
|
}
|
|
|
|
|
public void LuaRegister(Lua lua)
|
|
|
|
|
{
|
2012-01-21 20:05:53 +00:00
|
|
|
|
lua.RegisterFunction("print", this, this.GetType().GetMethod("print"));
|
2012-01-28 21:43:55 +00:00
|
|
|
|
|
2012-01-21 20:05:53 +00:00
|
|
|
|
//Register libraries
|
|
|
|
|
lua.NewTable("console");
|
|
|
|
|
for (int i = 0; i < ConsoleFunctions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("console." + ConsoleFunctions[i], this, this.GetType().GetMethod("console_" + ConsoleFunctions[i]));
|
2012-01-22 03:14:31 +00:00
|
|
|
|
LuaLibraryList += "console." + ConsoleFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua.NewTable("emu");
|
|
|
|
|
for (int i = 0; i < EmuFunctions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("emu." + EmuFunctions[i], this, this.GetType().GetMethod("emu_" + EmuFunctions[i]));
|
2012-01-22 03:14:31 +00:00
|
|
|
|
LuaLibraryList += "emu." + EmuFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua.NewTable("memory");
|
|
|
|
|
for (int i = 0; i < MemoryFunctions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("memory." + MemoryFunctions[i], this, this.GetType().GetMethod("memory_" + MemoryFunctions[i]));
|
2012-01-22 03:14:31 +00:00
|
|
|
|
LuaLibraryList += "memory." + MemoryFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua.NewTable("savestate");
|
|
|
|
|
for (int i = 0; i < SaveStateFunctions.Length; i++)
|
|
|
|
|
{
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//lua.RegisterFunction("statestate." + SaveStateFunctions[i], this, this.GetType().GetMethod("savestate_" + SaveStateFunctions[i]));
|
2012-01-22 03:14:31 +00:00
|
|
|
|
//LuaLibraryList += "savestate." + SaveStateFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua.NewTable("movie");
|
|
|
|
|
for (int i = 0; i < MovieFunctions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("movie." + MovieFunctions[i], this, this.GetType().GetMethod("movie_" + MovieFunctions[i]));
|
2012-01-22 03:14:31 +00:00
|
|
|
|
LuaLibraryList += "movie." + MovieFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua.NewTable("joypad");
|
|
|
|
|
for (int i = 0; i < JoypadFunctions.Length; i++)
|
|
|
|
|
{
|
2012-01-22 03:14:31 +00:00
|
|
|
|
lua.RegisterFunction("joypad." + JoypadFunctions[i], this, this.GetType().GetMethod("joypad_" + JoypadFunctions[i]));
|
|
|
|
|
LuaLibraryList += "joypad." + JoypadFunctions[i] + "\n";
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
2012-01-22 22:20:09 +00:00
|
|
|
|
|
|
|
|
|
lua.NewTable("client");
|
|
|
|
|
for (int i = 0; i < MultiClientFunctions.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
lua.RegisterFunction("client." + MultiClientFunctions[i], this, this.GetType().GetMethod("client_" + MultiClientFunctions[i]));
|
|
|
|
|
LuaLibraryList += "client." + MultiClientFunctions[i] + "\n";
|
|
|
|
|
}
|
2012-01-21 20:05:53 +00:00
|
|
|
|
}
|
2012-01-28 21:43:55 +00:00
|
|
|
|
private void LuaThreadFunction(object File)
|
|
|
|
|
{
|
|
|
|
|
string F = File.ToString();
|
|
|
|
|
isRunning = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lua.DoFile(F);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Exception caught. " + e.ToString());
|
|
|
|
|
}
|
|
|
|
|
isRunning = false;
|
|
|
|
|
LuaWait.Set();
|
|
|
|
|
}
|
2012-01-21 20:05:53 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
public void DoLuaFile(string File)
|
|
|
|
|
{
|
2012-01-28 21:43:55 +00:00
|
|
|
|
LuaThread = new Thread(new ParameterizedThreadStart(LuaThreadFunction));
|
|
|
|
|
LuaThread.Start(File);
|
2012-01-21 20:20:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-28 21:43:55 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
public void print(string s)
|
|
|
|
|
{
|
|
|
|
|
Caller.AddText(string.Format(s));
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-21 20:05:53 +00:00
|
|
|
|
/****************************************************/
|
|
|
|
|
/*************library definitions********************/
|
|
|
|
|
/****************************************************/
|
|
|
|
|
public static string[] ConsoleFunctions = new string[] {
|
2012-01-22 03:14:31 +00:00
|
|
|
|
"output",
|
|
|
|
|
"clear",
|
|
|
|
|
"getluafunctionslist"
|
2012-01-21 20:05:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
public static string[] EmuFunctions = new string[] {
|
2012-01-22 03:14:31 +00:00
|
|
|
|
"frameadvance",
|
2011-06-26 02:09:06 +00:00
|
|
|
|
"pause",
|
|
|
|
|
"unpause",
|
2012-01-21 20:20:06 +00:00
|
|
|
|
"togglepause",
|
|
|
|
|
//"speedmode",
|
2012-01-22 03:14:31 +00:00
|
|
|
|
"framecount",
|
|
|
|
|
"lagcount",
|
|
|
|
|
"islagged",
|
|
|
|
|
"getsystemid"
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//"registerbefore",
|
|
|
|
|
//"registerafter",
|
|
|
|
|
//"register"
|
2011-06-26 02:09:06 +00:00
|
|
|
|
};
|
|
|
|
|
public static string[] MemoryFunctions = new string[] {
|
2012-01-22 22:20:09 +00:00
|
|
|
|
//"usememorydomain",
|
|
|
|
|
//"getmemorydomainlist",
|
|
|
|
|
//"getcurrentmemorydomain",
|
2011-06-26 02:09:06 +00:00
|
|
|
|
"readbyte",
|
|
|
|
|
//"readbytesigned",
|
|
|
|
|
//"readword",
|
|
|
|
|
//"readwordsigned",
|
|
|
|
|
//"readdword",
|
|
|
|
|
//"readdwordsigned",
|
|
|
|
|
//"readbyterange",
|
|
|
|
|
//"writebyte",
|
|
|
|
|
//"writeword",
|
|
|
|
|
//"writedword",
|
|
|
|
|
//"registerwrite",
|
|
|
|
|
//"registerread",
|
|
|
|
|
};
|
|
|
|
|
public static string[] SaveStateFunctions = new string[] {
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//"create",
|
2011-06-26 02:09:06 +00:00
|
|
|
|
"save",
|
2012-01-22 22:20:09 +00:00
|
|
|
|
"load"
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//"write"
|
2011-06-26 02:09:06 +00:00
|
|
|
|
};
|
|
|
|
|
public static string[] MovieFunctions = new string[] {
|
|
|
|
|
"mode",
|
|
|
|
|
"rerecordcount",
|
|
|
|
|
"stop"
|
|
|
|
|
};
|
|
|
|
|
public static string[] JoypadFunctions = new string[] {
|
|
|
|
|
"set",
|
|
|
|
|
//"get",
|
|
|
|
|
};
|
2012-01-21 20:05:53 +00:00
|
|
|
|
|
2012-01-22 22:20:09 +00:00
|
|
|
|
public static string[] MultiClientFunctions = new string[] {
|
2012-01-22 22:42:40 +00:00
|
|
|
|
"openrom",
|
|
|
|
|
"closerom",
|
|
|
|
|
"opentoolbox",
|
|
|
|
|
"openramwatch",
|
|
|
|
|
"openramsearch",
|
|
|
|
|
"openrampoke",
|
|
|
|
|
"openhexeditor",
|
|
|
|
|
"opentasstudio",
|
|
|
|
|
"opencheats"
|
2012-01-22 22:20:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-01-21 20:05:53 +00:00
|
|
|
|
/****************************************************/
|
|
|
|
|
/*************function definitions********************/
|
|
|
|
|
/****************************************************/
|
2011-06-26 02:09:06 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Console library
|
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
public void console_output(object lua_input)
|
2011-06-26 02:09:06 +00:00
|
|
|
|
{
|
2012-01-21 20:20:06 +00:00
|
|
|
|
Global.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString());
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2012-01-21 20:20:06 +00:00
|
|
|
|
|
2012-01-22 03:14:31 +00:00
|
|
|
|
public void console_clear(object lua_input)
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LuaConsole1.ClearOutputWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string console_getluafunctionslist()
|
|
|
|
|
{
|
|
|
|
|
return LuaLibraryList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Emu library
|
|
|
|
|
//----------------------------------------------------
|
2012-01-22 03:14:31 +00:00
|
|
|
|
public void emu_frameadvance()
|
|
|
|
|
{
|
2012-01-28 21:43:55 +00:00
|
|
|
|
LuaWait.Set();
|
|
|
|
|
Global.MainForm.MainWait.WaitOne();
|
2012-01-22 03:14:31 +00:00
|
|
|
|
}
|
2012-01-21 20:20:06 +00:00
|
|
|
|
|
|
|
|
|
public void emu_pause()
|
2011-06-26 02:09:06 +00:00
|
|
|
|
{
|
2012-01-21 20:20:06 +00:00
|
|
|
|
Global.MainForm.PauseEmulator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void emu_unpause()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.UnpauseEmulator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void emu_togglepause()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.TogglePause();
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2012-01-21 20:20:06 +00:00
|
|
|
|
|
2012-01-22 03:14:31 +00:00
|
|
|
|
public int emu_framecount()
|
|
|
|
|
{
|
|
|
|
|
return Global.Emulator.Frame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int emu_lagcount()
|
|
|
|
|
{
|
|
|
|
|
return Global.Emulator.LagCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool emu_islagged()
|
|
|
|
|
{
|
|
|
|
|
return Global.Emulator.IsLagFrame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string emu_getsystemid()
|
|
|
|
|
{
|
|
|
|
|
return Global.Emulator.SystemId;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Memory library
|
|
|
|
|
//----------------------------------------------------
|
2012-01-28 21:43:55 +00:00
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
public string memory_readbyte(object lua_input)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
byte x;
|
|
|
|
|
if (lua_input.GetType() == typeof(string))
|
|
|
|
|
{
|
2012-01-22 22:42:40 +00:00
|
|
|
|
x = Global.Emulator.MemoryDomains[CurrentMemoryDomain].PeekByte(int.Parse((string)lua_input));
|
2011-06-26 02:09:06 +00:00
|
|
|
|
return x.ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
double y = (double)lua_input;
|
2012-01-22 22:42:40 +00:00
|
|
|
|
x = Global.Emulator.MemoryDomains[CurrentMemoryDomain].PeekByte(Convert.ToInt32(y));
|
2011-06-26 02:09:06 +00:00
|
|
|
|
return x.ToString();
|
2011-02-20 23:41:51 +00:00
|
|
|
|
}
|
2011-05-07 01:06:01 +00:00
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2012-01-21 20:20:06 +00:00
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
public void memory_writebyte(object lua_input)
|
|
|
|
|
{
|
2012-01-22 23:03:43 +00:00
|
|
|
|
Global.Emulator.MemoryDomains[CurrentMemoryDomain].PokeByte((int)lua_input, (byte)lua_input);
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2011-05-14 01:05:26 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Savestate library
|
|
|
|
|
//----------------------------------------------------
|
2011-05-14 01:05:26 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
public void savestate_save(object lua_input)
|
|
|
|
|
{
|
2012-01-22 22:20:09 +00:00
|
|
|
|
if (lua_input.GetType() == typeof(string))
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void savestate_load(object lua_input)
|
|
|
|
|
{
|
|
|
|
|
if (lua_input.GetType() == typeof(string))
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadStateFile(lua_input.ToString(), Path.GetFileName(lua_input.ToString()));
|
|
|
|
|
}
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2012-01-21 20:05:53 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Movie library
|
|
|
|
|
//----------------------------------------------------
|
2012-01-21 20:05:53 +00:00
|
|
|
|
public string movie_mode()
|
|
|
|
|
{
|
|
|
|
|
return Global.MovieSession.Movie.Mode.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
public string movie_rerecordcount()
|
|
|
|
|
{
|
2012-01-21 20:05:53 +00:00
|
|
|
|
return Global.MovieSession.Movie.Rerecords.ToString();
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
public void movie_stop()
|
|
|
|
|
{
|
2012-01-21 20:05:53 +00:00
|
|
|
|
Global.MovieSession.Movie.StopMovie();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Joypad library
|
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
|
2012-01-22 22:42:40 +00:00
|
|
|
|
//Currently sends all controllers, needs to control which ones it sends
|
|
|
|
|
public string joypad_get(object lua_input)
|
2012-01-21 20:05:53 +00:00
|
|
|
|
{
|
2012-01-22 22:42:40 +00:00
|
|
|
|
return Global.GetOutputControllersAsMnemonic();
|
2012-01-21 20:20:06 +00:00
|
|
|
|
}
|
2012-01-28 21:43:55 +00:00
|
|
|
|
|
2012-01-21 20:20:06 +00:00
|
|
|
|
public void joypad_set(object lua_input)
|
|
|
|
|
{
|
|
|
|
|
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2012-01-22 22:20:09 +00:00
|
|
|
|
|
|
|
|
|
//----------------------------------------------------
|
|
|
|
|
//Client library
|
|
|
|
|
//----------------------------------------------------
|
2012-01-22 22:42:40 +00:00
|
|
|
|
public void client_openrom(object lua_input)
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadRom(lua_input.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_closerom()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.CloseROM();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_opentoolbox()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadToolBox();
|
|
|
|
|
}
|
2012-01-22 22:20:09 +00:00
|
|
|
|
|
|
|
|
|
public void client_openramwatch()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadRamWatch();
|
|
|
|
|
}
|
2012-01-22 22:42:40 +00:00
|
|
|
|
|
|
|
|
|
public void client_openramsearch()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadRamSearch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_openrampoke()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadRamPoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_openhexeditor()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadHexEditor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_opentasstudio()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadTAStudio();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void client_opencheats()
|
|
|
|
|
{
|
|
|
|
|
Global.MainForm.LoadCheatsWindow();
|
|
|
|
|
}
|
2011-06-26 02:09:06 +00:00
|
|
|
|
}
|
2011-02-20 19:18:27 +00:00
|
|
|
|
}
|