Nuke error.hpp
Some checks failed
Build Dusk / build-linux (push) Failing after 2m7s
Build Dusk / build-psp (push) Failing after 2m19s

This commit is contained in:
2025-12-22 14:19:07 +10:00
parent 76b5c51ab6
commit 2e0f5f302b
49 changed files with 418 additions and 708 deletions

View File

@@ -10,14 +10,16 @@
#include "util/string.hpp"
#include "assert/assert.hpp"
#include "asset/assettype.h"
#include "engine/engine.hpp"
#include "engine/Engine.hpp"
#include "debug/debug.hpp"
errorret_t assetInit(void) {
using namespace Dusk;
void assetInit(void) {
memoryZero(&ASSET, sizeof(asset_t));
// Engine may have been provided the launch path
if(ENGINE.argc > 0) {
if(Engine::ENGINE.argc > 0) {
// This first arg is the executable, so on most platforms it is say
// "/path/file" or "C:\Path\file.exe". On PSP this would be something
// like "ms0:/PSP/GAME/DUSK/EBOOT.PBP" or if we are debugging it is
@@ -25,7 +27,7 @@ errorret_t assetInit(void) {
// Get the directory of the executable
char_t buffer[FILENAME_MAX];
stringCopy(buffer, ENGINE.argv[0], FILENAME_MAX);
stringCopy(buffer, Engine::ENGINE.argv[0], FILENAME_MAX);
size_t len = strlen(buffer);
// Normalize slashes
@@ -69,20 +71,26 @@ errorret_t assetInit(void) {
);
ASSET.pbpFile = fopen(pbpPath, "rb");
if(ASSET.pbpFile == NULL) {
errorThrow("Failed to open PBP file: %s", pbpPath);
throw std::runtime_error(
"Failed to open PBP file: " + std::string(pbpPath)
);
}
// Get size of PBP file.
if(fseek(ASSET.pbpFile, 0, SEEK_END) != 0) {
fclose(ASSET.pbpFile);
errorThrow("Failed to seek to end of PBP file : %s", pbpPath);
throw std::runtime_error(
"Failed to seek to end of PBP file : " + std::string(pbpPath)
);
}
size_t pbpSize = ftell(ASSET.pbpFile);
// Rewind to start
if(fseek(ASSET.pbpFile, 0, SEEK_SET) != 0) {
fclose(ASSET.pbpFile);
errorThrow("Failed to seek to start of PBP file : %s", pbpPath);
throw std::runtime_error(
"Failed to seek to start of PBP file : " + std::string(pbpPath)
);
}
// Read the PBP header
@@ -94,7 +102,9 @@ errorret_t assetInit(void) {
);
if(read != sizeof(assetpbp_t)) {
fclose(ASSET.pbpFile);
errorThrow("Failed to read PBP header", pbpPath);
throw std::runtime_error(
"Failed to read PBP header: " + std::string(pbpPath)
);
}
if(memoryCompare(
@@ -103,13 +113,17 @@ errorret_t assetInit(void) {
sizeof(ASSET_PBP_SIGNATURE)
) != 0) {
fclose(ASSET.pbpFile);
errorThrow("Invalid PBP signature in file: %s", pbpPath);
throw std::runtime_error(
"Invalid PBP signature in file: " + std::string(pbpPath)
);
}
// If we seek to the PSAR offset, we can read the WAD file from there
if(fseek(ASSET.pbpFile, ASSET.pbpHeader.psarOffset, SEEK_SET) != 0) {
fclose(ASSET.pbpFile);
errorThrow("Failed to seek to PSAR offset in PBP file: %s", pbpPath);
throw std::runtime_error(
"Failed to seek to PSAR offset in PBP file: " + std::string(pbpPath)
);
}
zip_uint64_t zipPsarOffset = (zip_uint64_t)ASSET.pbpHeader.psarOffset;
@@ -125,7 +139,9 @@ errorret_t assetInit(void) {
);
if(psarSource == NULL) {
fclose(ASSET.pbpFile);
errorThrow("Failed to create zip source in PBP file: %s", pbpPath);
throw std::runtime_error(
"Failed to create zip source in PBP file: " + std::string(pbpPath)
);
}
ASSET.zip = zip_open_from_source(
@@ -136,7 +152,9 @@ errorret_t assetInit(void) {
if(ASSET.zip == NULL) {
zip_source_free(psarSource);
fclose(ASSET.pbpFile);
errorThrow("Failed to open zip from PBP file: %s", pbpPath);
throw std::runtime_error(
"Failed to open zip from PBP file: " + std::string(pbpPath)
);
}
errorOk();
@@ -162,9 +180,7 @@ errorret_t assetInit(void) {
} while(*(++path) != NULL);
// Did we open the asset?
if(ASSET.zip == NULL) errorThrow("Failed to open asset file.");
errorOk();
if(ASSET.zip == NULL) throw std::runtime_error("Failed to open asset file.");
}
bool_t assetFileExists(const char_t *filename) {
@@ -175,14 +191,16 @@ bool_t assetFileExists(const char_t *filename) {
return true;
}
errorret_t assetLoad(const char_t *filename, void *output) {
void assetLoad(const char_t *filename, void *output) {
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
assertNotNull(output, "Output pointer cannot be NULL.");
// Try to open the file
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
if(file == NULL) {
errorThrow("Failed to open asset file: %s", filename);
throw std::runtime_error(
"Failed to open asset file: " + std::string(filename)
);
}
// Read the header.
@@ -191,7 +209,9 @@ errorret_t assetLoad(const char_t *filename, void *output) {
zip_int64_t bytesRead = zip_fread(file, &header, sizeof(assetheader_t));
if(bytesRead != sizeof(assetheader_t)) {
zip_fclose(file);
errorThrow("Failed to read asset header for: %s", filename);
throw std::runtime_error(
"Failed to read asset header for: " + std::string(filename)
);
}
// Find the asset type based on the header
@@ -215,7 +235,9 @@ errorret_t assetLoad(const char_t *filename, void *output) {
}
if(def == NULL) {
zip_fclose(file);
errorThrow("Unknown asset type for file: %s", filename);
throw std::runtime_error(
"Unknown asset type for file: " + std::string(filename)
);
}
// We found the asset type, now load the asset data
@@ -227,16 +249,24 @@ errorret_t assetLoad(const char_t *filename, void *output) {
if(bytesRead == 0 || bytesRead > def->dataSize) {
memoryFree(data);
zip_fclose(file);
errorThrow("Failed to read asset data for file: %s", filename);
throw std::runtime_error(
"Failed to read entire asset data for file: " +
std::string(filename)
);
}
// Close the file now we have the data
zip_fclose(file);
// Pass to the asset type loader
errorret_t ret = def->entire(data, output);
try {
def->entire(data, output);
} catch(...) {
memoryFree(data);
throw;
}
memoryFree(data);
errorChain(ret);
break;
}
@@ -246,15 +276,13 @@ errorret_t assetLoad(const char_t *filename, void *output) {
.zipFile = file,
.output = output
};
errorChain(def->custom(customData));
def->custom(customData);
break;
}
default:
assertUnreachable("Unknown asset load strategy.");
}
errorOk();
}
void assetDispose(void) {