forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappfat.cpp
664 lines (594 loc) · 14.9 KB
/
appfat.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include "diablo.h"
#include "../3rdParty/Storm/Source/storm.h"
char sz_error_buf[256];
BOOL terminating;
int cleanup_thread_id;
// delete overloads the delete operator.
//void operator delete(void *ptr)
//{
// if (ptr != NULL) {
// SMemFree(ptr, "delete", -1, 0);
// }
//}
void TriggerBreak()
{
#ifdef _DEBUG
LPTOP_LEVEL_EXCEPTION_FILTER pFilter;
pFilter = SetUnhandledExceptionFilter(BreakFilter);
#ifdef USE_ASM
__asm {
int 3
}
#else
__debugbreak();
#endif
SetUnhandledExceptionFilter(pFilter);
#endif
}
#ifdef _DEBUG
LONG __stdcall BreakFilter(PEXCEPTION_POINTERS pExc)
{
if (pExc->ExceptionRecord == NULL) {
return 0;
}
if (pExc->ExceptionRecord->ExceptionCode != EXCEPTION_BREAKPOINT) {
return 0;
}
if (((BYTE *)pExc->ContextRecord->Eip)[0] == 0xCC) { // int 3
pExc->ContextRecord->Eip++;
}
return -1;
}
#endif
char *GetErrorStr(DWORD error_code)
{
DWORD upper_code;
int size;
char *chr;
upper_code = (error_code >> 16) & 0x1FFF;
if (upper_code == 0x0878) {
TraceErrorDS(error_code, sz_error_buf, 256);
} else if (upper_code == 0x0876) {
TraceErrorDD(error_code, sz_error_buf, 256);
} else if (!SErrGetErrorStr(error_code, sz_error_buf, 256)
&& !FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0x400, sz_error_buf, 0x100, NULL)) {
wsprintf(sz_error_buf, "unknown error 0x%08x", error_code);
}
size = strlen(sz_error_buf);
chr = &sz_error_buf[size - 1];
while (size > 0) {
size--;
chr--;
if (*chr != '\r' && *chr != '\n')
break;
*chr = 0x00;
}
return sz_error_buf;
}
void TraceErrorDD(HRESULT hError, char *pszBuffer, DWORD dwMaxChars)
{
const char *szError;
switch (hError) {
case DDERR_CANTPAGEUNLOCK:
szError = "DDERR_CANTPAGEUNLOCK";
break;
case DDERR_NOTPAGELOCKED:
szError = "DDERR_NOTPAGELOCKED";
break;
case DD_OK:
szError = "DD_OK";
break;
case DDERR_CANTPAGELOCK:
szError = "DDERR_CANTPAGELOCK";
break;
case DDERR_BLTFASTCANTCLIP:
szError = "DDERR_BLTFASTCANTCLIP";
break;
case DDERR_NOBLTHW:
szError = "DDERR_NOBLTHW";
break;
case DDERR_NODDROPSHW:
szError = "DDERR_NODDROPSHW";
break;
case DDERR_OVERLAYNOTVISIBLE:
szError = "DDERR_OVERLAYNOTVISIBLE";
break;
case DDERR_NOOVERLAYDEST:
szError = "DDERR_NOOVERLAYDEST";
break;
case DDERR_INVALIDPOSITION:
szError = "DDERR_INVALIDPOSITION";
break;
case DDERR_NOTAOVERLAYSURFACE:
szError = "DDERR_NOTAOVERLAYSURFACE";
break;
case DDERR_EXCLUSIVEMODEALREADYSET:
szError = "DDERR_EXCLUSIVEMODEALREADYSET";
break;
case DDERR_NOTFLIPPABLE:
szError = "DDERR_NOTFLIPPABLE";
break;
case DDERR_CANTDUPLICATE:
szError = "DDERR_CANTDUPLICATE";
break;
case DDERR_NOTLOCKED:
szError = "DDERR_NOTLOCKED";
break;
case DDERR_CANTCREATEDC:
szError = "DDERR_CANTCREATEDC";
break;
case DDERR_NODC:
szError = "DDERR_NODC";
break;
case DDERR_WRONGMODE:
szError = "DDERR_WRONGMODE";
break;
case DDERR_IMPLICITLYCREATED:
szError = "DDERR_IMPLICITLYCREATED";
break;
case DDERR_NOTPALETTIZED:
szError = "DDERR_NOTPALETTIZED";
break;
case DDERR_NOMIPMAPHW:
szError = "DDERR_NOMIPMAPHW";
break;
case DDERR_INVALIDSURFACETYPE:
szError = "DDERR_INVALIDSURFACETYPE";
break;
case DDERR_DCALREADYCREATED:
szError = "DDERR_DCALREADYCREATED";
break;
case DDERR_NOPALETTEHW:
szError = "DDERR_NOPALETTEHW";
break;
case DDERR_DIRECTDRAWALREADYCREATED:
szError = "DDERR_DIRECTDRAWALREADYCREATED";
break;
case DDERR_NODIRECTDRAWHW:
szError = "DDERR_NODIRECTDRAWHW";
break;
case DDERR_PRIMARYSURFACEALREADYEXISTS:
szError = "DDERR_PRIMARYSURFACEALREADYEXISTS";
break;
case DDERR_NOEMULATION:
szError = "DDERR_NOEMULATION";
break;
case DDERR_REGIONTOOSMALL:
szError = "DDERR_REGIONTOOSMALL";
break;
case DDERR_CLIPPERISUSINGHWND:
szError = "DDERR_CLIPPERISUSINGHWND";
break;
case DDERR_NOCLIPPERATTACHED:
szError = "DDERR_NOCLIPPERATTACHED";
break;
case DDERR_NOHWND:
szError = "DDERR_NOHWND";
break;
case DDERR_HWNDSUBCLASSED:
szError = "DDERR_HWNDSUBCLASSED";
break;
case DDERR_HWNDALREADYSET:
szError = "DDERR_HWNDALREADYSET";
break;
case DDERR_NOPALETTEATTACHED:
szError = "DDERR_NOPALETTEATTACHED";
break;
case DDERR_INVALIDDIRECTDRAWGUID:
szError = "DDERR_INVALIDDIRECTDRAWGUID";
break;
case DDERR_UNSUPPORTEDFORMAT:
szError = "DDERR_UNSUPPORTEDFORMAT";
break;
case DDERR_UNSUPPORTEDMASK:
szError = "DDERR_UNSUPPORTEDMASK";
break;
case DDERR_VERTICALBLANKINPROGRESS:
szError = "DDERR_VERTICALBLANKINPROGRESS";
break;
case DDERR_WASSTILLDRAWING:
szError = "DDERR_WASSTILLDRAWING";
break;
case DDERR_XALIGN:
szError = "DDERR_XALIGN";
break;
case DDERR_TOOBIGWIDTH:
szError = "DDERR_TOOBIGWIDTH";
break;
case DDERR_CANTLOCKSURFACE:
szError = "DDERR_CANTLOCKSURFACE";
break;
case DDERR_SURFACEISOBSCURED:
szError = "DDERR_SURFACEISOBSCURED";
break;
case DDERR_SURFACELOST:
szError = "DDERR_SURFACELOST";
break;
case DDERR_SURFACENOTATTACHED:
szError = "DDERR_SURFACENOTATTACHED";
break;
case DDERR_TOOBIGHEIGHT:
szError = "DDERR_TOOBIGHEIGHT";
break;
case DDERR_TOOBIGSIZE:
szError = "DDERR_TOOBIGSIZE";
break;
case DDERR_SURFACEBUSY:
szError = "DDERR_SURFACEBUSY";
break;
case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
szError = "DDERR_OVERLAYCOLORKEYONLYONEACTIVE";
break;
case DDERR_PALETTEBUSY:
szError = "DDERR_PALETTEBUSY";
break;
case DDERR_COLORKEYNOTSET:
szError = "DDERR_COLORKEYNOTSET";
break;
case DDERR_SURFACEALREADYATTACHED:
szError = "DDERR_SURFACEALREADYATTACHED";
break;
case DDERR_SURFACEALREADYDEPENDENT:
szError = "DDERR_SURFACEALREADYDEPENDENT";
break;
case DDERR_OVERLAYCANTCLIP:
szError = "DDERR_OVERLAYCANTCLIP";
break;
case DDERR_NOVSYNCHW:
szError = "DDERR_NOVSYNCHW";
break;
case DDERR_NOZBUFFERHW:
szError = "DDERR_NOZBUFFERHW";
break;
case DDERR_NOZOVERLAYHW:
szError = "DDERR_NOZOVERLAYHW";
break;
case DDERR_OUTOFCAPS:
szError = "DDERR_OUTOFCAPS";
break;
case DDERR_OUTOFVIDEOMEMORY:
szError = "DDERR_OUTOFVIDEOMEMORY";
break;
case DDERR_NOTEXTUREHW:
szError = "DDERR_NOTEXTUREHW";
break;
case DDERR_NOROTATIONHW:
szError = "DDERR_NOROTATIONHW";
break;
case DDERR_NOSTRETCHHW:
szError = "DDERR_NOSTRETCHHW";
break;
case DDERR_NOT4BITCOLOR:
szError = "DDERR_NOT4BITCOLOR";
break;
case DDERR_NOT4BITCOLORINDEX:
szError = "DDERR_NOT4BITCOLORINDEX";
break;
case DDERR_NOT8BITCOLOR:
szError = "DDERR_NOT8BITCOLOR";
break;
case DDERR_NORASTEROPHW:
szError = "DDERR_NORASTEROPHW";
break;
case DDERR_NOEXCLUSIVEMODE:
szError = "DDERR_NOEXCLUSIVEMODE";
break;
case DDERR_NOFLIPHW:
szError = "DDERR_NOFLIPHW";
break;
case DDERR_NOGDI:
szError = "DDERR_NOGDI";
break;
case DDERR_NOMIRRORHW:
szError = "DDERR_NOMIRRORHW";
break;
case DDERR_NOTFOUND:
szError = "DDERR_NOTFOUND";
break;
case DDERR_NOOVERLAYHW:
szError = "DDERR_NOOVERLAYHW";
break;
case DDERR_NOCOLORKEYHW:
szError = "DDERR_NOCOLORKEYHW";
break;
case DDERR_NOALPHAHW:
szError = "DDERR_NOALPHAHW";
break;
case DDERR_NOCLIPLIST:
szError = "DDERR_NOCLIPLIST";
break;
case DDERR_NOCOLORCONVHW:
szError = "DDERR_NOCOLORCONVHW";
break;
case DDERR_NOCOOPERATIVELEVELSET:
szError = "DDERR_NOCOOPERATIVELEVELSET";
break;
case DDERR_NOCOLORKEY:
szError = "DDERR_NOCOLORKEY";
break;
case DDERR_NO3D:
szError = "DDERR_NO3D";
break;
case DDERR_INVALIDMODE:
szError = "DDERR_INVALIDMODE";
break;
case DDERR_INVALIDOBJECT:
szError = "DDERR_INVALIDOBJECT";
break;
case DDERR_INVALIDPIXELFORMAT:
szError = "DDERR_INVALIDPIXELFORMAT";
break;
case DDERR_INVALIDRECT:
szError = "DDERR_INVALIDRECT";
break;
case DDERR_LOCKEDSURFACES:
szError = "DDERR_LOCKEDSURFACES";
break;
case DDERR_INVALIDCLIPLIST:
szError = "DDERR_INVALIDCLIPLIST";
break;
case DDERR_CURRENTLYNOTAVAIL:
szError = "DDERR_CURRENTLYNOTAVAIL";
break;
case DDERR_EXCEPTION:
szError = "DDERR_EXCEPTION";
break;
case DDERR_HEIGHTALIGN:
szError = "DDERR_HEIGHTALIGN";
break;
case DDERR_INCOMPATIBLEPRIMARY:
szError = "DDERR_INCOMPATIBLEPRIMARY";
break;
case DDERR_INVALIDCAPS:
szError = "DDERR_INVALIDCAPS";
break;
case DDERR_CANNOTDETACHSURFACE:
szError = "DDERR_CANNOTDETACHSURFACE";
break;
case DDERR_UNSUPPORTED:
szError = "DDERR_UNSUPPORTED";
break;
case DDERR_GENERIC:
szError = "DDERR_GENERIC";
break;
case DDERR_OUTOFMEMORY:
szError = "DDERR_OUTOFMEMORY";
break;
case DDERR_INVALIDPARAMS:
szError = "DDERR_INVALIDPARAMS";
break;
case DDERR_ALREADYINITIALIZED:
szError = "DDERR_ALREADYINITIALIZED";
break;
case DDERR_CANNOTATTACHSURFACE:
szError = "DDERR_CANNOTATTACHSURFACE";
break;
default: {
const char szUnknown[] = "DDERR unknown 0x%x";
/// ASSERT: assert(dwMaxChars >= sizeof(szUnknown) + 10);
sprintf(pszBuffer, szUnknown, hError);
return;
}
}
strncpy(pszBuffer, szError, dwMaxChars);
}
void TraceErrorDS(HRESULT hError, char *pszBuffer, DWORD dwMaxChars)
{
const char *szError;
switch (hError) {
case DSERR_PRIOLEVELNEEDED:
szError = "DSERR_PRIOLEVELNEEDED";
break;
case DSERR_BADFORMAT:
szError = "DSERR_BADFORMAT";
break;
case DSERR_NODRIVER:
szError = "DSERR_NODRIVER";
break;
case DSERR_ALREADYINITIALIZED:
szError = "DSERR_ALREADYINITIALIZED";
break;
case DSERR_BUFFERLOST:
szError = "DSERR_BUFFERLOST";
break;
case DS_OK:
szError = "DS_OK";
break;
case DSERR_INVALIDCALL:
szError = "DSERR_INVALIDCALL";
break;
case E_NOINTERFACE:
szError = "E_NOINTERFACE";
break;
case DSERR_NOAGGREGATION:
szError = "DSERR_NOAGGREGATION";
break;
case DSERR_OUTOFMEMORY:
szError = "DSERR_OUTOFMEMORY";
break;
case DSERR_INVALIDPARAM:
szError = "DSERR_INVALIDPARAM";
break;
case DSERR_ALLOCATED:
szError = "DSERR_ALLOCATED";
break;
case DSERR_CONTROLUNAVAIL:
szError = "DSERR_CONTROLUNAVAIL";
break;
default: {
const char szUnknown[] = "DSERR unknown 0x%x";
/// ASSERT: assert(dwMaxChars >= sizeof(szUnknown) + 10);
sprintf(pszBuffer, szUnknown, hError);
return;
}
}
strncpy(pszBuffer, szError, dwMaxChars);
}
char *TraceLastError()
{
return GetErrorStr(GetLastError());
}
void __cdecl app_fatal(const char *pszFmt, ...)
{
va_list va;
va_start(va, pszFmt);
FreeDlg();
#ifdef _DEBUG
TriggerBreak();
#endif
if (pszFmt)
MsgBox(pszFmt, va);
va_end(va);
init_cleanup(FALSE);
exit(1);
}
void MsgBox(const char *pszFmt, va_list va)
{
char Text[256];
wvsprintf(Text, pszFmt, va);
if (ghMainWnd)
SetWindowPos(ghMainWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
MessageBox(ghMainWnd, Text, "ERROR", MB_TASKMODAL | MB_ICONHAND);
}
void FreeDlg()
{
if (terminating && cleanup_thread_id != GetCurrentThreadId())
Sleep(20000);
terminating = TRUE;
cleanup_thread_id = GetCurrentThreadId();
dx_cleanup();
if (gbMaxPlayers > 1) {
if (SNetLeaveGame(3))
Sleep(2000);
}
SNetDestroy();
ShowCursor(TRUE);
}
void __cdecl DrawDlg(char *pszFmt, ...)
{
char text[256];
va_list arglist;
va_start(arglist, pszFmt);
wvsprintf(text, pszFmt, arglist);
va_end(arglist);
SDrawMessageBox(text, "Diablo", MB_TASKMODAL | MB_ICONEXCLAMATION);
}
#ifdef _DEBUG
void assert_fail(int nLineNo, const char *pszFile, const char *pszFail)
{
app_fatal("assertion failed (%d:%s)\n%s", nLineNo, pszFile, pszFail);
}
#endif
void DDErrMsg(DWORD error_code, int log_line_nr, char *log_file_path)
{
char *msg;
if (error_code) {
msg = GetErrorStr(error_code);
app_fatal("Direct draw error (%s:%d)\n%s", log_file_path, log_line_nr, msg);
}
}
void DSErrMsg(DWORD error_code, int log_line_nr, char *log_file_path)
{
char *msg;
if (error_code) {
msg = GetErrorStr(error_code);
app_fatal("Direct sound error (%s:%d)\n%s", log_file_path, log_line_nr, msg);
}
}
void center_window(HWND hDlg)
{
LONG w, h;
int screenW, screenH;
struct tagRECT Rect;
HDC hdc;
GetWindowRect(hDlg, &Rect);
w = Rect.right - Rect.left;
h = Rect.bottom - Rect.top;
hdc = GetDC(hDlg);
screenW = GetDeviceCaps(hdc, HORZRES);
screenH = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(hDlg, hdc);
if (!SetWindowPos(hDlg, HWND_TOP, (screenW - w) / 2, (screenH - h) / 2, 0, 0, SWP_NOZORDER | SWP_NOSIZE)) {
app_fatal("center_window: %s", TraceLastError());
}
}
void ErrDlg(int template_id, DWORD error_code, char *log_file_path, int log_line_nr)
{
char *size;
LPARAM dwInitParam[128];
FreeDlg();
size = strrchr(log_file_path, '\\');
if (size)
log_file_path = size + 1;
wsprintf((LPSTR)dwInitParam, "%s\nat: %s line %d", GetErrorStr(error_code), log_file_path, log_line_nr);
if (DialogBoxParam(ghInst, MAKEINTRESOURCE(template_id), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)dwInitParam) == -1)
app_fatal("ErrDlg: %d", template_id);
app_fatal(NULL);
}
BOOL __stdcall FuncDlg(HWND hDlg, UINT uMsg, WPARAM wParam, char *text)
{
switch (uMsg) {
case WM_INITDIALOG:
TextDlg(hDlg, text);
break;
case WM_COMMAND:
if ((WORD)wParam == 1) {
EndDialog(hDlg, 1);
} else if ((WORD)wParam == 2) {
EndDialog(hDlg, 0);
}
break;
default:
return FALSE;
}
return TRUE;
}
void TextDlg(HWND hDlg, char *text)
{
center_window(hDlg);
if (text)
SetDlgItemText(hDlg, 1000, text);
}
void ErrOkDlg(int template_id, DWORD error_code, char *log_file_path, int log_line_nr)
{
char *size;
LPARAM dwInitParam[128];
size = strrchr(log_file_path, '\\');
if (size)
log_file_path = size + 1;
wsprintf((LPSTR)dwInitParam, "%s\nat: %s line %d", GetErrorStr(error_code), log_file_path, log_line_nr);
DialogBoxParam(ghInst, MAKEINTRESOURCE(template_id), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)dwInitParam);
}
void FileErrDlg(const char *error)
{
FreeDlg();
if (!error)
error = "";
if (DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_DIALOG3), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)error) == -1)
app_fatal("FileErrDlg");
app_fatal(NULL);
}
void DiskFreeDlg(char *error)
{
FreeDlg();
if (DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_DIALOG7), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)error) == -1)
app_fatal("DiskFreeDlg");
app_fatal(NULL);
}
BOOL InsertCDDlg()
{
int nResult;
ShowCursor(TRUE);
nResult = DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_DIALOG9), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM) "");
if (nResult == -1)
app_fatal("InsertCDDlg");
ShowCursor(FALSE);
return nResult == 1;
}
void DirErrorDlg(char *error)
{
FreeDlg();
if (DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_DIALOG11), ghMainWnd, (DLGPROC)FuncDlg, (LPARAM)error) == -1)
app_fatal("DirErrorDlg");
app_fatal(NULL);
}