Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Internal Calls #14

Merged
merged 15 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions Examples/ExperimentalTesting.Native/ExperimentalTesting.Native.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{e3ba4f2c-e79f-486e-8051-0835d8057c7d}</ProjectGuid>
<RootNamespace>ExperimentalTestingNative</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)Examples\ExperimentalTesting\bin\$(Configuration)\net8.0\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)Examples\ExperimentalTesting\bin\$(Configuration)\net8.0\</OutDir>
</PropertyGroup>
<PropertyGroup Label="Vcpkg">
<VcpkgEnabled>false</VcpkgEnabled>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SPL\InternalCall.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions Examples/ExperimentalTesting.Native/SPL/InternalCall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#ifndef SPL_INTERNAL_CALL
#define SPL_INTERNAL_CALL extern "C" __declspec(dllexport)
#endif

namespace SharpPluginLoader::Native {

struct InternalCall {
const char* Name;
void* Function;
};

}
36 changes: 36 additions & 0 deletions Examples/ExperimentalTesting.Native/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <corecrt_wstdio.h>
#include <cstdint>
#include <Windows.h>

#include "SPL/InternalCall.h"

namespace SPLNative = SharpPluginLoader::Native;

int64_t sum(int64_t a, int64_t b) {
return a + b;
}

void modify_value(int* ptr) {
*ptr *= 2;
}

void log_message(int level, const wchar_t* message) {
wchar_t buf[1024];
(void)swprintf_s(buf, L"LogLevel: %d", level);
MessageBoxW(nullptr, message, buf, MB_OK);
}

SPL_INTERNAL_CALL int get_internal_call_count() {
return 3;
}

SPL_INTERNAL_CALL void collect_internal_calls(SPLNative::InternalCall* icalls) {
icalls[0] = { "Sum", (void*)sum };
icalls[1] = { "ModifyValue", (void*)modify_value };
icalls[2] = { "LogMessage", (void*)log_message };
}

BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) {
return TRUE;
}

5 changes: 5 additions & 0 deletions Examples/ExperimentalTesting/ExperimentalTesting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\SharpPluginLoader.Core\SharpPluginLoader.Core.csproj">
<Private>False</Private>
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
<ProjectReference Include="..\..\SharpPluginLoader.InternalCallGenerator\SharpPluginLoader.InternalCallGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions Examples/ExperimentalTesting/InternalCalls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using SharpPluginLoader.InternalCallGenerator;
using SharpPluginLoader.Core.MtTypes;
using SharpPluginLoader.Core;

namespace ExperimentalTesting;

public struct HasRefTypes
{
public string str;
public MtVector2 vec;
}

[InternalCallManager]
public partial class InternalCalls
{
// Test with simple primitive types
[InternalCall(InternalCallOptions.Unsafe)]
public static partial long Sum(long a, long b);

// Test with single pointer type
[InternalCall]
public static unsafe partial void TestPointers(double* values);

// Test with mixed unmanaged types
[InternalCall]
public static unsafe partial char ProcessData(byte* data, int length, ref ushort dataId);

// Test with array and pointer types
[InternalCall]
public static unsafe partial void TransformCoordinates(float[] coords, MtVector3* transformMatrix);

// Test with InternalCallOptions and ref parameter
[InternalCall(InternalCallOptions.Unsafe)]
public static partial void ModifyValue(ref int value);

// Test with no return value and primitive parameters
[InternalCall]
public static unsafe partial void LogMessage(int level, [WideString] string message);

// Test with complex struct return type and primitive parameters
[InternalCall]
public static partial MtMatrix4X4 ComputeStruct(int param1, float param2);

// Test with pointer to struct and primitive types
[InternalCall]
public static unsafe partial MtVector2* CreateVector(int x, int y);

// Test with multiple ref and out parameters
[InternalCall]
public static partial void TestRefOutParameters(ref float a, out double b, ref MtVector2 vec);

// Test with mixed array, pointer, and primitive types
[InternalCall]
public static unsafe partial void ProcessBuffers(byte[] inputBuffer, float* outputBuffer, int bufferSize);

// Test with strings
[InternalCall]
public static partial MtVector2 Parse(string str);

[InternalCall]
[return: WideString]
public static partial string Format(MtVector2 vec, [WideString] string str);

[InternalCall]
public static partial void EnumTest(PropType type);

[InternalCall]
public static partial void StructTest(HasRefTypes type);

[InternalCall]
public static partial void SpanTest(Span<byte> span);

[InternalCall]
public static partial void ReadOnlySpanTest(ReadOnlySpan<byte> span);

[InternalCall]
public static partial void MemoryTest(Memory<byte> memory);

[InternalCall]
public static partial void ReadOnlyMemoryTest(ReadOnlyMemory<byte> memory);

[InternalCall]
public static partial void ListTest(List<byte> list);
}
Loading
Loading