Skip to content

Commit 86335d5

Browse files
committed
Update the ISaveRam interface for clarity. We no longer constrain the sram data length, because it already wasn't true for all cores. Cores that require a specific size should throw if they get the wrong size.
1 parent 87b268a commit 86335d5

File tree

80 files changed

+335
-190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+335
-190
lines changed

src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public static ITasMovie ConvertToSaveRamAnchoredMovie(this ITasMovie old, byte[]
133133

134134
foreach (var (k, v) in old.HeaderEntries) tas.HeaderEntries[k] = v;
135135

136-
tas.StartsFromSaveRam = true;
137136
tas.SyncSettingsJson = old.SyncSettingsJson;
138137

139138
foreach (string comment in old.Comments)

src/BizHawk.Client.Common/movie/bk2/Bk2Movie.HeaderApi.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,7 @@ public virtual bool StartsFromSavestate
5151
}
5252
}
5353

54-
public bool StartsFromSaveRam
55-
{
56-
// ReSharper disable SimplifyConditionalTernaryExpression
57-
get => Header.TryGetValue(HeaderKeys.StartsFromSaveram, out var s) ? bool.Parse(s) : false;
58-
// ReSharper restore SimplifyConditionalTernaryExpression
59-
set
60-
{
61-
if (value)
62-
{
63-
if (!Header.ContainsKey(HeaderKeys.StartsFromSaveram))
64-
{
65-
Header.Add(HeaderKeys.StartsFromSaveram, "True");
66-
}
67-
}
68-
else
69-
{
70-
Header.Remove(HeaderKeys.StartsFromSaveram);
71-
}
72-
}
73-
}
54+
public bool StartsFromSaveRam => SaveRam != null;
7455

7556
public override string GameName
7657
{

src/BizHawk.Client.Common/movie/interfaces/IMovie.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public interface IMovie : IBasicMovieInfo
6767
byte[] SaveRam { get; set; }
6868

6969
bool StartsFromSavestate { get; set; }
70-
bool StartsFromSaveRam { get; set; }
70+
bool StartsFromSaveRam { get; }
7171

7272
string LogKey { get; set; }
7373

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727

2828
using BizHawk.Emulation.Common;
2929
using BizHawk.Emulation.Cores;
30-
using BizHawk.Emulation.Cores.Computers.AppleII;
31-
using BizHawk.Emulation.Cores.Computers.Commodore64;
3230
using BizHawk.Emulation.Cores.Computers.DOS;
3331
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
34-
using BizHawk.Emulation.Cores.Consoles.SNK;
35-
using BizHawk.Emulation.Cores.Nintendo.GBA;
3632
using BizHawk.Emulation.Cores.Nintendo.NES;
3733
using BizHawk.Emulation.Cores.Nintendo.SNES;
3834

@@ -1921,36 +1917,24 @@ private void LoadSaveRam()
19211917
return;
19221918
}
19231919

1920+
byte[] sram = null;
19241921
try
19251922
{
1926-
byte[] sram;
1927-
1928-
// some cores might not know how big the saveram ought to be, so just send it the whole file
1929-
if (Emulator is AppleII or C64 or DOSBox or MGBAHawk or NeoGeoPort or NES { BoardName: "FDS" })
1930-
{
1931-
sram = File.ReadAllBytes(saveramToLoad.FullName);
1932-
}
1933-
else
1934-
{
1935-
var oldRam = Emulator.AsSaveRam().CloneSaveRam();
1936-
if (oldRam is null)
1937-
{
1938-
// we have a SaveRAM file, but the current core does not have save ram.
1939-
// just skip loading the saveram file in that case
1940-
return;
1941-
}
1942-
1943-
// why do we silently truncate\pad here instead of warning\erroring?
1944-
sram = new byte[oldRam.Length];
1945-
using var fs = saveramToLoad.OpenRead();
1946-
_ = fs.Read(sram, 0, sram.Length);
1947-
}
1923+
sram = File.ReadAllBytes(saveramToLoad.FullName);
1924+
}
1925+
catch (Exception e)
1926+
{
1927+
AddOnScreenMessage("An IO error occurred while loading Sram");
1928+
Console.Error.WriteLine(e);
1929+
}
19481930

1949-
Emulator.AsSaveRam().StoreSaveRam(sram);
1931+
try
1932+
{
1933+
if (sram != null) Emulator.AsSaveRam().StoreSaveRam(sram);
19501934
}
1951-
catch (IOException e)
1935+
catch (Exception e)
19521936
{
1953-
AddOnScreenMessage("An error occurred while loading Sram");
1937+
AddOnScreenMessage("The core threw an error while loading Sram");
19541938
Console.Error.WriteLine(e);
19551939
}
19561940
}
@@ -1976,9 +1960,7 @@ public bool FlushSaveRAM(bool autosave = false)
19761960
var backupPath = $"{path}.bak";
19771961
var backupFile = new FileInfo(backupPath);
19781962

1979-
var saveram = Emulator.AsSaveRam().CloneSaveRam();
1980-
if (saveram == null)
1981-
return true;
1963+
var saveram = Emulator.AsSaveRam().CloneSaveRam()!;
19821964

19831965
try
19841966
{

src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public RecordMovie(
103103
MaxDropDownItems = 32,
104104
Size = new(152, 21),
105105
};
106-
if (_emulator.HasSaveRam() && _emulator.AsSaveRam().CloneSaveRam(clearDirty: false) is not null) StartFromCombo.Items.Add(START_FROM_SAVERAM);
106+
if (_emulator.HasSaveRam()) StartFromCombo.Items.Add(START_FROM_SAVERAM);
107107
if (_emulator.HasSavestates()) StartFromCombo.Items.Add(START_FROM_SAVESTATE);
108108

109109
DefaultAuthorCheckBox = new()
@@ -242,7 +242,6 @@ private void Ok_Click(object sender, EventArgs e)
242242
else if (selectedStartFromValue is START_FROM_SAVERAM && _emulator.HasSaveRam())
243243
{
244244
var core = _emulator.AsSaveRam();
245-
movieToRecord.StartsFromSaveRam = true;
246245
movieToRecord.SaveRam = core.CloneSaveRam(clearDirty: false);
247246
}
248247

src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void StartANewProjectFromSaveRamMenuItem_Click(object sender, EventArgs
5555
{
5656
if (AskSaveChanges())
5757
{
58-
var saveRam = SaveRamEmulator?.CloneSaveRam(clearDirty: false) ?? throw new Exception("No SaveRam");
58+
var saveRam = SaveRamEmulator?.CloneSaveRam(clearDirty: false) ?? throw new Exception("No SaveRam; this button should have been disabled.");
5959
GoToFrame(TasView.AnyRowsSelected ? TasView.FirstSelectedRowIndex : 0);
6060
var newProject = CurrentTasMovie.ConvertToSaveRamAnchoredMovie(saveRam);
6161
MainForm.PauseEmulator();

src/BizHawk.Emulation.Common/Base Implementations/LinkedSaveRam.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private bool LinkedSaveRamModified()
2323
{
2424
for (int i = 0; i < _numCores; i++)
2525
{
26-
if (_linkedCores[i].AsSaveRam().SaveRamModified)
26+
if (_linkedCores[i].AsSaveRam()?.SaveRamModified == true)
2727
{
2828
return true;
2929
}
@@ -37,7 +37,7 @@ public byte[] CloneSaveRam(bool clearDirty)
3737
int len = 0;
3838
for (int i = 0; i < _numCores; i++)
3939
{
40-
linkedBuffers.Add(_linkedCores[i].AsSaveRam().CloneSaveRam(clearDirty) ?? Array.Empty<byte>());
40+
linkedBuffers.Add(_linkedCores[i].AsSaveRam()?.CloneSaveRam(clearDirty) ?? Array.Empty<byte>());
4141
len += linkedBuffers[i].Length;
4242
}
4343
byte[] ret = new byte[len];
@@ -55,13 +55,15 @@ public void StoreSaveRam(byte[] data)
5555
int pos = 0;
5656
for (int i = 0; i < _numCores; i++)
5757
{
58-
var toCopy = _linkedCores[i].AsSaveRam().CloneSaveRam(); // wait CloneSaveRam is already a copy, why are we copying it again
59-
if (toCopy is null) continue;
60-
var b = new byte[toCopy.Length];
58+
var numberBytesToCopy = _linkedCores[i].AsSaveRam()?.CloneSaveRam().Length;
59+
if (numberBytesToCopy is null) continue;
60+
var b = new byte[numberBytesToCopy.Value];
6161
Buffer.BlockCopy(data, pos, b, 0, b.Length);
6262
pos += b.Length;
6363
_linkedCores[i].AsSaveRam().StoreSaveRam(b);
6464
}
65+
66+
if (data.Length != pos) throw new InvalidOperationException("Incorrect sram size.");
6567
}
6668
}
6769
}

src/BizHawk.Emulation.Common/Interfaces/Services/ISaveRam.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ public interface ISaveRam : IEmulatorService
1010
{
1111
/// <summary>
1212
/// Returns a copy of the SaveRAM. Editing it won't do you any good unless you later call StoreSaveRam()
13-
/// This IS allowed to return null.
14-
/// Unfortunately, the core may think differently of a nonexisting (null) saveram vs a 0 size saveram.
15-
/// Frontend users of the ISaveRam should treat null as nonexisting (and thus not even write the file, so that the "does not exist" condition can be roundtripped and not confused with an empty file)
1613
/// </summary>
1714
/// <param name="clearDirty">Whether the saveram should be considered in a clean state after this call for purposes of <see cref="SaveRamModified"/></param>
18-
byte[]? CloneSaveRam(bool clearDirty = true);
15+
byte[] CloneSaveRam(bool clearDirty = true);
1916

2017
/// <summary>
21-
/// store new SaveRAM to the emu core. the data should be the same size as the return from ReadSaveRam()
18+
/// Store new SaveRAM to the emu core.
2219
/// </summary>
20+
/// <exception cref="Exception">The core may throw an exception if the given data is invalid.</exception>
2321
void StoreSaveRam(byte[] data);
2422

2523
/// <summary>

src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.IEmulator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public partial class MAME : IEmulator
88
public string SystemId => VSystemID.Raw.Arcade;
99
public bool DeterministicEmulation { get; }
1010
public int Frame { get; private set; }
11-
public IEmulatorServiceProvider ServiceProvider { get; }
11+
12+
private BasicServiceProvider _serviceProvider;
13+
public IEmulatorServiceProvider ServiceProvider => _serviceProvider;
14+
1215
public ControllerDefinition ControllerDefinition => MAMEController;
1316

1417
/// <summary>

src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.ISaveRam.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public byte[] CloneSaveRam(bool clearDirty)
2222
{
2323
if (_nvramFilenames.Count == 0)
2424
{
25-
return null;
25+
throw new InvalidOperationException("Core currently has no SRAM and should not be providing ISaveRam service.");
2626
}
2727

2828
for (int i = 0; i < _nvramFilenames.Count; i++)
@@ -53,7 +53,7 @@ public void StoreSaveRam(byte[] data)
5353
{
5454
if (_nvramFilenames.Count == 0)
5555
{
56-
return;
56+
throw new InvalidOperationException("Core currently has no SRAM and should not be providing ISaveRam service.");
5757
}
5858

5959
using var ms = new MemoryStream(data, false);

0 commit comments

Comments
 (0)