forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_rwopen.cpp
More file actions
403 lines (368 loc) · 10.9 KB
/
file_rwopen.cpp
File metadata and controls
403 lines (368 loc) · 10.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Platformer Game Engine by Wohlstand, a free platform for game making
* Copyright (c) 2014-2020 Vitaly Novichkov <admin@wohlnet.ru>
*
* 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
* 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/>.
*/
#ifdef PGE_FILES_QT
#include <QFileInfo>
#include <QDir>
#endif
#include "file_formats.h"
#include "pge_file_lib_private.h"
bool FileFormats::OpenLevelFile(PGESTRING filePath, LevelData &FileData)
{
PGE_FileFormats_misc::TextFileInput file;
if(!file.open(filePath, true))
{
FileData.meta.ReadFileValid = false;
FileData.meta.ERROR_info = "Can't open file";
FileData.meta.ERROR_linedata = "";
FileData.meta.ERROR_linenum = -1;
return false;
}
return OpenLevelFileT(file, FileData);
}
bool FileFormats::OpenLevelRaw(PGESTRING &rawdata, PGESTRING filePath, LevelData &FileData)
{
PGE_FileFormats_misc::RawTextInput file;
if(!file.open(&rawdata, filePath))
{
FileData.meta.ReadFileValid = false;
FileData.meta.ERROR_info = "Can't open file";
FileData.meta.ERROR_linedata = "";
FileData.meta.ERROR_linenum = -1;
return false;
}
return OpenLevelFileT(file, FileData);
}
bool FileFormats::OpenLevelFileT(PGE_FileFormats_misc::TextInput &file, LevelData &FileData)
{
PGESTRING firstLine;
FileData.meta.ERROR_info.clear();
firstLine = file.read(8);
file.seek(0, PGE_FileFormats_misc::TextInput::begin);
if(PGE_StartsWith(firstLine, "SMBXFile"))
{
//Read SMBX65-38A LVL File
if(!ReadSMBX38ALvlFile(file, FileData))
return false;
}
else if(PGE_FileFormats_misc::PGE_DetectSMBXFile(firstLine))
{
//Disable UTF8 for SMBX64 files
if(!file.reOpen(false))
return false;
//Read SMBX LVL File
if(!ReadSMBX64LvlFile(file, FileData))
return false;
}
else
{
//Read PGE LVLX File
if(!ReadExtendedLvlFile(file, FileData))
return false;
}
if(PGE_FileFormats_misc::TextFileInput::exists(file.getFilePath() + ".meta"))
{
if(!ReadNonSMBX64MetaDataF(file.getFilePath() + ".meta", FileData.metaData))
FileData.meta.ERROR_info = "Can't open meta-file";
}
return true;
}
bool FileFormats::OpenLevelFileHeader(PGESTRING filePath, LevelData &data)
{
PGE_FileFormats_misc::TextFileInput file;
data.meta.ERROR_info.clear();
if(!file.open(filePath, true))
{
data.meta.ReadFileValid = false;
data.meta.ERROR_info = "Can't open file";
data.meta.ERROR_linedata = "";
data.meta.ERROR_linenum = -1;
return false;
}
return OpenLevelFileHeaderT(file, data);
}
bool FileFormats::OpenLevelFileHeaderRaw(PGESTRING &rawdata, PGESTRING filePath, LevelData &data)
{
PGE_FileFormats_misc::RawTextInput file;
data.meta.ERROR_info.clear();
if(!file.open(&rawdata, filePath))
{
data.meta.ReadFileValid = false;
data.meta.ERROR_info = "Can't open file";
data.meta.ERROR_linedata = "";
data.meta.ERROR_linenum = -1;
return false;
}
return OpenLevelFileHeaderT(file, data);
}
bool FileFormats::OpenLevelFileHeaderT(PGE_FileFormats_misc::TextInput &file, LevelData &data)
{
PGESTRING firstLine;
firstLine = file.readLine();
file.seek(0, PGE_FileFormats_misc::TextInput::begin);
if(PGE_StartsWith(firstLine, "SMBXFile"))
{
//Read SMBX65-38A LVL File
return ReadSMBX38ALvlFileHeaderT(file, data);
}
else if(PGE_FileFormats_misc::PGE_DetectSMBXFile(firstLine))
{
//Disable UTF8 for SMBX64 files
if(!file.reOpen(false))
return false;
//Read SMBX LVL File
return ReadSMBX64LvlFileHeaderT(file, data);
}
else
{
//Read PGE LVLX File
return ReadExtendedLvlFileHeaderT(file, data);
}
}
bool FileFormats::SaveLevelFile(LevelData &FileData, PGESTRING filePath, LevelFileFormat format, unsigned int FormatVersion)
{
FileData.meta.ERROR_info.clear();
switch(format)
{
case LVL_PGEX:
{
smbx64CountStars(FileData);
if(!FileFormats::WriteExtendedLvlFileF(filePath, FileData))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".";
return false;
}
return true;
}
//break;
case LVL_SMBX64:
{
//Apply SMBX64-specific things to entire array
smbx64LevelPrepare(FileData);
if(!FileFormats::WriteSMBX64LvlFileF(filePath, FileData, FormatVersion))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".";
return false;
}
//save additional meta data
if(!FileData.metaData.bookmarks.empty())
{
if(!FileFormats::WriteNonSMBX64MetaDataF(filePath + ".meta", FileData.metaData))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".meta.";
return false;
}
}
return true;
}
//break;
case LVL_SMBX38A:
{
if(!FileFormats::WriteSMBX38ALvlFileF(filePath, FileData))
{
FileData.meta.ERROR_info = "Cannot save file " + filePath + ".";
return false;
}
return true;
}
//break;
}
FileData.meta.ERROR_info = "Unsupported file type";
return false;
}
bool FileFormats::SaveLevelData(LevelData &FileData, PGESTRING &RawData, LevelFileFormat format, unsigned int FormatVersion)
{
FileData.meta.ERROR_info.clear();
switch(format)
{
case LVL_PGEX:
{
smbx64CountStars(FileData);
WriteExtendedLvlFileRaw(FileData, RawData);
return true;
}
//break;
case LVL_SMBX64:
{
smbx64LevelPrepare(FileData);
WriteSMBX64LvlFileRaw(FileData, RawData, FormatVersion);
return true;
}
//break;
case LVL_SMBX38A:
{
FileFormats::WriteSMBX38ALvlFileRaw(FileData, RawData);
return true;
}
//break;
}
FileData.meta.ERROR_info = "Unsupported file type";
FileData.meta.ReadFileValid = false;
return false;
}
bool FileFormats::OpenWorldFile(PGESTRING filePath, WorldData &data)
{
data.meta.ERROR_info.clear();
PGE_FileFormats_misc::TextFileInput file;
PGESTRING firstLine;
if(!file.open(filePath, true))
{
data.meta.ERROR_info = "Can't open file";
data.meta.ERROR_linedata = "";
data.meta.ERROR_linenum = -1;
data.meta.ReadFileValid = false;
return false;
}
firstLine = file.read(8);
file.seek(0, PGE_FileFormats_misc::TextInput::begin);
if(PGE_StartsWith(firstLine, "SMBXFile"))
{
//Read SMBX-38A WLD File
if(!ReadSMBX38AWldFile(file, data))
return false;
}
else if(PGE_FileFormats_misc::PGE_DetectSMBXFile(firstLine))
{
//Disable UTF8 for SMBX64 files
if(!file.reOpen(false))
return false;
//Read SMBX WLD File
if(!ReadSMBX64WldFile(file, data))
return false;
}
else
{
//Read PGE WLDX File
if(!ReadExtendedWldFile(file, data))
return false;
}
if(PGE_FileFormats_misc::TextFileInput::exists(filePath + ".meta"))
{
if(!ReadNonSMBX64MetaDataF(filePath + ".meta", data.metaData))
data.meta.ERROR_info = "Can't open meta-file";
}
return true;
}
bool FileFormats::OpenWorldFileHeader(PGESTRING filePath, WorldData &data)
{
PGE_FileFormats_misc::TextFileInput file;
data.meta.ERROR_info.clear();
if(!file.open(filePath, true))
{
data.meta.ERROR_info = "Can't open file";
data.meta.ERROR_linedata = "";
data.meta.ERROR_linenum = -1;
data.meta.ReadFileValid = false;
return false;
}
PGESTRING firstLine;
firstLine = file.readLine();
file.close();
if(PGE_StartsWith(firstLine, "SMBXFile"))
{
//Read SMBX-38A WLD File
return ReadSMBX38AWldFileHeader(filePath, data);
}
else if(PGE_FileFormats_misc::PGE_DetectSMBXFile(firstLine))
{
//Disable UTF8 for SMBX64 files
if(!file.reOpen(false))
return false;
//Read SMBX WLD File
return ReadSMBX64WldFileHeader(filePath, data);
}
else
{
//Read PGE WLDX File
return ReadExtendedWldFileHeader(filePath, data);
}
}
bool FileFormats::SaveWorldFile(WorldData &FileData, PGESTRING filePath, FileFormats::WorldFileFormat format, unsigned int FormatVersion)
{
FileData.meta.ERROR_info.clear();
switch(format)
{
case WLD_PGEX:
{
if(!FileFormats::WriteExtendedWldFileF(filePath, FileData))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".";
return false;
}
return true;
}
//break;
case WLD_SMBX64:
{
if(!FileFormats::WriteSMBX64WldFileF(filePath, FileData, FormatVersion))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".";
return false;
}
//save additional meta data
if(!FileData.metaData.bookmarks.empty())
{
if(!FileFormats::WriteNonSMBX64MetaDataF(filePath + ".meta", FileData.metaData))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".meta.";
return false;
}
}
return true;
}
//break;
case WLD_SMBX38A:
{
if(!FileFormats::WriteSMBX38AWldFileF(filePath, FileData))
{
FileData.meta.ERROR_info += "Cannot save file " + filePath + ".";
return false;
}
return true;
}
//break;
}
FileData.meta.ERROR_info = "Unsupported file type";
return false;
}
bool FileFormats::SaveWorldData(WorldData &FileData, PGESTRING &RawData, FileFormats::WorldFileFormat format, unsigned int FormatVersion)
{
FileData.meta.ERROR_info.clear();
switch(format)
{
case WLD_PGEX:
{
WriteExtendedWldFileRaw(FileData, RawData);
return true;
}
//break;
case WLD_SMBX64:
{
WriteSMBX64WldFileRaw(FileData, RawData, FormatVersion);
return true;
}
//break;
case WLD_SMBX38A:
{
WriteSMBX38AWldFileRaw(FileData, RawData);
return true;
}
//break;
}
FileData.meta.ERROR_info = "Unsupported file type";
return false;
}