forked from Karasiq/MPQ-Repacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPluginForm.cpp
374 lines (366 loc) · 10.9 KB
/
UPluginForm.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#include "HashLib.h"
#pragma hdrstop
#include "UPluginForm.h"
#include "USettingsForm.h"
#include "URecursiveSearch.hpp"
#include "IniFiles.hpp"
#include "UFileLock.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
TfrmPlugins *frmPlugins;
TPluginFramework* PluginManager;
const UnicodeString PlgIni = AppDataPath + "Plugins.ini";
bool bDisableOverrides = false;
TLockedFile* IniLock = NULL;
//---------------------------------------------------------------------------
__fastcall TfrmPlugins::TfrmPlugins(TComponent* Owner)
: TForm(Owner)
{
IniLock = new TLockedFile(PlgIni);
IniLock->Lock();
if(!DirectoryExists(PluginsDir))
{
UnpackSamplePlugin();
}
PluginManager = new TPluginFramework(PluginsDir);
PluginManager->GetPluginList(chklstPlugins->Items);
if(chklstPlugins->Count)
{
chklstPlugins->ItemIndex = 0;
chklstPluginsClick(this);
}
Load();
}
bool TfrmPlugins::IsPluginsPresent()
{
if(chklstPlugins->Count) return true;
else return false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmPlugins::FormDestroy(TObject *Sender)
{
Save();
PluginManager->Free();
IniLock->Free();
}
//---------------------------------------------------------------------------
void __fastcall TfrmPlugins::chklstPluginsClickCheck(TObject *Sender)
{
const unsigned int i = chklstPlugins->ItemIndex;
if(chklstPlugins->Checked[i])
PluginManager->LoadPlugin(i);
else
PluginManager->UnloadPlugin(i);
}
//---------------------------------------------------------------------------
void __fastcall TfrmPlugins::chklstPluginsClick(TObject *Sender)
{
const unsigned int i = chklstPlugins->ItemIndex;
TPluginInfo inf = PluginManager->GetPluginInfo(i);
lblPlgInfo->Caption =
(UnicodeString)"Àâòîð: " + inf.szAuthor + "\n" +
"Íàçâàíèå: " + inf.szName + "\n" +
"Îïèñàíèå: " + inf.szDescription;
BOOL bSettingsPresent = PluginManager->CallSettings(i, false);
btnSettings->Enabled = bSettingsPresent;
}
//---------------------------------------------------------------------------
void UnpackSamplePlugin()
{
__MkDir(PluginsDir);
ResUnpack("SamplePlugin", PluginsDir + "SamplePlugin.dll");
ResUnpack("SamplePlugin_source", PluginsDir + "SamplePlugin_source.zip");
}
//---------------------------------------------------------------------------
void TfrmPlugins::Load()
{
IniLock->Unlock();
TIniFile* ini = new TIniFile(PlgIni);
bDisableOverrides = ini->ReadBool("Debug", "DisableOverrides", false);
if(ini->ReadString("MainSection", "ListHash", "") == SHA512(chklstPlugins->Items->Text))
{
const unsigned int PlgCount = ini->ReadInteger("MainSection", "Count", 0);
for(int i = 0; i < PlgCount; i++)
{
chklstPlugins->Checked[i] = ini->ReadBool("Flags", IntToStr(i), false);
if(chklstPlugins->Checked[i]) PluginManager->LoadPlugin(i);
}
}
ini->Free();
IniLock->Lock();
}
//---------------------------------------------------------------------------
void TfrmPlugins::Save()
{
IniLock->Unlock();
TIniFile* ini = new TIniFile(PlgIni);
if(ini->SectionExists("Flags")) ini->EraseSection("Flags");
const UnicodeString ListHash = SHA512(chklstPlugins->Items->Text);
ini->WriteString("MainSection", "ListHash", ListHash);
const unsigned int PlgCount = chklstPlugins->Count;
ini->WriteInteger("MainSection", "Count", PlgCount);
ini->WriteBool("Debug", "DisableOverrides", bDisableOverrides);
for(int i = 0; i < PlgCount; i++)
{
ini->WriteBool("Flags", IntToStr(i), chklstPlugins->Checked[i]);
}
ini->Free();
IniLock->Lock();
}
//---------------------------------------------------------------------------
DLLEXPORT API_GetTempPath(char* szBuffer)
{
__strcpy(szBuffer, MainForm->edtTempDir->Text);
return GetLastError();
}
DLLEXPORT API_GetDestMap(char* szBuffer)
{
__strcpy(szBuffer, __DestMap);//MainForm->edtDestMap->Text);
return GetLastError();
}
DLLEXPORT API_GetSourceMap(char* szBuffer)
{
__strcpy(szBuffer, __SourceMap);//MainForm->edtSourceMap->Text);
return GetLastError();
}
DLLEXPORT API_GetAppDataPath(char* szBuffer)
{
__strcpy(szBuffer, AppDataPath);
return GetLastError();
}
DLLEXPORT API_WriteLog(const char* szBuffer)
{
MainForm->mmoLog->Lines->Add(UnicodeString(szBuffer));
return GetLastError();
}
DLLEXPORT API_RegisterOverride(TOverrideInfo Info)
{
if(bDisableOverrides) return ERROR_ACCESS_DENIED;
if(PluginManager->IndexByHInstance(Info.hPlugin) == -1)
{
MainForm->mmoLog->Lines->Add("Override registration error: invalid Plugin");
return ERROR_INVALID_PARAMETER;
}
switch(Info.Code)
{
case OVERRIDE_SEARCH:
{
if(PluginManager->Overrides.Search.Enabled)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[PluginManager->IndexByHInstance(Info.hPlugin)];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
}
PluginManager->Overrides.Search.Enabled = true;
PluginManager->Overrides.Search.Function = Info.Function;
PluginManager->Overrides.Search.PluginIndex = PluginManager->IndexByHInstance(Info.hPlugin);
break;
}
case OVERRIDE_UNPACK:
{
if(PluginManager->Overrides.Unpack.Enabled)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[PluginManager->IndexByHInstance(Info.hPlugin)];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
}
PluginManager->Overrides.Unpack.Enabled = true;
PluginManager->Overrides.Unpack.Function = Info.Function;
PluginManager->Overrides.Unpack.PluginIndex = PluginManager->IndexByHInstance(Info.hPlugin);
break;
}
case OVERRIDE_PACK:
{
if(PluginManager->Overrides.Pack.Enabled)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[PluginManager->IndexByHInstance(Info.hPlugin)];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
}
PluginManager->Overrides.Pack.Enabled = true;
PluginManager->Overrides.Pack.Function = Info.Function;
PluginManager->Overrides.Pack.PluginIndex = PluginManager->IndexByHInstance(Info.hPlugin);
break;
}
default:
{
return ERROR_INVALID_PARAMETER;
}
}
return GetLastError();
}
DLLEXPORT API_UnregisterOverride(TOverrideInfo Info)
{
if(bDisableOverrides) return ERROR_ACCESS_DENIED;
const unsigned i = PluginManager->IndexByHInstance(Info.hPlugin);
if(i == -1)
{
MainForm->mmoLog->Lines->Add("Override removal error: Invalid Plugin");
return ERROR_INVALID_PARAMETER;
}
switch(Info.Code)
{
case OVERRIDE_SEARCH:
{
if(PluginManager->Overrides.Search.PluginIndex != i)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[i];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
return ERROR_ACCESS_DENIED;
}
PluginManager->Overrides.Search.Enabled = false;
break;
}
case OVERRIDE_UNPACK:
{
if(PluginManager->Overrides.Unpack.PluginIndex != i)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[i];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
return ERROR_ACCESS_DENIED;
}
PluginManager->Overrides.Unpack.Enabled = false;
break;
}
case OVERRIDE_PACK:
{
if(PluginManager->Overrides.Pack.PluginIndex != i)
{
const UnicodeString a = frmPlugins->chklstPlugins->Items->Strings[PluginManager->Overrides.Search.PluginIndex];
const UnicodeString b = frmPlugins->chklstPlugins->Items->Strings[i];
MainForm->mmoLog->Lines->Add("Plugin " + b + " conflicts with plugin " + a + "!");
return ERROR_ACCESS_DENIED;
}
PluginManager->Overrides.Pack.Enabled = false;
break;
}
default:
{
return ERROR_INVALID_PARAMETER;
}
}
return GetLastError();
}
DLLEXPORT API_GetMPQFileCount(char* szMPQFile)
{
int nResult = GetFileCount(szMPQFile);
return nResult;
}
DLLEXPORT API_GetFlagState(TMPQRepackerOption Opt)
{
switch(Opt)
{
case bUseXdep:
return MainForm->chkUseXdep->Checked;
case bReplaceW3I:
return MainForm->chkReplaceW3I->Checked;
case bReplaceDOO:
return MainForm->chkReplaceDOO->Checked;
case bInsertCheats:
return MainForm->chkInsertCheats->Checked;
case bPause:
return MainForm->chkManualEdit->Checked;
case bDeleteTemp:
return MainForm->chkDeleteTemp->Checked;
case bUnpackUnknown:
return MainForm->chkExtractUnknownFiles->Checked;
case bW3iWtsMapRename:
return bMapRename;
case bNotUseInternalListfile:
return bDontUseInternalListfile;
default:
{
return ERROR_INVALID_PARAMETER;
}
}
}
DLLEXPORT API_SetFlagState(TMPQRepackerOption Opt, const bool bState)
{
switch(Opt)
{
case bUseXdep:
{
MainForm->chkUseXdep->Checked = bState;
break;
}
case bReplaceW3I:
{
MainForm->chkReplaceW3I->Checked = bState;
break;
}
case bReplaceDOO:
{
MainForm->chkReplaceDOO->Checked = bState;
break;
}
case bInsertCheats:
{
MainForm->chkInsertCheats->Checked = bState;
break;
}
case bPause:
{
MainForm->chkManualEdit->Checked = bState;
break;
}
case bDeleteTemp:
{
MainForm->chkDeleteTemp->Checked = bState;
break;
}
case bUnpackUnknown:
{
MainForm->chkExtractUnknownFiles->Checked = bState;
break;
}
case bW3iWtsMapRename:
{
bMapRename = bState;
break;
}
case bNotUseInternalListfile:
{
bDontUseInternalListfile = bState;
break;
}
default:
{
return ERROR_INVALID_PARAMETER;
}
}
return GetLastError();
}
DLLEXPORT API_GetProgramStatus()
{
return __Status;
}
DLLEXPORT API_GetCompression()
{
return nCompression;
}
DLLEXPORT API_GetVersion()
{
return nVersion; // Constant (UMainForm.h)
}
//---------------------------------------------------------------------------
void __fastcall TfrmPlugins::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
const UnicodeString SourceFile = PluginsDir + "SamplePlugin_source.zip";
if(Key == VK_F1)
{
if(ResUnpack("SamplePlugin_source", SourceFile))
ShellExec(SourceFile);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmPlugins::btnSettingsClick(TObject *Sender)
{
PluginManager->CallSettings(chklstPlugins->ItemIndex);
}
//---------------------------------------------------------------------------