Skip to content

Commit

Permalink
fix: Fix some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed May 15, 2024
1 parent 5feabbd commit b717180
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/Generation/PredefinedTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public sealed class PredefinedTypeAttribute : Attribute
{
public required string TypeName { get; set; }

public bool IgnoreTemplateArgs { get; set; } = false;
public bool IgnoreTemplateArgs { get; set; }
}
2 changes: 1 addition & 1 deletion src/Hosihikari.NativeInterop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Hosihikari.FastElfQuery" Version="1.0.1"/>
<PackageReference Include="Hosihikari.FastElfQuery" Version="1.0.2"/>
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/Import/LibHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum HookResult
internal static partial class LibHook
{
private const string LibName = "dobby";
private const string DlsymLibName = "export";

[LibraryImport(LibName, EntryPoint = "DobbyHook")]
public static partial HookResult Hook(
Expand All @@ -24,6 +25,6 @@ out nint originFunc
public static partial HookResult Unhook(nint address);

[SupportedOSPlatform("windows")]
[LibraryImport(LibName, EntryPoint = "resolveSymbol", StringMarshalling = StringMarshalling.Utf8)]
[LibraryImport(DlsymLibName, EntryPoint = "resolveSymbol", StringMarshalling = StringMarshalling.Utf8)]
public static partial nint ResolveSymbol(string symbol);
}
2 changes: 1 addition & 1 deletion src/Import/LibNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ out void* sbuf
public static unsafe partial bool std_istream_read(void* stream, byte* buffer, int length);

#endregion
}
}
14 changes: 7 additions & 7 deletions src/SymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool TryDlsym(string symbolName, out nint address)
return true;
}

if (!s_symbolTable!.TryQuery(symbolName, out int offset))
if (!s_symbolTable!.TryQuery(symbolName, out nint offset))
{
address = default;
return false;
Expand Down Expand Up @@ -114,12 +114,12 @@ public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, Proper

public static string QuerySymbol(PropertyInfo fptrProperty)
{
if (TryQuerySymbol(out string? symbol, fptrProperty))
if (!TryQuerySymbol(out string? symbol, fptrProperty))
{
return symbol;
throw new InvalidDataException();
}

throw new InvalidDataException();
return symbol;
}

public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, MethodInfo method)
Expand Down Expand Up @@ -152,11 +152,11 @@ public static bool TryQuerySymbol([NotNullWhen(true)] out string? symbol, Delega

public static string QuerySymbol(Delegate method)
{
if (TryQuerySymbol(out string? symbol, method))
if (!TryQuerySymbol(out string? symbol, method))
{
return symbol;
throw new InvalidDataException();
}

throw new InvalidDataException();
return symbol;
}
}
66 changes: 33 additions & 33 deletions src/Unmanaged/CppTypeSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Hosihikari.NativeInterop.Unmanaged.Attributes;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Hosihikari.NativeInterop.Unmanaged;

Expand Down Expand Up @@ -60,15 +59,17 @@ public static T As<T>(this ICppInstanceNonGeneric @this, bool releaseSrc = false
}

public static unsafe void* GetVurtualFunctionPointerByIndex(void* vtable, int index)
=> (void*)*((long*)vtable + index);
{
return (void*)*((long*)vtable + index);
}
}

public interface IOverrideAttributeGeneric
{
public abstract int Index { get; }
public abstract int Offset { get; }
public int Index { get; }
public int Offset { get; }

public abstract ulong VTableLength { get; }
public ulong VTableLength { get; }
}

[AttributeUsage(AttributeTargets.Method)]
Expand All @@ -80,56 +81,55 @@ public sealed class OverrideAttribute<TVtable>(int virtualMethodIndex) : Attribu
public ulong VTableLength => TVtable.VtableLength;
}

public unsafe interface INativeVirtualMethodOverrideProvider<TSelf>
public interface INativeVirtualMethodOverrideProvider<TSelf>
where TSelf : class, ICppInstance<TSelf>, INativeVirtualMethodOverrideProvider<TSelf>
{
private static readonly VtableHandle vtableHandle;
// private static readonly VtableHandle vtableHandle;

static INativeVirtualMethodOverrideProvider()
{
throw new NotImplementedException();

var query = from method in typeof(TSelf).GetRuntimeMethods()
let attrs = method.GetCustomAttributes(false).ToList()
let overrideAttr = attrs.FirstOrDefault(a => a is IOverrideAttributeGeneric) as IOverrideAttributeGeneric
let unmanagedCallersOnlyAttr = attrs.OfType<UnmanagedCallersOnlyAttribute>().FirstOrDefault()
where overrideAttr != null && unmanagedCallersOnlyAttr != null
group (method.MethodHandle.GetFunctionPointer(), overrideAttr.Index, overrideAttr.Offset, overrideAttr.VTableLength)
by (overrideAttr.Offset, overrideAttr.VTableLength) into g
select (g.Key, g.Select(t => (t.Item1, t.Index)).ToArray());
// var query = from method in typeof(TSelf).GetRuntimeMethods()
// let attrs = method.GetCustomAttributes(false).ToList()
// let overrideAttr = attrs.FirstOrDefault(a => a is IOverrideAttributeGeneric) as IOverrideAttributeGeneric
// let unmanagedCallersOnlyAttr = attrs.OfType<UnmanagedCallersOnlyAttribute>().FirstOrDefault()
// where overrideAttr != null && unmanagedCallersOnlyAttr != null
// group (method.MethodHandle.GetFunctionPointer(), overrideAttr.Index, overrideAttr.Offset, overrideAttr.VTableLength)
// by (overrideAttr.Offset, overrideAttr.VTableLength) into g
// select (g.Key, g.Select(t => (t.Item1, t.Index)).ToArray());

vtableHandle = new(query);
// vtableHandle = new(query);
}
}

internal unsafe struct VTable
{
public int offset;
public void* ptr;
}
// internal unsafe struct VTable
// {
// public int offset;
// public void* ptr;
// }

internal unsafe class VtableHandle
internal class VtableHandle
{
VTable[] vtables;
// VTable[] vtables;

public VtableHandle(IEnumerable<((int offset, ulong vtableLength), (nint ptr, int index)[])> values)
{
throw new NotImplementedException();

List<VTable> list = [];
// List<VTable> list = [];

foreach (var ((offset, vtblLength), ptrs) in values)
{
var vtblPtr = NativeAlloc<nint>.NewArray(vtblLength);
for (int i = 0; i < (int)vtblLength; ++i)
{
vtblPtr[i] = ptrs[i].ptr;
}
}
// foreach (var ((offset, vtblLength), ptrs) in values)
// {
// var vtblPtr = NativeAlloc<nint>.NewArray(vtblLength);
// for (int i = 0; i < (int)vtblLength; ++i)
// {
// vtblPtr[i] = ptrs[i].ptr;
// }
// }
}

~VtableHandle()
{

}
}
2 changes: 1 addition & 1 deletion src/Unmanaged/ICppInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface ICppInstanceNonGeneric : IDisposable
public interface ICppInstance<TSelf> : ICppInstanceNonGeneric
where TSelf : class, ICppInstance<TSelf>
{
public new static abstract TSelf ConstructInstance(nint ptr, bool owns, bool ownsMemory);
public static new abstract TSelf ConstructInstance(nint ptr, bool owns, bool ownsMemory);

/// <summary>
/// noexcept
Expand Down
6 changes: 3 additions & 3 deletions src/Unmanaged/STL/StdInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public byte[] ReadToEnd()
byte* buffer = stackalloc byte[bufferSize];
while (LibNative.std_istream_read(_pointer, buffer, bufferSize))
{
writer.Write(new ReadOnlySpan<byte>(buffer, bufferSize).ToArray());
writer.Write([..new ReadOnlySpan<byte>(buffer, bufferSize)]);
NativeMemory.Clear(buffer, bufferSize);
}

byte[] data = new ReadOnlySpan<byte>(buffer, bufferSize).ToArray();
byte[] data = [..new ReadOnlySpan<byte>(buffer, bufferSize)];
int end = data.AsSpan().IndexOf((byte)0);
if (end > 0)
{
data = data.AsSpan(0, end).ToArray();
data = [..data.AsSpan(0, end)];
}

writer.Write(data);
Expand Down
4 changes: 1 addition & 3 deletions src/Unmanaged/STL/StdSharedPtr.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Hosihikari.NativeInterop.Generation;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;

namespace Hosihikari.NativeInterop.Unmanaged.STL;

[PredefinedType(TypeName = "class std::shared_ptr", IgnoreTemplateArgs = true)]
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct StdSharedPtr
public unsafe struct StdSharedPtr
{
public void* ptr;
public void* ctr;
Expand Down
4 changes: 1 addition & 3 deletions src/Unmanaged/STL/StdVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
using Hosihikari.NativeInterop.Import;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;

namespace Hosihikari.NativeInterop.Unmanaged.STL;

[PredefinedType(TypeName = "class std::vector", IgnoreTemplateArgs = true)]
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct StdVector
public unsafe struct StdVector
{
public void* begin;

Expand Down
5 changes: 4 additions & 1 deletion src/Unmanaged/UnexploredType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ namespace Hosihikari.NativeInterop.Unmanaged;

public struct UnexploredType<T> where T : class, ICppInstance<T>
{
static UnexploredType() => throw new InvalidOperationException();
static UnexploredType()
{
throw new InvalidOperationException();
}
}

0 comments on commit b717180

Please sign in to comment.