forked from myst6re/deling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FF8DiscArchive.cpp
271 lines (216 loc) · 7.13 KB
/
FF8DiscArchive.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
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
/****************************************************************************
** Deling Final Fantasy VIII Field Editor
** Copyright (C) 2009-2012 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 "FF8DiscArchive.h"
FF8DiscFile::FF8DiscFile() :
size(0)
{
}
FF8DiscFile::FF8DiscFile(quint32 pos, quint32 size) :
pos(pos), size(size)
{
}
bool FF8DiscFile::isValid() const
{
return size != 0;
}
quint32 FF8DiscFile::getPos() const
{
return pos;
}
quint32 FF8DiscFile::getSize() const
{
return size;
}
FF8DiscArchive::FF8DiscArchive(const QString &name) :
IsoArchive(name), disc(0), PAL(false)
{
}
bool FF8DiscArchive::findIMG()
{
if(seekToFile("FF8DISC1.IMG")) {
disc = 1;
} else if(seekToFile("FF8DISC2.IMG")) {
disc = 2;
} else if(seekToFile("FF8DISC3.IMG")) {
disc = 3;
} else if(seekToFile("FF8DISC4.IMG")) {
disc = 4;
} else {
return false;
}
posIMG = posIso();
return true;
}
bool FF8DiscArchive::IMGFound() const
{
return disc != 0;
}
quint8 FF8DiscArchive::discNumber() const
{
return disc;
}
bool FF8DiscArchive::isJp() const
{
foreach(IsoFile *file, IsoArchive::rootDirectory()->files()) {
const QString &name = file->name();
if(name.startsWith("SLPS") || name.startsWith("SCPS") || name.startsWith("SLPM"))
return true;
}
return false;
}
bool FF8DiscArchive::isPAL() const
{
return PAL;
}
QByteArray FF8DiscArchive::file(const FF8DiscFile &file)
{
if(!file.isValid())
return QByteArray();
if(seekToSector(file.getPos()))
return readIso(file.getSize());
else
return QByteArray();
}
QByteArray FF8DiscArchive::fileLZS(const FF8DiscFile &file, bool strict)
{
if(!file.isValid()) return QByteArray();
quint32 lzsSize;
if(!seekToSector(file.getPos())) return QByteArray();
if(readIso((char *)&lzsSize, 4) != 4) return QByteArray();
// strict = same size as specified | !strict = same sector count as specified
if((strict && file.getSize() != lzsSize+4) || (!strict && (lzsSize + 4)/SECTOR_SIZE_DATA + (int)(lzsSize%SECTOR_SIZE_DATA != 0) != file.getSize()/SECTOR_SIZE_DATA + (int)(file.getSize()%SECTOR_SIZE_DATA != 0)))
return QByteArray();
return LZS::decompressAll(readIso(lzsSize).constData(), lzsSize);
}
QByteArray FF8DiscArchive::fileGZ(const FF8DiscFile &file)
{
if(!file.isValid()) return QByteArray();
if(!seekToSector(file.getPos())) return QByteArray();
seekIso(posIso() + 8);
return GZIP::decompress(readIso(file.getSize()-8));
}
bool FF8DiscArchive::extract(const FF8DiscFile &file, const QString &destination)
{
QByteArray data = this->file(file);
if(data.isEmpty()) return false;
QFile ret(destination);
if(!ret.open(QIODevice::WriteOnly)) return false;
return ret.write(data) != -1;
}
bool FF8DiscArchive::extractLZS(const FF8DiscFile &file, const QString &destination, bool strict)
{
QByteArray data = fileLZS(file, strict);
if(data.isEmpty()) return false;
QFile ret(destination);
if(!ret.open(QIODevice::WriteOnly)) return false;
return ret.write(data) != -1;
}
bool FF8DiscArchive::extractGZ(const FF8DiscFile &file, const QString &destination)
{
QByteArray data = fileGZ(file);
if(data.isEmpty()) return false;
QFile ret(destination);
if(!ret.open(QIODevice::WriteOnly)) return false;
return ret.write(data) != -1;
}
const QList<FF8DiscFile> &FF8DiscArchive::rootDirectory()
{
if(!rootFiles.isEmpty() || !IMGFound()) return rootFiles;
quint32 position, size, numSectors = sectorCount();
qint64 maxPos;
seekIso(posIMG);
readIso((char *)&position, 4);
readIso((char *)&size, 4);
if(position >= numSectors || size == 0 || position + size/SECTOR_SIZE_DATA >= numSectors) {
seekIso(posIMG + 5 * SECTOR_SIZE_DATA); // PAL hack
readIso((char *)&position, 4);
readIso((char *)&size, 4);
if(position >= numSectors || size == 0 || position + size/SECTOR_SIZE_DATA >= numSectors) {
return rootFiles;
}
PAL = true;
}
rootFiles.append(FF8DiscFile(position, size));
maxPos = posIso() + SECTOR_SIZE_DATA;
while(posIso() < maxPos) {
readIso((char *)&position, 4);
readIso((char *)&size, 4);
if(position < numSectors && size != 0 && position + size/SECTOR_SIZE_DATA < numSectors)
rootFiles.append(FF8DiscFile(position, size));
else
break;
}
return rootFiles;
}
int FF8DiscArchive::rootCount()
{
return rootDirectory().size();
}
const FF8DiscFile &FF8DiscArchive::rootFile(int id)
{
return rootDirectory().at(id);
}
const QList<FF8DiscFile> &FF8DiscArchive::fieldDirectory()
{
if(!fieldFiles.isEmpty()) return fieldFiles;
quint32 position, size, numSectors = sectorCount();
if(2 >= rootCount()) return fieldFiles;
const FF8DiscFile &fieldbinFile = rootFile(2);
// // nbSectorsLzs != nbSectors
// if((lzsSize + 4)/SECTOR_SIZE_DATA + (int)(lzsSize%SECTOR_SIZE_DATA != 0) != fieldbinFile.getSize()/SECTOR_SIZE_DATA + (int)(fieldbinFile.getSize()%SECTOR_SIZE_DATA != 0))
// return fieldFiles;
const QByteArray fieldBin = fileLZS(fieldbinFile, false);
if(fieldBin.isEmpty())
return fieldFiles;
const char *fieldBinData = fieldBin.constData();
int index, lastIndex, tocSize;
if((index = fieldBin.indexOf(QByteArray("\x04\x00\x05\x24\x18\x00\xbf\x8f\x14\x00\xb1\x8f\x10\x00\xb0\x8f\x20\x00\xbd\x27\x08\x00\xe0\x03\x00\x00\x00\x00", 28))) != -1) {
index += 28;// with main field models
} else {
qWarning() << "bin invalide : index introuvable !";
return fieldFiles;
// index = jap ? 0x28668 : 0x286a0;// with main field models
}
if((lastIndex = fieldBin.indexOf(QByteArray("\x00\x00\x01\x00\x02\x00\x03\x00", 8), index)) != -1) {
if((lastIndex - index) % 8 != 0) {
qWarning() << "bin invalide : lastIndex invalide !" << index << lastIndex;
return fieldFiles;
}
tocSize = (lastIndex - index) / 8;
} else {
qWarning() << "bin invalide : lastIndex introuvable !" << index;
return fieldFiles;
}
for(int i=0 ; i<tocSize ; ++i) {
memcpy(&position, fieldBinData + index + i*8, 4);
memcpy(&size, fieldBinData + index + i*8 + 4, 4);
if(position < numSectors && size != 0 && position + size/SECTOR_SIZE_DATA < numSectors)
fieldFiles.append(FF8DiscFile(position, size));
else
break;
}
return fieldFiles;
}
int FF8DiscArchive::fieldCount()
{
return fieldDirectory().size();
}
const FF8DiscFile &FF8DiscArchive::fieldFile(int id)
{
return fieldDirectory().at(id);
}