Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Classes/MovieApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Save(string filename)
}
_movieSession.Movie.Filename = filename;
}
_movieSession.Movie.Save();
_movieSession.Movie.Save(_mainForm.Emulator);
}

public IReadOnlyDictionary<string, string> GetHeader()
Expand Down
6 changes: 3 additions & 3 deletions src/BizHawk.Client.Common/movie/BasicMovieInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public virtual string FirmwareHash

public IDictionary<string, string> HeaderEntries => Header;

public bool Load()
public bool Load(IEmulator emulator)
{
if (!File.Exists(Filename))
{
Expand All @@ -154,7 +154,7 @@ public bool Load()
using var bl = ZipStateLoader.LoadAndDetect(Filename, true);
if (bl is null) return false;
ClearBeforeLoad();
LoadFields(bl);
LoadFields(bl, emulator);
if (FrameCount == 0)
{
// only iterate the input log if it hasn't been loaded already
Expand All @@ -176,7 +176,7 @@ protected virtual void ClearBeforeLoad()
Comments.Clear();
}

protected virtual void LoadFields(ZipStateLoader bl)
protected virtual void LoadFields(ZipStateLoader bl, IEmulator emulator)
{
bl.GetLump(BinaryStateLump.Movieheader, abort: true, tr =>
{
Expand Down
9 changes: 5 additions & 4 deletions src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;

using BizHawk.Common.PathExtensions;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
{
Expand Down Expand Up @@ -71,7 +72,7 @@ public static IMovie ToBk2(this IMovie old)
return bk2;
}

public static ITasMovie ConvertToSavestateAnchoredMovie(this ITasMovie old, int frame, byte[] savestate)
public static ITasMovie ConvertToSavestateAnchoredMovie(this ITasMovie old, int frame, byte[] savestate, IEmulator emulator)
{
string newFilename = ConvertFileNameToTasMovie(old.Filename);

Expand Down Expand Up @@ -115,11 +116,11 @@ public static ITasMovie ConvertToSavestateAnchoredMovie(this ITasMovie old, int
}
}

tas.Save();
tas.Save(emulator);
return tas;
}

public static ITasMovie ConvertToSaveRamAnchoredMovie(this ITasMovie old, byte[] saveRam)
public static ITasMovie ConvertToSaveRamAnchoredMovie(this ITasMovie old, byte[] saveRam, IEmulator emulator)
{
string newFilename = ConvertFileNameToTasMovie(old.Filename);

Expand All @@ -146,7 +147,7 @@ public static ITasMovie ConvertToSaveRamAnchoredMovie(this ITasMovie old, byte[]
tas.Subtitles.Add(sub);
}

tas.Save();
tas.Save(emulator);
return tas;
}

Expand Down
32 changes: 18 additions & 14 deletions src/BizHawk.Client.Common/movie/MovieSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public MovieSession(

public string BackupDirectory { get; set; }

private IEmulator/*?*/ Emulator = null;

public IMovie Movie { get; private set; }
public bool ReadOnly { get; set; } = true;
public bool NewMovieQueued => _queuedMovie != null;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void HandleFrameBefore()
}
else if (Movie.IsFinished())
{
if (Movie.Emulator.Frame < Movie.FrameCount) // This scenario can happen from rewinding (suddenly we are back in the movie, so hook back up to the movie
if (Emulator.Frame < Movie.FrameCount) // This scenario can happen from rewinding (suddenly we are back in the movie, so hook back up to the movie
{
Movie.SwitchToPlay();
LatchInputToLog();
Expand All @@ -98,18 +100,19 @@ public void HandleFrameBefore()
else if (Movie.IsRecording())
{
LatchInputToUser();
Movie.RecordFrame(Movie.Emulator.Frame, MovieOut.Source);
var frame = Emulator.Frame;
Movie.RecordFrame(frame, frame, MovieOut.Source);
}
}

public void HandleFrameAfter(bool ignoreMovieEndAction)
{
if (Movie is ITasMovie tasMovie)
{
tasMovie.GreenzoneCurrentFrame();
tasMovie.GreenzoneCurrentFrame(Emulator);
}

if (!ignoreMovieEndAction && Movie.IsPlaying() && Movie.Emulator.Frame == Movie.FrameCount)
if (!ignoreMovieEndAction && Movie.IsPlaying() && Emulator.Frame == Movie.FrameCount)
{
HandlePlaybackEnd();
}
Expand Down Expand Up @@ -154,7 +157,7 @@ public bool HandleLoadState(TextReader reader)
{
Movie.SwitchToRecord();

var result = Movie.ExtractInputLog(reader, out var errorMsg);
var result = Movie.ExtractInputLog(reader, Emulator, out var errorMsg);
if (!result)
{
Output(errorMsg);
Expand Down Expand Up @@ -222,15 +225,16 @@ public void RunQueuedMovie(bool recordMode, IEmulator emulator)
MovieController = new Bk2Controller(emulator.ControllerDefinition, _queuedMovie.LogKey);

Movie = _queuedMovie;
Emulator = emulator;
Movie.Attach(emulator);
_queuedMovie = null;

Movie.ProcessSavestate(Movie.Emulator);
Movie.ProcessSram(Movie.Emulator);
Movie.ProcessSavestate(emulator);
Movie.ProcessSram(emulator);

if (recordMode)
{
Movie.StartNewRecording();
Movie.StartNewRecording(Emulator);
ReadOnly = false;
// If we are starting a movie recording while another one is playing, we need to switch back to user input
LatchInputToUser();
Expand Down Expand Up @@ -262,7 +266,7 @@ public void StopMovie(bool saveChanges = true)

if (saveChanges && Movie.Changes)
{
Movie.Save();
Movie.Save(Emulator);
Output($"{Path.GetFileName(Movie.Filename)} written to disk.");
}
Movie.Stop();
Expand All @@ -289,7 +293,7 @@ public IMovie Get(string path, bool loadMovie)
: new Bk2Movie(this, path);

if (loadMovie)
movie.Load();
movie.Load(Emulator);

return movie;
}
Expand All @@ -307,7 +311,7 @@ private void LatchInputToUser()
// Latch input from the input log, if available
private void LatchInputToLog()
{
var input = Movie.GetInputState(Movie.Emulator.Frame);
var input = Movie.GetInputState(Emulator.Frame);

MovieController.SetFrom(input ?? StickySource);
MovieOut.Source = MovieController;
Expand All @@ -317,10 +321,10 @@ private void HandlePlaybackEnd()
{
#if false // invariants given by single call-site
Debug.Assert(Movie.IsPlaying());
Debug.Assert(Movie.Emulator.Frame >= Movie.InputLogLength);
Debug.Assert(Emulator.Frame >= Movie.InputLogLength);
#endif
#if false // code below doesn't actually do anything as the cycle count is indiscriminately overwritten (or removed) on save anyway.
if (Movie.IsAtEnd() && Movie.Emulator.HasCycleTiming())
if (Movie.IsAtEnd() && Emulator.HasCycleTiming())
{
const string WINDOW_TITLE_MISMATCH = "Cycle count mismatch";
const string WINDOW_TITLE_MISSING = "Cycle count not yet saved";
Expand All @@ -330,7 +334,7 @@ private void HandlePlaybackEnd()
const string PFX_MISMATCH = "The cycle count (running time) saved into this movie ({0}) doesn't match the measured count ({1}) here at the end.\n";
const string ERR_FMT_STR_MISMATCH_READONLY = PFX_MISMATCH + "The movie was loaded in read-only mode. To correct the cycle count, load it in read-write mode and play to the end again.";
const string ERR_FMT_STR_MISMATCH_CONFIRM = PFX_MISMATCH + "Correct it now?";
var coreValue = Movie.Emulator.AsCycleTiming().CycleCount;
var coreValue = Emulator.AsCycleTiming().CycleCount;
if (!Movie.HeaderEntries.TryGetValue(HeaderKeys.CycleCount, out var movieValueStr)
|| !long.TryParse(movieValueStr, out var movieValue))
{
Expand Down
28 changes: 13 additions & 15 deletions src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ namespace BizHawk.Client.Common
{
public partial class Bk2Movie
{
public void Save()
{
Write(Filename);
}
public void Save(IEmulator emulator)
=> Write(Filename, emulator);

public void SaveBackup()
public void SaveBackup(IEmulator emulator)
{
if (string.IsNullOrWhiteSpace(Filename))
{
Expand All @@ -27,12 +25,12 @@ public void SaveBackup()
var backupName = Filename.InsertBeforeLast('.', insert: $".{DateTime.Now:yyyy-MM-dd HH.mm.ss}", out _);
backupName = Path.Combine(Session.BackupDirectory, Path.GetFileName(backupName));

Write(backupName, isBackup: true);
Write(backupName, emulator, isBackup: true);
}

protected virtual void Write(string fn, bool isBackup = false)
private void Write(string fn, IEmulator emulator, bool isBackup = false)
{
SetCycleValues();
SetCycleValues(emulator);
// EmulatorVersion used to store the unchanging original emulator version.
if (!Header.ContainsKey(HeaderKeys.OriginalEmulatorVersion))
{
Expand All @@ -50,10 +48,10 @@ protected virtual void Write(string fn, bool isBackup = false)
}
}

public void SetCycleValues() //TODO IEmulator should not be an instance prop of movies, it should be passed in to every call (i.e. from MovieService) --yoshi
public void SetCycleValues(IEmulator emulator)
{
// The saved cycle value will only be valid if the end of the movie has been emulated.
if (this.IsAtEnd() && Emulator.AsCycleTiming() is { } cycleCore)
if (this.IsAtEnd(emulator) && emulator.AsCycleTiming() is { } cycleCore)
{
// legacy movies may incorrectly have no ClockRate header value set
Header[HeaderKeys.ClockRate] = cycleCore.ClockRate.ToString(NumberFormatInfo.InvariantInfo);
Expand Down Expand Up @@ -117,18 +115,18 @@ private void ClearBk2Fields()
BinarySavestate = null;
}

protected override void LoadFields(ZipStateLoader bl)
protected override void LoadFields(ZipStateLoader bl, IEmulator emulator)
{
base.LoadFields(bl);
LoadBk2Fields(bl);
base.LoadFields(bl, emulator);
LoadBk2Fields(bl, emulator);
}

private void LoadBk2Fields(ZipStateLoader bl)
private void LoadBk2Fields(ZipStateLoader bl, IEmulator emulator)
{
bl.GetLump(BinaryStateLump.Input, abort: true, tr =>
{
IsCountingRerecords = false;
ExtractInputLog(tr, out _);
ExtractInputLog(tr, emulator, out _);
IsCountingRerecords = true;
});

Expand Down
5 changes: 3 additions & 2 deletions src/BizHawk.Client.Common/movie/bk2/Bk2Movie.InputLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
{
Expand Down Expand Up @@ -31,15 +32,15 @@ public string GetInputLogEntry(int frame)
: "";
}

public virtual bool ExtractInputLog(TextReader reader, out string errorMessage)
public virtual bool ExtractInputLog(TextReader reader, IEmulator emulator, out string errorMessage)
{
errorMessage = "";
int? stateFrame = null;

// We are in record mode so replace the movie log with the one from the savestate
if (Session.Settings.EnableBackupMovies && MakeBackup && Log.Count != 0)
{
SaveBackup();
SaveBackup(emulator);
MakeBackup = false;
}

Expand Down
8 changes: 5 additions & 3 deletions src/BizHawk.Client.Common/movie/bk2/Bk2Movie.ModeApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
namespace BizHawk.Client.Common
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
{
public partial class Bk2Movie
{
public MovieMode Mode { get; protected set; } = MovieMode.Inactive;

public virtual void StartNewRecording()
public virtual void StartNewRecording(IEmulator emulator)
{
Mode = MovieMode.Record;
if (MakeBackup && Session.Settings.EnableBackupMovies && Log.Count is not 0)
{
SaveBackup();
SaveBackup(emulator);
MakeBackup = false;
}

Expand Down
18 changes: 12 additions & 6 deletions src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand All @@ -13,14 +14,19 @@ public Bk2Movie(IMovieSession session, string filename) : base(filename)
Header[HeaderKeys.MovieVersion] = "BizHawk v2.0.0";
}

#pragma warning disable CS0618 // this is the sanctioned call-site
public virtual void Attach(IEmulator emulator)
{
Emulator = emulator;
}

protected bool IsAttached() => Emulator != null;
public void CheckAttachedMatches(IEmulator/*?*/ passed)
=> Debug.Assert(object.ReferenceEquals(passed, Emulator), $"Core instance doesn't match the object cached on the movie! (Missed call to {nameof(Attach)}?)");
#pragma warning restore CS0618

[Obsolete("do not use")]
protected IEmulator Emulator { get; private set; }

public IEmulator Emulator { get; private set; }
public IMovieSession Session { get; }

protected bool MakeBackup { get; set; } = true;
Expand Down Expand Up @@ -52,17 +58,17 @@ public void AppendFrame(IController source)
Changes = true;
}

public virtual void RecordFrame(int frame, IController source)
public virtual void RecordFrame(int targetFrame, int currentFrame, IController source)
{
if (Session.Settings.VBAStyleMovieLoadState)
{
if (Emulator.Frame < Log.Count)
if (currentFrame < Log.Count)
{
Truncate(Emulator.Frame);
Truncate(currentFrame);
}
}

SetFrameAt(frame, Bk2LogEntryGenerator.GenerateLogEntry(source));
SetFrameAt(targetFrame, Bk2LogEntryGenerator.GenerateLogEntry(source));

Changes = true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/BizHawk.Client.Common/movie/import/IMovieImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
{
public interface IMovieImport
{
ImportResult Import(
IDialogParent dialogParent,
IEmulator emulator,
IMovieSession session,
string path,
Config config);
Expand All @@ -26,6 +28,7 @@ internal abstract class MovieImporter : IMovieImport

public ImportResult Import(
IDialogParent dialogParent,
IEmulator emulator,
IMovieSession session,
string path,
Config config)
Expand Down Expand Up @@ -67,7 +70,7 @@ public ImportResult Import(
Result.Movie.Hash = hash;
}

Result.Movie.Save();
Result.Movie.Save(emulator);
}

return Result;
Expand Down
Loading