From 74fa19ec2d4b8bf4287d477ed33c4e93afe92ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= Date: Thu, 28 Dec 2023 22:27:22 +0800 Subject: [PATCH] chore: Bump to 1.0.6 --- .gitignore | 3 +- Directory.Build.props | 2 +- src/Hook/Function.cs | 2 +- src/Hook/ObjectOriented/EasyHook.cs | 13 ++--- src/Hook/ObjectOriented/Exceptions.cs | 12 +---- src/Hook/ObjectOriented/HookBase.cs | 8 +-- src/Properties/launchSettings.json | 9 ---- src/SymbolHelper.cs | 12 ++--- src/Unmanaged/Attributes/FlagBitsAttribute.cs | 5 +- src/Unmanaged/Attributes/RVAAttribute.cs | 6 +-- src/Unmanaged/Attributes/SymbolAttribute.cs | 6 +-- .../Attributes/VirtualCppClassAttribute.cs | 5 +- src/Unmanaged/CppTypeSystem.cs | 52 ++++++++----------- src/Unmanaged/ICppInstance.cs | 2 +- src/Unmanaged/INativeTypeFiller.cs | 17 +++--- src/Unmanaged/Memory.cs | 2 +- src/Unmanaged/MoveHandle.cs | 2 +- src/Unmanaged/RValueReference.cs | 2 +- src/Unmanaged/Result.cs | 21 +++----- src/Unmanaged/STL/StdErrorCode.cs | 2 +- src/Unmanaged/STL/StdString.cs | 37 ++++++------- src/Unmanaged/STL/StdVector.cs | 37 +++++++------ src/Unmanaged/ValuePointer.cs | 4 +- src/Utils/StringUtils.cs | 2 +- 24 files changed, 105 insertions(+), 158 deletions(-) delete mode 100644 src/Properties/launchSettings.json diff --git a/.gitignore b/.gitignore index c5b6d2e..693d6ef 100644 --- a/.gitignore +++ b/.gitignore @@ -402,4 +402,5 @@ ASALocalRun/ .mfractor/ # Local History for Visual Studio -.localhistory/ \ No newline at end of file +.localhistory/ +src/Properties/launchSettings.json diff --git a/Directory.Build.props b/Directory.Build.props index d32fe96..e857a8c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 1.0.5 + 1.0.6 diff --git a/src/Hook/Function.cs b/src/Hook/Function.cs index 44532a8..e577e3f 100644 --- a/src/Hook/Function.cs +++ b/src/Hook/Function.cs @@ -20,7 +20,7 @@ out HookInstance instance ) { HookResult result = LibHook.Hook(address, hook, out org); - instance = new HookInstance(address, org); + instance = new(address, org); return result; } diff --git a/src/Hook/ObjectOriented/EasyHook.cs b/src/Hook/ObjectOriented/EasyHook.cs index a707ba8..456cc59 100644 --- a/src/Hook/ObjectOriented/EasyHook.cs +++ b/src/Hook/ObjectOriented/EasyHook.cs @@ -1,19 +1,12 @@ namespace Hosihikari.NativeInterop.Hook.ObjectOriented; -public class EasyHook : HookBase +public class EasyHook(string rva, TDelegate func) : HookBase(rva) where TDelegate : Delegate { - public EasyHook(string rva, TDelegate func) - : base(rva) - { - HookedFunc = func; - } - public EasyHook(TDelegate oldFunc, TDelegate newFunc) - : base(SymbolHelper.QuerySymbol(oldFunc)) + : this(SymbolHelper.QuerySymbol(oldFunc), newFunc) { - HookedFunc = newFunc; } - public override TDelegate HookedFunc { get; } + public override TDelegate HookedFunc { get; } = func; } diff --git a/src/Hook/ObjectOriented/Exceptions.cs b/src/Hook/ObjectOriented/Exceptions.cs index 3ed1808..183334f 100644 --- a/src/Hook/ObjectOriented/Exceptions.cs +++ b/src/Hook/ObjectOriented/Exceptions.cs @@ -2,17 +2,9 @@ namespace Hosihikari.NativeInterop.Hook.ObjectOriented; -public class HookAlreadyInstalledException : Exception -{ - public HookAlreadyInstalledException() - : base($"Hook already registered") { } -} +public class HookAlreadyInstalledException() : Exception($"Hook already registered"); -public class HookNotInstalledException : Exception -{ - public HookNotInstalledException() - : base($"Hook not installed") { } -} +public class HookNotInstalledException() : Exception($"Hook not installed"); public class HookInstalledFailedException : Exception { diff --git a/src/Hook/ObjectOriented/HookBase.cs b/src/Hook/ObjectOriented/HookBase.cs index 0c1d990..a5f8c64 100644 --- a/src/Hook/ObjectOriented/HookBase.cs +++ b/src/Hook/ObjectOriented/HookBase.cs @@ -23,7 +23,7 @@ protected HookBase(nint address) private HookBase() { - _originalFuncInstance = new Lazy(() => + _originalFuncInstance = new(() => { unsafe { @@ -32,7 +32,7 @@ private HookBase() : Marshal.GetDelegateForFunctionPointer(new(_orgIntPtr)); } }); - _hookedFuncInstance = new Lazy(() => + _hookedFuncInstance = new(() => { TDelegate hookedFunc = HookedFunc; return HookedFunc is null ? throw new NullReferenceException("HookedFunc") : hookedFunc; @@ -59,9 +59,9 @@ public bool HasInstalled private unsafe void* _hookedFuncPointer; //alloc handle for delegate - private GCHandle? _handle = null; + private GCHandle? _handle; - private HookInstance? _instance = null; + private HookInstance? _instance; public void Install() { diff --git a/src/Properties/launchSettings.json b/src/Properties/launchSettings.json deleted file mode 100644 index 9dd7aca..0000000 --- a/src/Properties/launchSettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "profiles": { - "配置文件 1": { - "commandName": "Executable", - "executablePath": "C:\\Users\\minec\\Desktop\\bds\\bedrock_server_mod.exe", - "workingDirectory": "C:\\Users\\minec\\Desktop\\bds" - } - } -} \ No newline at end of file diff --git a/src/SymbolHelper.cs b/src/SymbolHelper.cs index 38a5b5e..d3bf678 100644 --- a/src/SymbolHelper.cs +++ b/src/SymbolHelper.cs @@ -26,7 +26,7 @@ static SymbolHelper() => public static bool TryDlsym(string symbolName, out nint address) { #if WINDOWS - var ptr = Dlsym(symbolName); + nint ptr = Dlsym(symbolName); if (ptr is not 0) { address = ptr; @@ -93,7 +93,7 @@ public static Lazy DlsymLazy(string symbolName) => public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, PropertyInfo fptrProperty) { symbol = null; - var attr = fptrProperty.GetCustomAttribute(); + SymbolAttribute? attr = fptrProperty.GetCustomAttribute(); if (attr is null) return false; symbol = attr.Symbol; @@ -102,7 +102,7 @@ public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, Proper public static string QuerySymbol(PropertyInfo fptrProperty) { - if (TryQuerySymbol(out var symbol, fptrProperty)) + if (TryQuerySymbol(out string? symbol, fptrProperty)) { return symbol; } @@ -113,7 +113,7 @@ public static string QuerySymbol(PropertyInfo fptrProperty) public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, MethodInfo method) { symbol = null; - var attr = method.GetCustomAttribute(); + SymbolAttribute? attr = method.GetCustomAttribute(); if (attr is null) return false; symbol = attr.Symbol; @@ -122,7 +122,7 @@ public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, Method public static string QuerySymbol(MethodInfo method) { - if (TryQuerySymbol(out var symbol, method)) + if (TryQuerySymbol(out string? symbol, method)) { return symbol; } @@ -135,7 +135,7 @@ public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, Delega public static string QuerySymbol(Delegate method) { - if (TryQuerySymbol(out var symbol, method)) + if (TryQuerySymbol(out string? symbol, method)) { return symbol; } diff --git a/src/Unmanaged/Attributes/FlagBitsAttribute.cs b/src/Unmanaged/Attributes/FlagBitsAttribute.cs index f5e6c71..84f74cf 100644 --- a/src/Unmanaged/Attributes/FlagBitsAttribute.cs +++ b/src/Unmanaged/Attributes/FlagBitsAttribute.cs @@ -1,8 +1,7 @@ namespace Hosihikari.NativeInterop.Unmanaged.Attributes; [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field)] -public class FlagBitsAttribute : Attribute +public class FlagBitsAttribute(int value) : Attribute { - public int FlagBits { get; private set; } - public FlagBitsAttribute(int value) => FlagBits = value; + public int FlagBits { get; private set; } = value; } diff --git a/src/Unmanaged/Attributes/RVAAttribute.cs b/src/Unmanaged/Attributes/RVAAttribute.cs index 6afe87c..7e98c62 100644 --- a/src/Unmanaged/Attributes/RVAAttribute.cs +++ b/src/Unmanaged/Attributes/RVAAttribute.cs @@ -1,9 +1,7 @@ namespace Hosihikari.NativeInterop.Unmanaged.Attributes; [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field)] -public class RVAAttribute : Attribute +public class RVAAttribute(ulong rva) : Attribute { - public ulong RVA { get; private set; } - - public RVAAttribute(ulong rva) => RVA = rva; + public ulong RVA { get; private set; } = rva; } diff --git a/src/Unmanaged/Attributes/SymbolAttribute.cs b/src/Unmanaged/Attributes/SymbolAttribute.cs index 206935c..443d7c2 100644 --- a/src/Unmanaged/Attributes/SymbolAttribute.cs +++ b/src/Unmanaged/Attributes/SymbolAttribute.cs @@ -1,9 +1,7 @@ namespace Hosihikari.NativeInterop.Unmanaged.Attributes; [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field)] -public class SymbolAttribute : Attribute +public class SymbolAttribute(string symbol) : Attribute { - public string Symbol { get; private set; } - - public SymbolAttribute(string symbol) => Symbol = symbol; + public string Symbol { get; private set; } = symbol; } diff --git a/src/Unmanaged/Attributes/VirtualCppClassAttribute.cs b/src/Unmanaged/Attributes/VirtualCppClassAttribute.cs index aaae11e..4ba8916 100644 --- a/src/Unmanaged/Attributes/VirtualCppClassAttribute.cs +++ b/src/Unmanaged/Attributes/VirtualCppClassAttribute.cs @@ -1,7 +1,4 @@ namespace Hosihikari.NativeInterop.Unmanaged.Attributes; [AttributeUsage(AttributeTargets.Class)] -public class VirtualCppClassAttribute : Attribute -{ - public VirtualCppClassAttribute() { } -} +public class VirtualCppClassAttribute() : Attribute; diff --git a/src/Unmanaged/CppTypeSystem.cs b/src/Unmanaged/CppTypeSystem.cs index d936b36..edd96ff 100644 --- a/src/Unmanaged/CppTypeSystem.cs +++ b/src/Unmanaged/CppTypeSystem.cs @@ -23,7 +23,7 @@ public static class CppTypeSystem throw new InvalidOperationException("\'VirtualCppClassAttribute\' instance is null."); } - public static unsafe ValuePointer GetVTable(Pointer ptr, bool cheekAttribute = true) + public static ValuePointer GetVTable(Pointer ptr, bool cheekAttribute = true) where T : class, ICppInstance where TVtable : unmanaged, ICppVtable => GetVTable(ptr, cheekAttribute); @@ -36,37 +36,33 @@ public static unsafe ref TVtable GetVTable(T obj, bool cheekAttribut public static T As(this ICppInstanceNonGeneric @this, bool releaseSrc = false) where T : class, IDisposable, ICppInstance { - if (releaseSrc) + if (!releaseSrc) { - T temp = T.ConstructInstance(@this.Pointer, @this.IsOwner, @this.IsTempStackValue); - @this.Pointer = 0; - @this.IsOwner = false; - @this.IsTempStackValue = false; + return T.ConstructInstance(@this.Pointer, false, true); + } - @this.Dispose(); + T temp = T.ConstructInstance(@this.Pointer, @this.IsOwner, @this.IsTempStackValue); + @this.Pointer = 0; + @this.IsOwner = false; + @this.IsTempStackValue = false; - return temp; - } - return T.ConstructInstance(@this.Pointer, false, true); + @this.Dispose(); + + return temp; } - public unsafe static void* GetVurtualFunctionPointerByIndex(nint ptr, int index) + public static unsafe void* GetVurtualFunctionPointerByIndex(nint ptr, int index) { - var vtable = *(long**)ptr; - var fptr = *(vtable + index); + long* vtable = *(long**)ptr; + long fptr = *(vtable + index); return (void*)fptr; } } [AttributeUsage(AttributeTargets.Method)] -public class OverrideAttribute : Attribute +public class OverrideAttribute(int virtualMethodIndex) : Attribute { - public int VirtualMethodIndex { get; private set; } - - public OverrideAttribute(int virtualMethodIndex) - { - VirtualMethodIndex = virtualMethodIndex; - } + public int VirtualMethodIndex { get; private set; } = virtualMethodIndex; } public unsafe interface INativeVirtualMethodOverrideProvider @@ -82,10 +78,6 @@ public VTableHandle() : base(0, true) { ptr = HeapAlloc.New(default); } - catch - { - throw; - } finally { if (ptr is not null) @@ -116,11 +108,11 @@ static INativeVirtualMethodOverrideProvider() isVirtualCppClass = typeof(T).GetCustomAttribute() is not null; if (isVirtualCppClass) return; - List<(int, nint)> list = new(); + List<(int, nint)> list = []; - foreach (var method in typeof(T).GetMethods(BindingFlags.Public | BindingFlags.NonPublic)) + foreach (MethodInfo method in typeof(T).GetMethods(BindingFlags.Public | BindingFlags.NonPublic)) { - var overrideAttr = method.GetCustomAttribute(); + OverrideAttribute? overrideAttr = method.GetCustomAttribute(); if (overrideAttr is null) continue; _ = method.GetCustomAttribute() ?? throw new InvalidProgramException(); @@ -134,12 +126,12 @@ static INativeVirtualMethodOverrideProvider() { if (isVirtualCppClass is false || virtFptrs is null) return null; - var handle = new VTableHandle(); + VTableHandle handle = new(); Unsafe.CopyBlock(handle.VTable, CppTypeSystem.GetVTable((void*)ins.Pointer), (uint)sizeof(TVtable)); - foreach (var (index, fptr) in virtFptrs) + foreach ((int index, nint fptr) in virtFptrs) { - *(void**)(((long)handle.VTable) + index * sizeof(void*)) = (void*)fptr; + *(void**)((long)handle.VTable + index * sizeof(void*)) = (void*)fptr; } *(void**)(void*)ins.Pointer = handle.VTable; diff --git a/src/Unmanaged/ICppInstance.cs b/src/Unmanaged/ICppInstance.cs index 6835240..ad91c35 100644 --- a/src/Unmanaged/ICppInstance.cs +++ b/src/Unmanaged/ICppInstance.cs @@ -45,5 +45,5 @@ public interface ICppInstance : ICppInstanceNonGeneric /// noexcept /// /// - public static unsafe abstract implicit operator void*(TSelf ins); + public static abstract unsafe implicit operator void*(TSelf ins); } diff --git a/src/Unmanaged/INativeTypeFiller.cs b/src/Unmanaged/INativeTypeFiller.cs index 4f3f361..6cfd231 100644 --- a/src/Unmanaged/INativeTypeFiller.cs +++ b/src/Unmanaged/INativeTypeFiller.cs @@ -2,11 +2,11 @@ namespace Hosihikari.NativeInterop.Unmanaged; -public interface INativeTypeFiller +public interface INativeTypeFiller where TFiller : unmanaged, INativeTypeFiller where TManagedType : class, ICppInstance { - public static unsafe abstract void Destruct(TFiller* @this); + public static abstract unsafe void Destruct(TFiller* @this); public static abstract implicit operator TManagedType(in TFiller filler); } @@ -17,18 +17,15 @@ public static unsafe bool TryGetDestructorFunctionPointer(out delegate* where TFiller : unmanaged { Type type = typeof(TFiller); - foreach (Type t in type.GetInterfaces()) + if (type.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(INativeTypeFiller<,>))) { - if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(INativeTypeFiller<,>)) - { - result = (delegate* managed) - type - .GetMethod("Destruct", BindingFlags.Public | BindingFlags.Static, new[] { typeof(TFiller*) })! + result = (delegate* managed) + type + .GetMethod("Destruct", BindingFlags.Public | BindingFlags.Static, [typeof(TFiller*)])! .MethodHandle .GetFunctionPointer() .ToPointer(); - return true; - } + return true; } result = null; return false; diff --git a/src/Unmanaged/Memory.cs b/src/Unmanaged/Memory.cs index 18b6f45..91cd1a1 100644 --- a/src/Unmanaged/Memory.cs +++ b/src/Unmanaged/Memory.cs @@ -1,6 +1,6 @@ namespace Hosihikari.NativeInterop.Unmanaged; -public unsafe static class Memory +public static unsafe class Memory { public static T* DAccess(void* address, int offset) where T : unmanaged diff --git a/src/Unmanaged/MoveHandle.cs b/src/Unmanaged/MoveHandle.cs index 9e23e3b..05ce859 100644 --- a/src/Unmanaged/MoveHandle.cs +++ b/src/Unmanaged/MoveHandle.cs @@ -1,7 +1,7 @@ namespace Hosihikari.NativeInterop.Unmanaged; #pragma warning disable CS8500 -public unsafe readonly ref struct MoveHandle +public readonly unsafe ref struct MoveHandle { private readonly T* _instance; diff --git a/src/Unmanaged/RValueReference.cs b/src/Unmanaged/RValueReference.cs index 42b198b..94ca6b7 100644 --- a/src/Unmanaged/RValueReference.cs +++ b/src/Unmanaged/RValueReference.cs @@ -4,7 +4,7 @@ { private readonly nint _ptr; - public readonly T Target => T.ConstructInstance(_ptr, false, false); + public T Target => T.ConstructInstance(_ptr, false, false); private RValueReference(nint ptr) => _ptr = ptr; diff --git a/src/Unmanaged/Result.cs b/src/Unmanaged/Result.cs index d3c00a1..8a98170 100644 --- a/src/Unmanaged/Result.cs +++ b/src/Unmanaged/Result.cs @@ -3,34 +3,29 @@ namespace Hosihikari.NativeInterop.Unmanaged; [StructLayout(LayoutKind.Explicit, Size = 8)] -public unsafe struct UnknownResult -{ - -} +public struct UnknownResult; -public unsafe struct Result where T : class, ICppInstance +public struct Result where T : class, ICppInstance { - private nint _ptr; - public T GetInstance() { - if (_ptr == IntPtr.Zero) + if (Value == nint.Zero) throw new InvalidOperationException("Null pointer."); - var ret = T.ConstructInstance(_ptr, true, true); - _ptr = IntPtr.Zero; + T ret = T.ConstructInstance(Value, true, true); + Value = nint.Zero; return ret; } public readonly void Drop() { - if (_ptr == nint.Zero) + if (Value == nint.Zero) { return; } - T.DestructInstance(_ptr); + T.DestructInstance(Value); } - public readonly nint Value => _ptr; + public nint Value { get; private set; } } diff --git a/src/Unmanaged/STL/StdErrorCode.cs b/src/Unmanaged/STL/StdErrorCode.cs index aa458bf..4f3dca6 100644 --- a/src/Unmanaged/STL/StdErrorCode.cs +++ b/src/Unmanaged/STL/StdErrorCode.cs @@ -18,7 +18,7 @@ public unsafe struct StdErrorCondition public unsafe struct StdErrorCategory { [StructLayout(LayoutKind.Sequential)] - public readonly unsafe struct VTable : ICppVtable + public readonly struct VTable : ICppVtable { // 0 public readonly delegate* unmanaged destructor; diff --git a/src/Unmanaged/STL/StdString.cs b/src/Unmanaged/STL/StdString.cs index f28679b..0c84596 100644 --- a/src/Unmanaged/STL/StdString.cs +++ b/src/Unmanaged/STL/StdString.cs @@ -1,6 +1,5 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; using Hosihikari.NativeInterop.Generation; using Hosihikari.NativeInterop.Utils; @@ -45,7 +44,7 @@ public override string ToString() { fixed (byte* buf = storage.buffer) { - return new string(res > BufferSize ? (sbyte*)storage.ptr : (sbyte*)buf, 0, (int)size); + return new(res > BufferSize ? (sbyte*)storage.ptr : (sbyte*)buf, 0, (int)size); } } @@ -70,40 +69,37 @@ private readonly ulong CalculateGrowth(ulong newSize) { ulong num = res; - ulong num2 = int.MaxValue / sizeof(byte); + const ulong num2 = int.MaxValue / sizeof(byte); ulong num3 = num >> 1; if (num > num2 - num3) { return num2; } ulong num4 = num3 + num; - return (num4 < newSize) ? newSize : num4; + return num4 < newSize ? newSize : num4; } private void EmplaceReallocate(ulong index, byte* ptr, ulong size) { fixed (byte* buf = storage.buffer) { - var oldSize = size; - var oldCap = res; - var newSize = oldSize + size; - var newCap = CalculateGrowth(newSize); + ulong oldSize = size; + ulong oldCap = res; + ulong newSize = oldSize + size; + ulong newCap = CalculateGrowth(newSize); - byte* newStr = null; - byte* oldStr = null; byte* temp = stackalloc byte[BufferSize]; - newStr = newCap > BufferSize ? (byte*)HeapAlloc.New(newCap) : temp; - oldStr = oldCap > BufferSize ? storage.ptr : buf; + byte* newStr = newCap > BufferSize ? (byte*)HeapAlloc.New(newCap) : temp; + byte* oldStr = oldCap > BufferSize ? storage.ptr : buf; byte* constructed = newStr; - var indexLength = index; - Unsafe.CopyBlock(constructed, oldStr, (uint)indexLength * sizeof(byte)); - constructed += indexLength; + Unsafe.CopyBlock(constructed, oldStr, (uint)index * sizeof(byte)); + constructed += index; Unsafe.CopyBlock(constructed, ptr, (uint)size * sizeof(byte)); constructed += size; - Unsafe.CopyBlock(constructed, oldStr + indexLength, (uint)(oldSize - indexLength) * sizeof(byte)); + Unsafe.CopyBlock(constructed, oldStr + index, (uint)(oldSize - index) * sizeof(byte)); if (newCap > BufferSize) { @@ -130,14 +126,13 @@ private void EmplaceWithUnusedCapacity(ulong index, byte* ptr, ulong size) fixed (byte* buf = storage.buffer) { byte* str = null; - var indexLength = index; str = res > BufferSize ? storage.ptr : buf; byte* temp = stackalloc byte[BufferSize]; byte* buffer = size > BufferSize ? (byte*)HeapAlloc.New(size) : temp; - Unsafe.CopyBlock(buffer, str + indexLength, (uint)(size - indexLength) * sizeof(byte)); - Unsafe.CopyBlock(str + indexLength + size, buffer, (uint)size * sizeof(byte)); - Unsafe.CopyBlock(str + indexLength, ptr, (uint)size * sizeof(byte)); + Unsafe.CopyBlock(buffer, str + index, (uint)(size - index) * sizeof(byte)); + Unsafe.CopyBlock(str + index + size, buffer, (uint)size * sizeof(byte)); + Unsafe.CopyBlock(str + index, ptr, (uint)size * sizeof(byte)); if (size > BufferSize) HeapAlloc.Delete(buffer); @@ -154,7 +149,7 @@ public StdString(byte* ptr, ulong size) public StdString(string str) { - var bytes = StringUtils.StringToManagedUtf8(str); + byte[] bytes = StringUtils.StringToManagedUtf8(str); res = size = (ulong)bytes.Length; fixed (byte* ptr = bytes) diff --git a/src/Unmanaged/STL/StdVector.cs b/src/Unmanaged/STL/StdVector.cs index f1cd29b..0c7c2ed 100644 --- a/src/Unmanaged/STL/StdVector.cs +++ b/src/Unmanaged/STL/StdVector.cs @@ -3,7 +3,7 @@ using System.Text.RegularExpressions; using Hosihikari.NativeInterop.Generation; using Hosihikari.NativeInterop.Layer; -using size_t = System.UInt64; +using size_t = ulong; namespace Hosihikari.NativeInterop.Unmanaged.STL; @@ -26,7 +26,7 @@ public unsafe partial struct CxxVector : ITypeReferenceProvider public static Regex Regex => StdVectorRegex(); - public static Type? Matched(Match match) => + public static Type Matched(Match match) => typeof(CxxVector); } @@ -58,7 +58,7 @@ public static implicit operator StdVector(in StdVectorFiller filler) { fixed (void* ptr = &filler) { - return new StdVector(ptr); + return new(ptr); } } } @@ -67,9 +67,9 @@ public static implicit operator StdVector(in StdVectorFiller filler) private static readonly delegate* managed DtorFptr; static StdVector() => IsFiller = NativeTypeFillerHelper.TryGetDestructorFunctionPointer(out DtorFptr); - public static ulong ClassSize => 24ul; + public static size_t ClassSize => 24ul; - public bool IsOwner { get => _isOwner; set => _isOwner = value; } + public bool IsOwner { get; set; } public bool IsTempStackValue { get; set; } @@ -84,7 +84,7 @@ public static void DestructInstance(nint ptr) if (IsFiller) { using StdVector vector = new(ptr); - ulong size = vector.Size(); + size_t size = vector.Size(); for (size_t i = 0; i < size; ++i) { fixed (T* currentPtr = &vector[i]) @@ -123,7 +123,7 @@ protected virtual void Dispose(bool disposing) return; } - if (_isOwner) + if (IsOwner) { Destruct(); LibNative.operator_delete(this); @@ -141,14 +141,13 @@ public void Dispose() } private CxxVector* _pointer; - private bool _isOwner; private bool disposedValue; public StdVector(nint ptr, bool isOwner = false, bool isTempStackValue = true) { _pointer = (CxxVector*)ptr.ToPointer(); - _isOwner = isOwner; + IsOwner = isOwner; } public StdVector(StdVector vec) @@ -159,14 +158,14 @@ public StdVector(StdVector vec) throw new NullReferenceException(nameof(vec._pointer)); } - ulong size = vec.Size(); + size_t size = vec.Size(); _pointer = HeapAlloc.New(default); First = HeapAlloc.NewArray(size); Unsafe.CopyBlock(First, vec.First, (uint)size * (uint)sizeof(T)); Last = First + size; End = Last + vec.Capacity(); - _isOwner = true; + IsOwner = true; IsTempStackValue = false; } @@ -201,29 +200,29 @@ public StdVector(MoveHandle> vec) public StdVector(nint pointer) { _pointer = (CxxVector*)pointer.ToPointer(); - _isOwner = false; + IsOwner = false; IsTempStackValue = true; } public StdVector(void* pointer) { _pointer = (CxxVector*)pointer; - _isOwner = false; + IsOwner = false; IsTempStackValue = true; } public StdVector() { _pointer = (CxxVector*)Marshal.AllocHGlobal(sizeof(CxxVector)).ToPointer(); - _isOwner = true; + IsOwner = true; Unsafe.InitBlock(_pointer, 0, (uint)sizeof(CxxVector)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public size_t Size() => (ulong)((Last - First) / sizeof(T)); + public size_t Size() => (size_t)((Last - First) / sizeof(T)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public size_t Capacity() => (ulong)((End - First) / sizeof(T)); + public size_t Capacity() => (size_t)((End - First) / sizeof(T)); public ref T this[size_t index] { @@ -246,7 +245,7 @@ private size_t CalculateGrowth(size_t newSize) return max_size; } - ulong geometric = oldCapacity + (oldCapacity / 2); + size_t geometric = oldCapacity + (oldCapacity / 2); return geometric >= newSize ? geometric : newSize; } @@ -260,8 +259,8 @@ public ref T EmplaceBack(in T val) ++Last; return ref Unsafe.AsRef(last); } - ulong newCapacity = CalculateGrowth(Size() + 1); - ulong oldSize = Size(); + size_t newCapacity = CalculateGrowth(Size() + 1); + size_t oldSize = Size(); T* newVec = (T*)Marshal.AllocHGlobal((int)newCapacity * sizeof(T)).ToPointer(); Unsafe.CopyBlock(newVec, First, (uint)(((int)oldSize) * sizeof(T))); Unsafe.Write(newVec + oldSize, val); diff --git a/src/Unmanaged/ValuePointer.cs b/src/Unmanaged/ValuePointer.cs index a2cff53..445da7c 100644 --- a/src/Unmanaged/ValuePointer.cs +++ b/src/Unmanaged/ValuePointer.cs @@ -6,9 +6,9 @@ namespace Hosihikari.NativeInterop.Unmanaged; { private readonly nint _ptr; - public readonly ref T Target => ref Unsafe.AsRef(_ptr.ToPointer()); + public ref T Target => ref Unsafe.AsRef(_ptr.ToPointer()); - public readonly T* Pointer => (T*)_ptr; + public T* Pointer => (T*)_ptr; private ValuePointer(nint ptr) => _ptr = ptr; diff --git a/src/Utils/StringUtils.cs b/src/Utils/StringUtils.cs index e0f3af3..8c31396 100644 --- a/src/Utils/StringUtils.cs +++ b/src/Utils/StringUtils.cs @@ -53,7 +53,7 @@ public static unsafe byte[] StringToManagedUtf8(string? s, out int length) if (string.IsNullOrEmpty(s)) { length = 0; - return new byte[] { 0 }; + return [0]; } Encoding utf8 = Encoding.UTF8; fixed (char* ptr = s)