-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|