1- #nullable enable
21using System ;
32using System . Runtime . InteropServices ;
4- using CESDK . Core ;
53using CESDK . Lua ;
4+ using System . IO ;
65
76namespace 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
0 commit comments