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-01-28 21:43:55 +00:00
Lua lua = new Lua ( ) ;
2011-06-26 02:09:06 +00:00
LuaConsole Caller ;
2012-03-28 12:26:43 +00:00
public List < string > LuaLibraryList = new List < string > ( ) ;
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-03-23 18:24:29 +00:00
Lua currThread ;
2012-03-27 21:09:36 +00:00
LuaFunction savestate_registersavefunc ;
2012-03-27 21:17:20 +00:00
LuaFunction savestate_registerloadfunc ;
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-03-28 00:13:25 +00:00
Global . MainForm . LuaConsole1 . WriteToOutputWindow ( "error running function attached by lua function savestate.registersave" +
"\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-03-28 00:13:25 +00:00
Global . MainForm . LuaConsole1 . WriteToOutputWindow ( "error running function attached by lua function savestate.registerload" +
"\nError message: " + e . Message ) ;
2012-03-27 21:17:20 +00:00
}
}
}
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-03-28 12:26:43 +00:00
LuaLibraryList . 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 + + )
{
lua . RegisterFunction ( "console." + ConsoleFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "console_" + ConsoleFunctions [ i ] ) ) ;
2012-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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 + + )
{
lua . RegisterFunction ( "mainmemory." + MainMemoryFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "mainmemory_" + MainMemoryFunctions [ i ] ) ) ;
2012-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-02-03 12:18:27 +00:00
lua . RegisterFunction ( "savestate." + SaveStateFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "savestate_" + SaveStateFunctions [ i ] ) ) ;
2012-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "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 + + )
{
lua . RegisterFunction ( "client." + MultiClientFunctions [ i ] , this , this . GetType ( ) . GetMethod ( "client_" + MultiClientFunctions [ i ] ) ) ;
2012-03-28 12:26:43 +00:00
LuaLibraryList . Add ( "client." + MultiClientFunctions [ i ] ) ;
2012-01-22 22:20:09 +00:00
}
2012-03-28 12:26:43 +00:00
LuaLibraryList . 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 )
{
return Convert . ToInt32 ( ( double ) lua_arg ) ;
}
private uint LuaUInt ( object lua_arg )
{
return Convert . ToUInt32 ( ( double ) lua_arg ) ;
}
2012-06-10 23:27:30 +00:00
public Color GetColor ( object color )
{
if ( color . GetType ( ) = = typeof ( Double ) )
{
return System . Drawing . Color . FromArgb ( int . Parse ( long . Parse ( color . ToString ( ) ) . ToString ( "X" ) , System . Globalization . NumberStyles . HexNumber ) ) ;
}
else
{
return System . Drawing . Color . FromName ( color . ToString ( ) . ToLower ( ) ) ;
}
}
public SolidBrush GetBrush ( object color )
{
return new System . Drawing . SolidBrush ( GetColor ( color ) ) ;
}
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 .
* /
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 ;
}
public ResumeResult ResumeScript ( Lua script )
2012-03-23 18:24:29 +00:00
{
2012-03-23 19:44:47 +00:00
currThread = script ;
script . Resume ( 0 ) ;
currThread = null ;
2012-03-23 23:03:39 +00:00
var result = new ResumeResult ( ) ;
result . WaitForFrame = FrameAdvanceRequested ;
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-01-28 22:11:39 +00:00
public static string [ ] ConsoleFunctions = new string [ ]
{
2012-01-22 03:14:31 +00:00
"output" ,
"clear" ,
2012-03-11 05:13:12 +00:00
"getluafunctionslist" ,
2012-01-21 20:05:53 +00:00
} ;
2012-01-28 22:44:48 +00:00
public static string [ ] GuiFunctions = new string [ ]
{
2012-03-11 00:54:24 +00:00
"text" ,
2012-03-11 05:13:12 +00:00
"alert" ,
2012-04-16 08:18:41 +00:00
"cleartext" ,
2012-04-17 03:21:16 +00:00
"drawPixel" ,
"drawLine" ,
"drawRectangle" ,
"drawEllipse" ,
"drawPolygon" ,
"drawBezier" ,
2012-04-17 21:41:21 +00:00
"drawPie" ,
2012-04-25 20:34:50 +00:00
"drawIcon" ,
2012-04-26 03:41:31 +00:00
"drawImage" ,
2012-01-28 22:44:48 +00:00
} ;
2012-01-28 22:11:39 +00:00
public static string [ ] EmuFunctions = new string [ ]
{
2012-01-22 03:14:31 +00:00
"frameadvance" ,
2012-03-23 23:03:39 +00:00
"yield" ,
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" ,
2012-03-11 05:13:12 +00:00
"getsystemid" ,
2012-03-11 05:47:38 +00:00
"setrenderplanes" ,
2011-06-26 02:09:06 +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-01-28 22:30:04 +00:00
"usememorydomain" ,
"getmemorydomainlist" ,
"getcurrentmemorydomain" ,
2012-03-10 03:07:05 +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" ,
2012-03-10 03:58:42 +00:00
"readbyte" ,
2012-03-11 05:13:12 +00:00
"writebyte" ,
2012-01-28 22:11:39 +00:00
//"registerwrite",
2012-03-11 05:13:12 +00:00
//"registerread",
2012-01-28 22:11:39 +00:00
} ;
public static string [ ] MainMemoryFunctions = new string [ ]
{
2012-03-10 03:07:05 +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" ,
2011-06-26 02:09:06 +00:00
//"registerwrite",
2012-03-11 05:13:12 +00:00
//"registerread",
2012-01-28 22:11:39 +00:00
} ;
2011-06-26 02:09:06 +00:00
public static string [ ] SaveStateFunctions = new string [ ] {
2012-02-03 12:18:27 +00:00
"saveslot" ,
"loadslot" ,
2011-06-26 02:09:06 +00:00
"save" ,
2012-03-11 05:13:12 +00:00
"load" ,
2012-03-27 21:09:36 +00:00
"registersave" ,
2012-03-27 21:17:20 +00:00
"registerload" ,
2011-06-26 02:09:06 +00:00
} ;
2012-02-03 12:18:27 +00:00
2011-06-26 02:09:06 +00:00
public static string [ ] MovieFunctions = new string [ ] {
"mode" ,
2012-03-18 19:33:38 +00:00
"isloaded" ,
2011-06-26 02:09:06 +00:00
"rerecordcount" ,
2012-03-18 19:33:38 +00:00
"length" ,
2012-03-11 05:13:12 +00:00
"stop" ,
2012-03-18 19:52:28 +00:00
"filename" ,
"getreadonly" ,
"setreadonly" ,
2012-03-29 03:09:46 +00:00
"getrerecordcounting" ,
"setrerecordcounting" ,
2012-03-27 21:09:36 +00:00
"getinput" ,
2011-06-26 02:09:06 +00:00
} ;
2012-02-03 12:18:27 +00:00
2012-03-24 15:55:22 +00:00
public static string [ ] InputFunctions = new string [ ] {
"get" ,
} ;
2011-06-26 02:09:06 +00:00
public static string [ ] JoypadFunctions = new string [ ] {
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
"set" ,
2012-03-18 19:14:50 +00:00
"get" ,
2012-03-23 22:52:02 +00:00
"getimmediate"
2011-06-26 02:09:06 +00:00
} ;
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" ,
2012-03-11 05:13:12 +00:00
"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-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 = "" ;
foreach ( string l in LuaLibraryList )
{
list + = l + "\n" ;
}
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-04-24 15:23:56 +00:00
private void do_gui_text ( object luaX , object luaY , object luaStr , bool alert , object background = null , 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-06-10 23:27:30 +00:00
Global . OSD . AddGUIText ( luaStr . ToString ( ) , LuaInt ( luaX ) , LuaInt ( luaY ) , alert , GetColor ( background ) , GetColor ( forecolor ) , a ) ;
2012-03-29 02:45:38 +00:00
}
2012-06-10 23:27:30 +00:00
public void gui_text ( object luaX , object luaY , object luaStr , object background = null , object forecolor = null , 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-04-16 20:04:43 +00:00
}
2012-06-10 22:38:44 +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-06-12 03:24:15 +00:00
using ( var pen = GetPen ( color ) )
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-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" ;
using ( var pen = GetPen ( color ) )
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-06-12 03:24:15 +00:00
using ( var pen = GetPen ( color ) )
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-04-17 21:41:21 +00:00
public void gui_drawPie ( object X , object Y , object width , object height , object startangle , object sweepangle , 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 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 )
{
using ( var brush = GetBrush ( background ) )
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 ( ) ) ;
}
g . DrawIcon ( icon , LuaInt ( x ) , LuaInt ( y ) ) ;
}
2012-06-10 22:38:44 +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 ( ) ) ;
if ( width = = null | | width . GetType ( ) ! = typeof ( int ) )
width = img . Width . ToString ( ) ;
if ( height = = null | | height . GetType ( ) ! = typeof ( int ) )
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-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-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-03-11 09:16:09 +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-03-11 09:51:23 +00:00
Global . CoreInputComm . PCE_ShowOBJ1 = Global . Config . PCEDispOBJ1 = ( bool ) lua_p [ 0 ] ;
Global . CoreInputComm . PCE_ShowBG1 = Global . Config . PCEDispBG1 = ( bool ) lua_p [ 1 ] ;
if ( lua_p . Length > 2 )
{
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 06:50:46 +00:00
}
2012-03-11 17:25:25 +00:00
else if ( Global . Emulator is BizHawk . Emulation . Consoles . Sega . SMS )
{
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 05:47:38 +00:00
}
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 )
{
if ( lua_input . GetType ( ) ! = typeof ( string ) )
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 ) ;
return ( sbyte ) M_R_U8 ( addr ) ;
}
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 ) ;
M_W_U8 ( addr , ( uint ) v ) ;
}
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 )
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 )
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 )
{
M_W_U_LE ( addr , ( uint ) v , size ) ;
}
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 )
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 )
{
M_W_U_BE ( addr , ( uint ) v , size ) ;
}
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 )
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 )
{
Global . Emulator . MemoryDomains [ CurrentMemoryDomain ] . PokeByte ( addr , ( byte ) v ) ;
}
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 ) ;
return ( sbyte ) MM_R_U8 ( addr ) ;
}
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 ) ;
MM_W_U8 ( addr , ( uint ) v ) ;
}
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 )
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 )
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 )
{
MM_W_U_LE ( addr , ( uint ) v , size ) ;
}
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 )
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 )
{
MM_W_U_BE ( addr , ( uint ) v , size ) ;
}
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 )
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 )
{
Global . Emulator . MainMemory . PokeByte ( addr , ( byte ) v ) ;
}
private int U2S ( uint u , int size )
{
int s = ( int ) u ;
2012-03-27 19:21:10 +00:00
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
//----------------------------------------------------
//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-06-07 04:47:54 +00:00
return Global . MovieSession . Movie . LogLength ( ) ;
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-03-29 19:03:14 +00:00
foreach ( var button in buttons . Keys )
2012-03-29 19:53:16 +00:00
{
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-03-29 19:53:16 +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-03-29 19:53:16 +00:00
}
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
}