2014-05-21 00:17:35 +00:00
using System ;
2016-01-30 13:09:58 +00:00
using System.IO ;
2017-07-10 04:51:02 +00:00
using NLua ;
2013-10-28 19:13:01 +00:00
2013-11-01 15:43:15 +00:00
namespace BizHawk.Client.Common
2013-10-28 19:13:01 +00:00
{
2014-06-01 22:02:59 +00:00
public sealed class MovieLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2013-10-31 13:07:42 +00:00
public MovieLuaLibrary ( Lua lua )
2014-05-21 00:17:35 +00:00
: base ( lua ) { }
public MovieLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2017-04-14 19:59:01 +00:00
public override string Name = > "movie" ;
2013-10-31 13:07:42 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("startsfromsavestate", "Returns whether or not the movie is a savestate-anchored movie")]
2015-08-01 22:16:40 +00:00
public bool StartsFromSavestate ( )
{
return Global . MovieSession . Movie . IsActive & & Global . MovieSession . Movie . StartsFromSavestate ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("startsfromsaveram", "Returns whether or not the movie is a saveram-anchored movie")]
2015-08-01 22:16:40 +00:00
public bool StartsFromSaveram ( )
{
return Global . MovieSession . Movie . IsActive & & Global . MovieSession . Movie . StartsFromSaveRam ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("filename", "Returns the file name including path of the currently loaded movie")]
2014-01-26 02:50:26 +00:00
public static string Filename ( )
2013-10-28 19:13:01 +00:00
{
return Global . MovieSession . Movie . Filename ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")]
2014-01-27 01:15:56 +00:00
public LuaTable GetInput ( int frame )
2013-10-28 19:13:01 +00:00
{
2016-03-12 09:05:24 +00:00
if ( ! Global . MovieSession . Movie . IsActive )
{
Log ( "No movie loaded" ) ;
return null ;
}
2014-05-20 20:34:51 +00:00
var input = Lua . NewTable ( ) ;
2014-06-18 02:26:22 +00:00
var adapter = Global . MovieSession . Movie . GetInputState ( frame ) ;
2013-10-28 19:13:01 +00:00
2016-03-12 09:05:24 +00:00
if ( adapter = = null )
{
Log ( "Can't get input of the last frame of the movie. Use the previous frame" ) ;
return null ;
}
2016-12-12 18:30:32 +00:00
foreach ( var button in adapter . Definition . BoolButtons )
2014-06-18 02:26:22 +00:00
{
2016-12-14 18:42:15 +00:00
input [ button ] = adapter . IsPressed ( button ) ;
2014-06-18 02:26:22 +00:00
}
2013-10-29 16:03:06 +00:00
2016-12-12 18:30:32 +00:00
foreach ( var button in adapter . Definition . FloatControls )
2013-10-29 16:03:06 +00:00
{
2016-12-14 19:22:01 +00:00
input [ button ] = adapter . GetFloat ( button ) ;
2013-10-29 16:03:06 +00:00
}
2013-10-28 19:13:01 +00:00
return input ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")]
2014-05-18 21:26:27 +00:00
public string GetInputAsMnemonic ( int frame )
{
2014-06-19 00:37:42 +00:00
if ( Global . MovieSession . Movie . IsActive & & frame < Global . MovieSession . Movie . InputLogLength )
2014-05-18 21:26:27 +00:00
{
2014-06-19 00:37:42 +00:00
var lg = Global . MovieSession . LogGeneratorInstance ( ) ;
lg . SetSource ( Global . MovieSession . Movie . GetInputState ( frame ) ) ;
return lg . GenerateLogEntry ( ) ;
2014-05-18 21:26:27 +00:00
}
2017-05-10 11:45:23 +00:00
return "" ;
2014-05-18 21:26:27 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getreadonly", "Returns true if the movie is in read-only mode, false if in read+write")]
2014-01-26 02:50:26 +00:00
public static bool GetReadOnly ( )
2013-10-28 19:13:01 +00:00
{
2013-12-03 18:08:45 +00:00
return Global . MovieSession . ReadOnly ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getrerecordcount", "Gets the rerecord count of the current movie.")]
2014-10-20 20:31:31 +00:00
public static ulong GetRerecordCount ( )
{
return Global . MovieSession . Movie . Rerecords ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getrerecordcounting", "Returns whether or not the current movie is incrementing rerecords on loadstate")]
2014-01-26 02:50:26 +00:00
public static bool GetRerecordCounting ( )
2013-10-28 19:13:01 +00:00
{
return Global . MovieSession . Movie . IsCountingRerecords ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("isloaded", "Returns true if a movie is loaded in memory (play, record, or finished modes), false if not (inactive mode)")]
2014-01-26 02:50:26 +00:00
public static bool IsLoaded ( )
2013-10-28 19:13:01 +00:00
{
2013-10-31 13:07:42 +00:00
return Global . MovieSession . Movie . IsActive ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("length", "Returns the total number of frames of the loaded movie")]
2014-01-26 02:50:26 +00:00
public static double Length ( )
2013-10-28 19:13:01 +00:00
{
2013-11-30 03:10:05 +00:00
return Global . MovieSession . Movie . FrameCount ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("mode", "Returns the mode of the current movie. Possible modes: \"PLAY\", \"RECORD\", \"FINISHED\", \"INACTIVE\"")]
2014-01-26 02:50:26 +00:00
public static string Mode ( )
2013-10-28 19:13:01 +00:00
{
if ( Global . MovieSession . Movie . IsFinished )
{
return "FINISHED" ;
}
2014-02-03 20:48:01 +00:00
if ( Global . MovieSession . Movie . IsPlaying )
2013-10-28 19:13:01 +00:00
{
return "PLAY" ;
}
2014-02-03 20:48:01 +00:00
if ( Global . MovieSession . Movie . IsRecording )
2013-10-28 19:13:01 +00:00
{
return "RECORD" ;
}
2014-02-03 20:48:01 +00:00
return "INACTIVE" ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("save", "Saves the current movie to the disc. If the filename is provided (no extension or path needed), the movie is saved under the specified name to the current movie directory. The filename may contain a subdirectory, it will be created if it doesn't exist. Existing files won't get overwritten.")]
2016-01-30 13:09:58 +00:00
public void Save ( string filename = "" )
{
if ( ! Global . MovieSession . Movie . IsActive )
2017-04-14 19:59:01 +00:00
{
2016-01-30 13:09:58 +00:00
return ;
2017-04-14 19:59:01 +00:00
}
2016-01-30 13:09:58 +00:00
if ( ! string . IsNullOrEmpty ( filename ) )
{
filename + = "." + Global . MovieSession . Movie . PreferredExtension ;
var test = new FileInfo ( filename ) ;
if ( test . Exists )
{
2017-04-14 19:59:01 +00:00
Log ( $"File {filename} already exists, will not overwrite" ) ;
2016-01-30 13:09:58 +00:00
return ;
}
2017-04-14 19:59:01 +00:00
2016-01-30 13:09:58 +00:00
Global . MovieSession . Movie . Filename = filename ;
}
2017-04-14 19:59:01 +00:00
2016-01-30 13:09:58 +00:00
Global . MovieSession . Movie . Save ( ) ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setreadonly", "Sets the read-only state to the given value. true for read only, false for read+write")]
2014-01-26 18:36:27 +00:00
public static void SetReadOnly ( bool readOnly )
2013-10-28 19:13:01 +00:00
{
2014-01-26 18:36:27 +00:00
Global . MovieSession . ReadOnly = readOnly ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setrerecordcount", "Sets the rerecord count of the current movie.")]
2014-10-20 20:31:31 +00:00
public static void SetRerecordCount ( double count )
{
// Lua numbers are always double, integer precision holds up
// to 53 bits, so throw an error if it's bigger than that.
2017-04-14 19:59:01 +00:00
const double PrecisionLimit = 9007199254740992d ;
2014-10-20 20:31:31 +00:00
2017-04-14 19:59:01 +00:00
if ( count > PrecisionLimit )
{
2014-10-20 20:31:31 +00:00
throw new Exception ( "Rerecord count exceeds Lua integer precision." ) ;
2017-04-14 19:59:01 +00:00
}
2014-10-20 20:31:31 +00:00
Global . MovieSession . Movie . Rerecords = ( ulong ) count ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("setrerecordcounting", "Sets whether or not the current movie will increment the rerecord counter on loadstate")]
2014-01-26 18:36:27 +00:00
public static void SetRerecordCounting ( bool counting )
2013-10-28 19:13:01 +00:00
{
2014-01-26 18:36:27 +00:00
Global . MovieSession . Movie . IsCountingRerecords = counting ;
2013-10-28 19:13:01 +00:00
}
2017-07-10 19:02:00 +00:00
[LuaMethod("stop", "Stops the current movie")]
2014-01-26 02:50:26 +00:00
public static void Stop ( )
2013-10-28 19:13:01 +00:00
{
Global . MovieSession . Movie . Stop ( ) ;
}
2014-04-22 21:27:08 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("getfps", "If a movie is loaded, gets the frames per second used by the movie to determine the movie length time")]
2014-04-22 21:27:08 +00:00
public static double GetFps ( )
{
if ( Global . MovieSession . Movie . IsActive )
{
2014-06-29 00:48:36 +00:00
var movie = Global . MovieSession . Movie ;
var system = movie . HeaderEntries [ HeaderKeys . PLATFORM ] ;
var pal = movie . HeaderEntries . ContainsKey ( HeaderKeys . PAL ) & &
movie . HeaderEntries [ HeaderKeys . PAL ] = = "1" ;
return new PlatformFrameRates ( ) [ system , pal ] ;
2014-04-22 21:27:08 +00:00
}
return 0.0 ;
}
2015-08-04 22:22:22 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")]
2015-08-04 22:22:22 +00:00
public LuaTable GetHeader ( )
{
var luaTable = Lua . NewTable ( ) ;
if ( Global . MovieSession . Movie . IsActive )
{
foreach ( var kvp in Global . MovieSession . Movie . HeaderEntries )
{
luaTable [ kvp . Key ] = kvp . Value ;
}
}
return luaTable ;
}
2015-08-04 22:33:22 +00:00
2017-07-10 19:02:00 +00:00
[LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")]
2015-08-04 22:33:22 +00:00
public LuaTable GetComments ( )
{
var luaTable = Lua . NewTable ( ) ;
if ( Global . MovieSession . Movie . IsActive )
{
for ( int i = 0 ; i < Global . MovieSession . Movie . Comments . Count ; i + + )
{
luaTable [ i ] = Global . MovieSession . Movie . Comments [ i ] ;
}
}
return luaTable ;
}
2017-07-10 19:02:00 +00:00
[LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")]
2015-08-04 22:33:22 +00:00
public LuaTable GetSubtitles ( )
{
var luaTable = Lua . NewTable ( ) ;
if ( Global . MovieSession . Movie . IsActive )
{
for ( int i = 0 ; i < Global . MovieSession . Movie . Subtitles . Count ; i + + )
{
luaTable [ i ] = Global . MovieSession . Movie . Subtitles [ i ] . ToString ( ) ;
}
}
return luaTable ;
}
2013-10-28 19:13:01 +00:00
}
}