Skip to content

Commit 287c780

Browse files
committed
Fix SDK to load
1 parent 61772d7 commit 287c780

15 files changed

Lines changed: 198 additions & 168 deletions

src/CESDK.cs

Lines changed: 93 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
1-
#nullable enable
21
using System;
32
using System.Runtime.InteropServices;
4-
using CESDK.Core;
53
using CESDK.Lua;
4+
using System.IO;
65

76
namespace CESDK
87
{
9-
/// <summary>
10-
/// Main CESDK class - provides the entry point for Cheat Engine
11-
/// </summary>
8+
[StructLayout(LayoutKind.Sequential)]
9+
public struct TExportedFunctions
10+
{
11+
public int sizeofExportedFunctions;
12+
public IntPtr GetLuaState;
13+
public IntPtr LuaRegister;
14+
public IntPtr LuaPushClassInstance;
15+
public IntPtr ProcessMessages;
16+
public IntPtr CheckSynchronize;
17+
}
18+
1219
public class CESDK
1320
{
14-
// Private backing field for current plugin
21+
private const int PLUGIN_VERSION = 6;
22+
private static CESDK? mainSelf;
1523
private static CheatEnginePlugin? _currentPlugin;
16-
17-
/// <summary>
18-
/// Public read-only property exposing the current plugin.
19-
/// </summary>
2024
public static CheatEnginePlugin? CurrentPlugin => _currentPlugin;
2125

22-
// Private field holding the shared Lua state
23-
private LuaNative _lua = PluginContext.Lua;
24-
25-
/// <summary>
26-
/// Public property exposing the shared Lua state.
27-
/// </summary>
28-
public LuaNative Lua => _lua;
26+
private UInt32 pluginId;
27+
private TExportedFunctions pluginExports;
2928

30-
private const int PLUGIN_VERSION = 6;
31-
private static CESDK? mainself;
3229
private static IntPtr PluginNamePtr;
3330

31+
#region Delegates
32+
3433
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
35-
private delegate void DelegateProcessMessages();
34+
private delegate bool delegateGetVersion(ref TPluginVersion PluginVersion, int TPluginVersionSize);
3635

3736
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
38-
private delegate bool DelegateCheckSynchronize(int timeout);
37+
private delegate bool delegateEnablePlugin(ref TExportedFunctions ExportedFunctions, UInt32 pluginid);
38+
39+
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
40+
private delegate bool delegateDisablePlugin();
41+
42+
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
43+
private delegate void delegateProcessMessages();
44+
45+
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
46+
private delegate bool delegateCheckSynchronize(int timeout);
47+
48+
private delegateGetVersion? delGetVersion;
49+
private delegateEnablePlugin? delEnablePlugin;
50+
private delegateDisablePlugin? delDisablePlugin;
51+
private delegateProcessMessages? delProcessMessages;
52+
private delegateCheckSynchronize? delCheckSynchronize;
53+
54+
#endregion
3955

40-
private readonly DelegateGetVersion delGetVersion;
41-
private readonly DelegateEnablePlugin delEnablePlugin;
42-
private readonly DelegateDisablePlugin delDisablePlugin;
43-
private static DelegateProcessMessages? delProcessMessages;
44-
private static DelegateCheckSynchronize? delCheckSynchronize;
56+
#region Internal Structures
57+
58+
[StructLayout(LayoutKind.Sequential)]
59+
private struct TPluginVersion
60+
{
61+
public UInt32 version;
62+
public IntPtr name;
63+
}
64+
65+
[StructLayout(LayoutKind.Sequential)]
66+
private struct TPluginInit
67+
{
68+
public IntPtr name;
69+
public IntPtr GetVersion;
70+
public IntPtr EnablePlugin;
71+
public IntPtr DisablePlugin;
72+
public int version;
73+
}
74+
75+
#endregion
4576

4677
private CESDK()
4778
{
@@ -50,33 +81,39 @@ private CESDK()
5081
delDisablePlugin = DisablePlugin;
5182
}
5283

53-
private static bool GetVersion(ref TPluginVersion pluginVersion, int TPluginVersionSize)
84+
#region Delegate Implementations
85+
86+
private static bool GetVersion(ref TPluginVersion PluginVersion, int TPluginVersionSize)
5487
{
55-
pluginVersion.name = PluginNamePtr;
56-
pluginVersion.version = PLUGIN_VERSION;
88+
PluginVersion.name = PluginNamePtr;
89+
PluginVersion.version = PLUGIN_VERSION;
5790
return true;
5891
}
5992

60-
private static bool EnablePlugin(ref TExportedFunctions exportedFunctions, uint pluginid)
93+
private static bool EnablePlugin(ref TExportedFunctions ExportedFunctions, UInt32 pluginid)
6194
{
6295
try
6396
{
64-
if (mainself == null) return false;
97+
if (mainSelf == null || CurrentPlugin == null)
98+
return false;
6599

100+
mainSelf.pluginId = pluginid;
101+
mainSelf.pluginExports = ExportedFunctions;
66102

103+
mainSelf.delProcessMessages ??= Marshal.GetDelegateForFunctionPointer<delegateProcessMessages>(mainSelf.pluginExports.ProcessMessages);
104+
mainSelf.delCheckSynchronize ??= Marshal.GetDelegateForFunctionPointer<delegateCheckSynchronize>(mainSelf.pluginExports.CheckSynchronize);
67105

68-
// Setup delegates for CE functions
69-
delProcessMessages ??= Marshal.GetDelegateForFunctionPointer<DelegateProcessMessages>(exportedFunctions.ProcessMessages);
70-
delCheckSynchronize ??= Marshal.GetDelegateForFunctionPointer<DelegateCheckSynchronize>(exportedFunctions.CheckSynchronize);
106+
// Initialize Lua pointer for CE
107+
PluginContext.Initialize(ExportedFunctions.GetLuaState);
71108

72-
// Use the shared Lua state from PluginContext
73-
mainself._lua ??= PluginContext.Lua;
109+
// Call the plugin enable hook
110+
CurrentPlugin.EnablePlugin();
74111

75-
_currentPlugin?.InternalOnEnable();
76-
return true;
112+
return true; // Must return true to CE
77113
}
78-
catch
114+
catch (Exception ex)
79115
{
116+
PluginLogger.LogException(ex);
80117
return false;
81118
}
82119
}
@@ -85,44 +122,45 @@ private static bool DisablePlugin()
85122
{
86123
try
87124
{
88-
_currentPlugin?.InternalOnDisable();
125+
CurrentPlugin?.DisablePlugin();
89126
return true;
90127
}
91128
catch
92129
{
93130
return false;
94131
}
132+
95133
}
96134

135+
#endregion
136+
137+
#region Public Helpers
138+
97139
public static void ProcessMessages()
98140
{
99-
delProcessMessages?.Invoke();
141+
mainSelf?.delProcessMessages?.Invoke();
100142
}
101143

102144
public static bool CheckSynchronize(int timeout)
103145
{
104-
return delCheckSynchronize?.Invoke(timeout) ?? false;
146+
return mainSelf?.delCheckSynchronize?.Invoke(timeout) ?? false;
105147
}
106148

107-
/// <summary>
108-
/// Entry point called by Cheat Engine to initialize the plugin
109-
/// </summary>
149+
#endregion
150+
110151
public static int CEPluginInitialize(string parameters)
111152
{
112153
try
113154
{
114-
mainself ??= new CESDK();
155+
mainSelf ??= new CESDK();
115156

116157
if (PluginNamePtr == IntPtr.Zero)
117158
{
118-
// Search for plugin using legacy pattern
119-
var types = typeof(CheatEnginePlugin).Assembly.GetTypes();
120-
121-
for (int i = 0; i < types.Length; i++)
159+
foreach (var type in typeof(CheatEnginePlugin).Assembly.GetTypes())
122160
{
123-
if (types[i].IsSubclassOf(typeof(CheatEnginePlugin)) && !types[i].IsAbstract)
161+
if (type.IsSubclassOf(typeof(CheatEnginePlugin)) && !type.IsAbstract)
124162
{
125-
_currentPlugin = (CheatEnginePlugin)Activator.CreateInstance(types[i]);
163+
_currentPlugin = (CheatEnginePlugin)Activator.CreateInstance(type)!;
126164
break;
127165
}
128166
}
@@ -137,9 +175,9 @@ public static int CEPluginInitialize(string parameters)
137175
var pluginInit = new TPluginInit
138176
{
139177
name = PluginNamePtr,
140-
GetVersion = Marshal.GetFunctionPointerForDelegate(mainself.delGetVersion),
141-
EnablePlugin = Marshal.GetFunctionPointerForDelegate(mainself.delEnablePlugin),
142-
DisablePlugin = Marshal.GetFunctionPointerForDelegate(mainself.delDisablePlugin),
178+
GetVersion = Marshal.GetFunctionPointerForDelegate(mainSelf.delGetVersion),
179+
EnablePlugin = Marshal.GetFunctionPointerForDelegate(mainSelf.delEnablePlugin),
180+
DisablePlugin = Marshal.GetFunctionPointerForDelegate(mainSelf.delDisablePlugin),
143181
version = PLUGIN_VERSION
144182
};
145183

src/CheatEnginePlugin.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using CESDK.Lua;
32

43
namespace CESDK
@@ -25,19 +24,9 @@ protected virtual void OnDisable() { }
2524

2625
#region Internal Plugin Interface
2726

28-
internal void InternalOnEnable() => OnEnable();
29-
internal void InternalOnDisable() => OnDisable();
27+
internal void EnablePlugin() => OnEnable();
28+
internal void DisablePlugin() => OnDisable();
3029

3130
#endregion
32-
33-
/// <summary>
34-
/// Processes pending Windows messages to keep CE responsive.
35-
/// </summary>
36-
protected static void ProcessMessages() => CESDK.ProcessMessages();
37-
38-
/// <summary>
39-
/// Checks for synchronization events with a timeout.
40-
/// </summary>
41-
protected static bool CheckSynchronize(int timeout) => CESDK.CheckSynchronize(timeout);
4231
}
4332
}

src/Classes/AOBScanner.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.Globalization;
@@ -39,7 +38,7 @@ private static void CallAOBScanFunction(string pattern, string? protectionFlags,
3938
}
4039

4140
var paramCount = PushScanParameters(pattern, protectionFlags, alignmentType, alignmentParam);
42-
41+
4342
var result = lua.PCall(paramCount, 1);
4443
if (result != 0)
4544
{
@@ -78,7 +77,7 @@ private static int PushScanParameters(string pattern, string? protectionFlags, i
7877
private static List<ulong> ProcessScanResults()
7978
{
8079
var addresses = new List<ulong>();
81-
80+
8281
if (!lua.IsUserData(-1))
8382
{
8483
lua.Pop(1);
@@ -108,7 +107,7 @@ private static void ProcessSingleResult(List<ulong> addresses, int index)
108107
{
109108
lua.PushInteger(index);
110109
lua.GetTable(-2);
111-
110+
112111
if (lua.IsString(-1))
113112
{
114113
var addressStr = lua.ToString(-1);

src/Classes/AddressResolver.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Globalization;
43
using CESDK.Lua;

src/Classes/Converter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using CESDK.Lua;
43

src/Classes/Debugger.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using CESDK.Lua;
43

src/Classes/Disassembler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Globalization;
43
using CESDK.Lua;

src/Classes/LuaLogger.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Globalization;
43
using CESDK.Lua;

src/Classes/MemoryAccess.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using CESDK.Lua;

src/Classes/Process.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using CESDK.Lua;
43

0 commit comments

Comments
 (0)