Skip to content

Commit df4df51

Browse files
committed
Add IMovieApi.PlayFromStart/movie.play_from_start (resolves #384)
1 parent 7fdc3f9 commit df4df51

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

src/BizHawk.Client.Common/Api/Classes/MovieApi.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ public IReadOnlyList<string> GetSubtitles()
100100

101101
public string Mode() => (_movieSession.Movie?.Mode ?? MovieMode.Inactive).ToString().ToUpper();
102102

103+
public bool PlayFromStart(string path = "")
104+
{
105+
if (string.IsNullOrEmpty(path))
106+
{
107+
try
108+
{
109+
return _mainForm.RestartMovie();
110+
}
111+
catch (Exception e)
112+
{
113+
LogCallback($"caught {e.GetType().Name} while trying to restart movie: {e.Message}");
114+
return false;
115+
}
116+
}
117+
try
118+
{
119+
return _mainForm.LoadMovie(filename: path);
120+
}
121+
catch (Exception e)
122+
{
123+
LogCallback($"caught {e.GetType().Name} while trying to load movie: {e.Message}");
124+
return false;
125+
}
126+
}
127+
103128
public void SetReadOnly(bool readOnly) => _movieSession.ReadOnly = readOnly;
104129

105130
public void SetRerecordCount(ulong count) => _movieSession.Movie.Rerecords = count;

src/BizHawk.Client.Common/Api/Interfaces/IMovieApi.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public interface IMovieApi : IExternalApi
1515
bool IsLoaded();
1616
int Length();
1717
string Mode();
18+
19+
/// <summary>
20+
/// Resets the core to frame 0 with the currently loaded movie in playback mode.
21+
/// If <paramref name="path"/> is specified, attempts to load it, then continues with playback if it was successful.
22+
/// </summary>
23+
/// <returns>true iff successful</returns>
24+
bool PlayFromStart(string path = "");
25+
1826
void Save(string filename = "");
1927
void SetReadOnly(bool readOnly);
2028
void SetRerecordCount(ulong count);

src/BizHawk.Client.Common/IMainFormForApi.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public interface IMainFormForApi
6060
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
6161
int GetApproxFramerate();
6262

63+
/// <summary>
64+
/// essentially <c>return MainForm.StartNewMovie(MovieSession.Get(filename), record: false);</c>,
65+
/// but also ensures a rom is loaded, and defers to TAStudio
66+
/// </summary>
67+
/// <param name="archive">unused</param>
68+
/// <remarks>only referenced from <see cref="MovieApi"/></remarks>
69+
bool LoadMovie(string filename, string archive = null);
70+
6371
/// <remarks>only referenced from <see cref="SaveStateApi"/></remarks>
6472
void LoadQuickSave(string quickSlotName, bool suppressOSD = false);
6573

@@ -77,6 +85,9 @@ public interface IMainFormForApi
7785
/// <remarks>only referenced from <c>EmuClientApi</c></remarks>
7886
void Render();
7987

88+
/// <remarks>only referenced from <see cref="MovieApi"/></remarks>
89+
bool RestartMovie();
90+
8091
/// <remarks>only referenced from <see cref="SaveStateApi"/></remarks>
8192
void SaveQuickSave(string quickSlotName, bool fromLua = false, bool suppressOSD = false);
8293

src/BizHawk.Client.Common/lua/CommonLibs/MovieLuaLibrary.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public int Length()
7272
public string Mode()
7373
=> APIs.Movie.Mode();
7474

75+
[LuaMethodExample(@"movie.play_from_start(""C:\\moviename.ext"");")]
76+
[LuaMethod("play_from_start", "Resets the core to frame 0 with the currently loaded movie in playback mode. If a path to a movie is specified, attempts to load it, then continues with playback if it was successful. Returns true iff successful.")]
77+
public bool PlayFromStart([LuaArbitraryStringParam] string path = "")
78+
{
79+
_luaLibsImpl.IsRebootingCore = true;
80+
var success = APIs.Movie.PlayFromStart(path);
81+
_luaLibsImpl.IsRebootingCore = false;
82+
return success;
83+
}
84+
7585
[LuaMethodExample("movie.save( \"C:\\moviename.ext\" );")]
7686
[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.")]
7787
public void Save([LuaArbitraryStringParam] string filename = "")

src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void LoadLuaSession(string filename, string archive = null)
8787
}
8888
}
8989

90-
private bool LoadMovie(string filename, string archive = null)
90+
public bool LoadMovie(string filename, string archive = null)
9191
{
9292
if (Emulator.IsNull())
9393
{

src/BizHawk.Client.EmuHawk/MainForm.Movie.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void StopMovie(bool saveChanges = true)
9999
}
100100
}
101101

102-
private bool RestartMovie()
102+
public bool RestartMovie()
103103
{
104104
if (IsSlave && Master.WantsToControlRestartMovie) return Master.RestartMovie();
105105
if (!MovieSession.Movie.IsActive()) return false;

0 commit comments

Comments
 (0)