-
Notifications
You must be signed in to change notification settings - Fork 16
/
paths.cpp
141 lines (132 loc) · 4.3 KB
/
paths.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
#include "paths.h"
#include "libwololokingdoms/platform.h"
#include "libwololokingdoms/string_helpers.h"
#include <fs.h>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <steam/steam_api.h>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#endif
static fs::path extractHDPath(fs::path steamPath) {
std::string line;
fs::path hdPath;
std::ifstream manifest(steamPath / "steamapps" / "appmanifest_221380.acf");
while (std::getline(manifest, line)) {
size_t i;
if ((i = line.find("installdir")) != std::string::npos) {
i = line.find("\"", i + 11);
int j = line.find("\"", i + 1);
line = line.substr(i + 1, j - i - 1);
hdPath = steamPath / "steamapps" / "common" / line;
break;
}
}
manifest.close();
return fs::path(hdPath);
}
fs::path getHDPath(fs::path steamPath) {
fs::path hdPath("../");
std::string line;
if (fs::exists(steamPath / "steamapps" / "appmanifest_221380.acf")) {
hdPath = extractHDPath(steamPath);
} else if (fs::exists(steamPath / "steamapps" / "libraryfolders.vdf")) {
std::ifstream libraryFolders(steamPath / "steamapps" /
"libraryfolders.vdf");
while (std::getline(libraryFolders, line)) {
size_t i;
if ((i = line.find("\"1\"")) != std::string::npos ||
(i = line.find("\"2\"")) != std::string::npos ||
(i = line.find("\"3\"")) != std::string::npos) {
i = line.find("\"", i + 3);
int j = line.find("\"", i + 1);
line = line.substr(i + 1, j - i - 1);
steamPath = line;
if (fs::exists(steamPath / "steamapps" / "appmanifest_221380.acf")) {
hdPath = extractHDPath(steamPath);
break;
}
}
}
libraryFolders.close();
}
if (!fs::exists(hdPath / "Launcher.exe")) {
if (fs::exists("../../Launcher.exe")) {
hdPath = fs::path("../../");
} else if (fs::exists(steamPath / "steamapps" / "common" / "Age2HD")) {
hdPath = fs::path(steamPath / "steamapps" / "common" / "Age2HD");
} else { // Error Case
hdPath = fs::path();
}
}
return hdPath;
}
#ifdef _WIN32
fs::path getExePath() {
wchar_t exePath[MAX_PATH];
auto pathLength = GetModuleFileName(nullptr, exePath, MAX_PATH);
if (pathLength == 0) {
return fs::path();
}
auto str = std::wstring(exePath);
return fs::path(str);
}
std::wstring readRegistryKey(std::wstring keyPath, std::wstring key) {
wchar_t temp[300];
unsigned long size = sizeof(temp);
HKEY hKey;
BOOL w64;
IsWow64Process(GetCurrentProcess(), &w64);
LONG result = 0;
for (int i = 0; i < 2; i++) {
if (w64)
result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
(L"Software\\WOW6432Node\\" + keyPath).c_str(), 0,
KEY_READ, &hKey);
else
result =
RegOpenKeyEx(HKEY_LOCAL_MACHINE, (L"Software\\" + keyPath).c_str(), 0,
KEY_READ, &hKey);
if (result == ERROR_SUCCESS)
break;
// If the key read wasn't successful, maybe the 32/64-bit check
// was erroneous, we'll check the other key
w64 = !w64;
}
if (result != ERROR_SUCCESS)
return L"";
RegQueryValueEx(hKey, key.c_str(), nullptr, nullptr,
reinterpret_cast<LPBYTE>(temp), &size);
RegCloseKey(hKey);
return std::wstring(temp);
}
fs::path getSteamPath() {
return readRegistryKey(L"Valve\\Steam", L"InstallPath");
}
fs::path getOutPath(fs::path hdPath) {
std::string outPathString =
wstrtostr(readRegistryKey(L"Microsoft\\DirectPlay\\Applications\\Age of "
L"Empires II - The Conquerors Expansion",
L"CurrentDirectory"));
if (outPathString.at(outPathString.length() - 1) != '\\')
outPathString += "\\";
fs::path outPath(outPathString);
if (!fs::exists(outPath / "age2_x1")) {
// If currentDirectory points one level too deep, had
// this happen with a faulty aoe2tools installation
if (fs::exists(outPath.parent_path().parent_path() / "age2_x1")) {
// ParentPath needs to be called twice because the
// first one only removes the trailing /
outPath = outPath.parent_path().parent_path();
} else if (fs::exists(hdPath / "age2_x1")) {
outPath = hdPath;
} else {
outPath = fs::path();
}
}
return outPath;
}
#endif