Skip to content

Commit

Permalink
chore: Bump to 1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Dec 28, 2023
1 parent 6411b96 commit 74fa19e
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 158 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,5 @@ ASALocalRun/
.mfractor/

# Local History for Visual Studio
.localhistory/
.localhistory/
src/Properties/launchSettings.json
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Hook/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 3 additions & 10 deletions src/Hook/ObjectOriented/EasyHook.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
namespace Hosihikari.NativeInterop.Hook.ObjectOriented;

public class EasyHook<TDelegate> : HookBase<TDelegate>
public class EasyHook<TDelegate>(string rva, TDelegate func) : HookBase<TDelegate>(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;
}
12 changes: 2 additions & 10 deletions src/Hook/ObjectOriented/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 4 additions & 4 deletions src/Hook/ObjectOriented/HookBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected HookBase(nint address)

private HookBase()
{
_originalFuncInstance = new Lazy<TDelegate>(() =>
_originalFuncInstance = new(() =>
{
unsafe
{
Expand All @@ -32,7 +32,7 @@ private HookBase()
: Marshal.GetDelegateForFunctionPointer<TDelegate>(new(_orgIntPtr));
}
});
_hookedFuncInstance = new Lazy<TDelegate>(() =>
_hookedFuncInstance = new(() =>
{
TDelegate hookedFunc = HookedFunc;
return HookedFunc is null ? throw new NullReferenceException("HookedFunc") : hookedFunc;
Expand All @@ -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()
{
Expand Down
9 changes: 0 additions & 9 deletions src/Properties/launchSettings.json

This file was deleted.

12 changes: 6 additions & 6 deletions src/SymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +93,7 @@ public static Lazy<nint> DlsymLazy(string symbolName) =>
public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, PropertyInfo fptrProperty)
{
symbol = null;
var attr = fptrProperty.GetCustomAttribute<SymbolAttribute>();
SymbolAttribute? attr = fptrProperty.GetCustomAttribute<SymbolAttribute>();
if (attr is null) return false;

symbol = attr.Symbol;
Expand All @@ -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;
}
Expand All @@ -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>();
SymbolAttribute? attr = method.GetCustomAttribute<SymbolAttribute>();
if (attr is null) return false;

symbol = attr.Symbol;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Unmanaged/Attributes/FlagBitsAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 2 additions & 4 deletions src/Unmanaged/Attributes/RVAAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 2 additions & 4 deletions src/Unmanaged/Attributes/SymbolAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 1 addition & 4 deletions src/Unmanaged/Attributes/VirtualCppClassAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
namespace Hosihikari.NativeInterop.Unmanaged.Attributes;

[AttributeUsage(AttributeTargets.Class)]
public class VirtualCppClassAttribute : Attribute
{
public VirtualCppClassAttribute() { }
}
public class VirtualCppClassAttribute() : Attribute;
52 changes: 22 additions & 30 deletions src/Unmanaged/CppTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class CppTypeSystem
throw new InvalidOperationException("\'VirtualCppClassAttribute\' instance is null.");
}

public static unsafe ValuePointer<TVtable> GetVTable<T, TVtable>(Pointer<T> ptr, bool cheekAttribute = true)
public static ValuePointer<TVtable> GetVTable<T, TVtable>(Pointer<T> ptr, bool cheekAttribute = true)
where T : class, ICppInstance<T>
where TVtable : unmanaged, ICppVtable
=> GetVTable<T, TVtable>(ptr, cheekAttribute);
Expand All @@ -36,37 +36,33 @@ public static unsafe ref TVtable GetVTable<T, TVtable>(T obj, bool cheekAttribut
public static T As<T>(this ICppInstanceNonGeneric @this, bool releaseSrc = false)
where T : class, IDisposable, ICppInstance<T>
{
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<T, TVtable>
Expand All @@ -82,10 +78,6 @@ public VTableHandle() : base(0, true)
{
ptr = HeapAlloc<TVtable>.New(default);
}
catch
{
throw;
}
finally
{
if (ptr is not null)
Expand Down Expand Up @@ -116,11 +108,11 @@ static INativeVirtualMethodOverrideProvider()
isVirtualCppClass = typeof(T).GetCustomAttribute<VirtualCppClassAttribute>() 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>();
OverrideAttribute? overrideAttr = method.GetCustomAttribute<OverrideAttribute>();
if (overrideAttr is null) continue;

_ = method.GetCustomAttribute<UnmanagedCallersOnlyAttribute>() ?? throw new InvalidProgramException();
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Unmanaged/ICppInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ public interface ICppInstance<TSelf> : ICppInstanceNonGeneric
/// noexcept
/// </summary>
/// <param name="ins"></param>
public static unsafe abstract implicit operator void*(TSelf ins);
public static abstract unsafe implicit operator void*(TSelf ins);
}
17 changes: 7 additions & 10 deletions src/Unmanaged/INativeTypeFiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Hosihikari.NativeInterop.Unmanaged;

public interface INativeTypeFiller<TFiller, TManagedType>
public interface INativeTypeFiller<TFiller, out TManagedType>
where TFiller : unmanaged, INativeTypeFiller<TFiller, TManagedType>
where TManagedType : class, ICppInstance<TManagedType>
{
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);
}
Expand All @@ -17,18 +17,15 @@ public static unsafe bool TryGetDestructorFunctionPointer<TFiller>(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<TFiller*, void>)
type
.GetMethod("Destruct", BindingFlags.Public | BindingFlags.Static, new[] { typeof(TFiller*) })!
result = (delegate* managed<TFiller*, void>)
type
.GetMethod("Destruct", BindingFlags.Public | BindingFlags.Static, [typeof(TFiller*)])!
.MethodHandle
.GetFunctionPointer()
.ToPointer();
return true;
}
return true;
}
result = null;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Unmanaged/Memory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Hosihikari.NativeInterop.Unmanaged;

public unsafe static class Memory
public static unsafe class Memory
{
public static T* DAccess<T>(void* address, int offset)
where T : unmanaged
Expand Down
2 changes: 1 addition & 1 deletion src/Unmanaged/MoveHandle.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Hosihikari.NativeInterop.Unmanaged;

#pragma warning disable CS8500
public unsafe readonly ref struct MoveHandle<T>
public readonly unsafe ref struct MoveHandle<T>
{
private readonly T* _instance;

Expand Down
2 changes: 1 addition & 1 deletion src/Unmanaged/RValueReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 74fa19e

Please sign in to comment.