Skip to content

Commit

Permalink
项目模板
Browse files Browse the repository at this point in the history
  • Loading branch information
CeSun committed Oct 2, 2024
1 parent b6aafc5 commit 25c983f
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Template/Metahook.Plugin/Exportfuncs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using GoldSrc.HLSDK.Native;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static Plugin.Global;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Plugin;
public unsafe static class Exportfuncs
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
public static int Initialize(cl_enginefunc_t *pEnginefuncs, int iVersion)
{
var s = sizeof(cl_enginefunc_t);

gEngfuncs = *pEnginefuncs;
return gExportfuncs.Initialize(pEnginefuncs, iVersion);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
public static void HUD_Init()
{
gExportfuncs.HUD_Init();
}
static int count = 0;
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
public static int HUD_Redraw(float time, int intermission)
{
if (count < 10)
{
using var str = $"Hello World!!!!!(hit:{count + 1})\n".GetNativeString();
gEngfuncs.Con_Printf(str);
count++;
}
return gExportfuncs.HUD_Redraw(time, intermission);
}
}


public static class StringHelper
{
public unsafe static NativeString GetNativeString(this string s) => new NativeString() { c_str = (sbyte*)Marshal.StringToHGlobalAnsi(s) };

}

public unsafe struct NativeString : IDisposable
{
internal sbyte* c_str;
public void Dispose() => Marshal.FreeHGlobal((nint)c_str);
public static implicit operator sbyte*(NativeString d) => d.c_str;
public static implicit operator byte*(NativeString d) => (byte*)d.c_str;
public static implicit operator nint(NativeString d) => (nint)d.c_str;
}
13 changes: 13 additions & 0 deletions Template/Metahook.Plugin/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GoldSrc.HLSDK.Native;
using GoldSrc.Metahook.Native;

namespace Plugin;

public unsafe static class Global
{
public static cl_exportfuncs_t gExportfuncs;
public static mh_interface_t* g_pInterface;
public static metahook_api_t* g_pMetaHookAPI;
public static mh_enginesave_t* g_pMetaSave;
public static cl_enginefunc_t gEngfuncs;
}
15 changes: 15 additions & 0 deletions Template/Metahook.Plugin/Metahook.Plugin.Template.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Metahook.Plugin.Template</id>
<version>1.0.0-alpha</version>
<description>Metahook Plugin</description>
<authors>CeSun</authors>
<packageTypes>
<packageType name="Metahook.Plugin.Template" />
</packageTypes>
</metadata>
<files>
<file src=".\**" target="Metahook.Plugin.Template" exclude="**\bin\**;**\obj\**;.\.vs\**;" />
</files>
</package>
17 changes: 17 additions & 0 deletions Template/Metahook.Plugin/PluginReplaceName.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Plugin</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metahook.Net" Version="1.0.0-alpha" />
</ItemGroup>


</Project>
26 changes: 26 additions & 0 deletions Template/Metahook.Plugin/PluginV1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using GoldSrc.HLSDK.Native;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Plugin;

public class PluginV1
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Init(nint self, cl_exportfuncs_t* pSave)
{

}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Shutdown(nint self, int restart)
{

}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Destructor(nint self)
{

}
}
55 changes: 55 additions & 0 deletions Template/Metahook.Plugin/PluginV2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using GoldSrc.HLSDK.Native;
using GoldSrc.Metahook.Native;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static Plugin.Global;

namespace Plugin;

public unsafe static class PluginV2
{

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Init(nint self, metahook_api_t* pAPI, mh_interface_t* pInterface, mh_enginesave_t* pSave)
{
g_pInterface = pInterface;
g_pMetaHookAPI = pAPI;
g_pMetaSave = pSave;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void LoadClient(nint self, cl_exportfuncs_t* pExportFunc)
{
gExportfuncs = *pExportFunc;

pExportFunc->Initialize = &Exportfuncs.Initialize;
pExportFunc->HUD_Init = &Exportfuncs.HUD_Init;
pExportFunc->HUD_Redraw = &Exportfuncs.HUD_Redraw;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void LoadEngine(nint self)
{

}


[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void ExitGame(nint self, int iResult)
{

}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Shutdown(nint self)
{

}


[UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])]
public static unsafe void Destructor(nint self)
{

}
}
75 changes: 75 additions & 0 deletions Template/Metahook.Plugin/Register.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using GoldSrc.Metahook.Native;
using GoldSrc.Metahook;
using GoldSrc.HLSDK.Native;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Plugin;

public static class Register
{

[UnmanagedCallersOnly(EntryPoint = "CreateInterface", CallConvs = [typeof(CallConvCdecl)])]
public static unsafe nint CreateInterface(nint ptr, int* num)
{
string? frameworkVersion = Marshal.PtrToStringAnsi(ptr);
if (frameworkVersion == null)
return IntPtr.Zero;
nint pPlugin = IntPtr.Zero;
int result = 1;
switch (frameworkVersion)
{
case PluginConst.METAHOOK_PLUGIN_API_VERSION_V2:
(pPlugin, result) = GetV2PluginInstance(frameworkVersion);
break;
case PluginConst.METAHOOK_PLUGIN_API_VERSION_V1:
(pPlugin, result) = GetV1PluginInstance(frameworkVersion);
break;
}
return pPlugin;
}


public static unsafe (nint, int) GetV1PluginInstance(string version)
{
var ptr = Marshal.AllocHGlobal(sizeof(PluginValueType));
ref var plugin = ref Unsafe.AsRef<PluginValueType>((void*)ptr);
var functionTable = Marshal.AllocHGlobal(sizeof(nint) * 3);
Span<nint> functions = new Span<nint>((void*)functionTable, 3);
functions[0] = (nint)(delegate* unmanaged[Thiscall]<nint, void>)&PluginV1.Destructor;
functions[1] = (nint)(delegate* unmanaged[Thiscall]<nint, cl_exportfuncs_t*, void>)&PluginV1.Init;
functions[2] = (nint)(delegate* unmanaged[Thiscall]<nint, int, void>)&PluginV1.Shutdown;
plugin = new PluginValueType
{
VirtualFunctionTable = functionTable
};
return (ptr, 0);
}


public static unsafe (nint, int) GetV2PluginInstance(string version)
{
var ptr = Marshal.AllocHGlobal(sizeof(PluginValueType));
ref var plugin = ref Unsafe.AsRef<PluginValueType>((void*)ptr);
var functionTable = Marshal.AllocHGlobal(sizeof(nint) * 6);
Span<nint> functions = new Span<nint>((void*)functionTable, 6);
functions[0] = (nint)(delegate* unmanaged[Thiscall]<nint, void>)&PluginV2.Destructor;
functions[1] = (nint)(delegate* unmanaged[Thiscall]<nint, metahook_api_t*, mh_interface_t*, mh_enginesave_t*, void>)&PluginV2.Init;
functions[2] = (nint)(delegate* unmanaged[Thiscall]<nint, void>)&PluginV2.Shutdown;
functions[3] = (nint)(delegate* unmanaged[Thiscall]<nint, void>)&PluginV2.LoadEngine;
functions[4] = (nint)(delegate* unmanaged[Thiscall]<nint, cl_exportfuncs_t*, void>)&PluginV2.LoadClient;
functions[5] = (nint)(delegate* unmanaged[Thiscall]<nint, int, void>)&PluginV2.ExitGame;
plugin = new PluginValueType
{
VirtualFunctionTable = functionTable
};
return (ptr, 0);
}

struct PluginValueType
{
public nint VirtualFunctionTable;
}
}


0 comments on commit 25c983f

Please sign in to comment.