2011-02-20 19:18:27 +00:00
using System ;
2012-03-19 03:39:56 +00:00
using System.Collections ;
2011-02-20 19:18:27 +00:00
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 ;
2012-04-18 14:11:16 +00:00
using System.Drawing ;
2011-02-20 19:18:27 +00:00
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-07-11 15:03:51 +00:00
public LuaDocumentation docs = new LuaDocumentation ( ) ;
2012-09-02 00:56:50 +00:00
private Lua lua = new Lua ( ) ;
private LuaConsole Caller ;
2012-01-28 21:43:55 +00:00
public EventWaitHandle LuaWait ;
public bool isRunning ;
2012-01-22 22:42:40 +00:00
private int CurrentMemoryDomain = 0 ; //Main memory by default
2012-03-23 23:03:39 +00:00
public bool FrameAdvanceRequested ;
2012-09-02 00:56:50 +00:00
private Lua currThread ;
private LuaFunction savestate_registersavefunc ;
private LuaFunction savestate_registerloadfunc ;
2012-09-02 16:23:42 +00:00
private LuaFunction frame_registerbeforefunc ;
private LuaFunction frame_registerafterfunc ;
2012-03-29 03:09:46 +00:00
2012-03-28 01:03:50 +00:00
public void SavestateRegisterSave ( string name )
2012-03-27 21:09:36 +00:00
{
if ( savestate_registersavefunc ! = null )
{
try
{
2012-03-28 01:03:50 +00:00
savestate_registersavefunc . Call ( name ) ;
2012-03-27 21:09:36 +00:00
}
2012-03-28 00:13:25 +00:00
catch ( SystemException e )
2012-03-27 21:09:36 +00:00
{
2012-09-02 00:56:50 +00:00
Global . MainForm . LuaConsole1 . WriteToOutputWindow (
"error running function attached by lua function savestate.registersave" +
2012-03-28 00:13:25 +00:00
"\nError message: " + e . Message ) ;
2012-03-27 21:09:36 +00:00
}
}
}
2012-01-21 20:05:53 +00:00
2012-03-28 01:03:50 +00:00
public void SavestateRegisterLoad ( string name )
2012-03-27 21:17:20 +00:00
{
if ( savestate_registerloadfunc ! = null )
{
try
{
2012-03-28 01:24:32 +00:00
savestate_registerloadfunc . Call ( name ) ;
2012-03-27 21:17:20 +00:00
}
2012-03-28 00:13:25 +00:00
catch ( SystemException e )
2012-03-27 21:17:20 +00:00
{
2012-09-02 00:56:50 +00:00
Global . MainForm . LuaConsole1 . WriteToOutputWindow (
"error running function attached by lua function savestate.registerload" +
2012-03-28 00:13:25 +00:00
"\nError message: " + e . Message ) ;
2012-03-27 21:17:20 +00:00
}
}
}
2012-09-02 16:23:42 +00:00
public void FrameRegisterBefore ( )
{
if ( frame_registerbeforefunc ! = null )
{
try
{
frame_registerbeforefunc . Call ( ) ;
}
catch ( SystemException e )
{
Global . MainForm . LuaConsole1 . WriteToOutputWindow (
"error running function attached by lua function emu.registerbefore" +
"\nError message: " + e . Message ) ;
}
}
}
public void FrameRegisterAfter ( )
{
if ( frame_registerafterfunc ! = null )
{
try
{
frame_registerafterfunc . Call ( ) ;
}
catch ( SystemException e )
{
Global . MainForm . LuaConsole1 . WriteToOutputWindow (
"error running function attached by lua function emu.registerafter" +
"\nError message: " + e . Message ) ;
}
}
}
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-07-11 15:03:51 +00:00
docs . Clear ( ) ;
2012-01-21 20:05:53 +00:00
Caller = passed . get ( ) ;
2012-01-28 21:43:55 +00:00
LuaRegister ( lua ) ;
}
2012-01-28 21:51:01 +00:00
public void Close ( )
{
2012-03-07 00:58:41 +00:00
lua = new Lua ( ) ;
2012-01-28 21:51:01 +00:00
}
2012-01-28 21:43:55 +00:00
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 + + )
{
2012-09-02 00:56:50 +00:00
lua . RegisterFunction ( "console." + ConsoleFunctions [ i ] , this ,
2012-09-02 14:47:12 +00:00
this . GetType ( ) . GetMethod ( "console_" + ConsoleFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "console" , ConsoleFunctions [ i ] , this . GetType ( ) . GetMethod ( "console_" + ConsoleFunctions [ i ] ) ) ;
2012-01-21 20:05:53 +00:00
}
2012-01-28 22:44:48 +00:00
lua . NewTable ( "gui" ) ;
for ( int i = 0 ; i < GuiFunctions . Length ; i + + )
{
lua . RegisterFunction ( "gui." + GuiFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "gui_" + GuiFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "gui" , GuiFunctions [ i ] , this . GetType ( ) . GetMethod ( "gui_" + GuiFunctions [ i ] ) ) ;
2012-01-28 22:44:48 +00:00
}
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-07-12 00:57:09 +00:00
docs . Add ( "emu" , EmuFunctions [ i ] , this . GetType ( ) . GetMethod ( "emu_" + EmuFunctions [ i ] ) ) ;
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-07-12 00:57:09 +00:00
docs . Add ( "memory" , MemoryFunctions [ i ] , this . GetType ( ) . GetMethod ( "memory_" + MemoryFunctions [ i ] ) ) ;
2012-01-21 20:05:53 +00:00
}
2012-01-28 22:11:39 +00:00
lua . NewTable ( "mainmemory" ) ;
for ( int i = 0 ; i < MainMemoryFunctions . Length ; i + + )
{
2012-09-02 00:56:50 +00:00
lua . RegisterFunction ( "mainmemory." + MainMemoryFunctions [ i ] , this ,
2012-09-02 14:47:12 +00:00
this . GetType ( ) . GetMethod ( "mainmemory_" + MainMemoryFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "mainmemory" , MainMemoryFunctions [ i ] , this . GetType ( ) . GetMethod ( "mainmemory_" + MainMemoryFunctions [ i ] ) ) ;
2012-01-28 22:11:39 +00:00
}
2012-01-21 20:05:53 +00:00
lua . NewTable ( "savestate" ) ;
for ( int i = 0 ; i < SaveStateFunctions . Length ; i + + )
{
2012-09-02 00:56:50 +00:00
lua . RegisterFunction ( "savestate." + SaveStateFunctions [ i ] , this ,
2012-09-02 14:47:12 +00:00
this . GetType ( ) . GetMethod ( "savestate_" + SaveStateFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "savestate" , SaveStateFunctions [ i ] , this . GetType ( ) . GetMethod ( "savestate_" + SaveStateFunctions [ i ] ) ) ;
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-07-12 00:57:09 +00:00
docs . Add ( "movie" , MovieFunctions [ i ] , this . GetType ( ) . GetMethod ( "movie_" + MovieFunctions [ i ] ) ) ;
2012-01-21 20:05:53 +00:00
}
2012-03-24 15:55:22 +00:00
lua . NewTable ( "input" ) ;
for ( int i = 0 ; i < InputFunctions . Length ; i + + )
{
lua . RegisterFunction ( "input." + InputFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "input_" + InputFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "input" , InputFunctions [ i ] , this . GetType ( ) . GetMethod ( "input_" + InputFunctions [ i ] ) ) ;
2012-03-24 15:55:22 +00:00
}
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 ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "joypad" , JoypadFunctions [ i ] , this . GetType ( ) . GetMethod ( "joypad_" + JoypadFunctions [ i ] ) ) ;
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 + + )
{
2012-09-02 00:56:50 +00:00
lua . RegisterFunction ( "client." + MultiClientFunctions [ i ] , this ,
2012-09-02 14:47:12 +00:00
this . GetType ( ) . GetMethod ( "client_" + MultiClientFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "client" , MultiClientFunctions [ i ] , this . GetType ( ) . GetMethod ( "client_" + MultiClientFunctions [ i ] ) ) ;
2012-01-22 22:20:09 +00:00
}
2012-03-28 12:26:43 +00:00
2012-07-10 19:04:35 +00:00
lua . NewTable ( "forms" ) ;
for ( int i = 0 ; i < FormsFunctions . Length ; i + + )
{
lua . RegisterFunction ( "forms." + FormsFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "forms_" + FormsFunctions [ i ] ) ) ;
2012-07-12 00:57:09 +00:00
docs . Add ( "forms" , FormsFunctions [ i ] , this . GetType ( ) . GetMethod ( "forms_" + FormsFunctions [ i ] ) ) ;
2012-07-10 19:04:35 +00:00
}
2012-09-02 00:56:50 +00:00
lua . NewTable ( "bit" ) ;
for ( int i = 0 ; i < BitwiseFunctions . Length ; i + + )
{
lua . RegisterFunction ( "bit." + BitwiseFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "bit_" + BitwiseFunctions [ i ] ) ) ;
docs . Add ( "bit" , BitwiseFunctions [ i ] , this . GetType ( ) . GetMethod ( "bit_" + BitwiseFunctions [ i ] ) ) ;
}
2012-07-11 15:03:51 +00:00
docs . Sort ( ) ;
2012-01-21 20:05:53 +00:00
}
2012-03-27 19:21:10 +00:00
2012-03-23 19:44:47 +00:00
public Lua SpawnCoroutine ( string File )
2012-01-21 20:20:06 +00:00
{
2012-03-27 07:25:36 +00:00
var t = lua . NewThread ( ) ;
2012-06-04 02:33:34 +00:00
//LuaRegister(t); //adelikat: Not sure why this was here but it was causing the entire luaimplmeentaiton to be duplicated each time, eventually resulting in crashes
2012-03-27 07:25:36 +00:00
var main = t . LoadFile ( File ) ;
t . Push ( main ) ; //push main function on to stack for subsequent resuming
return t ;
2012-01-21 20:20:06 +00:00
}
2012-03-11 09:16:09 +00:00
private int LuaInt ( object lua_arg )
{
2012-09-02 14:47:12 +00:00
return Convert . ToInt32 ( ( double ) lua_arg ) ;
2012-03-11 09:16:09 +00:00
}
private uint LuaUInt ( object lua_arg )
{
2012-09-02 14:47:12 +00:00
return Convert . ToUInt32 ( ( double ) lua_arg ) ;
2012-03-11 09:16:09 +00:00
}
2012-06-10 23:27:30 +00:00
public Color GetColor ( object color )
{
2012-09-02 14:47:12 +00:00
if ( color . GetType ( ) = = typeof ( Double ) )
2012-06-10 23:27:30 +00:00
{
2012-09-02 00:56:50 +00:00
return
System . Drawing . Color . FromArgb ( int . Parse ( long . Parse ( color . ToString ( ) ) . ToString ( "X" ) ,
2012-09-02 14:47:12 +00:00
System . Globalization . NumberStyles . HexNumber ) ) ;
2012-06-10 23:27:30 +00:00
}
else
{
return System . Drawing . Color . FromName ( color . ToString ( ) . ToLower ( ) ) ;
}
}
public SolidBrush GetBrush ( object color )
{
return new System . Drawing . SolidBrush ( GetColor ( color ) ) ;
}
2012-09-02 00:56:50 +00:00
2012-06-10 23:27:30 +00:00
public Pen GetPen ( object color )
{
return new System . Drawing . Pen ( GetColor ( color ) ) ;
}
2012-04-18 14:11:16 +00:00
2012-03-11 09:16:09 +00:00
/ * *
* 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 .
* /
2012-09-02 00:56:50 +00:00
2012-03-11 09:16:09 +00:00
private object [ ] LuaVarArgs ( params object [ ] lua_args )
{
int n = lua_args . Length ;
int trim = 0 ;
2012-03-27 19:21:10 +00:00
for ( int i = n - 1 ; i > = 0 ; - - i )
2012-03-11 09:16:09 +00:00
if ( lua_args [ i ] = = null ) + + trim ;
2012-03-27 19:21:10 +00:00
object [ ] lua_result = new object [ n - trim ] ;
Array . Copy ( lua_args , lua_result , n - trim ) ;
2012-03-11 09:16:09 +00:00
return lua_result ;
}
2012-03-23 23:03:39 +00:00
public class ResumeResult
{
public bool WaitForFrame ;
2012-07-10 19:40:35 +00:00
public bool Terminated ;
2012-03-23 23:03:39 +00:00
}
public ResumeResult ResumeScript ( Lua script )
2012-03-23 18:24:29 +00:00
{
2012-03-23 19:44:47 +00:00
currThread = script ;
2012-07-10 19:40:35 +00:00
int execResult = script . Resume ( 0 ) ;
2012-03-23 19:44:47 +00:00
currThread = null ;
2012-03-23 23:03:39 +00:00
var result = new ResumeResult ( ) ;
2012-07-10 19:40:35 +00:00
if ( execResult = = 0 )
{
//terminated
result . Terminated = true ;
}
else
{
//yielded
result . WaitForFrame = FrameAdvanceRequested ;
}
2012-03-23 23:03:39 +00:00
FrameAdvanceRequested = false ;
return result ;
2012-03-23 18:24:29 +00:00
}
2012-01-28 21:43:55 +00:00
2012-01-21 20:20:06 +00:00
public void print ( string s )
{
2012-03-10 03:07:05 +00:00
Caller . AddText ( s ) ;
Caller . AddText ( "\n" ) ;
2012-01-21 20:20:06 +00:00
}
2012-01-21 20:05:53 +00:00
/****************************************************/
/*************library definitions********************/
/****************************************************/
2012-09-02 00:56:50 +00:00
2012-01-28 22:11:39 +00:00
public static string [ ] ConsoleFunctions = new string [ ]
2012-09-02 00:56:50 +00:00
{
"output" ,
"log" ,
"clear" ,
"getluafunctionslist" ,
} ;
2012-01-21 20:05:53 +00:00
2012-01-28 22:44:48 +00:00
public static string [ ] GuiFunctions = new string [ ]
2012-09-02 00:56:50 +00:00
{
"text" ,
"alert" ,
"cleartext" ,
"drawPixel" ,
"drawLine" ,
2012-09-02 01:15:18 +00:00
"drawBox" ,
2012-09-02 00:56:50 +00:00
"drawRectangle" ,
"drawEllipse" ,
"drawPolygon" ,
"drawBezier" ,
"drawPie" ,
"drawIcon" ,
"drawImage" ,
} ;
2012-01-28 22:44:48 +00:00
2012-01-28 22:11:39 +00:00
public static string [ ] EmuFunctions = new string [ ]
2012-09-02 00:56:50 +00:00
{
"frameadvance" ,
"yield" ,
"pause" ,
"unpause" ,
"togglepause" ,
"ispaused" ,
"speedmode" ,
"framecount" ,
"lagcount" ,
"islagged" ,
"getsystemid" ,
"setrenderplanes" ,
"frameskip" ,
"minimizeframeskip" ,
"limitframerate" ,
"displayvsync" ,
2012-09-02 16:23:42 +00:00
"enablerewind" ,
"registerbefore" ,
"registerafter" ,
2012-09-02 00:56:50 +00:00
} ;
2012-03-27 19:21:10 +00:00
2012-01-28 22:11:39 +00:00
public static string [ ] MemoryFunctions = new string [ ]
2012-09-02 00:56:50 +00:00
{
"usememorydomain" ,
"getmemorydomainlist" ,
"getcurrentmemorydomain" ,
"read_s8" ,
"read_u8" ,
"read_s16_le" ,
"read_s24_le" ,
"read_s32_le" ,
"read_u16_le" ,
"read_u24_le" ,
"read_u32_le" ,
"read_s16_be" ,
"read_s24_be" ,
"read_s32_be" ,
"read_u16_be" ,
"read_u24_be" ,
"read_u32_be" ,
"write_s8" ,
"write_u8" ,
"write_s16_le" ,
"write_s24_le" ,
"write_s32_le" ,
"write_u16_le" ,
"write_u24_le" ,
"write_u32_le" ,
"write_s16_be" ,
"write_s24_be" ,
"write_s32_be" ,
"write_u16_be" ,
"write_u24_be" ,
"write_u32_be" ,
"readbyte" ,
"writebyte" ,
//"registerwrite",
//"registerread",
} ;
2012-01-28 22:11:39 +00:00
public static string [ ] MainMemoryFunctions = new string [ ]
2012-09-02 00:56:50 +00:00
{
"read_s8" ,
"read_u8" ,
"read_s16_le" ,
"read_s24_le" ,
"read_s32_le" ,
"read_u16_le" ,
"read_u24_le" ,
"read_u32_le" ,
"read_s16_be" ,
"read_s24_be" ,
"read_s32_be" ,
"read_u16_be" ,
"read_u24_be" ,
"read_u32_be" ,
"write_s8" ,
"write_u8" ,
"write_s16_le" ,
"write_s24_le" ,
"write_s32_le" ,
"write_u16_le" ,
"write_u24_le" ,
"write_u32_le" ,
"write_s16_be" ,
"write_s24_be" ,
"write_s32_be" ,
"write_u16_be" ,
"write_u24_be" ,
"write_u32_be" ,
//"registerwrite",
//"registerread",
} ;
public static string [ ] SaveStateFunctions = new string [ ]
{
"saveslot" ,
"loadslot" ,
"save" ,
"load" ,
"registersave" ,
"registerload" ,
} ;
public static string [ ] MovieFunctions = new string [ ]
{
"mode" ,
"isloaded" ,
"rerecordcount" ,
"length" ,
"stop" ,
"filename" ,
"getreadonly" ,
"setreadonly" ,
"getrerecordcounting" ,
"setrerecordcounting" ,
"getinput" ,
} ;
public static string [ ] InputFunctions = new string [ ]
{
"get" ,
"getmouse" ,
} ;
public static string [ ] JoypadFunctions = new string [ ]
{
"set" ,
"get" ,
"getimmediate"
} ;
public static string [ ] MultiClientFunctions = new string [ ]
{
"setwindowsize" ,
"openrom" ,
"closerom" ,
"opentoolbox" ,
"openramwatch" ,
"openramsearch" ,
"openrampoke" ,
"openhexeditor" ,
"opentasstudio" ,
"opencheats" ,
2012-09-02 16:49:58 +00:00
"screenwidth" ,
"screenheight" ,
2012-09-02 00:56:50 +00:00
} ;
public static string [ ] FormsFunctions = new string [ ]
{
"newform" ,
"destroy" ,
"destroyall" ,
"button" ,
"label" ,
"textbox" ,
"setlocation" ,
"setsize" ,
"settext" ,
"addclick" ,
"clearclicks" ,
"gettext" ,
} ;
public static string [ ] BitwiseFunctions = new string [ ]
{
"band" ,
"lshift" ,
"rshift" ,
"rol" ,
"ror" ,
"bor" ,
"bxor" ,
"bnot" ,
} ;
2012-07-10 19:04:35 +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-07-10 20:23:19 +00:00
if ( lua_input = = null )
{
Global . MainForm . LuaConsole1 . WriteToOutputWindow ( "NULL" ) ;
}
else
{
Global . MainForm . LuaConsole1 . WriteToOutputWindow ( lua_input . ToString ( ) ) ;
}
2011-06-26 02:09:06 +00:00
}
2012-01-21 20:20:06 +00:00
2012-07-10 19:04:35 +00:00
public void console_log ( object lua_input )
{
console_output ( lua_input ) ;
}
2012-03-10 03:07:05 +00:00
public void console_clear ( )
2012-01-22 03:14:31 +00:00
{
Global . MainForm . LuaConsole1 . ClearOutputWindow ( ) ;
}
public string console_getluafunctionslist ( )
{
2012-03-28 12:26:43 +00:00
string list = "" ;
2012-07-11 15:03:51 +00:00
foreach ( LuaDocumentation . LibraryFunction l in Global . MainForm . LuaConsole1 . LuaImp . docs . FunctionList )
2012-03-28 12:26:43 +00:00
{
2012-07-11 15:03:51 +00:00
list + = l . name + "\n" ;
2012-03-28 12:26:43 +00:00
}
2012-09-02 00:56:50 +00:00
2012-03-28 12:26:43 +00:00
return list ;
2012-01-22 03:14:31 +00:00
}
2012-01-21 20:20:06 +00:00
//----------------------------------------------------
2012-01-28 22:44:48 +00:00
//Gui library
//----------------------------------------------------
2012-09-02 00:56:50 +00:00
private void do_gui_text ( object luaX , object luaY , object luaStr , bool alert , object background = null ,
2012-09-02 14:47:12 +00:00
object forecolor = null , object anchor = null )
2012-01-28 22:44:48 +00:00
{
2012-04-25 19:58:17 +00:00
if ( ! alert )
{
if ( forecolor = = null )
2012-04-30 01:14:23 +00:00
forecolor = "white" ;
2012-04-25 19:58:17 +00:00
if ( background = = null )
2012-04-30 01:14:23 +00:00
background = "black" ;
2012-04-25 19:58:17 +00:00
}
2012-03-26 14:48:05 +00:00
int a = 0 ;
2012-03-27 19:21:10 +00:00
if ( anchor ! = null )
{
int x ;
if ( int . TryParse ( anchor . ToString ( ) , out x ) = = false )
{
if ( anchor . ToString ( ) . ToLower ( ) = = "topleft" )
a = 0 ;
else if ( anchor . ToString ( ) . ToLower ( ) = = "topright" )
a = 1 ;
else if ( anchor . ToString ( ) . ToLower ( ) = = "bottomleft" )
a = 2 ;
else if ( anchor . ToString ( ) . ToLower ( ) = = "bottomright" )
a = 3 ;
}
else
{
a = LuaInt ( anchor ) ;
}
}
2012-09-02 00:56:50 +00:00
Global . OSD . AddGUIText ( luaStr . ToString ( ) , LuaInt ( luaX ) , LuaInt ( luaY ) , alert , GetColor ( background ) , GetColor ( forecolor ) ,
2012-09-02 14:47:12 +00:00
a ) ;
2012-03-29 02:45:38 +00:00
}
2012-09-02 00:56:50 +00:00
public void gui_text ( object luaX , object luaY , object luaStr , object background = null , object forecolor = null ,
2012-09-02 14:47:12 +00:00
object anchor = null )
2012-03-29 02:45:38 +00:00
{
2012-06-10 23:27:30 +00:00
do_gui_text ( luaX , luaY , luaStr , false , background , forecolor , anchor ) ;
2012-01-28 22:44:48 +00:00
}
2012-03-29 02:45:38 +00:00
public void gui_alert ( object luaX , object luaY , object luaStr , object anchor = null )
2012-03-11 00:54:24 +00:00
{
2012-04-25 19:58:17 +00:00
do_gui_text ( luaX , luaY , luaStr , true , null , null , anchor ) ;
2012-03-11 00:54:24 +00:00
}
2012-04-16 08:18:41 +00:00
public void gui_cleartext ( )
{
Global . OSD . ClearGUIText ( ) ;
}
2012-04-27 20:15:07 +00:00
public DisplaySurface luaSurface ;
2012-04-16 08:18:41 +00:00
/// <summary>
/// sets the current drawing context to a new surface.
/// you COULD pass these back to lua to use as a target in rendering jobs, instead of setting it as current here.
/// could be more powerful.
/// performance test may reveal that repeatedly calling GetGraphics could be slow.
/// we may want to make a class here in LuaImplementation that wraps a DisplaySurface and a Graphics which would be created once
/// </summary>
public void gui_drawNew ( )
{
luaSurface = Global . DisplayManager . GetLuaSurfaceNative ( ) ;
}
2012-05-06 07:09:04 +00:00
public void gui_drawNewEmu ( )
{
luaSurface = Global . DisplayManager . GetLuaEmuSurfaceEmu ( ) ;
}
2012-04-16 08:18:41 +00:00
/// <summary>
/// finishes the current drawing and submits it to the display manager (at native [host] resolution pre-osd)
/// you would probably want some way to specify which surface to set it to, when there are other surfaces.
/// most notably, the client output [emulated] resolution
/// </summary>
public void gui_drawFinish ( )
{
Global . DisplayManager . SetLuaSurfaceNativePreOSD ( luaSurface ) ;
luaSurface = null ;
}
2012-05-06 07:09:04 +00:00
public void gui_drawFinishEmu ( )
{
Global . DisplayManager . SetLuaSurfaceEmu ( luaSurface ) ;
luaSurface = null ;
}
2012-04-16 08:18:41 +00:00
/// <summary>
/// draws a random rectangle for testing purposes
/// </summary>
2012-04-16 20:47:01 +00:00
public void gui_drawRectangle ( object X , object Y , object width , object height , object line , object background = null )
2012-04-16 08:18:41 +00:00
{
using ( var g = luaSurface . GetGraphics ( ) )
{
2012-04-16 20:04:43 +00:00
try
{
2012-05-11 21:11:29 +00:00
int int_x = LuaInt ( X ) ;
int int_y = LuaInt ( Y ) ;
int int_width = LuaInt ( width ) ;
int int_height = LuaInt ( height ) ;
2012-06-12 03:24:15 +00:00
using ( var pen = GetPen ( line ) )
{
g . DrawRectangle ( pen , int_x , int_y , int_width , int_height ) ;
if ( background ! = null )
using ( var brush = GetBrush ( background ) )
g . FillRectangle ( brush , int_x , int_y , int_width , int_height ) ;
2012-09-02 01:15:18 +00:00
}
}
catch ( Exception )
{
// need to stop the script from here
return ;
}
}
}
public void gui_drawBox ( object X , object Y , object X2 , object Y2 , object line , object background = null )
{
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
int int_x = LuaInt ( X ) ;
int int_y = LuaInt ( Y ) ;
2012-09-02 02:37:25 +00:00
int int_width = LuaInt ( X2 ) ;
int int_height = LuaInt ( Y2 ) ;
if ( int_x < int_width )
{
int_width = Math . Abs ( int_x - int_width ) ;
}
else
{
int_width = int_x - int_width ;
int_x - = int_width ;
}
if ( int_y < int_height )
{
int_height = Math . Abs ( int_y - int_height ) ;
}
else
{
int_height = int_y - int_height ;
int_y - = int_height ;
}
2012-09-02 01:15:18 +00:00
using ( var pen = GetPen ( line ) )
{
2012-09-02 14:47:12 +00:00
g . DrawRectangle ( pen , int_x , int_y , int_width , int_height ) ;
2012-09-02 01:15:18 +00:00
if ( background ! = null )
using ( var brush = GetBrush ( background ) )
g . FillRectangle ( brush , int_x , int_y , int_width , int_height ) ;
2012-06-12 03:24:15 +00:00
}
2012-04-16 20:04:43 +00:00
}
2012-09-02 00:56:50 +00:00
catch ( Exception )
2012-04-16 20:04:43 +00:00
{
// need to stop the script from here
return ;
}
2012-04-16 08:18:41 +00:00
}
}
2012-06-13 01:09:21 +00:00
public void gui_drawPixel ( object X , object Y , object color = null )
2012-04-17 03:21:16 +00:00
{
using ( var g = luaSurface . GetGraphics ( ) )
{
2012-04-17 19:58:27 +00:00
float x = LuaInt ( X ) + 0.1F ;
try
{
2012-06-13 01:09:21 +00:00
if ( color = = null )
color = "black" ;
2012-09-02 00:56:50 +00:00
using ( var pen = GetPen ( color ) )
2012-06-12 03:24:15 +00:00
g . DrawLine ( pen , LuaInt ( X ) , LuaInt ( Y ) , x , LuaInt ( Y ) ) ;
2012-04-17 19:58:27 +00:00
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 19:58:27 +00:00
{
return ;
}
2012-04-17 03:21:16 +00:00
}
}
2012-09-02 00:56:50 +00:00
2012-04-17 17:48:37 +00:00
public void gui_drawLine ( object x1 , object y1 , object x2 , object y2 , object color = null )
2012-04-17 03:21:16 +00:00
{
2012-04-17 17:48:37 +00:00
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
2012-06-12 03:24:15 +00:00
if ( color = = null )
color = "black" ;
2012-09-02 00:56:50 +00:00
using ( var pen = GetPen ( color ) )
2012-06-12 03:24:15 +00:00
g . DrawLine ( pen , LuaInt ( x1 ) , LuaInt ( y1 ) , LuaInt ( x2 ) , LuaInt ( y2 ) ) ;
2012-04-17 17:48:37 +00:00
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 17:48:37 +00:00
{
return ;
}
}
2012-04-17 03:21:16 +00:00
}
public void gui_drawEllipse ( object X , object Y , object width , object height , object line , object background = null )
{
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
2012-06-10 23:27:30 +00:00
using ( var pen = GetPen ( line ) )
2012-04-17 03:21:16 +00:00
{
2012-06-10 23:27:30 +00:00
g . DrawEllipse ( pen , LuaInt ( X ) , LuaInt ( Y ) , LuaInt ( width ) , LuaInt ( height ) ) ;
if ( background ! = null )
{
using ( var brush = GetBrush ( background ) )
g . FillEllipse ( brush , LuaInt ( X ) , LuaInt ( Y ) , LuaInt ( width ) , LuaInt ( height ) ) ;
}
2012-04-17 03:21:16 +00:00
}
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 03:21:16 +00:00
{
// need to stop the script from here
return ;
}
}
}
2012-04-17 19:58:27 +00:00
public void gui_drawPolygon ( LuaTable points , object line , object background = null )
2012-04-17 03:21:16 +00:00
{
2012-04-17 17:48:37 +00:00
//this is a test
using ( var g = luaSurface . GetGraphics ( ) )
{
2012-04-17 19:58:27 +00:00
try
{
System . Drawing . Point [ ] Points = new System . Drawing . Point [ points . Values . Count ] ;
int i = 0 ;
foreach ( LuaTable point in points . Values )
{
Points [ i ] = new System . Drawing . Point ( LuaInt ( point [ 1 ] ) , LuaInt ( point [ 2 ] ) ) ;
i + + ;
}
2012-06-10 23:27:30 +00:00
using ( var pen = GetPen ( line ) )
2012-04-17 19:58:27 +00:00
{
2012-06-10 23:27:30 +00:00
g . DrawPolygon ( pen , Points ) ;
if ( background ! = null )
{
using ( var brush = GetBrush ( background ) )
g . FillPolygon ( brush , Points ) ;
}
2012-04-17 19:58:27 +00:00
}
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 19:58:27 +00:00
{
return ;
}
2012-04-17 17:48:37 +00:00
}
2012-04-17 03:21:16 +00:00
}
2012-04-17 19:58:27 +00:00
public void gui_drawBezier ( LuaTable points , object color )
2012-04-17 03:21:16 +00:00
{
2012-04-17 19:58:27 +00:00
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
System . Drawing . Point [ ] Points = new System . Drawing . Point [ 4 ] ;
int i = 0 ;
foreach ( LuaTable point in points . Values )
{
Points [ i ] = new System . Drawing . Point ( LuaInt ( point [ 1 ] ) , LuaInt ( point [ 2 ] ) ) ;
i + + ;
if ( i > = 4 )
break ;
}
2012-09-02 00:56:50 +00:00
using ( var pen = GetPen ( color ) )
2012-06-12 03:24:15 +00:00
g . DrawBezier ( pen , Points [ 0 ] , Points [ 1 ] , Points [ 2 ] , Points [ 3 ] ) ;
2012-04-17 19:58:27 +00:00
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 19:58:27 +00:00
{
return ;
}
}
2012-04-17 03:21:16 +00:00
}
2012-04-04 20:09:50 +00:00
2012-09-02 00:56:50 +00:00
public void gui_drawPie ( object X , object Y , object width , object height , object startangle , object sweepangle ,
2012-09-02 14:47:12 +00:00
object line , object background = null )
2012-04-17 21:41:21 +00:00
{
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
2012-09-02 00:56:50 +00:00
using ( var pen = GetPen ( line ) )
2012-04-17 21:41:21 +00:00
{
2012-06-10 23:27:30 +00:00
g . DrawPie ( pen , LuaInt ( X ) , LuaInt ( Y ) , LuaInt ( width ) , LuaInt ( height ) , LuaInt ( startangle ) , LuaInt ( sweepangle ) ) ;
if ( background ! = null )
{
2012-09-02 00:56:50 +00:00
using ( var brush = GetBrush ( background ) )
2012-06-10 23:27:30 +00:00
g . FillPie ( brush , LuaInt ( X ) , LuaInt ( Y ) , LuaInt ( width ) , LuaInt ( height ) , LuaInt ( startangle ) , LuaInt ( sweepangle ) ) ;
}
2012-04-17 21:41:21 +00:00
}
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-17 21:41:21 +00:00
{
// need to stop the script from here
return ;
}
}
}
2012-04-25 20:34:50 +00:00
public void gui_drawIcon ( object Path , object x , object y , object width = null , object height = null )
{
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
Icon icon ;
if ( width ! = null & & height ! = null )
{
icon = new Icon ( Path . ToString ( ) , LuaInt ( width ) , LuaInt ( height ) ) ;
}
else
{
icon = new Icon ( Path . ToString ( ) ) ;
}
2012-09-02 00:56:50 +00:00
2012-04-25 20:34:50 +00:00
g . DrawIcon ( icon , LuaInt ( x ) , LuaInt ( y ) ) ;
}
2012-09-02 00:56:50 +00:00
catch ( Exception )
2012-04-25 20:34:50 +00:00
{
return ;
}
}
}
2012-04-26 03:41:31 +00:00
public void gui_drawImage ( object Path , object x , object y , object width = null , object height = null )
{
using ( var g = luaSurface . GetGraphics ( ) )
{
try
{
Image img = Image . FromFile ( Path . ToString ( ) ) ;
2012-09-02 14:47:12 +00:00
if ( width = = null | | width . GetType ( ) ! = typeof ( int ) )
2012-04-26 03:41:31 +00:00
width = img . Width . ToString ( ) ;
2012-09-02 14:47:12 +00:00
if ( height = = null | | height . GetType ( ) ! = typeof ( int ) )
2012-04-26 03:41:31 +00:00
height = img . Height . ToString ( ) ;
g . DrawImage ( img , LuaInt ( x ) , LuaInt ( y ) , int . Parse ( width . ToString ( ) ) , int . Parse ( height . ToString ( ) ) ) ;
}
2012-06-10 22:38:44 +00:00
catch ( Exception )
2012-04-26 03:41:31 +00:00
{
return ;
}
}
}
2012-04-27 20:15:07 +00:00
public void gui_clearGraphics ( )
{
luaSurface . Clear ( ) ;
}
2012-01-28 22:44:48 +00:00
//----------------------------------------------------
2012-01-21 20:20:06 +00:00
//Emu library
//----------------------------------------------------
2012-01-22 03:14:31 +00:00
public void emu_frameadvance ( )
2012-03-23 23:03:39 +00:00
{
FrameAdvanceRequested = true ;
currThread . Yield ( 0 ) ;
}
public void emu_yield ( )
2012-01-22 03:14:31 +00:00
{
2012-03-23 18:24:29 +00:00
currThread . Yield ( 0 ) ;
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-08-25 17:01:13 +00:00
public bool emu_ispaused ( )
{
return Global . MainForm . EmulatorPaused ;
}
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-07-12 18:39:24 +00:00
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%
{
Global . MainForm . ClickSpeedItem ( speed ) ;
}
else
{
console_log ( "Invalid speed value" ) ;
}
}
catch
{
console_log ( "Invalid speed value" ) ;
}
}
2012-07-12 19:23:39 +00:00
public void emu_minimizeframeskip ( object boolean )
2012-07-12 19:02:30 +00:00
{
2012-07-12 19:23:39 +00:00
string temp = boolean . ToString ( ) ;
2012-07-12 19:02:30 +00:00
if ( ! String . IsNullOrWhiteSpace ( temp ) )
{
if ( temp = = "0" | | temp . ToLower ( ) = = "false" )
{
Global . Config . AutoMinimizeSkipping = false ;
}
else
{
Global . Config . AutoMinimizeSkipping = true ;
}
Global . MainForm . MinimizeFrameskipMessage ( ) ;
}
}
2012-07-12 19:23:39 +00:00
public void emu_limitframerate ( object boolean )
{
string temp = boolean . ToString ( ) ;
if ( ! String . IsNullOrWhiteSpace ( temp ) )
{
if ( temp = = "0" | | temp . ToLower ( ) = = "false" )
{
Global . Config . LimitFramerate = false ;
}
else
{
Global . Config . LimitFramerate = true ;
}
Global . MainForm . LimitFrameRateMessage ( ) ;
}
}
2012-07-12 19:36:07 +00:00
public void emu_displayvsync ( object boolean )
{
string temp = boolean . ToString ( ) ;
if ( ! String . IsNullOrWhiteSpace ( temp ) )
{
if ( temp = = "0" | | temp . ToLower ( ) = = "false" )
{
Global . Config . DisplayVSync = false ;
}
else
{
Global . Config . DisplayVSync = true ;
}
Global . MainForm . VsyncMessage ( ) ;
}
}
2012-07-12 19:42:13 +00:00
public void emu_enablerewind ( object boolean )
{
string temp = boolean . ToString ( ) ;
if ( ! String . IsNullOrWhiteSpace ( temp ) )
{
if ( temp = = "0" | | temp . ToLower ( ) = = "false" )
{
Global . Config . RewindEnabled = false ;
}
else
{
Global . Config . RewindEnabled = true ;
}
Global . MainForm . RewindMessage ( ) ;
}
}
2012-07-12 18:39:24 +00:00
public void emu_frameskip ( object num_frames )
{
try
{
string temp = num_frames . ToString ( ) ;
int frames = Convert . ToInt32 ( temp ) ;
if ( frames > 0 )
{
Global . Config . FrameSkip = frames ;
Global . MainForm . FrameSkipMessage ( ) ;
}
else
{
console_log ( "Invalid frame skip value" ) ;
}
}
catch
{
console_log ( "Invalid frame skip value" ) ;
}
}
2012-03-11 09:16:09 +00:00
// For now, it accepts arguments up to 5.
public void emu_setrenderplanes (
object lua_p0 , object lua_p1 = null , object lua_p2 = null ,
object lua_p3 = null , object lua_p4 = null )
{
emu_setrenderplanes_do ( LuaVarArgs ( lua_p0 , lua_p1 , lua_p2 , lua_p3 , lua_p4 ) ) ;
}
// TODO: error handling for argument count mismatch
private void emu_setrenderplanes_do ( object [ ] lua_p )
2012-03-11 05:47:38 +00:00
{
if ( Global . Emulator is BizHawk . Emulation . Consoles . Nintendo . NES )
{
2012-09-02 14:47:12 +00:00
Global . CoreInputComm . NES_ShowOBJ = Global . Config . NESDispSprites = ( bool ) lua_p [ 0 ] ;
Global . CoreInputComm . NES_ShowBG = Global . Config . NESDispBackground = ( bool ) lua_p [ 1 ] ;
2012-03-11 05:47:38 +00:00
}
2012-03-11 17:25:25 +00:00
else if ( Global . Emulator is BizHawk . Emulation . Consoles . TurboGrafx . PCEngine )
2012-03-11 06:50:46 +00:00
{
2012-09-02 14:47:12 +00:00
Global . CoreInputComm . PCE_ShowOBJ1 = Global . Config . PCEDispOBJ1 = ( bool ) lua_p [ 0 ] ;
Global . CoreInputComm . PCE_ShowBG1 = Global . Config . PCEDispBG1 = ( bool ) lua_p [ 1 ] ;
2012-03-11 09:51:23 +00:00
if ( lua_p . Length > 2 )
{
2012-09-02 14:47:12 +00:00
Global . CoreInputComm . PCE_ShowOBJ2 = Global . Config . PCEDispOBJ2 = ( bool ) lua_p [ 2 ] ;
Global . CoreInputComm . PCE_ShowBG2 = Global . Config . PCEDispBG2 = ( bool ) lua_p [ 3 ] ;
2012-03-11 09:51:23 +00:00
}
2012-03-11 06:50:46 +00:00
}
2012-03-11 17:25:25 +00:00
else if ( Global . Emulator is BizHawk . Emulation . Consoles . Sega . SMS )
{
2012-09-02 14:47:12 +00:00
Global . CoreInputComm . SMS_ShowOBJ = Global . Config . SMSDispOBJ = ( bool ) lua_p [ 0 ] ;
Global . CoreInputComm . SMS_ShowBG = Global . Config . SMSDispBG = ( bool ) lua_p [ 1 ] ;
2012-03-11 17:25:25 +00:00
}
2012-03-11 05:47:38 +00:00
}
2012-09-02 16:23:42 +00:00
public void emu_registerbefore ( LuaFunction luaf )
{
frame_registerbeforefunc = luaf ;
}
public void emu_registerafter ( LuaFunction luaf )
{
frame_registerafterfunc = luaf ;
}
2012-01-21 20:20:06 +00:00
//----------------------------------------------------
//Memory library
//----------------------------------------------------
2012-01-28 21:43:55 +00:00
2012-01-28 22:30:04 +00:00
public bool memory_usememorydomain ( object lua_input )
{
2012-09-02 14:47:12 +00:00
if ( lua_input . GetType ( ) ! = typeof ( string ) )
2012-01-28 22:30:04 +00:00
return false ;
for ( int x = 0 ; x < Global . Emulator . MemoryDomains . Count ; x + + )
{
if ( Global . Emulator . MemoryDomains [ x ] . Name = = lua_input . ToString ( ) )
{
CurrentMemoryDomain = x ;
return true ;
}
}
return false ;
}
2012-03-27 19:21:10 +00:00
2012-01-28 22:30:04 +00:00
public string memory_getmemorydomainlist ( )
{
string list = "" ;
for ( int x = 0 ; x < Global . Emulator . MemoryDomains . Count ; x + + )
{
list + = Global . Emulator . MemoryDomains [ x ] . Name + '\n' ;
}
return list ;
}
public string memory_getcurrentmemorydomain ( )
{
return Global . Emulator . MemoryDomains [ CurrentMemoryDomain ] . Name ;
}
2012-03-10 03:58:42 +00:00
public uint memory_readbyte ( object lua_addr )
2011-06-26 02:09:06 +00:00
{
2012-03-10 03:58:42 +00:00
int addr = LuaInt ( lua_addr ) ;
return M_R_U8 ( addr ) ;
2011-06-26 02:09:06 +00:00
}
2012-01-21 20:20:06 +00:00
2012-03-10 03:58:42 +00:00
public void memory_writebyte ( object lua_addr , object lua_v )
2011-06-26 02:09:06 +00:00
{
2012-03-10 03:58:42 +00:00
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U8 ( addr , v ) ;
2011-06-26 02:09:06 +00:00
}
2011-05-14 01:05:26 +00:00
2012-03-10 03:07:05 +00:00
public int memory_read_s8 ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
2012-09-02 14:47:12 +00:00
return ( sbyte ) M_R_U8 ( addr ) ;
2012-03-10 03:07:05 +00:00
}
public uint memory_read_u8 ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U8 ( addr ) ;
}
public int memory_read_s16_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_LE ( addr , 2 ) ;
}
public int memory_read_s24_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_LE ( addr , 3 ) ;
}
public int memory_read_s32_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_LE ( addr , 4 ) ;
}
public uint memory_read_u16_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_LE ( addr , 2 ) ;
}
public uint memory_read_u24_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_LE ( addr , 3 ) ;
}
public uint memory_read_u32_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_LE ( addr , 4 ) ;
}
public int memory_read_s16_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_BE ( addr , 2 ) ;
}
public int memory_read_s24_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_BE ( addr , 3 ) ;
}
public int memory_read_s32_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_S_BE ( addr , 4 ) ;
}
public uint memory_read_u16_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_BE ( addr , 2 ) ;
}
public uint memory_read_u24_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_BE ( addr , 3 ) ;
}
public uint memory_read_u32_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return M_R_U_BE ( addr , 4 ) ;
}
public void memory_write_s8 ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
2012-09-02 14:47:12 +00:00
M_W_U8 ( addr , ( uint ) v ) ;
2012-03-10 03:07:05 +00:00
}
public void memory_write_u8 ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U8 ( addr , v ) ;
}
public void memory_write_s16_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_LE ( addr , v , 2 ) ;
}
public void memory_write_s24_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_LE ( addr , v , 3 ) ;
}
public void memory_write_s32_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_LE ( addr , v , 4 ) ;
}
public void memory_write_u16_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_LE ( addr , v , 2 ) ;
}
public void memory_write_u24_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_LE ( addr , v , 3 ) ;
}
public void memory_write_u32_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_LE ( addr , v , 4 ) ;
}
public void memory_write_s16_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_BE ( addr , v , 2 ) ;
}
public void memory_write_s24_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_BE ( addr , v , 3 ) ;
}
public void memory_write_s32_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
M_W_S_BE ( addr , v , 4 ) ;
}
public void memory_write_u16_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_BE ( addr , v , 2 ) ;
}
public void memory_write_u24_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_BE ( addr , v , 3 ) ;
}
public void memory_write_u32_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
M_W_U_BE ( addr , v , 4 ) ;
}
private int M_R_S_LE ( int addr , int size )
{
return U2S ( M_R_U_LE ( addr , size ) , size ) ;
}
private uint M_R_U_LE ( int addr , int size )
{
uint v = 0 ;
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
v | = M_R_U8 ( addr + i ) < < 8 * i ;
2012-03-10 03:07:05 +00:00
return v ;
}
private int M_R_S_BE ( int addr , int size )
{
return U2S ( M_R_U_BE ( addr , size ) , size ) ;
}
private uint M_R_U_BE ( int addr , int size )
{
uint v = 0 ;
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
v | = M_R_U8 ( addr + i ) < < 8 * ( size - 1 - i ) ;
2012-03-10 03:07:05 +00:00
return v ;
}
private void M_W_S_LE ( int addr , int v , int size )
{
2012-09-02 14:47:12 +00:00
M_W_U_LE ( addr , ( uint ) v , size ) ;
2012-03-10 03:07:05 +00:00
}
private void M_W_U_LE ( int addr , uint v , int size )
{
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
M_W_U8 ( addr + i , ( v > > ( 8 * i ) ) & 0xFF ) ;
2012-03-10 03:07:05 +00:00
}
private void M_W_S_BE ( int addr , int v , int size )
{
2012-09-02 14:47:12 +00:00
M_W_U_BE ( addr , ( uint ) v , size ) ;
2012-03-10 03:07:05 +00:00
}
private void M_W_U_BE ( int addr , uint v , int size )
{
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
M_W_U8 ( addr + i , ( v > > ( 8 * ( size - 1 - i ) ) ) & 0xFF ) ;
2012-03-10 03:07:05 +00:00
}
private uint M_R_U8 ( int addr )
{
return Global . Emulator . MemoryDomains [ CurrentMemoryDomain ] . PeekByte ( addr ) ;
}
private void M_W_U8 ( int addr , uint v )
{
2012-09-02 14:47:12 +00:00
Global . Emulator . MemoryDomains [ CurrentMemoryDomain ] . PokeByte ( addr , ( byte ) v ) ;
2012-03-10 03:07:05 +00:00
}
2012-01-28 22:11:39 +00:00
//----------------------------------------------------
//Main Memory library
//----------------------------------------------------
2012-03-10 03:58:42 +00:00
public uint mainmemory_readbyte ( object lua_addr )
2012-01-28 22:11:39 +00:00
{
2012-03-10 03:58:42 +00:00
int addr = LuaInt ( lua_addr ) ;
return MM_R_U8 ( addr ) ;
2012-01-28 22:11:39 +00:00
}
2012-03-10 03:58:42 +00:00
public void mainmemory_writebyte ( object lua_addr , object lua_v )
2012-01-28 22:11:39 +00:00
{
2012-03-10 03:58:42 +00:00
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U8 ( addr , v ) ;
2012-01-28 22:11:39 +00:00
}
2012-03-10 03:07:05 +00:00
public int mainmemory_read_s8 ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
2012-09-02 14:47:12 +00:00
return ( sbyte ) MM_R_U8 ( addr ) ;
2012-03-10 03:07:05 +00:00
}
public uint mainmemory_read_u8 ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U8 ( addr ) ;
}
public int mainmemory_read_s16_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_LE ( addr , 2 ) ;
}
public int mainmemory_read_s24_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_LE ( addr , 3 ) ;
}
public int mainmemory_read_s32_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_LE ( addr , 4 ) ;
}
public uint mainmemory_read_u16_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_LE ( addr , 2 ) ;
}
public uint mainmemory_read_u24_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_LE ( addr , 3 ) ;
}
public uint mainmemory_read_u32_le ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_LE ( addr , 4 ) ;
}
public int mainmemory_read_s16_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_BE ( addr , 2 ) ;
}
public int mainmemory_read_s24_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_BE ( addr , 3 ) ;
}
public int mainmemory_read_s32_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_S_BE ( addr , 4 ) ;
}
public uint mainmemory_read_u16_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_BE ( addr , 2 ) ;
}
public uint mainmemory_read_u24_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_BE ( addr , 3 ) ;
}
public uint mainmemory_read_u32_be ( object lua_addr )
{
int addr = LuaInt ( lua_addr ) ;
return MM_R_U_BE ( addr , 4 ) ;
}
public void mainmemory_write_s8 ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
2012-09-02 14:47:12 +00:00
MM_W_U8 ( addr , ( uint ) v ) ;
2012-03-10 03:07:05 +00:00
}
public void mainmemory_write_u8 ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U8 ( addr , v ) ;
}
public void mainmemory_write_s16_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_LE ( addr , v , 2 ) ;
}
public void mainmemory_write_s24_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_LE ( addr , v , 3 ) ;
}
public void mainmemory_write_s32_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_LE ( addr , v , 4 ) ;
}
public void mainmemory_write_u16_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_LE ( addr , v , 2 ) ;
}
public void mainmemory_write_u24_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_LE ( addr , v , 3 ) ;
}
public void mainmemory_write_u32_le ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_LE ( addr , v , 4 ) ;
}
public void mainmemory_write_s16_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_BE ( addr , v , 2 ) ;
}
public void mainmemory_write_s24_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_BE ( addr , v , 3 ) ;
}
public void mainmemory_write_s32_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
int v = LuaInt ( lua_v ) ;
MM_W_S_BE ( addr , v , 4 ) ;
}
public void mainmemory_write_u16_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_BE ( addr , v , 2 ) ;
}
public void mainmemory_write_u24_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_BE ( addr , v , 3 ) ;
}
public void mainmemory_write_u32_be ( object lua_addr , object lua_v )
{
int addr = LuaInt ( lua_addr ) ;
uint v = LuaUInt ( lua_v ) ;
MM_W_U_BE ( addr , v , 4 ) ;
}
private int MM_R_S_LE ( int addr , int size )
{
return U2S ( MM_R_U_LE ( addr , size ) , size ) ;
}
private uint MM_R_U_LE ( int addr , int size )
{
uint v = 0 ;
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
v | = MM_R_U8 ( addr + i ) < < 8 * i ;
2012-03-10 03:07:05 +00:00
return v ;
}
private int MM_R_S_BE ( int addr , int size )
{
return U2S ( MM_R_U_BE ( addr , size ) , size ) ;
}
private uint MM_R_U_BE ( int addr , int size )
{
uint v = 0 ;
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
v | = MM_R_U8 ( addr + i ) < < 8 * ( size - 1 - i ) ;
2012-03-10 03:07:05 +00:00
return v ;
}
private void MM_W_S_LE ( int addr , int v , int size )
{
2012-09-02 14:47:12 +00:00
MM_W_U_LE ( addr , ( uint ) v , size ) ;
2012-03-10 03:07:05 +00:00
}
private void MM_W_U_LE ( int addr , uint v , int size )
{
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
MM_W_U8 ( addr + i , ( v > > ( 8 * i ) ) & 0xFF ) ;
2012-03-10 03:07:05 +00:00
}
private void MM_W_S_BE ( int addr , int v , int size )
{
2012-09-02 14:47:12 +00:00
MM_W_U_BE ( addr , ( uint ) v , size ) ;
2012-03-10 03:07:05 +00:00
}
private void MM_W_U_BE ( int addr , uint v , int size )
{
2012-03-27 19:21:10 +00:00
for ( int i = 0 ; i < size ; + + i )
2012-09-02 14:47:12 +00:00
MM_W_U8 ( addr + i , ( v > > ( 8 * ( size - 1 - i ) ) ) & 0xFF ) ;
2012-03-10 03:07:05 +00:00
}
private uint MM_R_U8 ( int addr )
{
return Global . Emulator . MainMemory . PeekByte ( addr ) ;
}
private void MM_W_U8 ( int addr , uint v )
{
2012-09-02 14:47:12 +00:00
Global . Emulator . MainMemory . PokeByte ( addr , ( byte ) v ) ;
2012-03-10 03:07:05 +00:00
}
private int U2S ( uint u , int size )
{
2012-09-02 14:47:12 +00:00
int s = ( int ) u ;
s < < = 8 * ( 4 - size ) ;
s > > = 8 * ( 4 - size ) ;
2012-03-10 03:07:05 +00:00
return s ;
}
2012-01-21 20:20:06 +00:00
//----------------------------------------------------
2012-09-02 00:56:50 +00:00
//Bitwise Operator library
//----------------------------------------------------
2012-09-02 14:47:12 +00:00
public uint bit_band ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( LuaInt ( val ) & LuaInt ( amt ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_lshift ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( LuaInt ( val ) < < LuaInt ( amt ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_rshift ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( LuaInt ( val ) > > LuaInt ( amt ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_rol ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( ( LuaInt ( val ) < < LuaInt ( amt ) ) | ( LuaInt ( val ) > > ( 32 - LuaInt ( amt ) ) ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_ror ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( ( LuaInt ( val ) > > LuaInt ( amt ) ) | ( LuaInt ( val ) < < ( 32 - LuaInt ( amt ) ) ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_bor ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( LuaInt ( val ) | LuaInt ( amt ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_bxor ( object val , object amt )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( LuaInt ( val ) ^ LuaInt ( amt ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
public uint bit_bnot ( object val )
2012-09-02 00:56:50 +00:00
{
2012-09-02 16:26:43 +00:00
return ( uint ) ( ~ LuaInt ( val ) ) ;
2012-09-02 00:56:50 +00:00
}
2012-09-02 14:47:12 +00:00
//----------------------------------------------------
2012-01-21 20:20:06 +00:00
//Savestate library
//----------------------------------------------------
2012-02-03 12:18:27 +00:00
public void savestate_saveslot ( object lua_input )
{
int x = 0 ;
try //adelikat: This crap might not be necessary, need to test for a more elegant solution
{
x = int . Parse ( lua_input . ToString ( ) ) ;
}
catch
{
return ;
}
if ( x < 0 | | x > 9 )
return ;
2012-03-27 19:21:10 +00:00
Global . MainForm . SaveState ( "QuickSave" + x . ToString ( ) ) ;
2012-02-03 12:18:27 +00:00
}
public void savestate_loadslot ( object lua_input )
{
int x = 0 ;
try //adelikat: This crap might not be necessary, need to test for a more elegant solution
{
x = int . Parse ( lua_input . ToString ( ) ) ;
}
catch
{
return ;
}
if ( x < 0 | | x > 9 )
return ;
Global . MainForm . LoadState ( "QuickLoad" + x . ToString ( ) ) ;
}
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 ) )
{
2012-03-18 18:24:24 +00:00
string path = lua_input . ToString ( ) ;
var writer = new StreamWriter ( path ) ;
Global . MainForm . SaveStateFile ( writer , path , true ) ;
2012-01-22 22:20:09 +00:00
}
}
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-03-27 21:09:36 +00:00
public void savestate_registersave ( LuaFunction luaf )
{
savestate_registersavefunc = luaf ;
}
2012-03-27 21:17:20 +00:00
public void savestate_registerload ( LuaFunction luaf )
{
savestate_registerloadfunc = luaf ;
}
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
}
2012-03-27 19:21:10 +00:00
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-03-18 19:33:38 +00:00
public bool movie_isloaded ( )
{
if ( Global . MovieSession . Movie . Mode = = MOVIEMODE . INACTIVE )
return false ;
else
return true ;
}
public int movie_length ( )
{
2012-09-03 15:05:09 +00:00
return Global . MovieSession . Movie . TotalFrames ;
2012-03-18 19:33:38 +00:00
}
2012-03-18 19:52:28 +00:00
public string movie_filename ( )
{
return Global . MovieSession . Movie . Filename ;
}
public bool movie_getreadonly ( )
{
return Global . MainForm . ReadOnly ;
}
public void movie_setreadonly ( object lua_input )
{
if ( lua_input . ToString ( ) . ToUpper ( ) = = "TRUE" | | lua_input . ToString ( ) = = "1" )
Global . MainForm . SetReadOnly ( true ) ;
else
Global . MainForm . SetReadOnly ( false ) ;
}
2012-03-27 19:21:10 +00:00
public LuaTable movie_getinput ( object frame )
{
LuaTable input = lua . NewTable ( ) ;
2012-03-27 15:45:50 +00:00
2012-03-27 19:21:10 +00:00
string s = Global . MovieSession . Movie . GetInputFrame ( LuaInt ( frame ) ) ;
MovieControllerAdapter m = new MovieControllerAdapter ( ) ;
2012-03-27 20:10:05 +00:00
m . Type = Global . MovieSession . MovieControllerAdapter . Type ;
2012-03-27 19:21:10 +00:00
m . SetControllersAsMnemonic ( s ) ;
2012-03-29 03:09:46 +00:00
foreach ( string button in m . Type . BoolButtons )
input [ button ] = m [ button ] ;
2012-03-27 15:45:50 +00:00
2012-03-27 19:21:10 +00:00
return input ;
}
2012-03-27 15:45:50 +00:00
2012-03-29 03:16:55 +00:00
public bool movie_getrerecordcounting ( )
2012-03-29 03:09:46 +00:00
{
return Global . MovieSession . Movie . RerecordCounting ;
}
2012-03-29 03:16:55 +00:00
public void movie_setrerecordcounting ( object lua_input )
2012-03-29 03:09:46 +00:00
{
if ( lua_input . ToString ( ) . ToUpper ( ) = = "TRUE" | | lua_input . ToString ( ) = = "1" )
Global . MovieSession . Movie . RerecordCounting = true ;
else
Global . MovieSession . Movie . RerecordCounting = false ;
}
2012-03-24 15:55:22 +00:00
//----------------------------------------------------
//Input library
//----------------------------------------------------
public LuaTable input_get ( )
{
2012-03-24 18:39:55 +00:00
LuaTable buttons = lua . NewTable ( ) ;
foreach ( var kvp in Global . ControllerInputCoalescer . BoolButtons ( ) )
if ( kvp . Value )
buttons [ kvp . Key ] = true ;
return buttons ;
2012-03-24 15:55:22 +00:00
}
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
-As much as I dislike the new joypad.set() setup, the least I could do is make it consistent with joypad.get().
--If there is no controller parameter, then all of the buttons are returned as they are stored in the system, just like joypad.set(input) takes button names as is.
--If there is a controller parameter, all of the buttons for that controller are returned without the "PX ", just like joypad.set(input, controller) takes button names without the "PX " and assigns them to the matching buttons for that controller.
--No one approved this change, but seriously, this is common sense. I expect some "change denied" April Fool's stuff tomorrow...
-Implemented a blacklist for ButtonCount. By default, Lag, Pause, and Reset are blacklisted. I don't think any of these buttons should be tracked.
2012-04-01 08:08:40 +00:00
public LuaTable joypad_get ( object controller = null )
2012-01-21 20:05:53 +00:00
{
2012-03-24 18:39:55 +00:00
LuaTable buttons = lua . NewTable ( ) ;
-As much as I dislike the new joypad.set() setup, the least I could do is make it consistent with joypad.get().
--If there is no controller parameter, then all of the buttons are returned as they are stored in the system, just like joypad.set(input) takes button names as is.
--If there is a controller parameter, all of the buttons for that controller are returned without the "PX ", just like joypad.set(input, controller) takes button names without the "PX " and assigns them to the matching buttons for that controller.
--No one approved this change, but seriously, this is common sense. I expect some "change denied" April Fool's stuff tomorrow...
-Implemented a blacklist for ButtonCount. By default, Lag, Pause, and Reset are blacklisted. I don't think any of these buttons should be tracked.
2012-04-01 08:08:40 +00:00
foreach ( string button in Global . ControllerOutput . Source . Type . BoolButtons )
if ( controller = = null )
buttons [ button ] = Global . ControllerOutput [ button ] ;
else if ( button . Length > = 3 & & button . Substring ( 0 , 2 ) = = "P" + LuaInt ( controller ) . ToString ( ) )
buttons [ button . Substring ( 3 ) ] = Global . ControllerOutput [ "P" + LuaInt ( controller ) + " " + button . Substring ( 3 ) ] ;
2012-03-28 08:35:43 +00:00
Made joypad_get independent of mnemonics. Here's the Lua script I used to test:
while true do
joypad.set("Up", true)
local buttons = joypad.get()
local result = {}
for index, value in pairs(buttons) do
table.insert(result, index .. ": " .. tostring(value))
end
gui.text(0, 36, table.concat(result, "\n"))
emu.frameadvance()
end
For some bizarre reason, after a while, the ordering of the buttons goes from stable to chaotic, making it impossible to read the buttons pressed. adelikat says not to worry about this because order is meaningless in Lua. Still, this is very curious...
TODO: Set using a ClickyVirtualPadController and Global.StickyXORAdapter.SetSticky(Controller + " Up", false)...whatever that means.
2012-03-19 14:52:23 +00:00
buttons [ "clear" ] = null ;
buttons [ "getluafunctionslist" ] = null ;
buttons [ "output" ] = null ;
2012-03-27 19:21:10 +00:00
2012-03-23 22:52:02 +00:00
return buttons ;
}
public LuaTable joypad_getimmediate ( )
{
2012-03-24 18:39:55 +00:00
LuaTable buttons = lua . NewTable ( ) ;
2012-03-23 22:52:02 +00:00
foreach ( string button in Global . ActiveController . Type . BoolButtons )
buttons [ button ] = Global . ActiveController [ button ] ;
Made joypad_get independent of mnemonics. Here's the Lua script I used to test:
while true do
joypad.set("Up", true)
local buttons = joypad.get()
local result = {}
for index, value in pairs(buttons) do
table.insert(result, index .. ": " .. tostring(value))
end
gui.text(0, 36, table.concat(result, "\n"))
emu.frameadvance()
end
For some bizarre reason, after a while, the ordering of the buttons goes from stable to chaotic, making it impossible to read the buttons pressed. adelikat says not to worry about this because order is meaningless in Lua. Still, this is very curious...
TODO: Set using a ClickyVirtualPadController and Global.StickyXORAdapter.SetSticky(Controller + " Up", false)...whatever that means.
2012-03-19 14:52:23 +00:00
return buttons ;
2012-01-21 20:20:06 +00:00
}
2012-01-28 21:43:55 +00:00
-As much as I dislike the new joypad.set() setup, the least I could do is make it consistent with joypad.get().
--If there is no controller parameter, then all of the buttons are returned as they are stored in the system, just like joypad.set(input) takes button names as is.
--If there is a controller parameter, all of the buttons for that controller are returned without the "PX ", just like joypad.set(input, controller) takes button names without the "PX " and assigns them to the matching buttons for that controller.
--No one approved this change, but seriously, this is common sense. I expect some "change denied" April Fool's stuff tomorrow...
-Implemented a blacklist for ButtonCount. By default, Lag, Pause, and Reset are blacklisted. I don't think any of these buttons should be tracked.
2012-04-01 08:08:40 +00:00
public void joypad_set ( LuaTable buttons , object controller = null )
2012-01-21 20:20:06 +00:00
{
2012-09-02 14:47:12 +00:00
foreach ( var button in buttons . Keys )
{
if ( Convert . ToBoolean ( buttons [ button ] ) = = true )
-As much as I dislike the new joypad.set() setup, the least I could do is make it consistent with joypad.get().
--If there is no controller parameter, then all of the buttons are returned as they are stored in the system, just like joypad.set(input) takes button names as is.
--If there is a controller parameter, all of the buttons for that controller are returned without the "PX ", just like joypad.set(input, controller) takes button names without the "PX " and assigns them to the matching buttons for that controller.
--No one approved this change, but seriously, this is common sense. I expect some "change denied" April Fool's stuff tomorrow...
-Implemented a blacklist for ButtonCount. By default, Lag, Pause, and Reset are blacklisted. I don't think any of these buttons should be tracked.
2012-04-01 08:08:40 +00:00
if ( controller = = null )
2012-09-02 14:47:12 +00:00
Global . ClickyVirtualPadController . Click ( button . ToString ( ) ) ;
else
-As much as I dislike the new joypad.set() setup, the least I could do is make it consistent with joypad.get().
--If there is no controller parameter, then all of the buttons are returned as they are stored in the system, just like joypad.set(input) takes button names as is.
--If there is a controller parameter, all of the buttons for that controller are returned without the "PX ", just like joypad.set(input, controller) takes button names without the "PX " and assigns them to the matching buttons for that controller.
--No one approved this change, but seriously, this is common sense. I expect some "change denied" April Fool's stuff tomorrow...
-Implemented a blacklist for ButtonCount. By default, Lag, Pause, and Reset are blacklisted. I don't think any of these buttons should be tracked.
2012-04-01 08:08:40 +00:00
Global . ClickyVirtualPadController . Click ( "P" + controller . ToString ( ) + " " + button . ToString ( ) ) ;
2012-09-02 14:47:12 +00:00
}
2011-06-26 02:09:06 +00:00
}
2012-01-22 22:20:09 +00:00
//----------------------------------------------------
//Client library
//----------------------------------------------------
2012-09-02 16:49:58 +00:00
public int client_screenwidth ( )
{
return Global . RenderPanel . NativeSize . Width ;
}
public int client_screenheight ( )
{
return Global . RenderPanel . NativeSize . Height ;
}
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 ( )
{
2012-08-15 01:14:25 +00:00
Global . MainForm . LoadRamWatch ( true ) ;
2012-01-22 22:20:09 +00:00
}
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 ( ) ;
}
2012-07-12 22:20:48 +00:00
public void client_setwindowsize ( object window_size )
{
try
{
string temp = window_size . ToString ( ) ;
int size = Convert . ToInt32 ( temp ) ;
if ( size = = 1 | | size = = 2 | | size = = 3 | | size = = 4 | | size = = 5 | | size = = 10 )
{
Global . Config . TargetZoomFactor = size ;
Global . MainForm . FrameBufferResized ( ) ;
Global . OSD . AddMessage ( "Window size set to " + size . ToString ( ) + "x" ) ;
}
else
{
console_log ( "Invalid window size" ) ;
}
}
catch
{
console_log ( "Invalid window size" ) ;
}
}
2012-01-22 22:42:40 +00:00
public void client_opencheats ( )
{
Global . MainForm . LoadCheatsWindow ( ) ;
}
2012-07-10 19:04:35 +00:00
2012-08-25 17:01:13 +00:00
//----------------------------------------------------
//Winforms library
//----------------------------------------------------
2012-07-10 19:04:35 +00:00
public List < LuaWinform > LuaForms = new List < LuaWinform > ( ) ;
public int forms_newform ( object Width = null , object Height = null , object title = null )
{
2012-09-02 14:47:12 +00:00
2012-07-10 19:04:35 +00:00
LuaWinform theForm = new LuaWinform ( ) ;
LuaForms . Add ( theForm ) ;
if ( Width ! = null & & Height ! = null )
{
theForm . Size = new Size ( LuaInt ( Width ) , LuaInt ( Height ) ) ;
}
if ( title ! = null )
{
theForm . Text = title . ToString ( ) ;
}
theForm . Show ( ) ;
return ( int ) theForm . Handle ;
}
public void WindowClosed ( IntPtr handle )
{
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = handle )
{
LuaForms . Remove ( form ) ;
return ;
}
}
}
public bool forms_destroy ( object handle )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
form . Close ( ) ;
LuaForms . Remove ( form ) ;
return true ;
}
}
return false ;
}
public void forms_destroyall ( )
{
foreach ( LuaWinform form in LuaForms )
{
form . Close ( ) ;
LuaForms . Remove ( form ) ;
}
}
2012-07-10 20:23:19 +00:00
2012-07-10 21:15:30 +00:00
private LuaWinform GetForm ( object form_handle )
2012-07-10 20:23:19 +00:00
{
IntPtr ptr = new IntPtr ( LuaInt ( form_handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
2012-07-10 21:15:30 +00:00
return form ;
}
}
return null ;
}
private void SetLocation ( Control control , object X , object Y )
{
try
{
if ( X ! = null & & Y ! = null )
{
int x = LuaInt ( X ) ;
int y = LuaInt ( Y ) ;
control . Location = new Point ( x , y ) ;
}
}
catch
{
//Do nothing
}
}
private void SetSize ( Control control , object Width , object Height )
{
try
{
if ( Width ! = null & & Height ! = null )
{
int width = LuaInt ( Width ) ;
int height = LuaInt ( Height ) ;
control . Size = new Size ( width , height ) ;
}
}
catch
{
//Do nothing
}
}
2012-07-10 22:27:05 +00:00
private void SetText ( Control control , object caption )
{
if ( caption ! = null )
{
control . Text = caption . ToString ( ) ;
}
}
2012-08-25 17:01:13 +00:00
public int forms_button ( object form_handle , object caption , LuaFunction lua_event , object X = null , object Y = null , object width = null , object height = null )
2012-07-10 21:15:30 +00:00
{
LuaWinform form = GetForm ( form_handle ) ;
if ( form = = null )
{
return 0 ;
}
2012-09-02 14:47:12 +00:00
2012-07-10 21:15:30 +00:00
LuaButton button = new LuaButton ( ) ;
2012-07-10 22:27:05 +00:00
SetText ( button , caption ) ;
2012-07-10 21:15:30 +00:00
form . Controls . Add ( button ) ;
form . Control_Events . Add ( new LuaWinform . Lua_Event ( button . Handle , lua_event ) ) ;
2012-08-25 17:01:13 +00:00
if ( X ! = null & & Y ! = null )
SetLocation ( button , X , Y ) ;
if ( width ! = null & height ! = null )
SetSize ( button , width , height ) ;
2012-07-10 21:15:30 +00:00
return ( int ) button . Handle ;
}
2012-08-25 17:01:13 +00:00
public int forms_label ( object form_handle , object caption , object X = null , object Y = null , object width = null , object height = null )
2012-07-10 21:15:30 +00:00
{
LuaWinform form = GetForm ( form_handle ) ;
if ( form = = null )
{
return 0 ;
}
Label label = new Label ( ) ;
2012-07-10 22:27:05 +00:00
SetText ( label , caption ) ;
2012-07-10 21:15:30 +00:00
form . Controls . Add ( label ) ;
2012-08-25 17:01:13 +00:00
if ( X ! = null & & Y ! = null )
SetLocation ( label , X , Y ) ;
if ( width ! = null & height ! = null )
SetSize ( label , width , height ) ;
2012-07-10 21:15:30 +00:00
return ( int ) label . Handle ;
}
2012-07-10 23:09:06 +00:00
public int forms_textbox ( object form_handle , object caption = null , object width = null , object height = null , object boxtype = null , object X = null , object Y = null )
2012-07-10 21:15:30 +00:00
{
LuaWinform form = GetForm ( form_handle ) ;
if ( form = = null )
{
return 0 ;
}
2012-07-10 23:09:06 +00:00
LuaTextBox textbox = new LuaTextBox ( ) ;
2012-07-10 22:27:05 +00:00
SetText ( textbox , caption ) ;
2012-08-25 22:45:44 +00:00
2012-09-02 14:47:12 +00:00
if ( X ! = null & & Y ! = null )
SetLocation ( textbox , X , Y ) ;
2012-08-25 22:45:44 +00:00
2012-09-02 14:47:12 +00:00
if ( width ! = null & height ! = null )
SetSize ( textbox , width , height ) ;
2012-08-25 22:45:44 +00:00
2012-07-10 23:09:06 +00:00
if ( boxtype ! = null )
{
switch ( boxtype . ToString ( ) . ToUpper ( ) )
{
case "HEX" :
case "HEXADECIMAL" :
textbox . SetType ( BoxType . HEX ) ;
break ;
case "UNSIGNED" :
case "UINT" :
textbox . SetType ( BoxType . UNSIGNED ) ;
break ;
case "NUMBER" :
case "NUM" :
case "SIGNED" :
case "INT" :
textbox . SetType ( BoxType . SIGNED ) ;
break ;
}
}
2012-07-10 21:15:30 +00:00
form . Controls . Add ( textbox ) ;
return ( int ) textbox . Handle ;
}
public void forms_setlocation ( object handle , object X , object Y )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
SetLocation ( form , X , Y ) ;
}
else
{
foreach ( Control control in form . Controls )
2012-07-10 20:23:19 +00:00
{
2012-07-10 21:15:30 +00:00
if ( control . Handle = = ptr )
2012-07-10 20:23:19 +00:00
{
2012-07-10 21:15:30 +00:00
SetLocation ( control , X , Y ) ;
2012-07-10 20:23:19 +00:00
}
}
2012-07-10 21:15:30 +00:00
}
}
}
public void forms_setsize ( object handle , object Width , object Height )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
SetSize ( form , Width , Height ) ;
}
else
{
foreach ( Control control in form . Controls )
2012-07-10 20:23:19 +00:00
{
2012-07-10 21:15:30 +00:00
if ( control . Handle = = ptr )
{
SetSize ( control , Width , Height ) ;
}
2012-07-10 20:23:19 +00:00
}
}
}
}
2012-07-10 22:27:05 +00:00
public void forms_settext ( object handle , object caption )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
SetText ( form , caption ) ;
}
else
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
SetText ( control , caption ) ;
}
}
}
}
}
2012-07-10 22:52:06 +00:00
2012-09-02 14:47:12 +00:00
public void forms_setproperty ( object handle , object property , object value )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
form . GetType ( ) . GetProperty ( property . ToString ( ) ) . SetValue ( form , value , null ) ;
}
else
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
control . GetType ( ) . GetProperty ( property . ToString ( ) ) . SetValue ( control , value , null ) ;
}
}
}
}
}
2012-08-25 22:45:44 +00:00
2012-07-10 22:52:06 +00:00
public void forms_addclick ( object handle , LuaFunction lua_event )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
form . Control_Events . Add ( new LuaWinform . Lua_Event ( control . Handle , lua_event ) ) ;
}
}
}
}
public void forms_clearclicks ( object handle )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
List < LuaWinform . Lua_Event > lua_events = form . Control_Events . Where ( x = > x . Control = = ptr ) . ToList ( ) ;
foreach ( LuaWinform . Lua_Event levent in lua_events )
{
form . Control_Events . Remove ( levent ) ;
}
}
}
}
}
public string forms_gettext ( object handle )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
return form . Text ;
}
else
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
return control . Text ;
}
}
}
}
return "" ;
}
2012-07-13 22:21:01 +00:00
2012-09-02 14:47:12 +00:00
public string forms_getproperty ( object handle , object property )
{
IntPtr ptr = new IntPtr ( LuaInt ( handle ) ) ;
foreach ( LuaWinform form in LuaForms )
{
if ( form . Handle = = ptr )
{
return form . GetType ( ) . GetProperty ( property . ToString ( ) ) . GetValue ( form , null ) . ToString ( ) ;
}
else
{
foreach ( Control control in form . Controls )
{
if ( control . Handle = = ptr )
{
return control . GetType ( ) . GetProperty ( property . ToString ( ) ) . GetValue ( control , null ) . ToString ( ) ;
}
}
}
}
return "" ;
}
2012-08-25 22:45:44 +00:00
2012-07-13 22:21:01 +00:00
public LuaTable input_getmouse ( )
{
LuaTable buttons = lua . NewTable ( ) ;
buttons [ "X" ] = Control . MousePosition . X ;
buttons [ "Y" ] = Control . MousePosition . Y ;
buttons [ MouseButtons . Left . ToString ( ) ] = Control . MouseButtons & MouseButtons . Left ;
buttons [ MouseButtons . Middle . ToString ( ) ] = Control . MouseButtons & MouseButtons . Middle ;
buttons [ MouseButtons . Right . ToString ( ) ] = Control . MouseButtons & MouseButtons . Right ;
buttons [ MouseButtons . XButton1 . ToString ( ) ] = Control . MouseButtons & MouseButtons . XButton1 ;
buttons [ MouseButtons . XButton2 . ToString ( ) ] = Control . MouseButtons & MouseButtons . XButton2 ;
return buttons ;
}
2011-06-26 02:09:06 +00:00
}
2011-02-20 19:18:27 +00:00
}