forked from c-f-h/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
308 lines (264 loc) · 7.51 KB
/
main.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
// VPinball.cpp : Implementation of WinMain
#include "StdAfx.h"
#include "resource.h"
#include <initguid.h>
#define SET_CRT_DEBUG_FIELD(a) _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#include "vpinball_i.c"
#ifndef DISABLE_FORCE_NVIDIA_OPTIMUS
extern "C" {
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}
#endif
#if (WINVER <= 0x0601 /* _WIN32_WINNT_WIN7 */ )
typedef enum ORIENTATION_PREFERENCE {
ORIENTATION_PREFERENCE_NONE = 0x0,
ORIENTATION_PREFERENCE_LANDSCAPE = 0x1,
ORIENTATION_PREFERENCE_PORTRAIT = 0x2,
ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED = 0x4,
ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED = 0x8
} ORIENTATION_PREFERENCE;
typedef BOOL (WINAPI *pSDARP)(ORIENTATION_PREFERENCE orientation);
static pSDARP SetDisplayAutoRotationPreferences = NULL;
#endif
#if !defined(DEBUG_XXX) && !defined(_CRTDBG_MAP_ALLOC)
void *operator new( const size_t size_req )
{
return _aligned_malloc( size_req, 16 );
}
void operator delete( void *address )
{
_aligned_free(address);
}
void *operator new[]( const size_t size_req )
{
return _aligned_malloc( size_req, 16 );
}
void operator delete[]( void *address )
{
_aligned_free(address);
}
#endif
const DWORD dwPause = 1000; // time to wait for threads to finish up
CExeModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
PCHAR* CommandLineToArgvA(PCHAR CmdLine, int* _argc)
{
PCHAR* argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len+2)/2)*sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR*)malloc(i + (len+2)*sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv)+i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while( a = CmdLine[i] ) {
if(in_QM) {
if(a == '\"') {
in_QM = FALSE;
} else {
_argv[j] = a;
j++;
}
} else {
switch(a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if(in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
extern "C" int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int /*nShowCmd*/)
{
// disable auto-rotate on tablets
#if (WINVER <= 0x0601)
SetDisplayAutoRotationPreferences = (pSDARP) GetProcAddress(GetModuleHandle(TEXT("user32.dll")),
"SetDisplayAutoRotationPreferences");
if(SetDisplayAutoRotationPreferences)
SetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_LANDSCAPE);
#else
SetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_LANDSCAPE);
#endif
g_hinst = hInstance;
#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
#else
HRESULT hRes = CoInitialize(NULL);
#endif
_ASSERTE(SUCCEEDED(hRes));
_Module.Init(ObjectMap, hInstance, &LIBID_VPinballLib);
bool fFile = false;
bool fPlay = false;
bool bRun = true;
TCHAR szTableFileName[_MAX_PATH] = {0};
int nRet = 0;
int nArgs;
LPSTR *szArglist = CommandLineToArgvA(GetCommandLine(), &nArgs);
for(int i=0; i < nArgs; ++i)
{
if (lstrcmpi(szArglist[i], _T("-UnregServer"))==0 || lstrcmpi(szArglist[i], _T("/UnregServer"))==0)
{
_Module.UpdateRegistryFromResource(IDR_VPINBALL, FALSE);
nRet = _Module.UnregisterServer(TRUE);
bRun = false;
break;
}
if (lstrcmpi(szArglist[i], _T("-RegServer"))==0 || lstrcmpi(szArglist[i], _T("/RegServer"))==0)
{
_Module.UpdateRegistryFromResource(IDR_VPINBALL, TRUE);
nRet = _Module.RegisterServer(TRUE);
bRun = false;
break;
}
const bool editfile = (lstrcmpi(szArglist[i], _T("-Edit"))==0 || lstrcmpi(szArglist[i], _T("/Edit"))==0);
const bool playfile = (lstrcmpi(szArglist[i], _T("-Play"))==0 || lstrcmpi(szArglist[i], _T("/Play"))==0);
if ((editfile || playfile) && (i+1 < nArgs))
{
fFile = true;
fPlay = playfile;
// Remove leading - or /
char* filename;
if((szArglist[i+1][0] == '-') || (szArglist[i+1][0] == '/'))
filename = szArglist[i+1]+1;
else
filename = szArglist[i+1];
// Remove " "
if(filename[0] == '"') {
strcpy_s(szTableFileName,filename+1);
szTableFileName[strlen(szTableFileName)] = 0;
}
else
strcpy_s(szTableFileName,filename);
// Add current path
char szLoadDir[MAX_PATH];
if(szTableFileName[1] != ':') {
GetCurrentDirectory(MAX_PATH,szLoadDir);
strcat_s(szLoadDir,"\\");
strcat_s(szLoadDir,szTableFileName);
strcpy_s(szTableFileName,szLoadDir);
} else
// Or set from table path
if(playfile) {
PathFromFilename(szTableFileName, szLoadDir);
SetCurrentDirectory(szLoadDir);
}
if(playfile)
VPinball::SetOpenMinimized();
break;
}
}
free(szArglist);
// load and register VP type library for COM integration
char szFileName[_MAX_PATH];
if (GetModuleFileName(hInstance, szFileName, _MAX_PATH))
{
ITypeLib *ptl = NULL;
MAKE_WIDEPTR_FROMANSI(wszFileName, szFileName);
if (SUCCEEDED(LoadTypeLib(wszFileName, &ptl)))
{
// first try to register system-wide (if running as admin)
HRESULT hr = RegisterTypeLib(ptl, wszFileName, NULL);
if (!SUCCEEDED(hr))
{
// if failed, register only for current user
hr = RegisterTypeLibForUser(ptl, wszFileName, NULL);
if (!SUCCEEDED(hr))
MessageBox(0, "Could not register type library. Try running Visual Pinball as administrator.", "Error", MB_ICONWARNING);
}
ptl->Release();
}
else
MessageBox(0, "Could not load type library.", "Error", MB_ICONSTOP);
}
if (bRun)
{
#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
_ASSERTE(SUCCEEDED(hRes));
hRes = CoResumeClassObjects();
#else
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE);
#endif
_ASSERTE(SUCCEEDED(hRes));
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_COOL_CLASSES;
InitCommonControlsEx(&iccex);
g_pvp = new VPinball();
g_pvp->AddRef();
g_pvp->Init();
g_haccel = LoadAccelerators(g_hinst,MAKEINTRESOURCE(IDR_VPACCEL));
if (fFile)
{
g_pvp->LoadFileName(szTableFileName);
if (fPlay)
g_pvp->DoPlay();
}
// VBA APC handles message loop (bastards)
g_pvp->MainMsgLoop();
#ifdef VBA
g_pvp->ApcHost.Destroy();
#endif
g_pvp->Release();
DestroyAcceleratorTable(g_haccel);
_Module.RevokeClassObjects();
Sleep(dwPause); //wait for any threads to finish
}
_Module.Term();
CoUninitialize();
#ifdef _CRTDBG_MAP_ALLOC
#ifdef DEBUG_XXX //disable this in perference to DevPartner
_CrtSetDumpClient(MemLeakAlert);
#endif
_CrtDumpMemoryLeaks();
#endif
//SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
return nRet;
}