-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathContentFile.cpp
More file actions
434 lines (358 loc) · 17.4 KB
/
ContentFile.cpp
File metadata and controls
434 lines (358 loc) · 17.4 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "ContentFile.h"
#include "AudioMan.h"
#include "PresetMan.h"
#include "ConsoleMan.h"
#include "RTETools.h"
#include "System.h"
#include "FrameMan.h"
#include "png.h"
#include "fmod/fmod.hpp"
#include "fmod/fmod_errors.h"
#include <SDL3_image/SDL_image.h>
#include <cstring>
using namespace RTE;
const std::string ContentFile::c_ClassName = "ContentFile";
std::array<std::unordered_map<std::string, BITMAP*>, ContentFile::BitDepths::BitDepthCount> ContentFile::s_LoadedBitmaps;
std::unordered_map<std::string, FMOD::Sound*> ContentFile::s_LoadedSamples;
std::unordered_map<size_t, std::string> ContentFile::s_PathHashes;
void ContentFile::Clear() {
m_DataPath.clear();
m_DataPathExtension.clear();
m_DataPathWithoutExtension.clear();
m_DataPathIsImageFile = false;
m_FormattedReaderPosition.clear();
m_DataPathAndReaderPosition.clear();
m_DataModuleID = 0;
m_ImageFileInfo.fill(-1);
}
int ContentFile::Create(const char* filePath) {
SetDataPath(filePath);
return 0;
}
int ContentFile::Create(const ContentFile& reference) {
m_DataPath = reference.m_DataPath;
m_DataPathExtension = reference.m_DataPathExtension;
m_DataPathWithoutExtension = reference.m_DataPathWithoutExtension;
m_DataModuleID = reference.m_DataModuleID;
return 0;
}
void ContentFile::FreeAllLoaded() {
for (int depth = BitDepths::Eight; depth < BitDepths::BitDepthCount; ++depth) {
for (const auto& [bitmapPath, bitmapPtr]: s_LoadedBitmaps[depth]) {
destroy_bitmap(bitmapPtr);
}
}
}
int ContentFile::ReadProperty(const std::string_view& propName, Reader& reader) {
StartPropertyList(return Serializable::ReadProperty(propName, reader));
MatchForwards("FilePath") MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); });
EndPropertyList;
}
int ContentFile::Save(Writer& writer) const {
Serializable::Save(writer);
if (!m_DataPath.empty()) {
writer.NewPropertyWithValue("FilePath", m_DataPath);
}
return 0;
}
int ContentFile::GetDataModuleID() const {
return (m_DataModuleID < 0) ? g_PresetMan.GetModuleIDFromPath(m_DataPath) : m_DataModuleID;
}
void ContentFile::SetDataPath(const std::string& newDataPath) {
m_DataPath = g_PresetMan.GetFullModulePath(newDataPath);
m_DataPathExtension = std::filesystem::path(m_DataPath).extension().string();
RTEAssert(!m_DataPathExtension.empty(), "Failed to find file extension when trying to find file with path and name:\n" + m_DataPath + "\n" + GetFormattedReaderPosition());
m_DataPathIsImageFile = m_DataPathExtension == ".png" || m_DataPathExtension == ".bmp";
m_DataPathWithoutExtension = m_DataPath.substr(0, m_DataPath.length() - m_DataPathExtension.length());
s_PathHashes[GetHash()] = m_DataPath;
m_DataModuleID = g_PresetMan.GetModuleIDFromPath(m_DataPath);
}
size_t ContentFile::GetHash() const {
return Hash(m_DataPath);
}
void ContentFile::SetFormattedReaderPosition(const std::string& newPosition) {
m_FormattedReaderPosition = newPosition;
m_DataPathAndReaderPosition = m_DataPath + "\n" + newPosition;
}
int ContentFile::GetImageFileInfo(ImageFileInfoType infoTypeToGet) {
bool fetchFileInfo = false;
for (const int& fileInfoEntry: m_ImageFileInfo) {
if (fileInfoEntry == -1) {
fetchFileInfo = true;
break;
}
}
if (fetchFileInfo) {
FILE* imageFile = fopen(m_DataPath.c_str(), "rb");
RTEAssert(imageFile, "Failed to open file prior to reading info of image file with following path and name:\n\n" + m_DataPath + "\n\nThe file may not exist or be corrupt.");
if (m_DataPathExtension == ".png") {
ReadAndStorePNGFileInfo(imageFile);
} else if (m_DataPathExtension == ".bmp") {
ReadAndStoreBMPFileInfo(imageFile);
} else {
RTEAbort("Somehow ended up attempting to read image file info for an unsupported image file type.\nThe file path and name were:\n\n" + m_DataPath);
}
fclose(imageFile);
}
return m_ImageFileInfo[infoTypeToGet];
}
void ContentFile::ReadAndStorePNGFileInfo(FILE* imageFile) {
std::array<uint8_t, 8> fileSignature = {};
// Read the first 8 bytes to then verify they match the PNG file signature which is { 137, 80, 78, 71, 13, 10, 26, 10 } or { '\211', 'P', 'N', 'G', '\r', '\n', '\032', '\n' }.
fread(fileSignature.data(), sizeof(uint8_t), fileSignature.size(), imageFile);
if (png_sig_cmp(fileSignature.data(), 0, fileSignature.size()) == 0) {
png_structp pngReadStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
png_infop pngInfo = png_create_info_struct(pngReadStruct);
png_init_io(pngReadStruct, imageFile);
// Set the PNG reader to skip the first 8 bytes since we already handled them.
png_set_sig_bytes(pngReadStruct, fileSignature.size());
png_read_info(pngReadStruct, pngInfo);
m_ImageFileInfo[ImageFileInfoType::ImageBitDepth] = static_cast<int>(png_get_bit_depth(pngReadStruct, pngInfo));
m_ImageFileInfo[ImageFileInfoType::ImageWidth] = static_cast<int>(png_get_image_width(pngReadStruct, pngInfo));
m_ImageFileInfo[ImageFileInfoType::ImageHeight] = static_cast<int>(png_get_image_height(pngReadStruct, pngInfo));
png_destroy_read_struct(&pngReadStruct, &pngInfo, nullptr);
} else {
RTEAbort("Encountered invalid PNG file signature while attempting to read info of image file with following path and name:\n\n" + m_DataPath + "\n\nThe file may be corrupt or is not a PNG file.");
}
}
void ContentFile::ReadAndStoreBMPFileInfo(FILE* imageFile) {
std::array<uint8_t, 2> bmpSignature = {0x42, 0x4D}; // { 'B', 'M' }.
std::array<uint8_t, 2> fileSignature = {};
// Read the first 2 bytes to then verify they match the BMP file signature.
fread(fileSignature.data(), sizeof(uint8_t), fileSignature.size(), imageFile);
if (fileSignature == bmpSignature) {
std::array<uint8_t, 4> bmpData = {};
const auto toInt32 = [](uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) {
return (static_cast<int32_t>(b0)) | (static_cast<int32_t>(b1) << 8) | (static_cast<int32_t>(b2) << 16) | (static_cast<int32_t>(b3) << 24);
};
// Skip the next 16 bytes. Dimensions data starts at the 18th byte.
fseek(imageFile, 16, SEEK_CUR);
fread(bmpData.data(), sizeof(uint8_t), bmpData.size(), imageFile);
m_ImageFileInfo[ImageFileInfoType::ImageWidth] = toInt32(bmpData[0], bmpData[1], bmpData[2], bmpData[3]);
fread(bmpData.data(), sizeof(uint8_t), bmpData.size(), imageFile);
m_ImageFileInfo[ImageFileInfoType::ImageHeight] = toInt32(bmpData[0], bmpData[1], bmpData[2], bmpData[3]);
// Skip the next 2 bytes. Bit depth data starts at the 28th byte.
fseek(imageFile, 2, SEEK_CUR);
// Bit depth is stored as 2 bytes, so ignore the last 2 in the array when converting to int32.
fread(bmpData.data(), sizeof(uint8_t), bmpData.size(), imageFile);
m_ImageFileInfo[ImageFileInfoType::ImageBitDepth] = toInt32(bmpData[0], bmpData[1], 0, 0);
} else {
RTEAbort("Encountered invalid BMP file signature while attempting to read info of image file with following path and name:\n\n" + m_DataPath + "\n\nThe file may be corrupt or is not a BMP file.");
}
}
void ContentFile::ReloadAllBitmaps() {
for (const std::unordered_map<std::string, BITMAP*>& bitmapCache: s_LoadedBitmaps) {
for (const auto& [filePath, oldBitmap]: bitmapCache) {
ReloadBitmap(filePath);
}
}
g_ConsoleMan.PrintString("SYSTEM: Sprites reloaded");
}
BITMAP* ContentFile::GetAsBitmap(int conversionMode, bool storeBitmap, const std::string& dataPathToSpecificFrame) {
if (m_DataPath.empty()) {
return nullptr;
}
BITMAP* returnBitmap = nullptr;
const int bitDepth = conversionMode == COLORCONV_8_TO_32 ? BitDepths::ThirtyTwo : BitDepths::Eight;
std::string dataPathToLoad = dataPathToSpecificFrame.empty() ? m_DataPath : dataPathToSpecificFrame;
if (g_PresetMan.GetReloadEntityPresetCalledThisUpdate()) {
ReloadBitmap(dataPathToLoad, conversionMode);
}
// Check if the file has already been read and loaded from the disk and, if so, use that data.
std::unordered_map<std::string, BITMAP*>::iterator foundBitmap = s_LoadedBitmaps[bitDepth].find(dataPathToLoad);
if (storeBitmap && foundBitmap != s_LoadedBitmaps[bitDepth].end()) {
returnBitmap = (*foundBitmap).second;
} else {
if (!System::PathExistsCaseSensitive(dataPathToLoad)) {
const std::string dataPathWithoutExtension = dataPathToLoad.substr(0, dataPathToLoad.length() - m_DataPathExtension.length());
const std::string altFileExtension = (m_DataPathExtension == ".png") ? ".bmp" : ".png";
if (System::PathExistsCaseSensitive(dataPathWithoutExtension + altFileExtension)) {
g_ConsoleMan.AddLoadWarningLogExtensionMismatchEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
SetDataPath(m_DataPathWithoutExtension + altFileExtension);
dataPathToLoad = dataPathWithoutExtension + altFileExtension;
} else {
RTEAbort("Failed to find image file with following path and name:\n\n" + dataPathToLoad + " or " + altFileExtension + "\n" + m_FormattedReaderPosition);
}
}
returnBitmap = LoadAndReleaseBitmap(conversionMode, dataPathToLoad); // NOTE: This takes ownership of the bitmap file
// Insert the bitmap into the map, PASSING OVER OWNERSHIP OF THE LOADED DATAFILE
if (storeBitmap) {
s_LoadedBitmaps[bitDepth].try_emplace(dataPathToLoad, returnBitmap);
}
}
return returnBitmap;
}
void ContentFile::GetAsAnimation(std::vector<BITMAP*>& vectorToFill, int frameCount, int conversionMode) {
if (m_DataPath.empty() || frameCount < 1) {
return;
}
vectorToFill.reserve(frameCount);
if (frameCount == 1) {
// Check for 000 in the file name in case it is part of an animation but the FrameCount was set to 1. Do not warn about this because it's normal operation, but warn about incorrect extension.
if (!System::PathExistsCaseSensitive(m_DataPath)) {
const std::string altFileExtension = (m_DataPathExtension == ".png") ? ".bmp" : ".png";
if (System::PathExistsCaseSensitive(m_DataPathWithoutExtension + "000" + m_DataPathExtension)) {
SetDataPath(m_DataPathWithoutExtension + "000" + m_DataPathExtension);
} else if (System::PathExistsCaseSensitive(m_DataPathWithoutExtension + "000" + altFileExtension)) {
g_ConsoleMan.AddLoadWarningLogExtensionMismatchEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
SetDataPath(m_DataPathWithoutExtension + "000" + altFileExtension);
}
}
vectorToFill.emplace_back(GetAsBitmap(conversionMode));
} else {
char framePath[1024];
for (int frameNum = 0; frameNum < frameCount; ++frameNum) {
std::snprintf(framePath, sizeof(framePath), "%s%03i%s", m_DataPathWithoutExtension.c_str(), frameNum, m_DataPathExtension.c_str());
vectorToFill.emplace_back(GetAsBitmap(conversionMode, true, framePath));
}
}
}
SDL_Palette* ContentFile::DefaultPaletteToSDL() {
SDL_Palette* palette = SDL_CreatePalette(256);
std::array<SDL_Color, 256> paletteColor;
const PALETTE& defaultPalette = g_FrameMan.GetDefaultPalette();
paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0};
for (size_t i = 1; i < paletteColor.size(); ++i) {
paletteColor[i].r = defaultPalette[i].r;
paletteColor[i].g = defaultPalette[i].g;
paletteColor[i].b = defaultPalette[i].b;
paletteColor[i].a = 255;
}
SDL_SetPaletteColors(palette, paletteColor.data(), 0, 256);
return palette;
}
SDL_Surface* ContentFile::LoadImageAsSurface(int conversionMode, const std::string& dataPathToLoad) {
SDL_Surface* image = IMG_Load(dataPathToLoad.c_str());
bool convert8To32 = conversionMode & COLORCONV_8_TO_32;
bool convertTo8 = conversionMode & COLORCONV_REDUCE_TO_256;
int bitDepth = SDL_GetPixelFormatDetails(image->format)->bits_per_pixel;
if (convertTo8 && bitDepth != 8) {
SDL_Palette* palette = DefaultPaletteToSDL();
SDL_Surface* newImage = SDL_ConvertSurfaceAndColorspace(image, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
SDL_DestroyPalette(palette);
SDL_DestroySurface(image);
image = newImage;
bitDepth = 8;
} else if (bitDepth != 8 || convert8To32) {
SDL_Palette* palette = DefaultPaletteToSDL();
if (SDL_GetPixelFormatDetails(image->format)->bits_per_pixel == 8) {
SDL_SetSurfacePalette(image, palette);
SDL_SetSurfaceColorKey(image, true, 0);
}
SDL_DestroyPalette(palette);
SDL_Surface* newImage = SDL_ConvertSurface(image, SDL_PIXELFORMAT_RGBA32);
SDL_DestroySurface(image);
image = newImage;
bitDepth = 32;
}
return image;
}
BITMAP* ContentFile::LoadAndReleaseBitmap(int conversionMode, const std::string& dataPathToSpecificFrame) {
if (m_DataPath.empty()) {
return nullptr;
}
const std::string dataPathToLoad = dataPathToSpecificFrame.empty() ? m_DataPath : dataPathToSpecificFrame;
SDL_Surface* image = LoadImageAsSurface(conversionMode, dataPathToLoad);
int bitDepth = SDL_GetPixelFormatDetails(image->format)->bits_per_pixel;
BITMAP* returnBitmap = create_bitmap_ex(bitDepth, image->w, image->h);
// allegro doesn't (always) align lines to 4byte, so copy line by line. SDL_Surface.pitch is the size in bytes per line + alignment padding.
for (int y = 0; y < image->h; ++y) {
memcpy(returnBitmap->line[y], static_cast<unsigned char*>(image->pixels) + image->pitch * y, image->w * SDL_GetPixelFormatDetails(image->format)->bytes_per_pixel);
}
SDL_DestroySurface(image);
RTEAssert(returnBitmap, "Failed to load image file with following path and name:\n\n" + m_DataPathAndReaderPosition + "\nThe file may be corrupt, incorrectly converted or saved with unsupported parameters.");
return returnBitmap;
}
FMOD::Sound* ContentFile::GetAsSound(bool abortGameForInvalidSound, bool asyncLoading) {
if (m_DataPath.empty() || !g_AudioMan.IsAudioEnabled()) {
return nullptr;
}
FMOD::Sound* returnSample = nullptr;
std::unordered_map<std::string, FMOD::Sound*>::iterator foundSound = s_LoadedSamples.find(m_DataPath);
if (foundSound != s_LoadedSamples.end()) {
returnSample = (*foundSound).second;
} else {
returnSample = LoadAndReleaseSound(abortGameForInvalidSound, asyncLoading); // NOTE: This takes ownership of the sample file
// Insert the Sound object into the map, PASSING OVER OWNERSHIP OF THE LOADED FILE
s_LoadedSamples.try_emplace(m_DataPath, returnSample);
}
return returnSample;
}
FMOD::Sound* ContentFile::LoadAndReleaseSound(bool abortGameForInvalidSound, bool asyncLoading) {
if (m_DataPath.empty() || !g_AudioMan.IsAudioEnabled()) {
return nullptr;
}
if (!System::PathExistsCaseSensitive(m_DataPath)) {
bool foundAltExtension = false;
for (const std::string& altFileExtension: c_SupportedAudioFormats) {
const std::string altDataPathToLoad = m_DataPathWithoutExtension + altFileExtension;
if (System::PathExistsCaseSensitive(altDataPathToLoad)) {
g_ConsoleMan.AddLoadWarningLogExtensionMismatchEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
SetDataPath(altDataPathToLoad);
foundAltExtension = true;
break;
}
}
if (!foundAltExtension) {
std::string errorMessage = "Failed to find audio file with following path and name:\n\n" + m_DataPath + " or any alternative supported file type";
RTEAssert(!abortGameForInvalidSound, errorMessage + "\n" + m_FormattedReaderPosition);
g_ConsoleMan.PrintString(errorMessage + ". The file was not loaded!");
return nullptr;
}
}
if (std::filesystem::file_size(m_DataPath) == 0) {
const std::string errorMessage = "Failed to create sound because the file was empty. The path and name were: ";
RTEAssert(!abortGameForInvalidSound, errorMessage + "\n\n" + m_DataPathAndReaderPosition);
g_ConsoleMan.PrintString("ERROR: " + errorMessage + m_DataPath);
return nullptr;
}
FMOD::Sound* returnSample = nullptr;
FMOD_MODE fmodFlags = FMOD_CREATESAMPLE | FMOD_3D | (asyncLoading ? FMOD_NONBLOCKING : FMOD_DEFAULT);
FMOD_RESULT result = g_AudioMan.GetAudioSystem()->createSound(m_DataPath.c_str(), fmodFlags, nullptr, &returnSample);
if (result != FMOD_OK) {
const std::string errorMessage = "Failed to create sound because of FMOD error:\n" + std::string(FMOD_ErrorString(result)) + "\nThe path and name were: ";
RTEAssert(!abortGameForInvalidSound, errorMessage + "\n\n" + m_DataPathAndReaderPosition);
g_ConsoleMan.PrintString("ERROR: " + errorMessage + m_DataPath);
return returnSample;
}
return returnSample;
}
void ContentFile::ReloadBitmap(const std::string& filePath, int conversionMode) {
const int bitDepth = (conversionMode == COLORCONV_8_TO_32) ? BitDepths::ThirtyTwo : BitDepths::Eight;
auto bmpItr = s_LoadedBitmaps[bitDepth].find(filePath);
if (bmpItr == s_LoadedBitmaps[bitDepth].end()) {
return;
}
PALETTE currentPalette;
get_palette(currentPalette);
set_color_conversion((conversionMode == COLORCONV_NONE) ? COLORCONV_NONE : conversionMode);
BITMAP* loadedBitmap = (*bmpItr).second;
SDL_Surface* newImage = LoadImageAsSurface(conversionMode, filePath);
BITMAP* newBitmap = create_bitmap_ex(SDL_GetPixelFormatDetails(newImage->format)->bits_per_pixel, newImage->w, newImage->h);
// allegro doesn't (always) align lines to 4byte, so copy line by line. SDL_Surface.pitch is the size in bytes per line + alignment padding.
for (int y = 0; y < newImage->h; y++) {
memcpy(newBitmap->line[y], static_cast<unsigned char*>(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel);
}
//AddAlphaChannel(newBitmap);
BITMAP swap;
std::memcpy(&swap, loadedBitmap, sizeof(BITMAP));
std::memcpy(loadedBitmap, newBitmap, sizeof(BITMAP));
std::memcpy(newBitmap, &swap, sizeof(BITMAP));
destroy_bitmap(newBitmap);
}
void ContentFile::AddAlphaChannel(BITMAP* bitmap) {
if (!bitmap) {
return;
}
if (bitmap_color_depth(bitmap) != 32) {
return;
}
for (int y = 0; y < bitmap->h; ++y) {
for (int x = 0; x < bitmap->w; ++x) {
unsigned long color = _getpixel32(bitmap, x, y);
if (color != MASK_COLOR_32) {
_putpixel32(bitmap, x, y, color | (0xFF << _rgb_a_shift_32));
}
}
}
}