IEmulator: clarify naming and implementations so it's clear that CloneSaveRam() always returns a clone that you can do whatever you want with (but won't modify the underlying)

This commit is contained in:
goyuken 2014-08-13 17:52:13 +00:00
parent e29e4a1227
commit f0c8bd3842
28 changed files with 54 additions and 40 deletions

View File

@ -1354,7 +1354,7 @@ namespace BizHawk.Client.EmuHawk
} }
else else
{ {
var oldram = Global.Emulator.ReadSaveRam(); var oldram = Global.Emulator.CloneSaveRam();
if (oldram == null) if (oldram == null)
{ {
// we're eating this one now. the possible negative consequence is that a user could lose // we're eating this one now. the possible negative consequence is that a user could lose
@ -1401,7 +1401,7 @@ namespace BizHawk.Client.EmuHawk
} }
var writer = new BinaryWriter(new FileStream(path, FileMode.Create, FileAccess.Write)); var writer = new BinaryWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
var saveram = Global.Emulator.ReadSaveRam(); var saveram = Global.Emulator.CloneSaveRam();
writer.Write(saveram, 0, saveram.Length); writer.Write(saveram, 0, saveram.Length);
writer.Close(); writer.Close();

View File

@ -72,7 +72,7 @@ namespace BizHawk.Emulation.Common
public int LagCount { get { return 0; } set { return; } } public int LagCount { get { return 0; } set { return; } }
public bool IsLagFrame { get { return false; } } public bool IsLagFrame { get { return false; } }
public byte[] ReadSaveRam() { return null; } public byte[] CloneSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { } public void ClearSaveRam() { }
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation { get { return true; } }

View File

@ -81,7 +81,7 @@ namespace BizHawk.Emulation.Common
/// <summary> /// <summary>
/// return a copy of the saveram. editing it won't do you any good unless you later call StoreSaveRam() /// return a copy of the saveram. editing it won't do you any good unless you later call StoreSaveRam()
/// </summary> /// </summary>
byte[] ReadSaveRam(); byte[] CloneSaveRam();
/// <summary> /// <summary>
/// store new saveram to the emu core. the data should be the same size as the return from ReadSaveRam() /// store new saveram to the emu core. the data should be the same size as the return from ReadSaveRam()

View File

@ -523,7 +523,7 @@ namespace BizHawk.Emulation.Cores.Calculators
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation { get { return true; } }
public byte[] ReadSaveRam() { return null; } public byte[] CloneSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { } public void ClearSaveRam() { }
public bool SaveRamModified public bool SaveRamModified

View File

@ -21,7 +21,7 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64
SyncState(new Serializer(reader)); SyncState(new Serializer(reader));
} }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return null; return null;
} }

View File

@ -249,7 +249,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
ser.EndSection(); ser.EndSection();
} }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return null; return null;
} }

View File

@ -73,7 +73,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800
private int _frame = 0; private int _frame = 0;
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return (byte[])hsram.Clone(); return (byte[])hsram.Clone();
} }

View File

@ -169,7 +169,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision
} }
} }
public byte[] ReadSaveRam() { return null; } public byte[] CloneSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { } public void ClearSaveRam() { }
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }

View File

@ -161,7 +161,7 @@ namespace BizHawk.Emulation.Cores.Intellivision
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation { get { return true; } }
public byte[] ReadSaveRam() { return null; } public byte[] CloneSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { } public void ClearSaveRam() { }
public bool SaveRamModified public bool SaveRamModified

View File

@ -105,7 +105,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (disposed) if (disposed)
throw new ObjectDisposedException(this.GetType().ToString()); throw new ObjectDisposedException(this.GetType().ToString());

View File

@ -106,7 +106,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
#region SaveRam #region SaveRam
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return new byte[16]; return new byte[16];
} }

View File

@ -453,7 +453,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
int length = LibGambatte.gambatte_savesavedatalength(GambatteState); int length = LibGambatte.gambatte_savesavedatalength(GambatteState);

View File

@ -231,10 +231,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
byte[] lb = L.ReadSaveRam(); byte[] lb = L.CloneSaveRam();
byte[] rb = R.ReadSaveRam(); byte[] rb = R.CloneSaveRam();
byte[] ret = new byte[lb.Length + rb.Length]; byte[] ret = new byte[lb.Length + rb.Length];
Buffer.BlockCopy(lb, 0, ret, 0, lb.Length); Buffer.BlockCopy(lb, 0, ret, 0, lb.Length);
Buffer.BlockCopy(rb, 0, ret, lb.Length, rb.Length); Buffer.BlockCopy(rb, 0, ret, lb.Length, rb.Length);
@ -243,8 +243,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
public void StoreSaveRam(byte[] data) public void StoreSaveRam(byte[] data)
{ {
byte[] lb = new byte[L.ReadSaveRam().Length]; byte[] lb = new byte[L.CloneSaveRam().Length];
byte[] rb = new byte[R.ReadSaveRam().Length]; byte[] rb = new byte[R.CloneSaveRam().Length];
Buffer.BlockCopy(data, 0, lb, 0, lb.Length); Buffer.BlockCopy(data, 0, lb, 0, lb.Length);
Buffer.BlockCopy(data, lb.Length, rb, 0, rb.Length); Buffer.BlockCopy(data, lb.Length, rb, 0, rb.Length);
L.StoreSaveRam(lb); L.StoreSaveRam(lb);

View File

@ -276,7 +276,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64
public bool DeterministicEmulation { get { return false; } } public bool DeterministicEmulation { get { return false; } }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return api.SaveSaveram(); return api.SaveSaveram();
} }

View File

@ -577,7 +577,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64.NativeApi
{ {
if (saveram_backup != null) if (saveram_backup != null)
{ {
return saveram_backup; return (byte[])saveram_backup.Clone();
} }
else else
{ {

View File

@ -323,7 +323,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (board is FDS) if (board is FDS)
return (board as FDS).ReadSaveRam(); return (board as FDS).ReadSaveRam();

View File

@ -190,10 +190,10 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
SaveRamBuff = new byte[size]; SaveRamBuff = new byte[size];
} }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
LibQuickNES.ThrowStringError(LibQuickNES.qn_battery_ram_save(Context, SaveRamBuff, SaveRamBuff.Length)); LibQuickNES.ThrowStringError(LibQuickNES.qn_battery_ram_save(Context, SaveRamBuff, SaveRamBuff.Length));
return SaveRamBuff; return (byte[])SaveRamBuff.Clone();
} }
public void StoreSaveRam(byte[] data) public void StoreSaveRam(byte[] data)

View File

@ -737,7 +737,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
} }
} }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
byte* buf = api.QUERY_get_memory_data(LibsnesApi.SNES_MEMORY.CARTRIDGE_RAM); byte* buf = api.QUERY_get_memory_data(LibsnesApi.SNES_MEMORY.CARTRIDGE_RAM);
var size = api.QUERY_get_memory_size(LibsnesApi.SNES_MEMORY.CARTRIDGE_RAM); var size = api.QUERY_get_memory_size(LibsnesApi.SNES_MEMORY.CARTRIDGE_RAM);

View File

@ -62,7 +62,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES9X
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return new byte[0]; return new byte[0];
} }

View File

@ -357,7 +357,7 @@ namespace BizHawk.Emulation.Cores.PCEngine
public string Region { get; set; } public string Region { get; set; }
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation { get { return true; } }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (BRAM != null) if (BRAM != null)
return (byte[])BRAM.Clone(); return (byte[])BRAM.Clone();

View File

@ -41,7 +41,7 @@ namespace BizHawk.Emulation.Cores.Sega.Genesis
Console.WriteLine("SaveRAM enabled. Start: ${0:X6} End: ${1:X6} Length: ${2:X} Mode: {3}", SaveRamStartOffset, SaveRamEndOffset, SaveRamLength, RH_SRamInterpretation()); Console.WriteLine("SaveRAM enabled. Start: ${0:X6} End: ${1:X6} Length: ${2:X} Mode: {3}", SaveRamStartOffset, SaveRamEndOffset, SaveRamLength, RH_SRamInterpretation());
} }
public byte[] ReadSaveRam() { return (byte[])SaveRAM.Clone(); } public byte[] CloneSaveRam() { return (byte[])SaveRAM.Clone(); }
public void StoreSaveRam(byte[] data) { Array.Copy(data, SaveRAM, data.Length); } public void StoreSaveRam(byte[] data) { Array.Copy(data, SaveRAM, data.Length); }
public void ClearSaveRam() { SaveRAM = new byte[SaveRAM.Length]; } public void ClearSaveRam() { SaveRAM = new byte[SaveRAM.Length]; }

View File

@ -43,7 +43,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
public byte[] BiosRom; public byte[] BiosRom;
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (SaveRAM != null) if (SaveRAM != null)
return (byte[])SaveRAM.Clone(); return (byte[])SaveRAM.Clone();

View File

@ -308,11 +308,18 @@ namespace BizHawk.Emulation.Cores.Sega.Saturn
#region saveram #region saveram
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (Disposed) if (Disposed)
{ {
return DisposedSaveRam ?? new byte[0]; if (DisposedSaveRam != null)
{
return (byte[])DisposedSaveRam.Clone();
}
else
{
return new byte[0];
}
} }
else else
{ {
@ -554,7 +561,7 @@ namespace BizHawk.Emulation.Cores.Sega.Saturn
{ {
ActivateGL(); ActivateGL();
if (SaveRamModified) if (SaveRamModified)
DisposedSaveRam = ReadSaveRam(); DisposedSaveRam = CloneSaveRam();
LibYabause.libyabause_setvidbuff(IntPtr.Zero); LibYabause.libyabause_setvidbuff(IntPtr.Zero);
LibYabause.libyabause_setsndbuff(IntPtr.Zero); LibYabause.libyabause_setsndbuff(IntPtr.Zero);
LibYabause.libyabause_deinit(); LibYabause.libyabause_deinit();

View File

@ -412,11 +412,18 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx
byte[] DisposedSaveRam = null; byte[] DisposedSaveRam = null;
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (disposed) if (disposed)
{ {
return DisposedSaveRam ?? new byte[0]; if (DisposedSaveRam != null)
{
return (byte[])DisposedSaveRam.Clone();
}
else
{
return new byte[0];
}
} }
else else
{ {
@ -652,7 +659,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx
if (AttachedCore != this) if (AttachedCore != this)
throw new Exception(); throw new Exception();
if (SaveRamModified) if (SaveRamModified)
DisposedSaveRam = ReadSaveRam(); DisposedSaveRam = CloneSaveRam();
KillMemCallbacks(); KillMemCallbacks();
AttachedCore = null; AttachedCore = null;
disposed = true; disposed = true;

View File

@ -149,7 +149,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSP
get { return true; } get { return true; }
} }
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
return new byte[0]; return new byte[0];
} }

View File

@ -271,7 +271,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
public int LagCount { get { return 0; } set { return; } } public int LagCount { get { return 0; } set { return; } }
public bool IsLagFrame { get { return false; } } public bool IsLagFrame { get { return false; } }
public byte[] ReadSaveRam() { return null; } public byte[] CloneSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { } public void ClearSaveRam() { }
public bool DeterministicEmulation { get { return true; } } public bool DeterministicEmulation { get { return true; } }

View File

@ -185,11 +185,11 @@ namespace BizHawk.Emulation.Cores.WonderSwan
byte[] saverambuff; byte[] saverambuff;
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
if (!BizSwan.bizswan_saveramsave(Core, saverambuff, saverambuff.Length)) if (!BizSwan.bizswan_saveramsave(Core, saverambuff, saverambuff.Length))
throw new InvalidOperationException("bizswan_saveramsave() returned false!"); throw new InvalidOperationException("bizswan_saveramsave() returned false!");
return saverambuff; return (byte[])saverambuff.Clone();
} }
public void StoreSaveRam(byte[] data) public void StoreSaveRam(byte[] data)

View File

@ -243,7 +243,7 @@ namespace BizHawk.Emulation.Cores
byte[] saverambuff = new byte[0]; byte[] saverambuff = new byte[0];
public byte[] ReadSaveRam() public byte[] CloneSaveRam()
{ {
int size = (int)retro.retro_get_memory_size(LibRetro.RETRO_MEMORY.SAVE_RAM); int size = (int)retro.retro_get_memory_size(LibRetro.RETRO_MEMORY.SAVE_RAM);
if (saverambuff.Length != size) if (saverambuff.Length != size)
@ -254,7 +254,7 @@ namespace BizHawk.Emulation.Cores
throw new Exception("retro_get_memory_data(RETRO_MEMORY_SAVE_RAM) returned NULL"); throw new Exception("retro_get_memory_data(RETRO_MEMORY_SAVE_RAM) returned NULL");
Marshal.Copy(src, saverambuff, 0, size); Marshal.Copy(src, saverambuff, 0, size);
return saverambuff; return (byte[])saverambuff.Clone();
} }
public void StoreSaveRam(byte[] data) public void StoreSaveRam(byte[] data)