From 5a1341dd4753ddf1ba4b069b7368b13529008e66 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 15 Nov 2019 17:00:43 -0600 Subject: [PATCH] misc cleanups --- BizHawk.Client.Common/CoreFileProvider.cs | 16 ++--- .../rewind/RewindThreader.cs | 2 +- BizHawk.Client.Common/rewind/Rewinder.cs | 64 +++++++++---------- .../rewind/StreamBlobDatabase.cs | 36 +++++------ BizHawk.sln.DotSettings | 1 + 5 files changed, 54 insertions(+), 65 deletions(-) diff --git a/BizHawk.Client.Common/CoreFileProvider.cs b/BizHawk.Client.Common/CoreFileProvider.cs index 197ee0e031..9dbaad2a94 100644 --- a/BizHawk.Client.Common/CoreFileProvider.cs +++ b/BizHawk.Client.Common/CoreFileProvider.cs @@ -27,11 +27,6 @@ namespace BizHawk.Client.Common return Path.Combine(PathManager.GetExeDirectoryAbsolute(), "dll"); } - public string GetSaveRAMPath() - { - return PathManager.SaveRamPath(Global.Game); - } - public string GetRetroSaveRAMDirectory() { return PathManager.RetroSaveRAMDirectory(Global.Game); @@ -53,14 +48,14 @@ namespace BizHawk.Client.Common { if (required) { - var fullmsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? $": {msg}" : ".")}"; - throw new MissingFirmwareException(fullmsg); + var fullMsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? $": {msg}" : ".")}"; + throw new MissingFirmwareException(fullMsg); } if (msg != null) { - var fullmsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}"; - _showWarning(fullmsg); + var fullMsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}"; + _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) { - string path; - byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out path); + byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out var path); if (ret != null && path != null) { gi = Database.GetGameInfo(ret, path); diff --git a/BizHawk.Client.Common/rewind/RewindThreader.cs b/BizHawk.Client.Common/rewind/RewindThreader.cs index 85cafb4171..7169ce8929 100644 --- a/BizHawk.Client.Common/rewind/RewindThreader.cs +++ b/BizHawk.Client.Common/rewind/RewindThreader.cs @@ -64,7 +64,7 @@ namespace BizHawk.Client.Common return; } - byte[] savestateCopy = null; + byte[] savestateCopy; while (_stateBufferPool.TryPop(out savestateCopy) && savestateCopy.Length != coreSavestate.Length) { savestateCopy = null; diff --git a/BizHawk.Client.Common/rewind/Rewinder.cs b/BizHawk.Client.Common/rewind/Rewinder.cs index 69d3e8a2a6..c1343f32a8 100644 --- a/BizHawk.Client.Common/rewind/Rewinder.cs +++ b/BizHawk.Client.Common/rewind/Rewinder.cs @@ -348,47 +348,43 @@ namespace BizHawk.Client.Common 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(); - bool fullState = reader.ReadByte() == 1; - if (_rewindDeltaEnable) + if (fullState) { - if (fullState) - { - 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); - } + UpdateLastState(buf, 1, buf.Length - 1); } else { - if (!fullState) - { - throw new InvalidOperationException(); - } + int index = 1; + int offset = 0; - 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); } } } diff --git a/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs b/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs index 64a999f3b2..46f3e0f7d3 100644 --- a/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs +++ b/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs @@ -17,9 +17,7 @@ namespace BizHawk.Client.Common private readonly long _mCapacity; private byte[] _mAllocatedBuffer; - private Stream _mStream; private LinkedListNode _mHead, _mTail; - private long _mSize; 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. // 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...) - _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 { _mAllocatedBuffer = _mBufferManage(null, ref _mCapacity, true); - _mStream = new MemoryStream(_mAllocatedBuffer); + Stream = new MemoryStream(_mAllocatedBuffer); } } /// /// Gets the amount of the buffer that's used /// - public long Size => _mSize; + public long Size { get; private set; } /// /// 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 /// /// Gets the underlying stream to /// - public Stream Stream => _mStream; + public Stream Stream { get; private set; } public void Dispose() { - _mStream.Dispose(); - _mStream = null; + Stream.Dispose(); + Stream = null; if (_mAllocatedBuffer != null) { long capacity = 0; @@ -76,7 +74,7 @@ namespace BizHawk.Client.Common public void Clear() { _mHead = _mTail = null; - _mSize = 0; + Size = 0; _mBookmarks.Clear(); } @@ -88,8 +86,8 @@ namespace BizHawk.Client.Common var buf = seg.Array; int len = seg.Count; long offset = Enqueue(0, len); - _mStream.Position = offset; - _mStream.Write(buf, seg.Offset, len); + Stream.Position = offset; + Stream.Write(buf, seg.Offset, len); } /// @@ -108,14 +106,14 @@ namespace BizHawk.Client.Common private MemoryStream CreateMemoryStream(ListItem item) { var buf = new byte[item.Length]; - _mStream.Position = item.Index; - _mStream.Read(buf, 0, item.Length); + Stream.Position = item.Index; + Stream.Read(buf, 0, item.Length); return new MemoryStream(buf, 0, item.Length, false, true); } public long Enqueue(int timestamp, int amount) { - _mSize += amount; + Size += amount; if (_mHead == null) { @@ -126,7 +124,7 @@ namespace BizHawk.Client.Common long target = _mHead.Value.EndExclusive + amount; 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)); goto CLEANUP; } @@ -136,7 +134,7 @@ namespace BizHawk.Client.Common { 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)); 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) { var nextTail = _mTail.Next; - _mSize -= _mTail.Value.Length; + Size -= _mTail.Value.Length; _mBookmarks.Remove(_mTail); _mTail = nextTail; } @@ -196,7 +194,7 @@ namespace BizHawk.Client.Common } var ret = _mHead.Value; - _mSize -= ret.Length; + Size -= ret.Length; LinkedListNode nextHead = _mHead.Previous; _mBookmarks.Remove(_mHead); if (_mHead == _mTail) @@ -227,7 +225,7 @@ namespace BizHawk.Client.Common } var ret = _mTail.Value; - _mSize -= ret.Length; + Size -= ret.Length; var nextTail = _mTail.Next; _mBookmarks.Remove(_mTail); if (_mTail == _mHead) diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 936033440e..ffab5d0978 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -265,6 +265,7 @@ True True True + True True True True