BizHawk.Common house cleaning - remove a bunch of unused code, use some higher language level features in some places
This commit is contained in:
parent
2b00a98ab1
commit
049e3d12fc
|
@ -27,11 +27,6 @@ namespace BizHawk.Common
|
|||
return caller.Method;
|
||||
}
|
||||
|
||||
private static MethodInfo Method(Expression<Action> f)
|
||||
{
|
||||
return FromExpression(f.Body);
|
||||
}
|
||||
|
||||
private static MethodInfo Method<T>(Expression<Action<T>> f)
|
||||
{
|
||||
return FromExpression(f.Body);
|
||||
|
|
|
@ -85,9 +85,7 @@
|
|||
<Compile Include="IImportResolver.cs" />
|
||||
<Compile Include="IMonitor.cs" />
|
||||
<Compile Include="InstanceDll.cs" />
|
||||
<Compile Include="IPC\SharedMemoryBlock.cs" />
|
||||
<Compile Include="Log.cs" />
|
||||
<Compile Include="MruStack.cs" />
|
||||
<Compile Include="MutableIntRange.cs" />
|
||||
<Compile Include="OSTailoredCode.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -98,7 +96,6 @@
|
|||
<Compile Include="SwitcherStream.cs" />
|
||||
<Compile Include="TempFileManager.cs" />
|
||||
<Compile Include="UndoHistory.cs" />
|
||||
<Compile Include="UnmanagedResourceHeap.cs" />
|
||||
<Compile Include="Util.cs" />
|
||||
<Compile Include="Win32Hacks.cs" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace BizHawk.Common.BizInvoke
|
||||
{
|
||||
|
|
|
@ -194,8 +194,7 @@ namespace BizHawk.Common.BizInvoke
|
|||
TypeBuilder type, MethodInfo baseMethod, CallingConvention nativeCall, string entryPointName, FieldInfo monitorField)
|
||||
{
|
||||
// create the delegate type
|
||||
MethodBuilder delegateInvoke;
|
||||
var delegateType = BizInvokeUtilities.CreateDelegateType(baseMethod, nativeCall, type, out delegateInvoke);
|
||||
var delegateType = BizInvokeUtilities.CreateDelegateType(baseMethod, nativeCall, type, out var delegateInvoke);
|
||||
|
||||
var paramInfos = baseMethod.GetParameters();
|
||||
var paramTypes = paramInfos.Select(p => p.ParameterType).ToArray();
|
||||
|
|
|
@ -3,8 +3,6 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BizHawk.Common.BizInvoke
|
||||
{
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace BizHawk.Common.BizInvoke
|
||||
{
|
||||
|
@ -25,18 +22,14 @@ namespace BizHawk.Common.BizInvoke
|
|||
|
||||
public static byte[] Hash(byte[] data)
|
||||
{
|
||||
using (var h = SHA1.Create())
|
||||
{
|
||||
return h.ComputeHash(data);
|
||||
}
|
||||
using var h = SHA1.Create();
|
||||
return h.ComputeHash(data);
|
||||
}
|
||||
|
||||
public static byte[] Hash(Stream s)
|
||||
{
|
||||
using (var h = SHA1.Create())
|
||||
{
|
||||
return h.ComputeHash(s);
|
||||
}
|
||||
using var h = SHA1.Create();
|
||||
return h.ComputeHash(s);
|
||||
}
|
||||
|
||||
public static unsafe void ZeroMemory(IntPtr mem, long length)
|
||||
|
@ -49,24 +42,19 @@ namespace BizHawk.Common.BizInvoke
|
|||
}
|
||||
}
|
||||
|
||||
public static long Timestamp()
|
||||
{
|
||||
return DateTime.UtcNow.Ticks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// system page size
|
||||
/// </summary>
|
||||
public static int PageSize { get; private set; }
|
||||
public static int PageSize { get; }
|
||||
|
||||
/// <summary>
|
||||
/// bitshift corresponding to PageSize
|
||||
/// </summary>
|
||||
public static int PageShift { get; private set; }
|
||||
public static int PageShift { get; }
|
||||
/// <summary>
|
||||
/// bitmask corresponding to PageSize
|
||||
/// </summary>
|
||||
public static ulong PageMask { get; private set; }
|
||||
public static ulong PageMask { get; }
|
||||
|
||||
static WaterboxUtils()
|
||||
{
|
||||
|
|
|
@ -20,41 +20,24 @@ namespace BizHawk.Common
|
|||
return new CBuffer<T>(amt, itemsize);
|
||||
}
|
||||
|
||||
public void Write08(uint addr, byte val) { this.Byteptr[addr] = val; }
|
||||
public void Write16(uint addr, ushort val) { *(ushort*)(this.Byteptr + addr) = val; }
|
||||
public void Write32(uint addr, uint val) { *(uint*)(this.Byteptr + addr) = val; }
|
||||
public void Write64(uint addr, ulong val) { *(ulong*)(this.Byteptr + addr) = val; }
|
||||
public byte Read08(uint addr) { return this.Byteptr[addr]; }
|
||||
public ushort Read16(uint addr) { return *(ushort*)(this.Byteptr + addr); }
|
||||
public uint Read32(uint addr) { return *(uint*)(this.Byteptr + addr); }
|
||||
public ulong Read64(uint addr) { return *(ulong*)(this.Byteptr + addr); }
|
||||
public void Write08(int addr, byte val) { this.Byteptr[addr] = val; }
|
||||
public void Write16(int addr, ushort val) { *(ushort*)(this.Byteptr + addr) = val; }
|
||||
public void Write32(int addr, uint val) { *(uint*)(this.Byteptr + addr) = val; }
|
||||
public void Write64(int addr, ulong val) { *(ulong*)(this.Byteptr + addr) = val; }
|
||||
public byte Read08(int addr) { return this.Byteptr[addr]; }
|
||||
public ushort Read16(int addr) { return *(ushort*)(this.Byteptr + addr); }
|
||||
public uint Read32(int addr) { return *(uint*)(this.Byteptr + addr); }
|
||||
public ulong Read64(int addr) { return *(ulong*)(this.Byteptr + addr); }
|
||||
|
||||
public CBuffer(T[] arr, int itemsize)
|
||||
{
|
||||
this.Itemsize = itemsize;
|
||||
this.Len = arr.Length;
|
||||
this.Arr = arr;
|
||||
this.Hnd = GCHandle.Alloc(arr, GCHandleType.Pinned);
|
||||
this.Ptr = this.Hnd.AddrOfPinnedObject().ToPointer();
|
||||
this.Byteptr = (byte*)this.Ptr;
|
||||
Itemsize = itemsize;
|
||||
Len = arr.Length;
|
||||
Arr = arr;
|
||||
Hnd = GCHandle.Alloc(arr, GCHandleType.Pinned);
|
||||
Ptr = Hnd.AddrOfPinnedObject().ToPointer();
|
||||
Byteptr = (byte*)Ptr;
|
||||
}
|
||||
public CBuffer(int amt, int itemsize)
|
||||
{
|
||||
this.Itemsize = itemsize;
|
||||
this.Len = amt;
|
||||
this.Arr = new T[amt];
|
||||
this.Hnd = GCHandle.Alloc(this.Arr, GCHandleType.Pinned);
|
||||
this.Ptr = this.Hnd.AddrOfPinnedObject().ToPointer();
|
||||
this.Byteptr = (byte*)this.Ptr;
|
||||
Util.Memset(this.Byteptr, 0, this.Len * itemsize);
|
||||
Itemsize = itemsize;
|
||||
Len = amt;
|
||||
Arr = new T[amt];
|
||||
Hnd = GCHandle.Alloc(this.Arr, GCHandleType.Pinned);
|
||||
Ptr = Hnd.AddrOfPinnedObject().ToPointer();
|
||||
Byteptr = (byte*)Ptr;
|
||||
Util.Memset(Byteptr, 0, Len * itemsize);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -67,11 +50,11 @@ namespace BizHawk.Common
|
|||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (this.Arr != null)
|
||||
if (Arr != null)
|
||||
{
|
||||
this.Hnd.Free();
|
||||
Hnd.Free();
|
||||
}
|
||||
this.Arr = null;
|
||||
Arr = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,12 +44,6 @@ namespace BizHawk.Common
|
|||
[Serializable]
|
||||
public class Bag<K, V> : BagBase<K, V, Dictionary<K, List<V>>, List<V>> { }
|
||||
|
||||
/// <summary>
|
||||
/// a Dictionary-of-lists with key K and values List<V>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class SortedBag<K, V> : BagBase<K, V, SortedDictionary<K, List<V>>, List<V>> { }
|
||||
|
||||
/// <summary>
|
||||
/// base class for Bag and SortedBag
|
||||
/// </summary>
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BizHawk.Common
|
||||
|
@ -50,27 +49,6 @@ namespace BizHawk.Common
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// test if two arrays are equal in contents; arrays should have same type
|
||||
/// </summary>
|
||||
private static bool ArrayEquals<T>(T[] o1, T[] o2)
|
||||
{
|
||||
if (o1.Length != o2.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < o1.Length; i++)
|
||||
{
|
||||
if (!DeepEquals(o1[i], o2[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static MethodInfo ArrayEqualsGeneric = typeof(DeepEquality).GetMethod("ArrayEquals", BindingFlags.NonPublic | BindingFlags.Static);
|
||||
|
||||
/// <summary>
|
||||
|
@ -101,7 +79,7 @@ namespace BizHawk.Common
|
|||
// this is actually pretty fast; it allows using fast ldelem and stelem opcodes on
|
||||
// arbitrary array types without emitting custom IL
|
||||
var method = ArrayEqualsGeneric.MakeGenericMethod(new Type[] { t1.GetElementType() });
|
||||
return (bool)method.Invoke(null, new object[] { o1, o2 });
|
||||
return (bool)method.Invoke(null, new[] { o1, o2 });
|
||||
}
|
||||
|
||||
if (t1.IsPrimitive)
|
||||
|
|
|
@ -3,10 +3,7 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BizHawk.Common
|
||||
{
|
||||
|
|
|
@ -93,11 +93,9 @@ namespace BizHawk.Common.BufferExtensions
|
|||
|
||||
public static string HashMD5(this byte[] data, int offset, int len)
|
||||
{
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
md5.ComputeHash(data, offset, len);
|
||||
return md5.Hash.BytesToHexString();
|
||||
}
|
||||
using var md5 = MD5.Create();
|
||||
md5.ComputeHash(data, offset, len);
|
||||
return md5.Hash.BytesToHexString();
|
||||
}
|
||||
|
||||
public static string HashMD5(this byte[] data)
|
||||
|
@ -107,11 +105,9 @@ namespace BizHawk.Common.BufferExtensions
|
|||
|
||||
public static string HashSHA1(this byte[] data, int offset, int len)
|
||||
{
|
||||
using (var sha1 = SHA1.Create())
|
||||
{
|
||||
sha1.ComputeHash(data, offset, len);
|
||||
return sha1.Hash.BytesToHexString();
|
||||
}
|
||||
using var sha1 = SHA1.Create();
|
||||
sha1.ComputeHash(data, offset, len);
|
||||
return sha1.Hash.BytesToHexString();
|
||||
}
|
||||
|
||||
public static string HashSHA1(this byte[] data)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BizHawk.Common.CollectionExtensions
|
||||
{
|
||||
|
@ -95,25 +93,5 @@ namespace BizHawk.Common.CollectionExtensions
|
|||
|
||||
throw new InvalidOperationException("Item not found");
|
||||
}
|
||||
|
||||
public static byte[] ToByteArray(this IEnumerable<bool> list)
|
||||
{
|
||||
var bits = new BitArray(list.ToArray());
|
||||
byte[] bytes = new byte[(bits.Length / 8) + (bits.Length % 8 == 0 ? 0 : 1)];
|
||||
bits.CopyTo(bytes, 0);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts any byte array into a bit array represented as a list of booleans
|
||||
/// </summary>
|
||||
public static IEnumerable<bool> ToBools(this byte[] bytes)
|
||||
{
|
||||
var bits = new BitArray(bytes);
|
||||
var bools = new bool[bits.Length];
|
||||
bits.CopyTo(bools, 0);
|
||||
|
||||
return bools;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,39 +116,6 @@ namespace BizHawk.Common.IOExtensions
|
|||
}
|
||||
}
|
||||
|
||||
public static int[] ReadInt32s(this BinaryReader br, int num)
|
||||
{
|
||||
int[] ret = new int[num];
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
ret[i] = br.ReadInt32();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static short[] ReadInt16s(this BinaryReader br, int num)
|
||||
{
|
||||
short[] ret = new short[num];
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
ret[i] = br.ReadInt16();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static ushort[] ReadUInt16s(this BinaryReader br, int num)
|
||||
{
|
||||
ushort[] ret = new ushort[num];
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
ret[i] = br.ReadUInt16();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void WriteBit(this BinaryWriter bw, Bit bit)
|
||||
{
|
||||
bw.Write((bool)bit);
|
||||
|
|
|
@ -3,8 +3,6 @@ using System.Linq;
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BizHawk.Common.ReflectionExtensions
|
||||
{
|
||||
|
@ -118,64 +116,6 @@ namespace BizHawk.Common.ReflectionExtensions
|
|||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes an object and determines if it has methodName as a public method
|
||||
/// </summary>
|
||||
/// <returns>Returns whether or not the object both contains the method name and the method is public</returns>
|
||||
public static bool HasExposedMethod(this object obj, string methodName)
|
||||
{
|
||||
var method = obj.GetType().GetMethod(methodName);
|
||||
|
||||
if (method != null)
|
||||
{
|
||||
return method.IsPublic;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes an object and invokes the method
|
||||
/// The method must exist and be public
|
||||
/// </summary>
|
||||
/// <returns>The return value of the method, as an object.
|
||||
/// If the method returns void, the return value is null
|
||||
/// If the method does not exist or is not public, it returns null
|
||||
/// </returns>
|
||||
public static object InvokeMethod(this object obj, string methodName, object[] args)
|
||||
{
|
||||
var method = obj.GetType().GetMethod(methodName);
|
||||
if (method != null && method.IsPublic)
|
||||
{
|
||||
return method.Invoke(obj, args);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool HasPublicProperty(this object obj, string propertyName)
|
||||
{
|
||||
var property = obj.GetType().GetProperty(propertyName);
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
return property.CanRead;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static object GetPropertyValue(this object obj, string propertyName)
|
||||
{
|
||||
var property = obj.GetType().GetProperty(propertyName);
|
||||
if (property != null && property.CanRead)
|
||||
{
|
||||
return property.GetValue(obj, null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes an enum Type and generates a list of strings from the description attributes
|
||||
/// </summary>
|
||||
|
@ -193,62 +133,5 @@ namespace BizHawk.Common.ReflectionExtensions
|
|||
{
|
||||
return (T)o.GetType().GetCustomAttributes(typeof(T), false)[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// where the fields begin relative to the address an object references points to
|
||||
/// </summary>
|
||||
public static IntPtr ManagedFieldStart => _managedfieldstart;
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private class Junkus
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public IntPtr s;
|
||||
}
|
||||
|
||||
static IntPtr _managedfieldstart = GetManagedOffset(typeof(Junkus).GetField("s"));
|
||||
|
||||
/// <summary>
|
||||
/// the address of a field relative to the address an object reference of that type points to. this function is very expensive to call.
|
||||
/// </summary>
|
||||
public static IntPtr GetManagedOffset(this FieldInfo field)
|
||||
{
|
||||
Type type = field.DeclaringType;
|
||||
|
||||
var dyn = new System.Reflection.Emit.DynamicMethod(
|
||||
"xxz0", typeof(IntPtr), new Type[] { typeof(object) }, typeof(ReflectionExtensions).Module, true);
|
||||
var il = dyn.GetILGenerator();
|
||||
|
||||
var pin = il.DeclareLocal(type, true);
|
||||
var baseaddr = il.DeclareLocal(typeof(IntPtr));
|
||||
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Stloc, pin); // force cast object to type (invalid), and pin
|
||||
|
||||
il.Emit(OpCodes.Ldloc, pin); // base address of reference (points to typeinfo)
|
||||
il.Emit(OpCodes.Conv_I); // convert object ref to intptr (invalid)
|
||||
il.Emit(OpCodes.Stloc, baseaddr);
|
||||
|
||||
il.Emit(OpCodes.Ldloc, pin);
|
||||
il.Emit(OpCodes.Ldflda, field); // address of desired field
|
||||
il.Emit(OpCodes.Conv_I); // convert field& to intptr (invalid)
|
||||
il.Emit(OpCodes.Ldloc, baseaddr);
|
||||
il.Emit(OpCodes.Sub);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
return (IntPtr)dyn.Invoke(null, new object[] { new object() });
|
||||
}
|
||||
|
||||
public static bool ThrowsError(this MethodInfo info)
|
||||
{
|
||||
var il = info.GetMethodBody().GetILAsByteArray();
|
||||
return il[il.Length - 1] == 0x7A;
|
||||
}
|
||||
|
||||
public static bool IsEmpty(this MethodInfo info)
|
||||
{
|
||||
var il = info.GetMethodBody().GetILAsByteArray();
|
||||
return il.Length == 1 && il[0] == 0x2A;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,12 +24,6 @@ namespace BizHawk.Common.StringExtensions
|
|||
return str.Substring(0, index);
|
||||
}
|
||||
|
||||
public static bool IsValidRomExtentsion(this string str, params string[] romExtensions)
|
||||
{
|
||||
var strUpper = str.ToUpper();
|
||||
return romExtensions.Any(ext => strUpper.EndsWith(ext.ToUpper()));
|
||||
}
|
||||
|
||||
public static bool In(this string str, params string[] options)
|
||||
{
|
||||
return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase));
|
||||
|
@ -128,12 +122,7 @@ namespace BizHawk.Common.StringExtensions
|
|||
/// </summary>
|
||||
public static bool IsHex(this string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return str.All(IsHex);
|
||||
return !string.IsNullOrWhiteSpace(str) && str.All(IsHex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -154,12 +143,7 @@ namespace BizHawk.Common.StringExtensions
|
|||
/// </summary>
|
||||
public static bool IsBinary(this string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return str.All(IsBinary);
|
||||
return !string.IsNullOrWhiteSpace(str) && str.All(IsBinary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BizHawk.Common
|
||||
{
|
||||
public unsafe class SharedMemoryBlock : IDisposable
|
||||
{
|
||||
public string Name;
|
||||
public string BlockName;
|
||||
public int Size;
|
||||
public byte* Ptr;
|
||||
byte[] bytes;
|
||||
GCHandle handle;
|
||||
|
||||
public void Allocate()
|
||||
{
|
||||
bytes = new byte[Size];
|
||||
handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
||||
Ptr = (byte*)handle.AddrOfPinnedObject();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
handle.Free();
|
||||
bytes = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -69,7 +69,7 @@ namespace BizHawk.Common
|
|||
return GetProcAddress(_hModule, procName);
|
||||
}
|
||||
|
||||
public IntPtr HModule { get { return _hModule; } }
|
||||
public IntPtr HModule => _hModule;
|
||||
|
||||
IntPtr IImportResolver.Resolve(string entryPoint)
|
||||
{
|
||||
|
|
|
@ -1,62 +0,0 @@
|
|||
namespace BizHawk.Common
|
||||
{
|
||||
public class MruStack<T>
|
||||
{
|
||||
private readonly T[] _store;
|
||||
private int _count;
|
||||
private int _head;
|
||||
|
||||
public MruStack(int capacity)
|
||||
{
|
||||
_store = new T[capacity];
|
||||
Clear();
|
||||
}
|
||||
|
||||
public int Count => _count;
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_head = 0;
|
||||
_count = 0;
|
||||
for (int i = 0; i < _store.Length; i++)
|
||||
{
|
||||
_store[i] = default(T);
|
||||
}
|
||||
}
|
||||
|
||||
public void Push(T value)
|
||||
{
|
||||
_store[_head] = value;
|
||||
_head = (_head + 1) % _store.Length;
|
||||
|
||||
if (_count < _store.Length)
|
||||
{
|
||||
_count++;
|
||||
}
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
_head--;
|
||||
if (_head < 0)
|
||||
{
|
||||
_head = _store.Length - 1;
|
||||
}
|
||||
|
||||
_count--;
|
||||
T value = _store[_head];
|
||||
_store[_head] = default(T);
|
||||
return value;
|
||||
}
|
||||
|
||||
public bool HasElements()
|
||||
{
|
||||
return _count > 0;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,10 +9,7 @@ namespace BizHawk.Common
|
|||
|
||||
public int Min
|
||||
{
|
||||
get
|
||||
{
|
||||
return _min;
|
||||
}
|
||||
get => _min;
|
||||
set
|
||||
{
|
||||
if (_max < value) throw new ArgumentException();
|
||||
|
@ -22,10 +19,7 @@ namespace BizHawk.Common
|
|||
|
||||
public int Max
|
||||
{
|
||||
get
|
||||
{
|
||||
return _max;
|
||||
}
|
||||
get => _max;
|
||||
set
|
||||
{
|
||||
if (value < _min) throw new ArgumentException();
|
||||
|
|
|
@ -19,14 +19,11 @@ namespace BizHawk.Common
|
|||
|
||||
public T this[int index]
|
||||
{
|
||||
get { return buffer[index]; }
|
||||
set { buffer[index] = value; }
|
||||
get => buffer[index];
|
||||
set => buffer[index] = value;
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return Position; }
|
||||
}
|
||||
public int Count => Position;
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
|
@ -53,7 +50,7 @@ namespace BizHawk.Common
|
|||
buffer = new T[capacity];
|
||||
}
|
||||
|
||||
public int Count { get { return size; } }
|
||||
public int Count => size;
|
||||
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
|
@ -130,10 +127,7 @@ namespace BizHawk.Common
|
|||
return dict.ContainsKey(key);
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys
|
||||
{
|
||||
get { return dict.Keys; }
|
||||
}
|
||||
public ICollection<TKey> Keys => dict.Keys;
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
|
@ -145,15 +139,12 @@ namespace BizHawk.Common
|
|||
return dict.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public ICollection<TValue> Values
|
||||
{
|
||||
get { return dict.Values; }
|
||||
}
|
||||
public ICollection<TValue> Values => dict.Values;
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get { return dict[key]; }
|
||||
set { throw new InvalidOperationException(); }
|
||||
get => dict[key];
|
||||
set => throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
|
@ -176,15 +167,9 @@ namespace BizHawk.Common
|
|||
dict.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return dict.Count; }
|
||||
}
|
||||
public int Count => dict.Count;
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
|
|
|
@ -14,40 +14,19 @@ namespace BizHawk.Common
|
|||
|
||||
#region Public
|
||||
|
||||
public bool IsReader
|
||||
{
|
||||
get { return _isReader; }
|
||||
}
|
||||
public bool IsReader => _isReader;
|
||||
|
||||
public bool IsWriter
|
||||
{
|
||||
get { return !IsReader; }
|
||||
}
|
||||
public bool IsWriter => !IsReader;
|
||||
|
||||
public bool IsText
|
||||
{
|
||||
get { return _isText; }
|
||||
}
|
||||
public bool IsText => _isText;
|
||||
|
||||
public BinaryReader BinaryReader
|
||||
{
|
||||
get { return _br; }
|
||||
}
|
||||
public BinaryReader BinaryReader => _br;
|
||||
|
||||
public BinaryWriter BinaryWriter
|
||||
{
|
||||
get { return _bw; }
|
||||
}
|
||||
public BinaryWriter BinaryWriter => _bw;
|
||||
|
||||
public TextReader TextReader
|
||||
{
|
||||
get { return _tr; }
|
||||
}
|
||||
public TextReader TextReader => _tr;
|
||||
|
||||
public TextWriter TextWriter
|
||||
{
|
||||
get { return _tw; }
|
||||
}
|
||||
public TextWriter TextWriter => _tw;
|
||||
|
||||
public Serializer(BinaryWriter bw)
|
||||
{
|
||||
|
@ -118,7 +97,7 @@ namespace BizHawk.Common
|
|||
|
||||
public void BeginSection(string name)
|
||||
{
|
||||
this._sections.Push(name);
|
||||
_sections.Push(name);
|
||||
if (IsText)
|
||||
{
|
||||
if (IsWriter)
|
||||
|
@ -135,7 +114,7 @@ namespace BizHawk.Common
|
|||
|
||||
public void EndSection()
|
||||
{
|
||||
var name = this._sections.Pop();
|
||||
var name = _sections.Pop();
|
||||
if (IsText)
|
||||
{
|
||||
if (IsWriter)
|
||||
|
|
|
@ -15,10 +15,6 @@ namespace BizHawk.Common
|
|||
// switchstream method? flush old stream?
|
||||
private Stream _currStream;
|
||||
|
||||
public SwitcherStream()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if this is enabled, seeks to Begin,0 will get ignored; anything else will be an exception
|
||||
/// </summary>
|
||||
|
@ -34,11 +30,7 @@ namespace BizHawk.Common
|
|||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return _currStream.Position;
|
||||
}
|
||||
|
||||
get => _currStream.Position;
|
||||
set
|
||||
{
|
||||
if (DenySeekHack)
|
||||
|
@ -55,11 +47,6 @@ namespace BizHawk.Common
|
|||
}
|
||||
}
|
||||
|
||||
public void SetCurrStream(Stream str)
|
||||
{
|
||||
_currStream = str;
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_currStream.Flush();
|
||||
|
|
|
@ -16,12 +16,6 @@ namespace BizHawk.Common
|
|||
Enabled = enabled;
|
||||
}
|
||||
|
||||
public UndoHistory(IEnumerable<T> newState, bool enabled)
|
||||
{
|
||||
AddState(newState);
|
||||
Enabled = enabled;
|
||||
}
|
||||
|
||||
public bool Enabled { get; }
|
||||
|
||||
public bool CanUndo => Enabled && _curPos > 1;
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BizHawk.Common
|
||||
{
|
||||
public class UnmanagedResourceHeap : IDisposable
|
||||
{
|
||||
public IntPtr StringToHGlobalAnsi(string str)
|
||||
{
|
||||
var ret = Marshal.StringToHGlobalAnsi(str);
|
||||
HGlobals.Add(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<IntPtr> HGlobals = new List<IntPtr>();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var h in HGlobals)
|
||||
{
|
||||
Marshal.FreeHGlobal(h);
|
||||
}
|
||||
|
||||
HGlobals.Clear();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ using System.Text;
|
|||
|
||||
namespace BizHawk.Common
|
||||
{
|
||||
public static unsafe partial class Util
|
||||
public static unsafe class Util
|
||||
{
|
||||
public static void CopyStream(Stream src, Stream dest, long len)
|
||||
{
|
||||
|
@ -89,29 +89,6 @@ namespace BizHawk.Common
|
|||
return TryWaitForFileToVanish(pathWant);
|
||||
}
|
||||
|
||||
public static bool IsPowerOfTwo(int x)
|
||||
{
|
||||
if (x == 0 || x == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
public static int SaveRamBytesUsed(byte[] saveRam)
|
||||
{
|
||||
for (var i = saveRam.Length - 1; i >= 0; i--)
|
||||
{
|
||||
if (saveRam[i] != 0)
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Could be extension method
|
||||
public static byte[] HexStringToBytes(string str)
|
||||
{
|
||||
|
@ -351,12 +328,6 @@ namespace BizHawk.Common
|
|||
return ret;
|
||||
}
|
||||
|
||||
public static int Memcmp(void* a, string b, int len)
|
||||
{
|
||||
fixed (byte* bp = Encoding.ASCII.GetBytes(b))
|
||||
return Memcmp(a, bp, len);
|
||||
}
|
||||
|
||||
public static int Memcmp(void* a, void* b, int len)
|
||||
{
|
||||
var ba = (byte*)a;
|
||||
|
@ -384,17 +355,6 @@ namespace BizHawk.Common
|
|||
}
|
||||
}
|
||||
|
||||
public static void Memset32(void* ptr, int val, int len)
|
||||
{
|
||||
System.Diagnostics.Debug.Assert(len % 4 == 0);
|
||||
int dwords = len / 4;
|
||||
int* dwptr = (int*)ptr;
|
||||
for (int i = 0; i < dwords; i++)
|
||||
{
|
||||
dwptr[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
public static string FormatFileSize(long filesize)
|
||||
{
|
||||
decimal size = filesize;
|
||||
|
@ -447,8 +407,7 @@ namespace BizHawk.Common
|
|||
|
||||
foreach (var kvp in first)
|
||||
{
|
||||
TValue secondValue;
|
||||
if (!second.TryGetValue(kvp.Key, out secondValue))
|
||||
if (!second.TryGetValue(kvp.Key, out var secondValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -481,23 +440,6 @@ namespace BizHawk.Common
|
|||
}
|
||||
}
|
||||
|
||||
public static class BitConverterLE
|
||||
{
|
||||
public static void WriteBytes(ushort value, byte[] dst, int index)
|
||||
{
|
||||
dst[index ] = (byte)(value );
|
||||
dst[index + 1] = (byte)(value >> 8);
|
||||
}
|
||||
|
||||
public static void WriteBytes(uint value, byte[] dst, int index)
|
||||
{
|
||||
dst[index ] = (byte)(value );
|
||||
dst[index + 1] = (byte)(value >> 8);
|
||||
dst[index + 2] = (byte)(value >> 16);
|
||||
dst[index + 3] = (byte)(value >> 24);
|
||||
}
|
||||
}
|
||||
|
||||
public static class VLInteger
|
||||
{
|
||||
public static void WriteUnsigned(uint value, byte[] data, ref int index)
|
||||
|
|
Loading…
Reference in New Issue