-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemory.cs
101 lines (84 loc) · 3.06 KB
/
Memory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
namespace SkyboxChanger;
class MemoryManager
{
private static IntPtr pSpawnGroupMgrGameSystemReallocatingFactory = 0;
private static T PlatformExecute<T>(Func<T> OnWindows, Func<T> OnLinux)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OnWindows();
}
else
{
return OnLinux();
}
}
private static void PlatformExecute(Action? OnWindows = null, Action? OnLinux = null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (OnWindows != null) OnWindows();
}
else
{
if (OnLinux != null) OnLinux();
}
}
public static void Load()
{
}
public static void Unload()
{
}
public static void CreateLoadingSpawnGroupAndSpawnEntities(uint spawnGroupHandle, bool bSynchronouslySpawnEntities, bool bConfirmResourcesLoaded, IntPtr pEntityKeyValues)
{
var pSpawnGroupMgrGameSystem = FindSpawnGroupMgrGameSystem();
var CreateLoadingSpawnGroup = VirtualFunction.Create<nint, uint, byte, byte, nint, nint>(pSpawnGroupMgrGameSystem, 3);
var pLoadingSpawnGroup = CreateLoadingSpawnGroup.Invoke(
pSpawnGroupMgrGameSystem,
spawnGroupHandle,
Convert.ToByte(bSynchronouslySpawnEntities),
Convert.ToByte(bConfirmResourcesLoaded),
pEntityKeyValues);
var SpawnEntities = VirtualFunction.CreateVoid<nint>(pLoadingSpawnGroup, 10);
SpawnEntities.Invoke(pLoadingSpawnGroup);
}
public static void RemoveCachedFactory()
{
pSpawnGroupMgrGameSystemReallocatingFactory = 0;
}
private static unsafe IntPtr FindSpawnGroupMgrGameSystem()
{
if (pSpawnGroupMgrGameSystemReallocatingFactory != 0)
{
return **(nint**)(pSpawnGroupMgrGameSystemReallocatingFactory + 3 * 8);
}
var pFirstSig = GameData.GetSignature("IGameSystem_InitAllSystems_pFirst");
var server = Path.Join(Server.GameDirectory, Constants.GameBinaryPath, Constants.ModulePrefix + "server" + Constants.ModuleSuffix);
var pFirstOpcodeAddr = NativeAPI.FindSignature(server, pFirstSig) + 3;
var offset = *(uint*)pFirstOpcodeAddr;
// CBaseGameSystemFactory**
var pFirst = pFirstOpcodeAddr + 4 + offset;
// CBaseGameSystemFactory*
var pFactoryList = *(nint*)pFirst;
while (pFactoryList != 0 && pFactoryList != 0xFFFFFFFF && pFactoryList != -1L)
{
var pName = *(nint*)(pFactoryList + 0x10);
string? name = Marshal.PtrToStringAnsi(pName);
if (name == "SpawnGroupManagerGameSystem")
{
pSpawnGroupMgrGameSystemReallocatingFactory = pFactoryList;
return **(nint**)(pSpawnGroupMgrGameSystemReallocatingFactory + 3 * 8);
}
var pNext = *(nint*)(pFactoryList + 0x8);
pFactoryList = pNext;
}
throw new Exception("FAILED TO FIND SpawnGroupManagerGameSystem. The game may have been updated, please report it to the author.");
}
}