-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
142 lines (127 loc) · 3.9 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
#include "Assets/Assets.h"
#include "General/Exceptions.h"
#include "General/Utils.h"
#include "Mod.h"
#include <iostream>
#include <windows.h>
#include <shlwapi.h>
using namespace std;
// Returns the base directory for a game, based on
// 1. Registry
// 2. Default path, expanded
static wstring GetExePath(const wchar_t* regkey, const wchar_t* valname, const wchar_t* defpath = NULL)
{
// The games are 32-bit games, so make these calls in 32-bit mode
wstring result;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &hKey ) == ERROR_SUCCESS)
{
DWORD type, size = MAX_PATH;
TCHAR path[MAX_PATH];
if (RegQueryValueEx(hKey, valname, NULL, &type, (LPBYTE)path, &size) == ERROR_SUCCESS)
{
if (PathRemoveFileSpec(path) && PathIsDirectory(path))
{
result = path;
}
}
RegCloseKey(hKey);
}
if (result.empty() && defpath != NULL)
{
// Use the default path
TCHAR path[MAX_PATH];
if (ExpandEnvironmentStrings(defpath, path, MAX_PATH) != 0)
{
if (PathIsDirectory(path))
{
result = path;
}
}
}
return result;
}
// Returns the base directory for a game
static wstring GetBaseDirForGame(GameID game)
{
switch (game)
{
case GID_EAW_FOC:
return GetExePath(L"Software\\LucasArts\\Star Wars Empire at War Forces of Corruption\\1.0", L"ExePath", L"%PROGRAMFILES%\\LucasArts\\Star Wars Empire at War Forces of Corruption");
case GID_EAW:
return GetExePath(L"Software\\LucasArts\\Star Wars Empire at War\\1.0", L"ExePath", L"%PROGRAMFILES%\\LucasArts\\Star Wars Empire at War\\GameData");
}
return L"";
}
int main(int argc, const char* argv[])
{
#ifndef NDEBUG
// In debug mode we turn on memory checking
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
#endif
#ifdef NDEBUG
try
#endif
{
GameID game = GID_NONE;
// Parse arguments
for (int i = 1; i < argc; i++)
{
if (argv[i][0] == '/' || argv[i][0] == '-')
{
if (_stricmp(argv[i] + 1, "EAW") == 0) {
game = GID_EAW;
}
else if (_stricmp(argv[i] + 1, "FOC") == 0) {
game = GID_EAW_FOC;
}
}
}
if (game == GID_NONE)
{
// Try to find game type based on current path
TCHAR buffer[MAX_PATH];
GetCurrentDirectory(MAX_PATH, buffer);
wstring curdir = Utils::Uppercase(buffer);
for (int i = 0; i < NUM_GAMES; i++)
{
wstring path = Utils::Uppercase(GetBaseDirForGame( (1 << i) ));
if (curdir.compare(0, path.length(), path) == 0) {
game = (1 << i);
break;
}
}
if (game == GID_NONE)
{
throw runtime_error("Unable to detect game type. Please specify /FOC or /EAW on the command line.");
}
}
wstring old_path, main_path = GetBaseDirForGame(GID_EAW);
if (game == GID_EAW_FOC) {
old_path = main_path;
main_path = GetBaseDirForGame(GID_EAW_FOC);
}
ChecksumMap reference;
// Load the reference objects without mod path
Assets::Initialize(wstring(), main_path, old_path);
Mod::LoadReferenceObjects(reference);
// Now do everything, with mod path
Assets::Initialize(L".", main_path, old_path);
Mod mod(game, reference);
Assets::Uninitialize();
}
#ifdef NDEBUG
catch (exception& e)
{
cerr << e.what() << endl;
return 1;
}
catch (wexception& e)
{
wcerr << e.what() << endl;
return 1;
}
#endif
return 0;
}