· 6 years ago · Oct 02, 2019, 07:16 PM
1#pragma once
2#include <WTypes.h>
3
4/*
5 * This is a header file for SmartGuard Plugins Interface.
6 * It defines basic API functions that can be used in your plugin to ease your work and ensure compatibility with SmartGuard.
7 *
8 * List of basic rules and guidelines when designing a plugin:
9 * 1. Do not create threads. For security reasons it is highly recommended to avoid creating threads in your DLL.
10 * 2. Avoid calling functions from [CPluginInterface] from withing virtualized/mutated code. This can lead to undefined behavior.
11 * 3. Some versions of Lineage 2 game client do not support common SSE2/MMX instruction sets.
12 * It is highly advised to instruct your compiler to avoid generating such code for compatibility reasons.
13 * 4. It is strictly forbidden to directly manipulate game code/memory. Contact SmartGuard support in case of doubts.
14 * Use existing API functions or request API enhancements when needed.
15 * 5. Using [OnMouseEvent] and [OnKeyboardEvent] callbacks avoid "heavy" code, this will degrade I/O operations.
16 * 6. Using [TOnNetServerPacket] and [TOnNetClientPacket] callbacks avoid modifying packed data or size.
17 * 7. Use [TOnAllPluginsLoaded] callback to establish communication with other plugins to be sure that all available plugins are loaded.
18 */
19
20#define SMART_PLUGIN_API_VER 7
21
22//------------------------------------------------------------------------------------------------
23// Plugin Callbacks
24//------------------------------------------------------------------------------------------------
25
26#pragma pack(push, 4)
27struct CNetworkPacket
28{
29 BYTE Id;
30 WORD SubId;
31 LPVOID Data;
32 USHORT Size;
33};
34#pragma pack(pop)
35
36typedef void (WINAPI * TVoid)(void);
37typedef TVoid TOnLoad;
38typedef TVoid TOnUnLoad;
39typedef TVoid TOnAllPluginsLoaded;
40typedef void (WINAPI * TOnClientInit)(LPVOID pGameEngine, LPVOID pNetworkHandler);
41typedef BOOL (WINAPI * TOnNetServerPacket)(const CNetworkPacket& packet);
42typedef BOOL (WINAPI * TOnNetClientPacket)(const CNetworkPacket& packet);
43typedef void (WINAPI * TOnStateChaged)(DWORD State);
44typedef BOOL (WINAPI * TOnMouseEvent_)(RAWMOUSE& pRawMouse);
45typedef BOOL (WINAPI * TOnKeyboardEvent_)(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
46typedef void (WINAPI * TOnTick)(float DeltaTime);
47
48#pragma pack(push, 4)
49struct CPluginCallbacks
50{
51 CPluginCallbacks() : cb(sizeof(CPluginCallbacks)), ApiVersion(0) // must be set to "SMART_PLUGIN_API_VER" by plugin
52 , OnLoad(nullptr)
53 , OnUnLoad(nullptr)
54 , OnClientInit(nullptr)
55 , OnNetServerPacket(nullptr)
56 , OnNetClientPacket(nullptr)
57 , OnNetworkStateChaged(nullptr)
58 , OnMouseEvent(nullptr)
59 , OnKeyboardEvent(nullptr)
60 , OnAllPluginsLoaded(nullptr)
61 , OnTick(nullptr)
62 , OnGameStateChaged(nullptr)
63 {}
64
65 int cb;
66 DWORD ApiVersion;
67
68 /*
69 These functions are called by SmartGuard when appropriate events occur.
70 Please only set pointers to function-handlers that you actually need to use.
71 */
72
73 TOnLoad OnLoad; /* Called when plugin has been successfully loaded */
74 TOnUnLoad OnUnLoad; /* Called when plugin is about to be unloaded, e.g. on game shutdown */
75 TOnClientInit OnClientInit; /* Called after UGameEngine::Init has been called */
76 TOnNetServerPacket OnNetServerPacket; /* Server->Client network packet handler, return TRUE if you handled this packet */
77 TOnNetClientPacket OnNetClientPacket; /* Client->Server network packet handler, return TRUE if you handled this packet */
78 TOnStateChaged OnNetworkStateChaged; /* Called when game changes network states, e.i. player gets connected or exits game */
79 TOnMouseEvent_ OnMouseEvent;
80 TOnKeyboardEvent_ OnKeyboardEvent;
81
82// added in api v2:
83 TOnAllPluginsLoaded OnAllPluginsLoaded; /* Called when all available plugins have been loaded */
84
85// added in api v4:
86 TOnTick OnTick; /* Called on each game tick. "Heavy" code in this callback will degrade game performance. */
87
88// added in api v5:
89 TOnStateChaged OnGameStateChaged; /* Called when game changes network states, e.i. player gets connected or exits game */
90};
91#pragma pack(pop)
92
93//------------------------------------------------------------------------------------------------
94// SmartGuard Interface Functions
95//------------------------------------------------------------------------------------------------
96namespace UNetworkState
97{
98 enum UNetworkState
99 {
100 DISCONNECTED,
101 SERVER_SELECTION,
102 CONNECTED
103 };
104}
105
106namespace UGameState
107{
108 enum UGameState
109 {
110 UNKNOWN,
111 TRANSITION,
112 AUTH,
113 CHAR_CREATE,
114 CHAR_SELECT,
115 INGAME
116 };
117}
118
119namespace SmartGuardModule
120{
121 enum SmartGuardModule
122 {
123 SGM_ModuleCopyPaste = 0,
124 SGM_ModuleAntiClick = 1,
125 SGM_ModuleD3DXHook = 2,
126 SGM_ModuleInputFilter = 3
127 };
128}
129
130typedef DWORD (WINAPI * TGetProcAddress)(HMODULE hModule, LPCSTR Func);
131typedef BOOL (WINAPI * TVirtualProtect)(_In_ LPVOID lpAddress, _In_ SIZE_T dwSize, _In_ DWORD flNewProtect, _Out_ PDWORD lpflOldProtect);
132typedef LPVOID (WINAPI * TDetourCreate)(_In_ LPVOID FunctionToHook, _In_ LPVOID FunctionToCall);
133typedef BOOL (WINAPI * TDetourRemove)(LPVOID Function);
134typedef UNetworkState::UNetworkState (WINAPI * TGetNetworkState)();
135typedef UGameState::UGameState (WINAPI * TGetGameState)();
136typedef USHORT (WINAPI * TGetSmartGuardClientID)();
137typedef void (WINAPI * TShowMessageBox)(const wchar_t* Message, BOOL ExitGame);
138typedef BOOL (WINAPI * TRegisterServicePacket)(BYTE Id, TOnNetServerPacket Event);
139typedef BOOL (WINAPI * TIsModuleAvailable)(SmartGuardModule::SmartGuardModule Module);
140typedef BOOL (WINAPI * TIsModuleEnabled)(SmartGuardModule::SmartGuardModule Module);
141
142typedef int (WINAPI * TIniReadInt)(const PWCHAR Key, INT32 Default, BOOL CommitIfMissing);
143typedef ULONG (WINAPI * TIniReadString)(const PWCHAR Key, PWCHAR Default, BOOL CommitIfMissing, PWCHAR pwResultBuff, ULONG BuffSize);
144typedef BOOL (WINAPI * TIniReadBool)(const PWCHAR Key, BOOL Default, BOOL CommitIfMissing);
145typedef void (WINAPI * TIniSave)(void);
146
147typedef bool (WINAPI * TGetCallbacks)(CPluginCallbacks& Callbacks);
148typedef void (WINAPI * TUpdateCallbacks)(CPluginCallbacks Callbacks);
149
150typedef void (WINAPI * TSendDataPacket)(LPCVOID PacketData, SIZE_T DataSize);
151
152typedef BOOL (WINAPI * TGetHWID)(_In_opt_ LPVOID HwidData, _Inout_ SIZE_T* DataSize);
153
154/*
155 These are SmartGuard API functions that you may want to use
156*/
157
158#pragma pack(push, 4)
159struct CPluginInterface
160{
161 CPluginInterface() : cb(sizeof(CPluginInterface))
162 , GetProcAddress(nullptr), VirtualProtect(nullptr), DetourCreate(nullptr)
163 , DetourRemove(nullptr), GetNetworkState(nullptr), GetSmartGuardClientID(nullptr)
164 , ShowMessageBox(nullptr), RegisterServicePacket(nullptr) , IsModuleAvailable(nullptr)
165 , IsModuleEnabled(nullptr), IniGetInt(nullptr), IniGetString(nullptr), IniGetBool(nullptr)
166 , IniSave(nullptr), GetCallbacks(nullptr), UpdateCallbacks(nullptr), GetGameState(nullptr)
167 , SendPacket(nullptr)
168 {}
169
170 int cb;
171
172 TGetProcAddress GetProcAddress;
173 TVirtualProtect VirtualProtect;
174
175 /*
176 @desc Creates a splicing hook (JMP XX XX XX XX) on a specified address.
177 @param FunctionToHook - Pointer to memory address you want to place a hook at.
178 @param FunctionToCall - Your function that will called when game executes code at FunctionToHook address.
179 @return Returns pointer to original function that you may want to use if you desire to call original function from your code.
180 This pointer is also used to remove hook.
181 */
182 TDetourCreate DetourCreate;
183 /*
184 @desc Removes previously placed hook
185 @param Function - pointer returned by [DetourCreate] call.
186 @return Returns TRUE if hook was successfully removed or FALSE otherwise.
187 */
188 TDetourRemove DetourRemove;
189
190 TGetNetworkState GetNetworkState; /* Returns current network connection state. */
191 TGetSmartGuardClientID GetSmartGuardClientID; /* Returns SmartGuard license ID. For licensing purposes only. */
192 TShowMessageBox ShowMessageBox; /* Displays a message in a form of game's modal window in center of the viewport. Pass TRUE for second argument if you want game to close after player hits OK. */
193 TRegisterServicePacket RegisterServicePacket; /* Used to register Service Packet Handler. This handler is called when client receives packet FF [ID]. */
194
195// added in api v3:
196 TIsModuleAvailable IsModuleAvailable; /* Returns 1 if current SmartGuard license supports module, 0 otherwise */
197 TIsModuleEnabled IsModuleEnabled; /* Returns 1 if modules supported and enabled at this moment */
198
199 /*
200 * Interface used to read plugin configuration
201 * Data is read from section name "Plugins.[PluginDllName]"
202 * Example:
203 *
204 * [Plugins.TestPlugin]
205 * Int = 100
206 * Bool = true|on|1|enabled
207 * String = example string
208 */
209 TIniReadInt IniGetInt;
210 TIniReadString IniGetString;
211 TIniReadBool IniGetBool;
212 TIniSave IniSave; /* Commits changes to disk */
213
214// added in api v4:
215 TGetCallbacks GetCallbacks; /* Returns pointer to plugin's callback structure */
216 TUpdateCallbacks UpdateCallbacks; /* Called to update plugin's callbacks */
217
218// added in api v5:
219 TGetGameState GetGameState;
220
221// added in api v6:
222 TSendDataPacket SendPacket;
223
224// added in api v7:
225
226 /*
227 *
228 *
229 */
230 /*
231 @desc Returns HWID value for current PC.
232 @param HwidData - Pointer to buffer that will store HWID value. Pass NULL in order to get required size in DataSize
233 @param DataSize - Size of buffer
234 @return Returns TRUE if succeeded or FALSE otherwise.
235 */
236 TGetHWID GetHWID;
237};
238#pragma pack(pop)
239
240/*
241 Your plugin needs to define this function in order to be loaded correctly.
242 Return TRUE if you have successfully initialized your plugin or return FALSE if you have not and want plugin to be unloaded from memory.
243*/
244extern "C" __declspec(dllexport) BOOL WINAPI SmartPluginInit(DWORD L2ProtocolVersion, CPluginInterface& Interface, CPluginCallbacks* pPluginCallbacks);