-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUserDirectory.cpp
139 lines (123 loc) · 3.63 KB
/
UserDirectory.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
/****************************************************************************
** Hyne Final Fantasy VIII Save Editor
** Copyright (C) 2013 Arzel Jérôme <[email protected]>
**
** 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 "UserDirectory.h"
UserDirectory::UserDirectory()
{
}
UserDirectory::UserDirectory(const QString &dirname)
{
setDirname(dirname);
}
UserDirectory::UserDirectory(const QString &metadataPath, const QString &userID) :
_userID(userID)
{
_metadata.setFilename(metadataPath);
_dirname = metadataPath.left(metadataPath.lastIndexOf('/'));
}
bool UserDirectory::isValid() const
{
return hasUserId() && hasMetadata();
}
bool UserDirectory::hasMetadata() const
{
return !_metadata.filename().isEmpty();
}
bool UserDirectory::hasUserId() const
{
return !_userID.isEmpty();
}
bool UserDirectory::openMetadata()
{
return _metadata.open();
}
void UserDirectory::updateMetadata(quint8 slot, quint8 num, const QByteArray &saveData)
{
if (saveData.isEmpty()) {
_metadata.setTimestamp(slot, num, TIMESTAMP_EMPTY);
} else if (_metadata.timestamp(slot, num) <= 0) {
_metadata.setTimestamp(slot, num, QDateTime::currentMSecsSinceEpoch());
}
_metadata.updateSignature(slot, num, saveData, _userID);
}
bool UserDirectory::updateSignatures()
{
if (!openMetadata()) {
return false;
}
for (quint8 slot = 1; slot <= 2; ++slot) {
for (quint8 num = 1; num <= 30; ++num) {
QFile f(QString("%1/slot%2_save%3.ff8").arg(_dirname).arg(slot).arg(num, 2, 10, QChar('0')));
if (f.open(QIODevice::ReadOnly)) {
updateMetadata(slot, num, f.readAll());
f.close();
} else if (!f.exists()) {
updateMetadata(slot, num);
} else {
return false;
}
}
}
QFile f(QString("%1/chocorpg.ff8").arg(_dirname));
if (f.open(QIODevice::ReadOnly)) {
if (_metadata.timestamp() <= 0) {
_metadata.setTimestamp(QDateTime::currentMSecsSinceEpoch());
}
_metadata.updateSignature(f.readAll(), _userID);
f.close();
} else if (!f.exists()) {
_metadata.setTimestamp(TIMESTAMP_EMPTY);
_metadata.updateSignature(QByteArray(), _userID);
} else {
return false;
}
if (!saveMetadata()) {
return false;
}
return true;
}
bool UserDirectory::saveMetadata()
{
return _metadata.save();
}
void UserDirectory::setDirname(const QString &dirname)
{
_dirname = dirname;
_userID = extractUserID(_dirname);
QString metadataPath = _dirname + "/metadata.xml";
if (QFile::exists(metadataPath)) {
_metadata.setFilename(metadataPath);
}
}
const QString &UserDirectory::errorString() const
{
return _metadata.errorString();
}
QString UserDirectory::extractUserID(const QString &dirname)
{
int index = dirname.lastIndexOf('/', -2);
QString lastDirname;
if (index < 0) {
lastDirname = dirname;
} else {
lastDirname = dirname.mid(index + 1);
}
if (lastDirname.startsWith("user_")) {
return lastDirname.mid(5);
}
return QString();
}