Skip to content

Commit

Permalink
feat: Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed Dec 28, 2023
1 parent dc1367f commit 6411b96
Show file tree
Hide file tree
Showing 14 changed files with 631 additions and 172 deletions.
8 changes: 8 additions & 0 deletions src/Hosihikari.NativeInterop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Hosihikari.FastElfQuery" Version="1.0.0" />
</ItemGroup>
Expand Down
18 changes: 3 additions & 15 deletions src/Layer/LibC.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
#if !WINDOWS
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace Hosihikari.NativeInterop.Layer;

public static partial class LibC
{
#if !WINDOWS
private const string LibName = "c";

[LibraryImport(LibName, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint Dlsym(nint handle, string symbol);

[LibraryImport(LibName, StringMarshalling = StringMarshalling.Utf16)]
public static partial nint Dlopen(string filename, int flags);

[LibraryImport(LibName)]
public static partial int Dlclose(nint handle);

[LibraryImport(LibName, SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial int Symlink(string target, string symlink);

[LibraryImport(LibName)]
public static partial IntPtr Strerror(int errnum);

[LibraryImport(LibName, SetLastError = true)]
public static partial long Readlink(
[MarshalAs(UnmanagedType.LPArray)] byte[] filename,
Expand All @@ -31,5 +19,5 @@ long len

[LibraryImport(LibName, SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial int Unlink(string path);
}
#endif
}
9 changes: 9 additions & 0 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"配置文件 1": {
"commandName": "Executable",
"executablePath": "C:\\Users\\minec\\Desktop\\bds\\bedrock_server_mod.exe",
"workingDirectory": "C:\\Users\\minec\\Desktop\\bds"
}
}
}
11 changes: 9 additions & 2 deletions src/Unmanaged/CppTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Hosihikari.NativeInterop.Unmanaged;

public static class CppTypeSystem
{
public static unsafe void* GetVTable(void* ptr) => (void*)Unsafe.Read<long>(ptr);
public static unsafe void* GetVTable(void* ptr) => *(void**)ptr;

public static unsafe TVtable* GetVTable<TVtable>(void* ptr)
where TVtable : unmanaged, ICppVtable
Expand Down Expand Up @@ -49,6 +49,13 @@ public static T As<T>(this ICppInstanceNonGeneric @this, bool releaseSrc = false
}
return T.ConstructInstance(@this.Pointer, false, true);
}

public unsafe static void* GetVurtualFunctionPointerByIndex(nint ptr, int index)
{
var vtable = *(long**)ptr;
var fptr = *(vtable + index);
return (void*)fptr;
}
}

[AttributeUsage(AttributeTargets.Method)]
Expand Down Expand Up @@ -83,7 +90,7 @@ public VTableHandle() : base(0, true)
{
if (ptr is not null)
{
HeapAlloc<TVtable>.Delete(ptr);
HeapAlloc.Delete(ptr);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Unmanaged/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public unsafe static class Memory
{
public static T* DAccessAsPointer<T>(void* address, int offset)
public static T* DAccess<T>(void* address, int offset)
where T : unmanaged
=> (T*)((nint)address + offset);

Expand Down
12 changes: 7 additions & 5 deletions src/Unmanaged/MoveHandle.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace Hosihikari.NativeInterop.Unmanaged;

public readonly ref struct MoveHandle<T> where T : class, ICppInstance<T>
#pragma warning disable CS8500
public unsafe readonly ref struct MoveHandle<T>
{
private readonly T _instance;
private readonly T* _instance;

internal MoveHandle(T val)
internal MoveHandle(in T val)
{
_instance = val;
fixed (T* ptr = &val)
_instance = ptr;
}

public T Target => _instance;
public T* Target => _instance;
}
2 changes: 1 addition & 1 deletion src/Unmanaged/RValueReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

private RValueReference(nint ptr) => _ptr = ptr;

public static implicit operator RValueReference<T>(in MoveHandle<T> handle) => new(handle.Target.Pointer);
public static implicit operator RValueReference<T>(in MoveHandle<T> handle) => new(handle.Target->Pointer);
}
10 changes: 9 additions & 1 deletion src/Unmanaged/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Hosihikari.NativeInterop.Unmanaged;

[StructLayout(LayoutKind.Explicit, Size = 8)]
public unsafe struct UnknownResult
{

}

public unsafe struct Result<T> where T : class, ICppInstance<T>
{
private nint _ptr;
Expand All @@ -11,7 +17,7 @@ public T GetInstance()
if (_ptr == IntPtr.Zero)
throw new InvalidOperationException("Null pointer.");

var ret =T.ConstructInstance(_ptr, true, true);
var ret = T.ConstructInstance(_ptr, true, true);
_ptr = IntPtr.Zero;
return ret;
}
Expand All @@ -24,5 +30,7 @@ public readonly void Drop()
}
T.DestructInstance(_ptr);
}

public readonly nint Value => _ptr;
}

45 changes: 45 additions & 0 deletions src/Unmanaged/STL/StdErrorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Hosihikari.NativeInterop.Unmanaged.STL;

[StructLayout(LayoutKind.Sequential)]
public unsafe struct StdErrorCondition
{
public int val;
public StdErrorCategory* category;
}

[StructLayout(LayoutKind.Explicit, Size = 16)]
public unsafe struct StdErrorCategory
{
[StructLayout(LayoutKind.Sequential)]
public readonly unsafe struct VTable : ICppVtable
{
// 0
public readonly delegate* unmanaged<StdErrorCategory*, void> destructor;
// 1
public readonly delegate* unmanaged<StdErrorCategory*, byte*> name;
// 2
public readonly delegate* unmanaged<StdErrorCategory*, int, StdString> message;
// 3
public readonly delegate* unmanaged<StdErrorCategory*, int, StdErrorCondition> default_error_condition;
// 4
public readonly delegate* unmanaged<StdErrorCategory*, int, in StdErrorCondition, bool> equivalent;
// 5
public readonly delegate* unmanaged<StdErrorCategory*, in StdErrorCode, int, bool> equivalent_overload;

public static ulong VtableLength => 6;
}
}

[StructLayout(LayoutKind.Sequential)]
public unsafe struct StdErrorCode
{
public int val;
public void* errorCategory;
}
38 changes: 38 additions & 0 deletions src/Unmanaged/STL/StdSharedPtr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Hosihikari.NativeInterop.Generation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Hosihikari.NativeInterop.Unmanaged.STL;

[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct CxxSharedPtr : ITypeReferenceProvider
{
public void* ptr;

public void* ctr;

#if WINDOWS
[GeneratedRegex("^class std::shared_ptr<(?<class_type>.*)>")]
internal static partial Regex StdSharedPtrRegex();
#else
internal static Regex StdVectorRegex() => throw new NotImplementedException();
#endif

public static Regex Regex => StdSharedPtrRegex();

public static Type? Matched(Match match) => typeof(CxxSharedPtr);

public readonly void* Target<T>() where T : class, ICppInstance<T>
=> T.ConstructInstance((nint)ptr, false, true);
}

//public class StdSharedPtr<T>
// where T : class, ICppInstance<T>
//{

//}
Loading

0 comments on commit 6411b96

Please sign in to comment.