misc cleanups

This commit is contained in:
adelikat 2019-11-15 17:00:43 -06:00
parent e974006ea6
commit 5a1341dd47
5 changed files with 54 additions and 65 deletions

View File

@ -27,11 +27,6 @@ namespace BizHawk.Client.Common
return Path.Combine(PathManager.GetExeDirectoryAbsolute(), "dll"); return Path.Combine(PathManager.GetExeDirectoryAbsolute(), "dll");
} }
public string GetSaveRAMPath()
{
return PathManager.SaveRamPath(Global.Game);
}
public string GetRetroSaveRAMDirectory() public string GetRetroSaveRAMDirectory()
{ {
return PathManager.RetroSaveRAMDirectory(Global.Game); return PathManager.RetroSaveRAMDirectory(Global.Game);
@ -53,14 +48,14 @@ namespace BizHawk.Client.Common
{ {
if (required) if (required)
{ {
var fullmsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? $": {msg}" : ".")}"; var fullMsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? $": {msg}" : ".")}";
throw new MissingFirmwareException(fullmsg); throw new MissingFirmwareException(fullMsg);
} }
if (msg != null) if (msg != null)
{ {
var fullmsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}"; var fullMsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}";
_showWarning(fullmsg); _showWarning(fullMsg);
} }
} }
@ -112,8 +107,7 @@ namespace BizHawk.Client.Common
public byte[] GetFirmwareWithGameInfo(string sysId, string firmwareId, bool required, out GameInfo gi, string msg = null) public byte[] GetFirmwareWithGameInfo(string sysId, string firmwareId, bool required, out GameInfo gi, string msg = null)
{ {
string path; byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out var path);
byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out path);
if (ret != null && path != null) if (ret != null && path != null)
{ {
gi = Database.GetGameInfo(ret, path); gi = Database.GetGameInfo(ret, path);

View File

@ -64,7 +64,7 @@ namespace BizHawk.Client.Common
return; return;
} }
byte[] savestateCopy = null; byte[] savestateCopy;
while (_stateBufferPool.TryPop(out savestateCopy) && savestateCopy.Length != coreSavestate.Length) while (_stateBufferPool.TryPop(out savestateCopy) && savestateCopy.Length != coreSavestate.Length)
{ {
savestateCopy = null; savestateCopy = null;

View File

@ -348,47 +348,43 @@ namespace BizHawk.Client.Common
private void LoadPreviousState() private void LoadPreviousState()
{ {
using (var reader = new BinaryReader(GetPreviousStateMemoryStream())) using var reader = new BinaryReader(GetPreviousStateMemoryStream());
byte[] buf = ((MemoryStream)reader.BaseStream).GetBuffer();
bool fullState = reader.ReadByte() == 1;
if (_rewindDeltaEnable)
{ {
byte[] buf = ((MemoryStream)reader.BaseStream).GetBuffer(); if (fullState)
bool fullState = reader.ReadByte() == 1;
if (_rewindDeltaEnable)
{ {
if (fullState) UpdateLastState(buf, 1, buf.Length - 1);
{
UpdateLastState(buf, 1, buf.Length - 1);
}
else
{
int index = 1;
int offset = 0;
while (index < buf.Length)
{
int offsetDelta = (int)VLInteger.ReadUnsigned(buf, ref index);
int length = (int)VLInteger.ReadUnsigned(buf, ref index);
offset += offsetDelta;
Buffer.BlockCopy(buf, index, _lastState, offset, length);
index += length;
}
}
using (var lastStateReader = new BinaryReader(new MemoryStream(_lastState)))
{
Global.Emulator.AsStatable().LoadStateBinary(lastStateReader);
}
} }
else else
{ {
if (!fullState) int index = 1;
{ int offset = 0;
throw new InvalidOperationException();
}
Global.Emulator.AsStatable().LoadStateBinary(reader); while (index < buf.Length)
{
int offsetDelta = (int)VLInteger.ReadUnsigned(buf, ref index);
int length = (int)VLInteger.ReadUnsigned(buf, ref index);
offset += offsetDelta;
Buffer.BlockCopy(buf, index, _lastState, offset, length);
index += length;
}
} }
using var lastStateReader = new BinaryReader(new MemoryStream(_lastState));
Global.Emulator.AsStatable().LoadStateBinary(lastStateReader);
}
else
{
if (!fullState)
{
throw new InvalidOperationException();
}
Global.Emulator.AsStatable().LoadStateBinary(reader);
} }
} }
} }

View File

@ -17,9 +17,7 @@ namespace BizHawk.Client.Common
private readonly long _mCapacity; private readonly long _mCapacity;
private byte[] _mAllocatedBuffer; private byte[] _mAllocatedBuffer;
private Stream _mStream;
private LinkedListNode<ListItem> _mHead, _mTail; private LinkedListNode<ListItem> _mHead, _mTail;
private long _mSize;
public StreamBlobDatabase(bool onDisk, long capacity, StreamBlobDatabaseBufferManager mBufferManage) public StreamBlobDatabase(bool onDisk, long capacity, StreamBlobDatabaseBufferManager mBufferManage)
{ {
@ -32,19 +30,19 @@ namespace BizHawk.Client.Common
// I checked the DeleteOnClose operation to make sure it cleans up when the process is aborted, and it seems to. // I checked the DeleteOnClose operation to make sure it cleans up when the process is aborted, and it seems to.
// Otherwise we would have a more complex tempfile management problem here. // Otherwise we would have a more complex tempfile management problem here.
// 4KB buffer chosen due to similarity to .net defaults, and fear of anything larger making hiccups for small systems (we could try asyncing this stuff though...) // 4KB buffer chosen due to similarity to .net defaults, and fear of anything larger making hiccups for small systems (we could try asyncing this stuff though...)
_mStream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose); Stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose);
} }
else else
{ {
_mAllocatedBuffer = _mBufferManage(null, ref _mCapacity, true); _mAllocatedBuffer = _mBufferManage(null, ref _mCapacity, true);
_mStream = new MemoryStream(_mAllocatedBuffer); Stream = new MemoryStream(_mAllocatedBuffer);
} }
} }
/// <summary> /// <summary>
/// Gets the amount of the buffer that's used /// Gets the amount of the buffer that's used
/// </summary> /// </summary>
public long Size => _mSize; public long Size { get; private set; }
/// <summary> /// <summary>
/// Gets the current fullness ratio (Size/Capacity). Note that this wont reach 100% due to the buffer size not being a multiple of a fixed savestate size. /// Gets the current fullness ratio (Size/Capacity). Note that this wont reach 100% due to the buffer size not being a multiple of a fixed savestate size.
@ -59,12 +57,12 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// Gets the underlying stream to /// Gets the underlying stream to
/// </summary> /// </summary>
public Stream Stream => _mStream; public Stream Stream { get; private set; }
public void Dispose() public void Dispose()
{ {
_mStream.Dispose(); Stream.Dispose();
_mStream = null; Stream = null;
if (_mAllocatedBuffer != null) if (_mAllocatedBuffer != null)
{ {
long capacity = 0; long capacity = 0;
@ -76,7 +74,7 @@ namespace BizHawk.Client.Common
public void Clear() public void Clear()
{ {
_mHead = _mTail = null; _mHead = _mTail = null;
_mSize = 0; Size = 0;
_mBookmarks.Clear(); _mBookmarks.Clear();
} }
@ -88,8 +86,8 @@ namespace BizHawk.Client.Common
var buf = seg.Array; var buf = seg.Array;
int len = seg.Count; int len = seg.Count;
long offset = Enqueue(0, len); long offset = Enqueue(0, len);
_mStream.Position = offset; Stream.Position = offset;
_mStream.Write(buf, seg.Offset, len); Stream.Write(buf, seg.Offset, len);
} }
/// <summary> /// <summary>
@ -108,14 +106,14 @@ namespace BizHawk.Client.Common
private MemoryStream CreateMemoryStream(ListItem item) private MemoryStream CreateMemoryStream(ListItem item)
{ {
var buf = new byte[item.Length]; var buf = new byte[item.Length];
_mStream.Position = item.Index; Stream.Position = item.Index;
_mStream.Read(buf, 0, item.Length); Stream.Read(buf, 0, item.Length);
return new MemoryStream(buf, 0, item.Length, false, true); return new MemoryStream(buf, 0, item.Length, false, true);
} }
public long Enqueue(int timestamp, int amount) public long Enqueue(int timestamp, int amount)
{ {
_mSize += amount; Size += amount;
if (_mHead == null) if (_mHead == null)
{ {
@ -126,7 +124,7 @@ namespace BizHawk.Client.Common
long target = _mHead.Value.EndExclusive + amount; long target = _mHead.Value.EndExclusive + amount;
if (_mTail != null && target <= _mTail.Value.Index) if (_mTail != null && target <= _mTail.Value.Index)
{ {
// theres room to add a new head before the tail // there's room to add a new head before the tail
_mHead = _mBookmarks.AddAfter(_mHead, new ListItem(timestamp, _mHead.Value.EndExclusive, amount)); _mHead = _mBookmarks.AddAfter(_mHead, new ListItem(timestamp, _mHead.Value.EndExclusive, amount));
goto CLEANUP; goto CLEANUP;
} }
@ -136,7 +134,7 @@ namespace BizHawk.Client.Common
{ {
if (target <= _mCapacity) if (target <= _mCapacity)
{ {
// theres room to add a new head before the end of capacity // there's room to add a new head before the end of capacity
_mHead = _mBookmarks.AddAfter(_mHead, new ListItem(timestamp, _mHead.Value.EndExclusive, amount)); _mHead = _mBookmarks.AddAfter(_mHead, new ListItem(timestamp, _mHead.Value.EndExclusive, amount));
goto CLEANUP; goto CLEANUP;
} }
@ -164,7 +162,7 @@ namespace BizHawk.Client.Common
if (_mHead.Value.EndExclusive > _mTail.Value.Index && _mHead.Value.Index <= _mTail.Value.Index && _mHead != _mTail) if (_mHead.Value.EndExclusive > _mTail.Value.Index && _mHead.Value.Index <= _mTail.Value.Index && _mHead != _mTail)
{ {
var nextTail = _mTail.Next; var nextTail = _mTail.Next;
_mSize -= _mTail.Value.Length; Size -= _mTail.Value.Length;
_mBookmarks.Remove(_mTail); _mBookmarks.Remove(_mTail);
_mTail = nextTail; _mTail = nextTail;
} }
@ -196,7 +194,7 @@ namespace BizHawk.Client.Common
} }
var ret = _mHead.Value; var ret = _mHead.Value;
_mSize -= ret.Length; Size -= ret.Length;
LinkedListNode<ListItem> nextHead = _mHead.Previous; LinkedListNode<ListItem> nextHead = _mHead.Previous;
_mBookmarks.Remove(_mHead); _mBookmarks.Remove(_mHead);
if (_mHead == _mTail) if (_mHead == _mTail)
@ -227,7 +225,7 @@ namespace BizHawk.Client.Common
} }
var ret = _mTail.Value; var ret = _mTail.Value;
_mSize -= ret.Length; Size -= ret.Length;
var nextTail = _mTail.Next; var nextTail = _mTail.Next;
_mBookmarks.Remove(_mTail); _mBookmarks.Remove(_mTail);
if (_mTail == _mHead) if (_mTail == _mHead)

View File

@ -265,6 +265,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=performant/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=performant/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Phaser/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Phaser/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pollable/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Pollable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=preload/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Prereqs/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Prereqs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=quicksave/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=quicksave/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Regionable/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Regionable/@EntryIndexedValue">True</s:Boolean>