Repository navigation
Expand file tree
/
Copy pathisotool.cpp
More file actions
290 lines (234 loc) · 9.45 KB
/
Copy pathisotool.cpp
File metadata and controls
290 lines (234 loc) · 9.45 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
278
279
280
281
282
283
284
285
286
287
288
289
290
// ============================================================
// isotool.cpp
// دوال التعامل مع ملفات ISO (Xbox 360 / XDVDFS) + استخراج حزم STFS تلقائياً
// (نسخة محسّنة للذاكرة: كتابة مباشرة على القرص بدل تخزين كل ملف بالكامل في RAM)
// ============================================================
#include "isotool.h"
#include "debughelper.h"
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <fstream>
#include "iso_reader.h"
#include "xex_parser.h"
#include "stfs_reader.h"
// ============================================================
// 1. ثوابت (Constants)
// ============================================================
namespace {
const QString ERROR_FILE_NOT_FOUND = "File not found: ";
const QString ERROR_OPEN_ISO = "Failed to open ISO file: ";
const QString ERROR_READ_CONTENTS = "Failed to read ISO contents.";
const QString ERROR_NO_FILES = "No file was extracted.";
}
// ============================================================
// 2. دوال مساعدة (Helper Functions)
// ============================================================
static bool fileExists(const QString &filePath, QString &errorMessage)
{
if (!QFileInfo(filePath).exists()) {
errorMessage = ERROR_FILE_NOT_FOUND + filePath;
return false;
}
return true;
}
static bool createOutputDir(const QString &outputPath, QString &errorMessage)
{
if (!QDir().mkpath(outputPath)) {
errorMessage = "Failed to create the destination folder: " + outputPath;
return false;
}
return true;
}
// ============================================================
// 3. عرض محتويات ISO
// ============================================================
QStringList getIsoContents(const QString &isoPath, QString &errorMessage)
{
debugFunctionStart("getIsoContents");
errorMessage.clear();
if (!fileExists(isoPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("getIsoContents");
return QStringList();
}
IsoReader iso;
if (!iso.Open(isoPath.toStdString())) {
errorMessage = ERROR_OPEN_ISO + isoPath;
debugError(errorMessage);
debugFunctionEnd("getIsoContents");
return QStringList();
}
auto entries = iso.ListAllFiles();
QStringList files;
for (const auto &e : entries) {
files << QString::fromStdString(e.path);
}
debugFileList("Files in ISO", files);
debugFunctionEnd("getIsoContents");
return files;
}
// ============================================================
// 4. استخراج ملف واحد من ISO (بالمسار الكامل) - كتابة مباشرة على القرص
// ============================================================
bool extractFileFromIso(const QString &isoPath, const QString &fileName, const QString &outputPath, QString &errorMessage)
{
debugFunctionStart("extractFileFromIso");
errorMessage.clear();
if (!fileExists(isoPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractFileFromIso");
return false;
}
if (!createOutputDir(outputPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractFileFromIso");
return false;
}
IsoReader iso;
if (!iso.Open(isoPath.toStdString())) {
errorMessage = ERROR_OPEN_ISO + isoPath;
debugError(errorMessage);
debugFunctionEnd("extractFileFromIso");
return false;
}
auto entries = iso.ListAllFiles();
std::string target = fileName.toStdString();
const IsoFileEntry *match = nullptr;
for (const auto &e : entries) {
if (!e.isDirectory && e.path == target) {
match = &e;
break;
}
}
if (!match) {
errorMessage = "The file is not present in the ISO: " + fileName;
debugError(errorMessage);
debugFunctionEnd("extractFileFromIso");
return false;
}
QString baseName = fileName;
int lastSep = baseName.lastIndexOf('\\');
if (lastSep >= 0) baseName = baseName.mid(lastSep + 1);
QString fullOutputPath = outputPath + "/" + baseName;
// Streaming write: bounded memory regardless of file size.
if (!iso.ExtractBySectorToDisk(match->startSector, match->fileSize, fullOutputPath.toStdString())) {
errorMessage = "ISO file read/write error";
debugError(errorMessage);
debugFunctionEnd("extractFileFromIso");
return false;
}
debugQuaZipResult("extractFileFromIso", true, fileName);
debugFunctionEnd("extractFileFromIso");
return true;
}
// ============================================================
// 5. استخراج كل الملفات من ISO + استخراج حزم STFS تلقائياً
// (كتابة مباشرة على القرص لكل ملف - لا يتم تحميل أي ملف بالكامل في RAM)
// ============================================================
bool extractAllFromIso(const QString &isoPath, const QString &outputPath,
QString &errorMessage,
ExtractProgressCallback progressCallback)
{
debugFunctionStart("extractAllFromIso");
errorMessage.clear();
if (!fileExists(isoPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractAllFromIso");
return false;
}
if (!createOutputDir(outputPath, errorMessage)) {
debugError(errorMessage);
debugFunctionEnd("extractAllFromIso");
return false;
}
IsoReader iso;
if (!iso.Open(isoPath.toStdString())) {
errorMessage = ERROR_OPEN_ISO + isoPath;
debugError(errorMessage);
debugFunctionEnd("extractAllFromIso");
return false;
}
auto entries = iso.ListAllFiles();
int xdvdfsTotal = 0;
for (const auto &e : entries) if (!e.isDirectory) xdvdfsTotal++;
int currentIndex = 0;
bool anySuccess = false;
for (const auto &e : entries) {
if (e.isDirectory) continue;
QString relPath = QString::fromStdString(e.path);
relPath.replace('\\', '/');
QString fullOutputPath = outputPath + "/" + relPath;
QFileInfo fi(fullOutputPath);
if (!QDir().mkpath(fi.absolutePath())) {
qDebug() << " Failed to create directory for:" << relPath;
currentIndex++;
if (progressCallback) progressCallback(currentIndex, xdvdfsTotal, relPath);
continue;
}
// Streaming write directly to disk - bounded memory regardless of file size.
if (!iso.ExtractBySectorToDisk(e.startSector, e.fileSize, fullOutputPath.toStdString())) {
qDebug() << " Failed to extract:" << relPath;
currentIndex++;
if (progressCallback) progressCallback(currentIndex, xdvdfsTotal, relPath);
continue;
}
anySuccess = true;
qDebug() << " Extracted Done:" << relPath << "(" << e.fileSize << "bytes)";
currentIndex++;
if (progressCallback) progressCallback(currentIndex, xdvdfsTotal, relPath);
}
if (!anySuccess) {
errorMessage = ERROR_NO_FILES;
debugError(errorMessage);
debugFunctionEnd("extractAllFromIso");
return false;
}
// ---- Auto-detect and recursively extract any STFS packages ----
for (const auto &e : entries) {
if (e.isDirectory) continue;
QString relPath = QString::fromStdString(e.path);
relPath.replace('\\', '/');
QString fullOutputPath = outputPath + "/" + relPath;
StfsReader stfs;
if (!stfs.Open(fullOutputPath.toStdString())) {
continue; // not an STFS package, skip silently
}
qDebug() << " STFS package detected:" << relPath
<< "-" << QString::fromStdString(stfs.GetDisplayName());
QString stfsOutDir = fullOutputPath + "_extracted";
auto stfsFiles = stfs.ListAllFiles();
int stfsTotal = 0;
for (const auto &sf : stfsFiles) if (!sf.isDirectory) stfsTotal++;
int stfsIndex = 0;
int stfsSuccessCount = 0;
for (const auto &sf : stfsFiles) {
if (sf.isDirectory) continue;
QString sfRelPath = QString::fromStdString(sf.path);
sfRelPath.replace('\\', '/');
QString sfFullPath = stfsOutDir + "/" + sfRelPath;
QFileInfo sfInfo(sfFullPath);
if (!QDir().mkpath(sfInfo.absolutePath())) {
qDebug() << " Failed to create directory for:" << sfRelPath;
stfsIndex++;
if (progressCallback) progressCallback(stfsIndex, stfsTotal, sfRelPath);
continue;
}
// Streaming write directly to disk - bounded memory regardless of file size.
if (!stfs.ExtractFileToDisk(sf, sfFullPath.toStdString())) {
qDebug() << " Failed to extract STFS entry:" << sfRelPath;
stfsIndex++;
if (progressCallback) progressCallback(stfsIndex, stfsTotal, sfRelPath);
continue;
}
stfsSuccessCount++;
stfsIndex++;
if (progressCallback) progressCallback(stfsIndex, stfsTotal, sfRelPath);
}
qDebug() << " STFS extracted:" << stfsSuccessCount << "files ->" << stfsOutDir;
}
// ---- END STFS auto-extraction ----
debugQuaZipResult("extractAllFromIso", true, "All files extracted");
debugFunctionEnd("extractAllFromIso");
return true;
}