Migrate MovieLuaLibrary to ApiHawk delegation

This commit is contained in:
YoshiRulz 2019-11-30 23:32:00 +10:00
parent bd7dfbd487
commit 4a84e1a8a9
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 48 additions and 186 deletions

View File

@ -8,6 +8,15 @@ namespace BizHawk.Client.Common
{
public sealed class MovieApi : IInputMovie
{
public MovieApi(Action<string> logCallback)
{
LogCallback = logCallback;
}
public MovieApi() : this(Console.WriteLine) {}
private readonly Action<string> LogCallback;
private static class MoviePluginStatic
{
public static string Filename()
@ -105,8 +114,6 @@ namespace BizHawk.Client.Common
}
}
public MovieApi()
{ }
public bool StartsFromSavestate()
{
@ -122,7 +129,7 @@ namespace BizHawk.Client.Common
{
if (!Global.MovieSession.Movie.IsActive)
{
Console.WriteLine("No movie loaded");
LogCallback("No movie loaded");
return null;
}
@ -131,7 +138,7 @@ namespace BizHawk.Client.Common
if (adapter == null)
{
Console.WriteLine("Can't get input of the last frame of the movie. Use the previous frame");
LogCallback("Can't get input of the last frame of the movie. Use the previous frame");
return null;
}
@ -173,7 +180,7 @@ namespace BizHawk.Client.Common
var test = new FileInfo(filename);
if (test.Exists)
{
Console.WriteLine($"File {filename} already exists, will not overwrite");
LogCallback($"File {filename} already exists, will not overwrite");
return;
}

View File

@ -1,11 +1,11 @@
using System;
using System.IO;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
public sealed class MovieLuaLibrary : LuaLibraryBase
public sealed class MovieLuaLibrary : DelegatingLuaLibrary
{
public MovieLuaLibrary(Lua lua)
: base(lua) { }
@ -17,253 +17,108 @@ namespace BizHawk.Client.Common
[LuaMethodExample("if ( movie.startsfromsavestate( ) ) then\r\n\tconsole.log( \"Returns whether or not the movie is a savestate-anchored movie\" );\r\nend;")]
[LuaMethod("startsfromsavestate", "Returns whether or not the movie is a savestate-anchored movie")]
public bool StartsFromSavestate()
{
return Global.MovieSession.Movie.IsActive && Global.MovieSession.Movie.StartsFromSavestate;
}
public bool StartsFromSavestate() => APIs.Movie.StartsFromSavestate();
[LuaMethodExample("if ( movie.startsfromsaveram( ) ) then\r\n\tconsole.log( \"Returns whether or not the movie is a saveram-anchored movie\" );\r\nend;")]
[LuaMethod("startsfromsaveram", "Returns whether or not the movie is a saveram-anchored movie")]
public bool StartsFromSaveram()
{
return Global.MovieSession.Movie.IsActive && Global.MovieSession.Movie.StartsFromSaveRam;
}
public bool StartsFromSaveram() => APIs.Movie.StartsFromSaveram();
[LuaMethodExample("local stmovfil = movie.filename( );")]
[LuaMethod("filename", "Returns the file name including path of the currently loaded movie")]
public static string Filename()
{
return Global.MovieSession.Movie.Filename;
}
public string Filename() => APIs.Movie.Filename();
[LuaMethodExample("local nlmovget = movie.getinput( 500 );")]
[LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")]
public LuaTable GetInput(int frame)
{
if (!Global.MovieSession.Movie.IsActive)
{
Log("No movie loaded");
return null;
}
var input = Lua.NewTable();
var adapter = Global.MovieSession.Movie.GetInputState(frame);
if (adapter == null)
{
Log("Can't get input of the last frame of the movie. Use the previous frame");
return null;
}
foreach (var button in adapter.Definition.BoolButtons)
{
input[button] = adapter.IsPressed(button);
}
foreach (var button in adapter.Definition.FloatControls)
{
input[button] = adapter.GetFloat(button);
}
return input;
var result = APIs.Movie.GetInput(frame);
var table = Lua.NewTable();
foreach (var kvp in result) table[kvp.Key] = kvp.Value;
return table;
}
[LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")]
[LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")]
public string GetInputAsMnemonic(int frame)
{
if (Global.MovieSession.Movie.IsActive && frame < Global.MovieSession.Movie.InputLogLength)
{
var lg = Global.MovieSession.LogGeneratorInstance();
lg.SetSource(Global.MovieSession.Movie.GetInputState(frame));
return lg.GenerateLogEntry();
}
return "";
}
public string GetInputAsMnemonic(int frame) => APIs.Movie.GetInputAsMnemonic(frame);
[LuaMethodExample("if ( movie.getreadonly( ) ) then\r\n\tconsole.log( \"Returns true if the movie is in read-only mode, false if in read+write\" );\r\nend;")]
[LuaMethod("getreadonly", "Returns true if the movie is in read-only mode, false if in read+write")]
public static bool GetReadOnly()
{
return Global.MovieSession.ReadOnly;
}
public bool GetReadOnly() => APIs.Movie.GetReadOnly();
[LuaMethodExample("local ulmovget = movie.getrerecordcount();")]
[LuaMethod("getrerecordcount", "Gets the rerecord count of the current movie.")]
public static ulong GetRerecordCount()
{
return Global.MovieSession.Movie.Rerecords;
}
public ulong GetRerecordCount() => APIs.Movie.GetRerecordCount();
[LuaMethodExample("if ( movie.getrerecordcounting( ) ) then\r\n\tconsole.log( \"Returns whether or not the current movie is incrementing rerecords on loadstate\" );\r\nend;")]
[LuaMethod("getrerecordcounting", "Returns whether or not the current movie is incrementing rerecords on loadstate")]
public static bool GetRerecordCounting()
{
return Global.MovieSession.Movie.IsCountingRerecords;
}
public bool GetRerecordCounting() => APIs.Movie.GetRerecordCounting();
[LuaMethodExample("if ( movie.isloaded( ) ) then\r\n\tconsole.log( \"Returns true if a movie is loaded in memory ( play, record, or finished modes ), false if not ( inactive mode )\" );\r\nend;")]
[LuaMethod("isloaded", "Returns true if a movie is loaded in memory (play, record, or finished modes), false if not (inactive mode)")]
public static bool IsLoaded()
{
return Global.MovieSession.Movie.IsActive;
}
public bool IsLoaded() => APIs.Movie.IsLoaded();
[LuaMethodExample("local domovlen = movie.length( );")]
[LuaMethod("length", "Returns the total number of frames of the loaded movie")]
public static double Length()
{
return Global.MovieSession.Movie.FrameCount;
}
public double Length() => APIs.Movie.Length();
[LuaMethodExample("local stmovmod = movie.mode( );")]
[LuaMethod("mode", "Returns the mode of the current movie. Possible modes: \"PLAY\", \"RECORD\", \"FINISHED\", \"INACTIVE\"")]
public static string Mode()
{
if (Global.MovieSession.Movie.IsFinished)
{
return "FINISHED";
}
if (Global.MovieSession.Movie.IsPlaying)
{
return "PLAY";
}
if (Global.MovieSession.Movie.IsRecording)
{
return "RECORD";
}
return "INACTIVE";
}
public string Mode() => APIs.Movie.Mode();
[LuaMethodExample("movie.save( \"C:\\moviename.ext\" );")]
[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.")]
public void Save(string filename = "")
{
if (!Global.MovieSession.Movie.IsActive)
{
return;
}
if (!string.IsNullOrEmpty(filename))
{
filename += $".{Global.MovieSession.Movie.PreferredExtension}";
var test = new FileInfo(filename);
if (test.Exists)
{
Log($"File {filename} already exists, will not overwrite");
return;
}
Global.MovieSession.Movie.Filename = filename;
}
Global.MovieSession.Movie.Save();
}
public void Save(string filename = "") => APIs.Movie.Save();
[LuaMethodExample("movie.setreadonly( false );")]
[LuaMethod("setreadonly", "Sets the read-only state to the given value. true for read only, false for read+write")]
public static void SetReadOnly(bool readOnly)
{
Global.MovieSession.ReadOnly = readOnly;
}
public void SetReadOnly(bool readOnly) => APIs.Movie.SetReadOnly(readOnly);
[LuaMethodExample("movie.setrerecordcount( 20.0 );")]
[LuaMethod("setrerecordcount", "Sets the rerecord count of the current movie.")]
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.
const double precisionLimit = 9007199254740992d;
if (count > precisionLimit)
{
throw new Exception("Rerecord count exceeds Lua integer precision.");
}
Global.MovieSession.Movie.Rerecords = (ulong)count;
}
public void SetRerecordCount(double count) => APIs.Movie.SetRerecordCount(count);
[LuaMethodExample("movie.setrerecordcounting( true );")]
[LuaMethod("setrerecordcounting", "Sets whether or not the current movie will increment the rerecord counter on loadstate")]
public static void SetRerecordCounting(bool counting)
{
Global.MovieSession.Movie.IsCountingRerecords = counting;
}
public void SetRerecordCounting(bool counting) => APIs.Movie.SetRerecordCounting(counting);
[LuaMethodExample("movie.stop( );")]
[LuaMethod("stop", "Stops the current movie")]
public static void Stop()
{
Global.MovieSession.Movie.Stop();
}
public void Stop() => APIs.Movie.Stop();
[LuaMethodExample("local domovget = movie.getfps( );")]
[LuaMethod("getfps", "If a movie is loaded, gets the frames per second used by the movie to determine the movie length time")]
public static double GetFps()
{
if (Global.MovieSession.Movie.IsActive)
{
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];
}
return 0.0;
}
public double GetFps() => APIs.Movie.GetFps();
[LuaMethodExample("local nlmovget = movie.getheader( );")]
[LuaMethod("getheader", "If a movie is active, will return the movie header as a lua table")]
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;
var result = APIs.Movie.GetHeader();
var table = Lua.NewTable();
foreach (var kvp in result) table[kvp.Key] = kvp.Value;
return table;
}
[LuaMethodExample("local nlmovget = movie.getcomments( );")]
[LuaMethod("getcomments", "If a movie is active, will return the movie comments as a lua table")]
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;
var result = APIs.Movie.GetComments();
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
}
[LuaMethodExample("local nlmovget = movie.getsubtitles( );")]
[LuaMethod("getsubtitles", "If a movie is active, will return the movie subtitles as a lua table")]
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;
var result = APIs.Movie.GetSubtitles();
var table = Lua.NewTable();
var count = result.Count;
for (var i = 0; i != count; i++) table[i] = result[i];
return table;
}
}
}