-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduilib.cpp
551 lines (529 loc) · 14.4 KB
/
duilib.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include "duilib.h"
HMODULE g_twinUI;
RP_CDUIContext* g_DUIContext;
HRESULT CDUIResourceManager::_RegisterElements()
{
HRESULT result = 0;
for (DWORD i = 0; i < this->_cRegister; i++)
{
result = ((VoidCall)this->_rgRegister[i])();
if (result < 0)
break;
}
return result;
}
HRESULT CDUIResourceManager::_DebugGetDUIPath(LPWSTR* pszDUIPath)
{
HKEY regKey;
HRESULT result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", 0, KEY_QUERY_VALUE, ®Key);
if (result)
return result;
DWORD pathSize;
RegQueryValueEx(regKey, _pszMarkupOverrideRegValue, NULL, NULL, NULL, &pathSize);
if (!pathSize)
return E_FAIL;
*pszDUIPath = new WCHAR[pathSize];
result = RegQueryValueEx(regKey, _pszMarkupOverrideRegValue, NULL, NULL, (LPBYTE)*pszDUIPath, &pathSize);
if (result)
{
delete[] *pszDUIPath;
*pszDUIPath = NULL;
}
RegCloseKey(regKey);
return result;
}
HRESULT CDUIResourceManager::s_DebugGetDUIFileLastWrite(LPWSTR szDUIPath, FILETIME* writeTime)
{
(*writeTime).dwLowDateTime = 0;
(*writeTime).dwHighDateTime = 0;
WIN32_FILE_ATTRIBUTE_DATA attribData;
if (GetFileAttributesEx(szDUIPath, GetFileExInfoStandard, &attribData))
{
*writeTime = attribData.ftLastWriteTime;
return ERROR_SUCCESS;
}
return HRESULT_FROM_WIN32(GetLastError());
}
HRESULT CDUIResourceManager::GetModuleResourceData(HINSTANCE hModule, LPCWSTR pwszName, LPCWSTR pwszType, LPCSTR* prgb, DWORD* pcb)
{
HRSRC resource;
DWORD resSize;
HGLOBAL resGlobal;
LPCSTR resLockStr;
if ((resource = FindResource(hModule, pwszName, pwszType)) &&
(resSize = SizeofResource(hModule, resource)) &&
(resGlobal = LoadResource(hModule, resource)) &&
(resLockStr = (LPCSTR)LockResource(resGlobal)))
{
*prgb = resLockStr;
*pcb = resSize;
return ERROR_SUCCESS;
}
return HRESULT_FROM_WIN32(GetLastError());
}
HRESULT CDUIResourceManager::s_DebugPrettyPrintXMLToFile(LPCSTR pszXML, IStream* stream, DWORD resSize)
{
ULARGE_INTEGER zero = { 0 };
HRESULT comResult = stream->SetSize(zero);
if (!SUCCEEDED(comResult))
return comResult;
DWORD indentLevel = 0;
LPCSTR xmlEnd = pszXML + resSize - 1;
LPCSTR pszNextElement = pszXML;
while (true)
{
LPSTR pszElementStart = StrPBrkA(pszNextElement, "<");
if (!pszElementStart)
return comResult;
pszNextElement = 0;
CHAR nextChar = *(pszElementStart + 1);
if (nextChar == '/')
--indentLevel;
for (DWORD i = 0; i < indentLevel; i++)
{
comResult = stream->Write(" ", 3, NULL);
if (!SUCCEEDED(comResult))
return comResult;
}
if (nextChar != '/' && nextChar != '!')
++indentLevel;
LPSTR pszElementEnd = pszElementStart;
while (true)
{
if (pszNextElement || !pszElementEnd)
{
comResult = stream->Write(pszElementStart, (ULONG)(pszNextElement - pszElementStart), NULL);
if (!SUCCEEDED(comResult))
return comResult;
if (pszNextElement >= xmlEnd)
break;
comResult = stream->Write("\r\n", 2, NULL);
if (!SUCCEEDED(comResult))
return comResult;
break;
}
// Find one of the following: " > \ /
pszElementEnd = StrPBrkA(pszElementEnd, "\">\\/");
CHAR chElementEnd = *pszElementEnd;
if (chElementEnd == '\\')
pszElementEnd += 2;
else if (chElementEnd == '>')
pszNextElement = pszElementEnd + 1;
else if (chElementEnd == '/' && *(pszElementEnd + 1) == '>')
{
--indentLevel;
pszNextElement = pszElementEnd + 2;
}
else
{
if (chElementEnd == '\\')
pszElementEnd += 2;
++pszElementEnd;
}
}
if (pszNextElement >= xmlEnd)
break;
}
if (!SUCCEEDED(comResult))
return comResult;
return stream->Commit(STGC_DEFAULT);
}
HRESULT CDUIResourceManager::_DebugSaveDUIResourceToFile(LPWSTR szDUIPath)
{
LPCSTR pszXML;
DWORD cbXMLSize;
HRESULT result = GetModuleResourceData(_hInst, (LPCWSTR)_uResourceID, L"UIFILE", &pszXML, &cbXMLSize);
if (!SUCCEEDED(result))
return result;
CComPtr<IStream> stream;
result = SHCreateStreamOnFileW(szDUIPath, STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, &stream);
if (SUCCEEDED(result))
result = s_DebugPrettyPrintXMLToFile(pszXML, stream, cbXMLSize);
return result;
}
HRESULT CDUIResourceManager::_DebugEnsureDUIFile(LPWSTR* ppszPath, FILETIME* pftLastWrite)
{
LPWSTR filePath;
HRESULT result = _DebugGetDUIPath(&filePath);
if (!SUCCEEDED(result))
return result;
FILETIME lastWrite;
result = s_DebugGetDUIFileLastWrite(filePath, &lastWrite);
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
result = _DebugSaveDUIResourceToFile(filePath);
if (!SUCCEEDED(result))
{
delete[] filePath;
return result;
}
result = s_DebugGetDUIFileLastWrite(filePath, &lastWrite);
}
if (!SUCCEEDED(result))
{
delete[] filePath;
return result;
}
*ppszPath = filePath;
*pftLastWrite = lastWrite;
return result;
}
HRESULT CDUIResourceManager::s_DebugLoadFileToMemory(LPCWSTR pszFile, LPWSTR* ppszOutBuf)
{
CComPtr<IStream> stream;
HRESULT result = SHCreateStreamOnFile(pszFile, STGM_READ, &stream);
if (!SUCCEEDED(result))
return result;
STATSTG stats = { 0 };
result = stream->Stat(&stats, STATFLAG_DEFAULT);
if (!SUCCEEDED(result))
return result;
if (stats.cbSize.HighPart)
return E_FAIL;
DWORD ansiSize = stats.cbSize.LowPart + 1;
if (ansiSize < stats.cbSize.LowPart)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
LPSTR ansiBuf = new CHAR[ansiSize];
DWORD readBytes = 0;
result = stream->Read(ansiBuf, stats.cbSize.LowPart, &readBytes);
if (stats.cbSize.LowPart != readBytes)
{
delete[] ansiBuf;
return E_FAIL;
}
ansiBuf[stats.cbSize.LowPart] = 0;
DWORD uniSize = ansiSize * 2;
if (uniSize < ansiSize)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
*ppszOutBuf = new WCHAR[uniSize];
SHAnsiToUnicode(ansiBuf, *ppszOutBuf, ansiSize);
delete[] ansiBuf;
return result;
}
HMODULE CDUIResourceManager::GetShellStyleHInstance()
{
HMODULE hInst = NULL;
LPWSTR pszThemeFileName = new WCHAR[MAX_PATH];
LPWSTR pszColorBuff = new WCHAR[100];
if (!GetCurrentThemeName(pszThemeFileName, MAX_PATH, pszColorBuff, 100, NULL, 0))
{
PathAppendW(pszThemeFileName, L"Shell");
PathAppendW(pszThemeFileName, pszColorBuff);
PathAppendW(pszThemeFileName, L"ShellStyle.dll");
hInst = LoadLibraryExW(pszThemeFileName, 0i64, 2u);
}
if (!hInst)
{
ExpandEnvironmentStringsW(L"%SystemRoot%\\System32\\ShellStyle.dll", pszThemeFileName, 0x104u);
hInst = LoadLibraryExW(pszThemeFileName, 0i64, 2u);
}
delete[] pszThemeFileName;
delete[] pszColorBuff;
return hInst;
}
HRESULT CDUIResourceManager::_DebugLoadParser(LPCWSTR pszPath, LPVOID** parser)
{
LPWSTR uiFile;
HRESULT result = s_DebugLoadFileToMemory(pszPath, &uiFile);
if (SUCCEEDED(result))
{
if (g_DUIContext)
result = g_DUIContext->CreateParser(parser);
else
result = DirectUI::DUIXmlParser::Create((DirectUI::DUIXmlParser**)parser, NULL, NULL, NULL, NULL);
if (SUCCEEDED(result))
{
HMODULE shellStyle = GetShellStyleHInstance();
BOOL unloadLater = TRUE;
if (!shellStyle)
{
shellStyle = _hInst;
unloadLater = FALSE;
}
if (g_DUIContext)
result = g_DUIContext->Parser_SetXML(*parser, uiFile, shellStyle);
else
result = ((DirectUI::DUIXmlParser*)*parser)->SetXML((unsigned const short*)uiFile, shellStyle, g_twinUI);
if (!SUCCEEDED(result))
{
if (g_DUIContext)
g_DUIContext->Parser_Destroy(*parser);
else
((DirectUI::DUIXmlParser*)*parser)->Destroy();
*parser = 0;
}
if (unloadLater)
FreeLibrary(shellStyle);
}
delete[] uiFile;
}
return result;
}
HRESULT CDUIResourceManager::DUI_LoadUIFileFromResources(HINSTANCE hModuleInstance, DWORD uFileID, LPWSTR* ppszUIFile)
{
LPCSTR ansiBuf;
DWORD ansiSize;
HRESULT result = GetModuleResourceData(hModuleInstance, (LPCWSTR)uFileID, L"UIFILE", &ansiBuf, &ansiSize);
if (!SUCCEEDED(result))
return result;
DWORD uniSize = (ansiSize + 1) * 2;
if (uniSize < ansiSize)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
*ppszUIFile = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, uniSize);
if (!*ppszUIFile)
return HRESULT_FROM_WIN32(GetLastError());
SHAnsiToUnicode(ansiBuf, *ppszUIFile, uniSize);
return result;
}
HRESULT CDUIResourceManager::DUI_CreateParserFromResource(HINSTANCE hinst, DWORD uID, LPVOID** ppParser)
{
HMODULE shellStyle = GetShellStyleHInstance();
BOOL unloadLater = TRUE;
if (!shellStyle)
{
shellStyle = _hInst;
unloadLater = FALSE;
}
LPWSTR uiFile;
HRESULT result = DUI_LoadUIFileFromResources(hinst, uID, &uiFile);
if (SUCCEEDED(result))
{
if (g_DUIContext)
result = g_DUIContext->CreateParser(ppParser);
else
result = DirectUI::DUIXmlParser::Create((DirectUI::DUIXmlParser**)ppParser, NULL, NULL, NULL, NULL);
if (SUCCEEDED(result))
{
if (g_DUIContext)
result = g_DUIContext->Parser_SetXML(*ppParser, uiFile, shellStyle);
else
result = ((DirectUI::DUIXmlParser*)*ppParser)->SetXML((unsigned const short*)uiFile, shellStyle, g_twinUI);
}
}
if (uiFile)
LocalFree((HLOCAL)uiFile);
if (unloadLater)
FreeLibrary(shellStyle);
if (!SUCCEEDED(result) && *ppParser)
{
if (g_DUIContext)
g_DUIContext->Parser_Destroy(*ppParser);
else
((DirectUI::DUIXmlParser*)*ppParser)->Destroy();
*ppParser = 0;
}
return result;
}
HRESULT CDUIResourceManager::_DebugCreateParser(REFCOUNTEDPARSER* prcp, LPVOID** ppParser)
{
LPWSTR debugUiFilePath;
FILETIME debugUiFileLastWrite;
if (SUCCEEDED(_DebugEnsureDUIFile(&debugUiFilePath, &debugUiFileLastWrite)))
{
HRESULT result = _DebugLoadParser(debugUiFilePath, ppParser);
if (SUCCEEDED(result))
{
if (prcp->spszDUIPath)
delete[] prcp->spszDUIPath;
prcp->spszDUIPath = debugUiFilePath;
prcp->ftDUILastWrite = debugUiFileLastWrite;
return result;
}
}
return DUI_CreateParserFromResource(_hInst, _uResourceID, ppParser);
}
HRESULT CDUIResourceManager::_InitParserForThread()
{
REFCOUNTEDPARSER* rcp = (REFCOUNTEDPARSER*)TlsGetValue(this->_tlsParser);
if (rcp)
{
++_uInitCount;
return ERROR_SUCCESS;
}
rcp = new REFCOUNTEDPARSER();
LPVOID* pParser = 0;
HRESULT result = _DebugCreateParser(rcp, &pParser);
if (SUCCEEDED(result))
{
rcp->cRefCount = 1;
rcp->pParser = pParser;
result = TlsSetValue(_tlsParser, rcp) == 0 ? E_FAIL : 0;
if (SUCCEEDED(result))
return result;
if (g_DUIContext)
g_DUIContext->Parser_Destroy(pParser);
else
((DirectUI::DUIXmlParser*)pParser)->Destroy();
}
if (rcp)
{
if (rcp->spszDUIPath)
delete[] rcp->spszDUIPath;
delete rcp;
}
TlsSetValue(_tlsParser, NULL);
return result;
}
void CDUIResourceManager::_UninitParserForThread()
{
REFCOUNTEDPARSER* rcp = ((REFCOUNTEDPARSER*)TlsGetValue(_tlsParser));
if (--rcp->cRefCount == 1)
{
if (g_DUIContext)
g_DUIContext->Parser_Destroy(rcp->pParser);
else
((DirectUI::DUIXmlParser*)rcp->pParser)->Destroy();
if (rcp->spszDUIPath)
delete[] rcp->spszDUIPath;
delete rcp;
TlsSetValue(_tlsParser, NULL);
}
}
void CDUIResourceManager::Initialize(HINSTANCE hInst, DWORD uResourceID, LPCVOID *rgRegister, DWORD cRegister, LPCWSTR pszMarkupOverrideRegValue, BOOL fDebug)
{
_hInst = hInst;
_uInitCount = 0;
_tlsParser = -1;
_uResourceID = uResourceID;
_rgRegister = rgRegister;
_cRegister = cRegister;
_pszMarkupOverrideRegValue = pszMarkupOverrideRegValue;
_fDebug = fDebug;
InitializeSRWLock(&this->_srwlock);
}
HRESULT CDUIResourceManager::InitThread()
{
ExclusiveSRW scopeLock(&this->_srwlock);
HRESULT result = 0;
if (!this->_uInitCount)
{
if (g_DUIContext)
result = g_DUIContext->InitProcess();
else
result = InitProcessPriv(9, g_twinUI, 1, 1, FALSE);
if (!SUCCEEDED(result))
return result;
result = _RegisterElements();
if (!SUCCEEDED(result))
return result;
_tlsParser = TlsAlloc();
if (_tlsParser == TLS_OUT_OF_INDEXES)
{
if (g_DUIContext)
g_DUIContext->UnInitProcess();
else
UnInitProcessPriv(g_twinUI);
return E_FAIL;
}
}
if (g_DUIContext)
result = g_DUIContext->InitThread();
else
result = ::InitThread(2);
if (!SUCCEEDED(result))
return result;
result = _InitParserForThread();
if (SUCCEEDED(result))
{
++_uInitCount;
return result;
}
if (g_DUIContext)
g_DUIContext->UnInitThread();
else
::UnInitThread();
if (!_uInitCount)
{
TlsFree(_tlsParser);
_tlsParser = -1;
if (g_DUIContext)
g_DUIContext->UnInitProcess();
else
UnInitProcessPriv(g_twinUI);
}
return result;
}
void CDUIResourceManager::UnInitThread()
{
ExclusiveSRW scopeLock(&this->_srwlock);
--_uInitCount;
_UninitParserForThread();
if (g_DUIContext)
g_DUIContext->UnInitThread();
else
::UnInitThread();
if (!_uInitCount)
{
TlsFree(_tlsParser);
_tlsParser = -1;
if (g_DUIContext)
g_DUIContext->UnInitProcess();
else
UnInitProcessPriv(g_twinUI);
}
}
LPVOID CDUIResourceManager::GetParserForThread()
{
return ((REFCOUNTEDPARSER*)TlsGetValue(_tlsParser))->pParser;
}
HRESULT CDUIResourceManager::ReloadParser(LPVOID* ppParser)
{
REFCOUNTEDPARSER* rcp = (REFCOUNTEDPARSER*)TlsGetValue(_tlsParser);
if (!rcp)
return E_FAIL;
LPVOID* pParser;
HRESULT result = _DebugCreateParser(rcp, &pParser);
if (SUCCEEDED(result))
{
if (rcp->pParser)
{
if (g_DUIContext)
g_DUIContext->Parser_Destroy(rcp->pParser);
else
((DirectUI::DUIXmlParser*)rcp->pParser)->Destroy();
}
rcp->pParser = pParser;
ppParser = pParser;
}
return result;
}
BOOL CDUIResourceManager::DebugReloadParser()
{
REFCOUNTEDPARSER* rcp = (REFCOUNTEDPARSER*)TlsGetValue(_tlsParser);
if (!rcp)
return FALSE;
LPWSTR debugUiFilePath;
FILETIME debugUiFileLastWrite;
LPVOID* pParser;
if (SUCCEEDED(_DebugEnsureDUIFile(&debugUiFilePath, &debugUiFileLastWrite)))
{
// Debug file is the same, or we failed loading using a newer one
if (rcp->spszDUIPath && !StrCmpIW(rcp->spszDUIPath, debugUiFilePath) && !CompareFileTime(&rcp->ftDUILastWrite, &debugUiFileLastWrite) ||
!SUCCEEDED(_DebugLoadParser(debugUiFilePath, &pParser)))
{
delete[] debugUiFilePath;
return FALSE;
}
// Loaded using a newer debug file
if (rcp->spszDUIPath)
delete[] rcp->spszDUIPath;
rcp->spszDUIPath = debugUiFilePath;
}
else
{
// Debug file wasn't used or regular DLL resource failed
if (!rcp->spszDUIPath || !SUCCEEDED(DUI_CreateParserFromResource(_hInst, _uResourceID, &pParser)))
return FALSE;
// Loaded using regular DLL resource after a debug one was used
delete[] rcp->spszDUIPath;
debugUiFileLastWrite.dwLowDateTime = 0;
debugUiFileLastWrite.dwHighDateTime = 0;
}
rcp->ftDUILastWrite = debugUiFileLastWrite;
if (g_DUIContext)
g_DUIContext->Parser_Destroy(rcp->pParser);
else
((DirectUI::DUIXmlParser*)rcp->pParser)->Destroy();
rcp->pParser = pParser;
return TRUE;
}