forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinmain.cpp
214 lines (186 loc) · 5.37 KB
/
winmain.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
#include "CompilerEnvironment.h"
// Pre written headers
#include <windows.h>
#include <excpt.h>
// Our headers.
#include "SafeProgramming.h"
#include "Object.h"
#include "String.h"
#include "GameWindow.h"
#include "Populous.h"
//#include "GameControl.h"
#include "RegistryFunctions.h"
#include "GameDefines.h"
// ----
// Global variable to store address of exception data.
LPEXCEPTION_POINTERS GlobalExecptionPointerStore;
// ----
// Function call as Structured Exception Handling (SEH)
// exception filter.
int CatchAll(LPEXCEPTION_POINTERS _a)
{
GlobalExecptionPointerStore = _a;
return EXCEPTION_EXECUTE_HANDLER;
}
// ----
// Program entry point.
int WINAPI WinMain( HINSTANCE _hInstance,
HINSTANCE _hPrevInstance,
LPTSTR _lpCmdLine,
int _nCmdShow)
{
// No execptions yet.
GlobalExecptionPointerStore = NULL;
bool ErrorOccured = false;
bool FileMissing = false;
bool LowMemWarning = false;
CPopulous* TheGame = NULL;
// Check memory levels.
MEMORYSTATUS StartMem;
StartMem.dwLength = sizeof(StartMem);
GlobalMemoryStatus(&StartMem);
TCHAR memconvert[80];
wsprintf(memconvert, TEXT("Free mem: %d\n"), StartMem.dwAvailPhys );
OutputDebugString(memconvert);
if (StartMem.dwAvailPhys < 3040870) // Demain 2.9Mb - gives us room to move
{
LowMemWarning = true;
}
//#ifndef NOT_FINAL_BUILD
//#if 0
__try
{
//#endif
//#endif
bool ContinueTitles = true;
bool QuickEscape = false;
NEW(TheGame, CPopulous());
SAFELY_CONTROL(if, TheGame, Init(_hInstance, _lpCmdLine, _nCmdShow), ==, true)
{
while (ContinueTitles)
{
switch (TheGame->title())
{
case -10:
TheGame->OptionsScreen();
break;
case -11:
TheGame->Help();
break;
case -12:
QuickEscape = true;
ContinueTitles = false;
break;
#ifndef BOXEDSALE
// Product activation
case -13:
if (!TheGame->GetPreferences()->Registered)
{
TheGame->RegistrationScreen();
}
break;
#endif
default:
ContinueTitles = false;
}
}
if (!QuickEscape)
{
SAFELY_CALL(TheGame, Run());
ErrorOccured = TheGame->DidAnErrorOccur();
}
}
else
ErrorOccured = true;
// See if it was a missing file that caused the problem
if (ErrorOccured)
FileMissing = SAFELY_RETURN(TheGame, WasFileMissingError());
//#ifndef NOT_FINAL_BUILD
//#if 0
}
__except(CatchAll(GetExceptionInformation()))
{
__try
{
if (GlobalExecptionPointerStore)
{
FILE* FH;
SYSTEMTIME ExceptionTime;
GetSystemTime(&ExceptionTime);
TCHAR convert[256];
wsprintf( convert,
TEXT("\\Populous_Failure%d%d%d-%d%d%d%d.dat"),
ExceptionTime.wYear,
ExceptionTime.wMonth,
ExceptionTime.wDay,
ExceptionTime.wHour,
ExceptionTime.wMinute,
ExceptionTime.wSecond,
ExceptionTime.wMilliseconds);
// Open save file.
FH = FOPEN(convert, "wb");
if (FH)
{
fwrite("CONTEXT\n", 9, 1, FH);
fwrite(GlobalExecptionPointerStore->ContextRecord, sizeof(CONTEXT), 1, FH);
EXCEPTION_RECORD *ER = GlobalExecptionPointerStore->ExceptionRecord;
while (ER)
{
fwrite("\nRECORD\n", 9, 1, FH);
fwrite(ER->ExceptionRecord, sizeof(EXCEPTION_RECORD), 1, FH);
ER = ER->ExceptionRecord;
}
fwrite("\nEND", 5, 1, FH);
fclose(FH);
}
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
OutputDebugString(TEXT("\n\nFAILURE\nDied in first exeception handler. Giving up!\n"));
}
ErrorOccured = true;
}
//#endif
//#endif
// Take outside the exception handle.
SAFELY_DELETE(TheGame);
// Make this give the option to create an automatic error report.
if (ErrorOccured)
{
if (LowMemWarning)
{
// Low memory warning ignored
MessageBox(NULL, TEXT("Unfortunately Populous failed as the system is low on free memory. Close other applications and delete old files, and try running the game again."), PROJECT_NAME, MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
}
else if (FileMissing)
{
// File(s) missing
MessageBox(NULL, TEXT("Sorry, one (or more) file(s) required by Populous are corrupt or missing.\n\nPlease check that the game is correctly installed.\n\nIf your problems persist, or you require further information then please email [email protected] stating the nature of the problem."), PROJECT_NAME, MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
}
else
{
// Generic
MessageBox(NULL, TEXT("Sorry, an error occured whilst running Populous.\n\nPlease check that the game is correctly installed, and that the data files have not been moved or deleted. \n\nIf your problems persist, or you require further information then please email [email protected] stating the nature of the problem."), PROJECT_NAME, MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
}
}
// Ensure that all file handles are dropped.
_fcloseall();
// Print out a list of undeleted objects...
OutputDebugString(TEXT("\n\n\nTAKE NOTE:\nA list of CObject based objects in existance at shutdown follows:"));
CObject::OutputObjectList();
#if 0
#ifdef NOT_FINAL_BUILD
#ifdef _UNICODE
TCHAR buffer[256];
swprintf(buffer, TEXT("%d objects not freed.\n\nA list of objects, and where they were created can be found by looking at the debug output.\n"), CObject::GetObjectCount());
OutputDebugString(buffer);
#ifdef NOT_FINAL_BUILD
if (CObject::GetObjectCount())
MessageBox(NULL, buffer, PROJECT_NAME, MB_OK);
#endif
#endif
#endif
#endif
return 0;
}