~sithlord48/hyne/master

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
/****************************************************************************
 ** 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 "Metadata.h"

Metadata::Metadata()
{
}

Metadata::Metadata(const QString &filename) :
	_filename(filename)
{
}

bool Metadata::open()
{
	QFile f(_filename);
	if(!f.open(QIODevice::ReadOnly)) {
		setErrorString(f.errorString());
		return false;
	}

	QXmlStreamReader xml(&f);
	bool isInSaveFile = false, ok;
	quint8 num = 0, slot = 0;
	QString type;
	MetadataSaveFile saveFile;

	_hasChocoFile = false;

	while(!xml.atEnd()) {
		switch(xml.readNext()) {

		case QXmlStreamReader::StartElement:

			if(!isInSaveFile && xml.name().compare("savefile", Qt::CaseInsensitive) == 0) {
				isInSaveFile = true;
				saveFile = MetadataSaveFile();
				QXmlStreamAttributes attrs = xml.attributes();
				type = attrs.value("type").toString();
				if(type.compare("ff8", Qt::CaseInsensitive) == 0) {
					num = attrs.value("num").toString().toUInt(&ok);
					if (!ok) {
						setErrorString(QObject::tr("Attribut 'num' invalide"));
						return false;
					}
					slot = attrs.value("slot").toString().toUInt(&ok);
					if (!ok) {
						setErrorString(QObject::tr("Attribut 'slot' invalide"));
						return false;
					}
				}
			} else if(isInSaveFile) {
				if(xml.name().compare("timestamp", Qt::CaseInsensitive) == 0) {
					QString timestamp = xml.readElementText();
					if(!timestamp.isEmpty()) {
						saveFile.timestamp = timestamp.toLongLong(&ok);
						if (!ok) {
							saveFile.timestamp = TIMESTAMP_INVALID;
						}
					} else {
						saveFile.timestamp = TIMESTAMP_EMPTY;
					}
				} else if(xml.name().compare("signature", Qt::CaseInsensitive) == 0) {
					saveFile.signature = xml.readElementText();
				}
			}
			break;

		case QXmlStreamReader::EndElement:

			if(isInSaveFile && xml.name().compare("savefile", Qt::CaseInsensitive) == 0) {
				isInSaveFile = false;
				if(type.compare("ff8", Qt::CaseInsensitive) == 0) {
					_saveFiles.insert(SAVE_FILES_KEY(slot, num),
									  saveFile);
				} else if(type.compare("choco", Qt::CaseInsensitive) == 0) {
					_chocoFile = saveFile; // FIXME: several choco elements?
					_hasChocoFile = true;
				}
			}
			break;

		default:
			break;
		}

	}
	if (xml.hasError()) {
		setErrorString(xml.errorString());
		return false;
	}

	return true;
}

bool Metadata::save()
{
	QFile f(_filename);
	if(!f.open(QIODevice::WriteOnly)) {
		setErrorString(f.errorString());
		return false;
	}

	QXmlStreamWriter xml(&f);
	xml.setAutoFormatting(true);
	xml.setAutoFormattingIndent(METADATA_INDENT);

	xml.writeStartDocument();
	xml.writeStartElement("gamestatus");

	QMapIterator<quint16, MetadataSaveFile> it(_saveFiles);
	while(it.hasNext()) {
		it.next();
		const MetadataSaveFile &saveFile = it.value();

		xml.writeStartElement("savefile");
		QXmlStreamAttributes attrs;
		attrs.append("num", QString::number(GET_NUM(it.key())));
		attrs.append("type", "ff8");
		attrs.append("slot", QString::number(GET_SLOT(it.key())));
		xml.writeAttributes(attrs);

		writeSavefileContents(&xml, saveFile);

		xml.writeEndElement(); // savefile
	}

	if(_hasChocoFile) {
		xml.writeStartElement("savefile");
		QXmlStreamAttributes attrs;
		attrs.append("type", "choco");
		xml.writeAttributes(attrs);

		writeSavefileContents(&xml, _chocoFile);

		xml.writeEndElement(); // savefile
	}

	xml.writeEndDocument();

	if (xml.hasError()) {
		setErrorString(QObject::tr("Erreur inconnue"));
		return false;
	}

	return true;
}

void Metadata::writeSavefileContents(QXmlStreamWriter *xml, const MetadataSaveFile &saveFile)
{
	QString timestamp;
	if(saveFile.timestamp > 0) {
		timestamp = QString::number(saveFile.timestamp);
	}
	xml->writeTextElement("timestamp", timestamp);
	xml->writeTextElement("signature", saveFile.signature);
}

const QString &Metadata::filename() const
{
	return _filename;
}

void Metadata::setFilename(const QString &filename)
{
	_filename = filename;
}

QString Metadata::signature(quint8 slot, quint8 num) const
{
	return _saveFiles.value(SAVE_FILES_KEY(slot, num)).signature;
}

void Metadata::updateSignature(quint8 slot, quint8 num, const QByteArray &saveData, const QString &userID)
{
	 _saveFiles[SAVE_FILES_KEY(slot, num)].signature = md5sum(saveData, userID);
}

qint64 Metadata::timestamp(quint8 slot, quint8 num) const
{
	return _saveFiles.value(SAVE_FILES_KEY(slot, num)).timestamp;
}

void Metadata::setTimestamp(quint8 slot, quint8 num, qint64 timestamp)
{
	 _saveFiles[SAVE_FILES_KEY(slot, num)].timestamp = timestamp;
}

QString Metadata::signature() const
{
	return _chocoFile.signature;
}

void Metadata::updateSignature(const QByteArray &saveData, const QString &userID)
{
	if(_hasChocoFile) {
		_chocoFile.signature = md5sum(saveData, userID);
	}
}

qint64 Metadata::timestamp() const
{
	return _hasChocoFile ? _chocoFile.timestamp : TIMESTAMP_INVALID;
}

void Metadata::setTimestamp(qint64 timestamp)
{
	if(_hasChocoFile) {
		_chocoFile.timestamp = timestamp;
	}
}

QString Metadata::md5sum(const QByteArray &lzsData, const QString &userID)
{
	// In FF8 md5sum is computed on save data (LZSed) + userID
	QCryptographicHash md5(QCryptographicHash::Md5);
	md5.addData(lzsData + userID.toLatin1());
	return md5.result().toHex().toLower();
}

MetadataSaveFile Metadata::createSaveFile(quint8 slot, quint8 num)
{
	MetadataSaveFile saveFile = MetadataSaveFile();
	_saveFiles.insert(SAVE_FILES_KEY(slot, num), saveFile);

	return saveFile;
}

MetadataSaveFile Metadata::createSaveFile()
{
	_chocoFile = MetadataSaveFile();

	return _chocoFile;
}

const QString &Metadata::errorString() const
{
	return _lastErrorString;
}

void Metadata::setErrorString(const QString &str)
{
	_lastErrorString = str;
}