forked from myst6re/hyne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFF8Installation.cpp
More file actions
277 lines (239 loc) · 7.61 KB
/
FF8Installation.cpp
File metadata and controls
277 lines (239 loc) · 7.61 KB
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
/****************************************************************************
** Hyne Final Fantasy VIII Save Editor
** Copyright (C) 2013 Arzel Jérôme <myst6re@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "FF8Installation.h"
#ifdef Q_OS_WIN
#include <windows.h>
#include <winbase.h>
//#include <winerror.h>
#include <winreg.h>
#endif
FF8Installation::FF8Installation() :
_type(Custom)
{
}
FF8Installation::FF8Installation(Type type) :
_type(type)
{
switch(type) {
case Standard:
_appPath = standardFF8AppPath();
_savePaths.append(_appPath + "/save"); // TODO: \AppData\Local\VirtualStore\Program Files\Eidos Interactive\Square Soft, Inc\FINAL FANTASY VIII
break;
case Steam:
_appPath = steamFF8AppPath();
_savePaths = steamFF8UserDataPaths();
break;
case Custom:
break;
}
}
FF8Installation::FF8Installation(const QString &appPath, const QString &savePath) :
_type(Custom)
{
_appPath = appPath;
_savePaths = QStringList(savePath);
}
bool FF8Installation::isValid() const
{
return !_appPath.isEmpty() && QFile::exists(_appPath)
&& !_savePaths.isEmpty();
}
QString FF8Installation::savePath(int slot) const
{
if(_savePaths.isEmpty()) {
return QString();
}
QString path = _savePaths.first();
switch(_type) {
case Standard:
return QString("%1/Slot%2").arg(path).arg(slot);
case Steam:
return path;
case Custom: // TODO: Custom custom!
return path;
}
Q_ASSERT(false);
return QString();
}
QString FF8Installation::exeFilename() const
{
switch(_type) {
case Standard:
return "FF8.exe";
case Steam:
return "FF8_Launcher.exe";
case Custom: // TODO: Custom custom!
return QString();
}
Q_ASSERT(false);
return QString();
}
QString FF8Installation::typeString() const
{
switch(_type) {
case Standard:
return QObject::tr("FF8 Standard");
case Steam:
return QObject::tr("FF8 Steam");
case Custom:
return QObject::tr("FF8 personnalisé");
}
Q_ASSERT(false);
return QString();
}
QString FF8Installation::saveNamePattern(quint8 slot) const
{
switch(_type) {
case Standard:
return "save{num}";
case Steam:
return QString("slot%1_save{num}.ff8").arg(slot);
case Custom:
return "save{num}"; // TODO!
}
Q_ASSERT(false);
return QString();
}
bool FF8Installation::hasMetadata() const
{
return _type == Steam; // TODO: Custom
}
QMap<FF8Installation::Type, FF8Installation> FF8Installation::installations()
{
QMap<FF8Installation::Type, FF8Installation> ret;
ret[Standard] = FF8Installation(Standard);
ret[Steam] = FF8Installation(Steam);
QMutableMapIterator<FF8Installation::Type, FF8Installation> it(ret);
while(it.hasNext()) {
if(!it.next().value().isValid()) {
it.remove();
}
}
return ret;
}
QString FF8Installation::standardFF8AppPath()
{
return QDir::cleanPath( QDir::fromNativeSeparators( regValue("Square Soft, Inc/Final Fantasy VIII/1.00", "AppPath") ) );
}
QString FF8Installation::steamFF8AppPath()
{
QStringList apps = searchInstalledApps("FINAL FANTASY VIII", "SQUARE ENIX", 1);
return apps.isEmpty() ? QString() : apps.first();
}
QStringList FF8Installation::steamFF8UserDataPaths(int max)
{
QStringList ff8UserDataPaths;
foreach (const QString &documentsPath, QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)) {
QDir ff8UserDataDir(QString("%1/Square Enix").arg(documentsPath));
foreach(const QString &dir, ff8UserDataDir.entryList(QStringList("FINAL FANTASY VIII*"), QDir::Dirs)) {
QDirIterator it(ff8UserDataDir.absoluteFilePath(dir),
QStringList("user_*"),
QDir::Dirs,
QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
while(it.hasNext()) {
it.next();
ff8UserDataPaths.append(it.filePath());
if(max > 0 && ff8UserDataPaths.size() >= max) {
return ff8UserDataPaths;
}
}
}
}
return ff8UserDataPaths;
}
QString FF8Installation::regValue(const QString ®Path, const QString ®Key)
{
#ifdef Q_OS_WIN
HKEY phkResult;
LONG error;
REGSAM flags = KEY_READ;
#ifdef KEY_WOW64_32KEY
flags |= KEY_WOW64_32KEY; // if you compile in 64-bit, force reg search into 32-bit entries
#endif
// Open regPath relative to HKEY_LOCAL_MACHINE
error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (wchar_t *)QDir::toNativeSeparators("SOFTWARE/" + regPath).utf16(), 0, flags, &phkResult);
if(ERROR_SUCCESS == error) {
BYTE value[MAX_PATH];
DWORD cValue = MAX_PATH, type;
// Open regKey which must is a string value (REG_SZ)
RegQueryValueEx(phkResult, (wchar_t *)regKey.utf16(), NULL, &type, value, &cValue);
if(ERROR_SUCCESS == error && type == REG_SZ) {
RegCloseKey(phkResult);
return QString::fromUtf16((ushort *)value);
}
RegCloseKey(phkResult);
}
#else
Q_UNUSED(regPath)
Q_UNUSED(regKey)
#endif
return QString();
}
QStringList FF8Installation::searchInstalledApps(const QString &appName, const QString &publisher, int max)
{
QStringList ret;
#ifdef Q_OS_WIN
HKEY phkResult, phkResult2;
LONG error;
error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"), 0, KEY_READ, &phkResult);
if(ERROR_SUCCESS == error) {
DWORD index = 0;
WCHAR subKeyName[MAX_PATH];
DWORD subKeyCName = MAX_PATH;
while(ERROR_NO_MORE_ITEMS != (error = RegEnumKeyEx(phkResult, index, subKeyName, &subKeyCName, NULL, NULL, NULL, NULL))) {
QString subKeyNameStr = QString::fromUtf16((ushort *)subKeyName);
error = RegOpenKeyEx(phkResult, (LPCWSTR)QString("%1\\").arg(subKeyNameStr).utf16(), 0, KEY_READ, &phkResult2);
if(ERROR_SUCCESS == error) {
BYTE value[MAX_PATH];
DWORD cValue = MAX_PATH, type;
error = RegQueryValueEx(phkResult2, TEXT("DisplayName"), NULL, &type, value, &cValue);
if(ERROR_SUCCESS == error && REG_SZ == type) {
QString softwareNameStr = QString::fromUtf16((ushort *)value);
if(softwareNameStr.compare(appName, Qt::CaseInsensitive) == 0) {
error = RegQueryValueEx(phkResult2, TEXT("Publisher"), NULL, &type, value, &cValue);
if(ERROR_SUCCESS == error && REG_SZ == type) {
QString publisherStr = QString::fromUtf16((ushort *)value);
if(publisherStr.compare(publisher, Qt::CaseInsensitive) == 0) {
cValue = MAX_PATH;
error = RegQueryValueEx(phkResult2, TEXT("InstallLocation"), NULL, &type, value, &cValue);
if(ERROR_SUCCESS == error && REG_SZ == type) {
ret.append(QDir::fromNativeSeparators(QDir::cleanPath(QString::fromUtf16((ushort *)value))));
if(max > 0 && ret.size() >= max) {
RegCloseKey(phkResult2);
RegCloseKey(phkResult);
return ret;
}
}
}
}
}
}
}
RegCloseKey(phkResult2);
++index;
subKeyCName = MAX_PATH;
}
RegCloseKey(phkResult);
}
#else
Q_UNUSED(appName)
Q_UNUSED(publisher)
Q_UNUSED(max)
#endif
return ret;
}