Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Jan 31, 2023
1 parent 2f0e9e2 commit 2f3791f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 50 deletions.
11 changes: 5 additions & 6 deletions src/FetchBannerlordVersion.Native/Bindings.Shared.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BUTR.NativeAOT.Shared;

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace FetchBannerlordVersion.Native
Expand All @@ -14,9 +13,9 @@ public static unsafe partial class Bindings
Logger.LogInput(size);
try
{
var result = Allocator.Alloc(size, true);
var result = Allocator.Alloc(size);

Logger.LogOutputPrimitive((int) result);
Logger.LogOutput(new IntPtr(result).ToString("x16"), nameof(Alloc));
return result;
}
catch (Exception e)
Expand All @@ -29,10 +28,10 @@ public static unsafe partial class Bindings
[UnmanagedCallersOnly(EntryPoint = "dealloc")]
public static void Dealloc(param_ptr* ptr)
{
Logger.LogInput(ptr);
Logger.LogInput(new IntPtr(ptr).ToString("x16"), nameof(Dealloc));
try
{
Allocator.Free(ptr, true);
Allocator.Free(ptr);

Logger.LogOutput();
}
Expand All @@ -50,7 +49,7 @@ public static int AllocAliveCount()
{
var result = Allocator.GetCurrentAllocations();

Logger.LogOutputPrimitive(result);
Logger.LogOutput(result);
return result;
}
catch (Exception e)
Expand Down
6 changes: 3 additions & 3 deletions src/FetchBannerlordVersion.Native/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static unsafe partial class Bindings

var result = (uint) Fetcher.GetChangeSet(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutputPrimitive(result);
Logger.LogOutput(result);
return return_value_uint32.AsValue(result, false);
}
catch (Exception e)
Expand All @@ -41,7 +41,7 @@ public static unsafe partial class Bindings

var result = Fetcher.GetVersion(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutput(result);
Logger.LogOutput(result, nameof(GetVersion));
return return_value_string.AsValue(Utils.Copy(result, false), false);
}
catch (Exception e)
Expand All @@ -62,7 +62,7 @@ public static unsafe partial class Bindings

var result = (uint) Fetcher.GetVersionType(Path.GetFullPath(gameFolderPath), libAssembly);

Logger.LogOutputPrimitive(result);
Logger.LogOutput(result);
return return_value_uint32.AsValue(result, false);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<IsPackable>false</IsPackable>

<DefineConstants>$(DefineConstants);TRACK_ALLOCATIONS;</DefineConstants>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -17,7 +14,11 @@
<DebugType>none</DebugType>
<NativeLib>Shared</NativeLib>
</PropertyGroup>
<PropertyGroup>
<PropertyGroup Condition="$(Configuration) == 'Debug'">
<DefineConstants>$(DefineConstants);TRACK_ALLOCATIONS;LOGGING;</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration) == 'Release'">
<DefineConstants>$(DefineConstants);TRACK_ALLOCATIONS;</DefineConstants>
<DebuggerSupport>false</DebuggerSupport>
<AutoreleasePoolSupport>false</AutoreleasePoolSupport>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
Expand All @@ -42,7 +43,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.36" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.46" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="ConcurrentHashSet" Version="1.3.0" />
</ItemGroup>

Expand Down
94 changes: 64 additions & 30 deletions src/FetchBannerlordVersion.Native/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace FetchBannerlordVersion.Native
{
Expand All @@ -13,95 +16,126 @@ public static class Logger
[Conditional("LOGGING")]
public static void LogInput([CallerMemberName] string? caller = null)
{
Log($"Received call to {caller}!");
Log($"{caller} - Starting");
}
[Conditional("LOGGING")]
public static void LogInput(nuint param1, [CallerMemberName] string? caller = null)
public static void LogInput(string param, [CallerMemberName] string? caller = null)
{
Log($"Received call to {caller}! {param1}");
Log($"{caller} - Starting: {param}");
}
[Conditional("LOGGING")]
public static void LogInput<T1>(T1 param1, [CallerMemberName] string? caller = null)
where T1 : IFormattable
{
Log($"{caller} - Starting: {param1.ToString(null, CultureInfo.InvariantCulture)}");
}
[Conditional("LOGGING")]
public static unsafe void LogInput<T1>(T1* param1, [CallerMemberName] string? caller = null)
where T1 : unmanaged, IParameterWithSpan<T1>
where T1 : unmanaged, IParameterSpanFormattable<T1>
{
Log($"Received call to {caller}! {T1.ToSpan(param1)}");
Log($"{caller} - Starting: {T1.ToSpan(param1)}");
}
[Conditional("LOGGING")]
public static unsafe void LogInput<T1, T2>(T1* param1, T2* param2, [CallerMemberName] string? caller = null)
where T1 : unmanaged, IParameterWithSpan<T1>
where T2 : unmanaged, IParameterWithSpan<T2>
where T1 : unmanaged, IParameterSpanFormattable<T1>
where T2 : unmanaged, IParameterSpanFormattable<T2>
{
Log($"Received call to {caller}! {T1.ToSpan(param1)}; {T2.ToSpan(param2)}");
Log($"{caller} - Starting: {T1.ToSpan(param1)}; {T2.ToSpan(param2)}");
}
[Conditional("LOGGING")]
public static unsafe void LogInput<T1, T2, T3>(T1* param1, T2* param2, T3* param3, [CallerMemberName] string? caller = null)
where T1 : unmanaged, IParameterWithSpan<T1>
where T2 : unmanaged, IParameterWithSpan<T2>
where T3 : unmanaged, IParameterWithSpan<T3>
where T1 : unmanaged, IParameterSpanFormattable<T1>
where T2 : unmanaged, IParameterSpanFormattable<T2>
where T3 : unmanaged, IParameterSpanFormattable<T3>
{
Log($"Received call to {caller}! {T1.ToSpan(param1)}; {T2.ToSpan(param2)}; {T3.ToSpan(param3)}");
Log($"{caller} - Starting: {T1.ToSpan(param1)}; {T2.ToSpan(param2)}; {T3.ToSpan(param3)}");
}
[Conditional("LOGGING")]
public static unsafe void LogInput<T1, T2, T3, T4>(T1* param1, T2* param2, T3* param3, T4* param4, [CallerMemberName] string? caller = null)
where T1 : unmanaged, IParameterWithSpan<T1>
where T2 : unmanaged, IParameterWithSpan<T2>
where T3 : unmanaged, IParameterWithSpan<T3>
where T4 : unmanaged, IParameterWithSpan<T4>
where T1 : unmanaged, IParameterSpanFormattable<T1>
where T2 : unmanaged, IParameterSpanFormattable<T2>
where T3 : unmanaged, IParameterSpanFormattable<T3>
where T4 : unmanaged, IParameterSpanFormattable<T4>
{
Log($"Received call to {caller}! {T1.ToSpan(param1)}; {T2.ToSpan(param2)}; {T3.ToSpan(param3)}; {T4.ToSpan(param4)}");
Log($"{caller} - Starting: {T1.ToSpan(param1)}; {T2.ToSpan(param2)}; {T3.ToSpan(param3)}; {T4.ToSpan(param4)}");
}

[Conditional("LOGGING")]
public static unsafe void LogInputChar(char* param1, [CallerMemberName] string? caller = null)
public static unsafe void LogPinned(char* param1, [CallerMemberName] string? caller = null)
{
var p1 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param1);
Log($"Received call to {caller}! {p1}");
Log($"{caller} - Pinned: {p1}");
}
[Conditional("LOGGING")]
public static unsafe void LogInputChar(char* param1, char* param2, [CallerMemberName] string? caller = null)
public static unsafe void LogPinned(char* param1, char* param2, [CallerMemberName] string? caller = null)
{
var p1 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param1);
var p2 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param2);
Log($"Received call to {caller}! {p1}; {p2}");
Log($"{caller} - Pinned: {p1}; {p2}");
}
[Conditional("LOGGING")]
public static unsafe void LogInputChar(char* param1, char* param2, char* param3, [CallerMemberName] string? caller = null)
public static unsafe void LogPinned(char* param1, char* param2, char* param3, [CallerMemberName] string? caller = null)
{
var p1 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param1);
var p2 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param2);
var p3 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param3);
Log($"Received call to {caller}! {p1}; {p2}; {p3}");
Log($"{caller} - Pinned: {p1}; {p2}; {p3}");
}
[Conditional("LOGGING")]
public static unsafe void LogInputChar(char* param1, char* param2, char* param3, char* param4, [CallerMemberName] string? caller = null)
public static unsafe void LogPinned(char* param1, char* param2, char* param3, char* param4, [CallerMemberName] string? caller = null)
{
var p1 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param1);
var p2 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param2);
var p3 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param3);
var p4 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(param4);
Log($"Received call to {caller}! {p1}; {p2}; {p3}; {p4}");
Log($"{caller} - Pinned: {p1}; {p2}; {p3}; {p4}");
}

[Conditional("LOGGING")]
public static void LogOutput([CallerMemberName] string? caller = null)
{
Log($"Result of {caller}");
Log($"{caller} - Finished");
}
[Conditional("LOGGING")]
public static void LogOutput(string param, [CallerMemberName] string? caller = null)
{
Log($"{caller} - Finished: {param}");
}
[Conditional("LOGGING")]
public static void LogOutputPrimitive<TResult>(TResult result, [CallerMemberName] string? caller = null) where TResult : unmanaged
public static void LogOutput<T1>(T1 result, [CallerMemberName] string? caller = null)
{
Log($"Result of {caller}: {result}");
//if (Bindings.CustomSourceGenerationContext.GetTypeInfo(typeof(T1)) is JsonTypeInfo<T1> jsonTypeInfo)
// Log($"{caller} - Finished: JSON - {JsonSerializer.Serialize(result, jsonTypeInfo)}");
//else
Log($"{caller} - Finished: RAW - {result?.ToString()}");
}
[Conditional("LOGGING")]
public static void LogOutput(bool result, [CallerMemberName] string? caller = null)
{
Log($"{caller} - Finished: {result}");
}

[Conditional("LOGGING")]
public static void LogException(Exception e, [CallerMemberName] string? caller = null)
{
Log($"Exception of {caller}: {e}");
Log($"{caller} - Exception: {e}");
}

private static readonly ReaderWriterLockSlim _lock = new();
private static void Log(string message)
{
File.AppendAllText("Bannerlord.VortexExtension.Native.log", $"{message}{Environment.NewLine}");
_lock.EnterWriteLock();

try
{
using var fs = new FileStream("FetchBannerlordVersion.Native.log", FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan);
using var sw = new StreamWriter(fs, Encoding.UTF8, -1, true);
sw.WriteLine(message);
}
finally
{
_lock.ExitWriteLock();
}
}
}
}
12 changes: 12 additions & 0 deletions test/FetchBannerlordVersion.Native.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using static FetchBannerlordVersion.Native.Tests.Utils2;

namespace FetchBannerlordVersion.Native.Tests
{
public class BaseTests
{
public BaseTests()
{
LibrarySetAllocator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.36" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.46" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
Expand Down
3 changes: 0 additions & 3 deletions test/FetchBannerlordVersion.Native.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace FetchBannerlordVersion.Native.Tests
{
public partial class Tests
{
private const string DllPath = "../../../../../src/FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/native/FetchBannerlordVersion.Native.dll";


[LibraryImport(DllPath), UnmanagedCallConv(CallConvs = new[] { typeof(CallConvStdcall) })]
private static unsafe partial return_value_uint32* bfv_get_change_set(param_string* p_game_folder_path, param_string* p_lib_assembly);

Expand Down
5 changes: 3 additions & 2 deletions test/FetchBannerlordVersion.Native.Tests/Utils2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace FetchBannerlordVersion.Native.Tests
{
public static partial class Utils2
internal static partial class Utils2
{
private const string DllPath = "../../../../../src/FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/native/FetchBannerlordVersion.Native.dll";
public const string DllPath = "../../../../../src/FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/native/FetchBannerlordVersion.Native.dll";


static unsafe Utils2()
Expand All @@ -22,6 +22,7 @@ static unsafe Utils2()
[LibraryImport(DllPath), UnmanagedCallConv(CallConvs = new[] { typeof(CallConvStdcall) })]
private static unsafe partial int alloc_alive_count();

public static unsafe void LibrarySetAllocator() => Allocator.SetCustom(&alloc, &dealloc);
public static int LibraryAliveCount() => alloc_alive_count();

public static unsafe ReadOnlySpan<char> ToSpan(param_string* value) => new SafeStringMallocHandle((char*) value, false).ToSpan();
Expand Down

0 comments on commit 2f3791f

Please sign in to comment.