-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPQDraftPlugin.h
411 lines (327 loc) · 15.7 KB
/
MPQDraftPlugin.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
QDPlugin.h
The central hive of the MPQDraft plugin system.
In addition to the standard MPQ "adding" functionality of MPQDraft,
MPQDraft also supports a powerful plugin system to acommodate addition
methods of patching. These plugins are specially constructed DLLs which
1) Export the GetMPQDraftPlugin function, and 2) Impliment the
IMPQDraftPlugin interface. Up to 8 plugins may be loaded in one patching
session (although 8 is a rather arbitrary number, and may be changed in
later versions, if there is reason to do so).
- PLUGIN MODULES -
MPQDraft supports the creation of completely self-contained SEMPQs. But,
this ability creates a palette of problems all its own. All of a plugin's
data files have to be packed into the SEMPQ. This means that a special
mechanism is needed, to marshall the data files from the plugin to the
SEMPQ, and back to the plugin; this is the plugin module system. Each data
file of the plugin is referred to as a "module", and identified by
a pair of DWORDs: a plugin ID, and a module ID. The plugin ID is the
globally unique ID of the plugin; the module ID is the plugin-specific ID
of the module.
[FURTHER TEXT TO BE ADDED HERE]
- SETUP SIDE -
There are two halves to each plugin: a setup side and a patching side. The
setup side is when the plugin gets loaded in MPQDraft, and displayed in
the plugin list, in either the patching wizard or SEMPQ wizard.
Specifically, MPQDraft loads all plugins when the plugins page in the
wizard first gets activated. The plugins will remain loaded until the
wizard is closed. If a patch is performed, the plugins will remain loaded
until the patch is completed. This allows plugins to get the opportunity
to delete any temporary files that were created for the patch.
Order and timing of calls to setup-side plugin functions:
When the plugins wizard page is first activated, and MPQDraft is building
its plugin database:
1. The plugin gets loaded with LoadLibrary.
2. The plugin's GetMPQDraftPlugin function gets called to obtain the
plugin's IMPQDraftPlugin interface.
3. IMPQDraftPlugin::Identify gets called.
4. IMPQDraftPlugin::GetPluginName gets called.
Each time the plugins page is switched to, MPQDraft determines which
plugins to display in the plugin list:
- IMPQDraftPlugin::CanPatchExecutable gets called for the currently
selected executable. If the plugin can patch the executable, it will
appear in the plugin list if it can't, then it won't appear. NOTE: this
step does not occur when selecting plugins for use in an SEMPQ. In this
case, ALL plugins will be available to select.
If the "Configure" button is clicked while a plugin is selected:
- IMPQDraftPlugin::Configure is called, with the HWND to the wizard in
use. This allows the plugin to create a modal dialog with various plugin
settings.
If the "Finish" button is clicked to initiate a patch:
1. IMPQDraftPlugin::ReadyForPatch is called to discern whether the plugin
is properly configured, and ready to perform the patch.
2. IMPQDraftPlugin::GetModules is called to obtain the number of plugin
modules the plugin will require.
3. IMPQDraftPlugin::GetModule is called again to get the plugin's modules.
Either after the patch is completed or the wizard is cancelled:
- FreeLibrary is called to unload the plugin. The plugin is responsible
for cleaning up any data files it created temporarily.
NOTE: Plugins will not be loaded in setup-side when an SEMPQ is executed.
- PATCHING SIDE -
The patching side is when a plugin gets loaded by the MPQDraft patching
kernel. The plugin will be loaded inside the patchee (the process being
patched) BEFORE any patchee code gets executed, but after any DLLs the
patchee uses are loaded and initialized (DllMain is called). This means
that plugins will be able to modify initialized data in the patchee, but
not uninitialized data. If the latter is necessary, a thread can be
spawned by the plugin to wait until the data has been initialized.
Order and timing of calls to patching-side plugin functions:
After MPQDraft performs its own initializations, it will load each
selected plugin (this is done before any MPQs are loaded):
1. LoadLibrary gets called to load the plugin inside the patch target.
2. GetMPQDraftPlugin gets called to obtain an IMPQDraftPlugin interface.
3. IMPQDraftPlugin::InitializePlugin gets called with a pointer to the
MPQDraft server interface, to allow the plugin to locate its data files.
When the patchee is closing down, and MPQDraft is terminating:
1. IMPQDraftPlugin::TerminatePlugin gets called to remove any patches the
plugin performed.
2. The plugin is unloaded with FreeLibrary.
*/
#ifndef QDPLUGIN_H
#define QDPLUGIN_H
#include <windows.h>
// The maximum length of a plugin module's filename. INCLUDES final NULL.
#define MPQDRAFT_MAX_PATH 264
// The maximum length of a plugin's name. INCLUDES final NULL.
#define MPQDRAFT_MAX_PLUGIN_NAME 64
/*
MPQDRAFTPLUGINMODULE
Structure used by IMPQDraftPlugin::GetModules to notify MPQDraft of any
files (called plugin modules) that are to be loaded. Read description of
that function for more information.
*/
#include <pshpack1.h>
struct MPQDRAFTPLUGINMODULE
{
/* dwComponentID: The ID of the plugin. Should be the same value as is
returned by IMPQDraftPlugin::Identify. Must be globally unique. */
DWORD dwComponentID;
/* dwModuleID: The unique ID of the plugin module. This will be used
instead of the actual filename for identifying plugin modules. */
DWORD dwModuleID;
// bExecute: Used internally by MPQDraft. Must be set to FALSE.
BOOL bExecute;
// szModuleFileName: The absolute path of the plugin module file.
char szModuleFileName[MPQDRAFT_MAX_PATH];
};
#include <poppack.h>
/*
IMPQDraftServer
Serves as a portal back to MPQDraft, allowing the plugin to
not only be executed by MPQDraft, but also it communicate with MPQDraft.
A plugin will be given an IMPQDraftServer pointer when MPQDraft calls
IMPQDraftPlugin::InitializePlugin.
*/
struct IMPQDraftServer
{
/*
IMPQDraftServer::GetPluginModule
Allows a plugin to locate its modules when it is loaded in patch-side.
Return TRUE on success, and FALSE on failure.
Parameters:
dwPluginID [in] - The ID of the plugin who is attempting to locate
its modules.
dwModuleID [in] - The ID of the module to be located.
lpszFileName [out] - Pointer to a buffer where MPQDraft will copy
the file name of the module to. This buffer should be
MPQDRAFT_MAX_PATH characters long.
Behavior:
- If lpszFileName is NULL, GetPluginModules will fail.
- If the buffer pointer to by lpszFileName is shorter than the
length of the module's filename (which will never be more than
MPQDRAFT_MAX_PATH), a crash will result.
- If no module identified by dwPluginID and dwModuleID can be
found, GetPluginModule will fail.
- If more than one module with identical dwPluginID and
dwModuleID exist, MPQDraft will arbitrarily choose one to return.
- If the desired module exists, GetPluginModule will copy the
filename of the module to lpszFileName.
*/
virtual BOOL WINAPI GetPluginModule(DWORD dwPluginID, DWORD dwModuleID, LPSTR lpszFileName) = 0;
};
/*
IMPQDraftPlugin
The primary gateway between the MPQDraft patching kernel and the plugin.
This interface must be fully implimented by every MPQDraft plugin (or at
least until it is superseded by IMPQDraftPlugin2, in a much later version
of MPQDraft). MPQDraft will obtain this interface when it calls
GetMPQDraftPlugin. It will then store the interface in its plugin
database, and use it in all subsequent calls to the plugin.
NOTE: The specifications for this interface are based on the recommended
responses. In some places it may be legal to slightly depart from the
recommended specs (i.e. functions may fail instead of asserting).
*/
struct IMPQDraftPlugin
{
/*
IMPQDraftPlugin::Identify
Identifies the plugin to MPQDraft with a globally unique ID code.
Returns TRUE on success, and FALSE on failure (which should never
happen).
Parameters:
lpdwPluginID [out] - A pointer to a DWORD that will receive the
plugin's ID. MPQDraft will supply this DWORD.
Behavior:
- If lpdwPluginID is NULL, Identify will assert.
- On success, Identify will copy its ID to lpdwPluginID.
*/
virtual BOOL WINAPI Identify(LPDWORD lpdwPluginID) = 0;
/*
GetPluginName
Retrieves the name of the plugin which will be displayed in the
plugin list in either of the MPQDraft wizards. This name should also
include the version of the plugin, i.e. "StarGraft v1.08 QD".
Returns TRUE on success, and FALSE on failure.
Parameters:
lpszPluginName [out] - A pointer to a buffer which will receive
the name of the plugin. This buffer will by provided by MPQDraft,
and will usually be MPQDRAFT_MAX_PLUGIN_NAME chars long.
nNameBufferLength [in] - The length of the buffer pointed to by
lpszPluginName, including space for the final NULL.
Behavior:
- If lpszPluginName is null, GetPluginName will assert.
- If nNameBufferLength is shorter than the name of the plugin,
GetPluginName will fail.
- On success, GetPluginName will copy the plugin name to the
buffer pointed to by lpszPluginName.
*/
virtual BOOL WINAPI GetPluginName(LPSTR lpszPluginName, DWORD nNameBufferLength) = 0;
/*
IMPQDraftPlugin::CanPatchExecutable
Called by MPQDraft in the plugins page of the patch wizard (but not on
the SEMPQ wizard). Its return value determines whether or not the
plugin will appear in the list of available plugins, as MPQDraft will
only list plugins which are compatible with the currently selected
patch target.
Returns TRUE if the plugin can patch the selected executable, and
FALSE if it cannot.
Parameters:
lpszEXEFileName [in] - The absolute path of the currently selected
executable.
Behavior:
- If lpszEXEFileName is NULL, CanPatchExecutable will assert.
- If an error occurs (i.e. the executable cannot be opened or read
from, CanPatchExecutable will fail.
*/
virtual BOOL WINAPI CanPatchExecutable(LPCSTR lpszEXEFileName) = 0;
/*
IMPQDraftPlugin::Configure
Called by MPQDraft from the plugin page in either of the wizards.
Configure should present the user with settings which can be
adjusted to change the way the plugin will function (i.e. selecting
the PAT to use in StarGraft). If necessary, the plugin can create a
settings dialog. It is recommended that the plugin stores the settings
from the last time it was configured in the registry, but this is not
mandatory. Some plugins may even not require any configuration at all,
in which case this function would be a simple "return TRUE".
Returns TRUE on success and FALSE on failure.
Parameters:
hParentWnd [in] - A handle to the wizard from which Configure was
called. This handle is to be used exclusively to pass to
DialogBox, to create a modal dialog. It is NOT to be used to
attempt to modify the wizard. Such an attempt will probably crash.
Behavior:
- If hParentWnd is NULL, Configure will assert.
- If an error occurs while configuring the plugin, Configure will
fail.
- If the configuration completed sucessfully (even if the user
pressed the "Cancel" button on a dialog), Configure will succeed.
*/
virtual BOOL WINAPI Configure(HWND hParentWnd) = 0;
/*
IMPQDraftPlugin::ReadyForPatch
Called by MPQDraft right before a patch, to determine whether all
plugins are properly configured, and ready to patch.
Returns TRUE if the plugin is configured properly, FALSE if it isn't.
*/
virtual BOOL WINAPI ReadyForPatch() = 0;
/*
IMPQDraftPlugin::GetModules
Called twice by MPQDraft right before it is about to perform a patch.
The first time, MPQDraft collects the number of modules from all
plugins, so that it can allocate the proper amount of memory to hold
the list of modules. The second it will be to actually retrieve the
list of modules. These modules will be packed into an SEMPQ. Or, if
if a straight patch is being performed, MPQDraft will just pass on the
module list to the patching kernel.
Returns TRUE on success, and FALSE on failure.
Parameters:
lpPluginModules [out] - A pointer to the memory MPQDraft has
allocated to hold the list of modules. The plugin must list each
module it will require in the patching process. When MPQDraft
first calls GetModules, lpPluginModules will be NULL.
lpnNumModules [out] - A pointer to the number of plugin modules
the plugin will need. This number ought not to exceed 4.
Behavior:
- If lpnNumModules is NULL, GetModules will assert.
- If an error occurs, and GetModules is unable to supply the
required information, GetModules will fail. In this case, MPQDraft
will abort the patch.
- If lpPluginModules is non-NULL, GetModules will copy the
list of modules to lpPluginModules, and give the number of modules
in lpnNumModules.
- If lpPluginModules is NULL, GetModules will give the exact
number of modules it will require in lpnNumModules, and succeed.
*/
virtual BOOL WINAPI GetModules(MPQDRAFTPLUGINMODULE *lpPluginModules, LPDWORD lpnNumModules) = 0;
/*
IMPQDraftPlugin::InitializePlugin
Called by MPQDraft from inside the patch target, to allow the plugin
to perform its patch. Any patches the plugin makes should be stored,
and undone upon TerminatePlugin.
A return value of TRUE indicates that MPQDraft should continue with
the patch. FALSE indicates that MPQDraft should abort the patch. The
plugin should report any errors BEFORE it returns FALSE, as MPQDraft
will terminate the patch silently.
Parameters:
lpMPQDraftServer [in] - A pointer to an IMPQDraftServer interface,
provided by MPQDraft. This interface can be used to locate the
plugin's modules. This pointer should be saved in case it is
needed in future use.
Behavior:
- If lpMPQDraftServer is NULL, InitializePlugin will assert.
- If the plugin was unable to perform the patch, and the the patch
target should be terminated, InitializePlugin will display an
error message box and return FALSE. MPQDraft will abort the patch.
- If the plugin was unable to perform the patch, and MPQDraft
should ignore the error and continue, InitializePlugin will
return TRUE. MPQDraft will continue the patch.
- If the patch was performed successfully, InitializePlugin will
return TRUE.
*/
virtual BOOL WINAPI InitializePlugin(IMPQDraftServer *lpMPQDraftServer) = 0;
/*
IMPQDraftPlugin::TerminatePlugin
Called by MPQDraft inside the patch target during the shutdown
process, to allow the plugin to unload any patches it made.
Returns TRUE on success, and FALSE on failure.
Behavior:
- If InitializePlugin was not called previously, TerminatePlugin
asserts.
- If the patches performed in InitializePlugin were not
successfully removed, TerminatePlugin fails.
- If the patches performed in InitializePlugin were successfully
removed, TerminatePlugin succeeds.
- It is possible that on rare occasions InitializePlugin will be
called, then the plugin will be unloaded before TerminatePlugin
gets called. The plugin should check for this on
DLL_PROCESS_DETACH, and call TerminatePlugin itself.
*/
virtual BOOL WINAPI TerminatePlugin() = 0;
};
/*
GetMPQDraftPlugin
Exported by name from the plugin DLL. Called by MPQDraft to obtain the
IMPQDraftPlugin interface of the plugin.
Returns TRUE on success, and FALSE on failure.
Parameters:
lppMPQDraftPlugin [out] - A pointer to a pointer that will hold the
IMPQDraftPlugin. The plugin must set this to point to the plugin's
IMPQDraftPlugin interface.
Behavior:
- GetMPQDraftPlugin will only be called once, so it isn't necessary
to instantiate an IMPQDraftPlugin for each call. A single global
IMPQDraftPlugin is sufficient.
*/
BOOL GetMPQDraftPlugin(IMPQDraftPlugin **lppMPQDraftPlugin);
#endif // #ifndef QDPLUGIN_H