Added reasons to assertions

This commit is contained in:
2023-07-23 10:15:37 -07:00
parent 1b0218c9f3
commit a5b36fb24a
79 changed files with 359 additions and 291 deletions

View File

@ -8,8 +8,8 @@
using namespace Dawn;
Asset::Asset(AssetManager *assetManager, std::string name) {
assertTrue(name.size() > 0);
assertNotNull(assetManager);
assertTrue(name.size() > 0, "Asset::Asset: Name cannot be empty");
assertNotNull(assetManager, "Asset::Asset: AssetManager cannot be null");
this->assetManager = assetManager;
this->name = name;

View File

@ -8,58 +8,58 @@
using namespace Dawn;
AssetLoader::AssetLoader(std::string fileName) {
assertTrue(fileName.size() > 0);
assertTrue(fileName.size() > 0, "AssetLoader::AssetLoader: fileName must be greater than 0");
this->fileName = fileName;
this->handle = nullptr;
}
void AssetLoader::open() {
assertNull(this->handle);
assertNull(this->handle, "AssetLoader::open: File is already open");
std::string pathFull = DAWN_ASSET_BUILD_PREFIX + this->fileName;
this->handle = fopen(pathFull.c_str(), "rb");
assertNotNull(this->handle);
assertNotNull(this->handle, "AssetLoader::open: Failed to open file");
}
int32_t AssetLoader::close() {
assertNotNull(this->handle);
assertNotNull(this->handle, "AssetLoader::close: File is not open");
int32_t ret = fclose(this->handle);
this->handle = nullptr;
return ret;
}
size_t AssetLoader::read(uint8_t *buffer, size_t size) {
assertNotNull(buffer);
assertTrue(size > 0);
assertNotNull(this->handle);
assertNotNull(buffer, "AssetLoader::read: buffer must not be null");
assertTrue(size > 0, "AssetLoader::read: size must be greater than 0");
assertNotNull(this->handle, "AssetLoader::read: File is not open");
return fread(buffer, 1, size, this->handle);
}
int32_t AssetLoader::end() {
assertNotNull(this->handle);
assertNotNull(this->handle, "AssetLoader::end: File is not open");
return fseek(this->handle, 0, SEEK_END);
}
size_t AssetLoader::skip(size_t n) {
assertTrue(n > 0);
assertNotNull(this->handle);
assertTrue(n > 0, "AssetLoader::skip: n must be greater than 0");
assertNotNull(this->handle, "AssetLoader::skip: File is not open");
return fseek(this->handle, n, SEEK_CUR);
}
int32_t AssetLoader::rewind() {
assertNotNull(this->handle);
assertNotNull(this->handle, "AssetLoader::rewind: File is not open");
return fseek(this->handle, 0, SEEK_SET);
}
size_t AssetLoader::getPosition() {
assertNotNull(this->handle);
assertNotNull(this->handle, "AssetLoader::getPosition: File is not open");
return ftell(this->handle);
}
size_t AssetLoader::loadRaw(uint8_t **buffer) {
size_t length, read;
assertNotNull(buffer);
assertNotNull(buffer, "AssetLoader::loadRaw: buffer must not be null");
// Open a buffer.
this->open();
@ -92,7 +92,7 @@ size_t AssetLoader::loadRaw(uint8_t **buffer) {
}
size_t AssetLoader::setPosition(size_t position) {
assertTrue(position >= 0);
assertTrue(position >= 0, "AssetLoader::setPosition: position must be greater than or equal to 0");
this->rewind();
return this->skip(position);
}

View File

@ -79,7 +79,7 @@ namespace Dawn {
*/
template<class T>
T * get(std::string name) {
assertTrue(name.size() > 0);
assertTrue(name.size() > 0, "AssetManager::get: name must be greater than 0");
auto existing = this->assets.find(name);
if(existing != this->assets.end()) {
@ -105,11 +105,11 @@ namespace Dawn {
template<class T>
void unload(T *asset) {
assertUnreachable();
assertUnreachable("AssetManager::unload: NOT IMPLEMENTED");
}
void unload(std::string name) {
assertUnreachable();
assertUnreachable("AssetManager::unload: NOT IMPLEMENTED");
}
/**

View File

@ -41,7 +41,7 @@ void AudioAsset::updateAsync() {
end = strchr(start, '|');
*end = '\0';
this->channelCount = atoi(start);
assertTrue(this->channelCount > 0);
assertTrue(this->channelCount > 0, "AudioAsset::updateAsync: Channel count must be greater than 0");
// Sample Rate
this->state = 0x04;
@ -49,7 +49,7 @@ void AudioAsset::updateAsync() {
end = strchr(start, '|');
*end = '\0';
this->sampleRate = (uint32_t)strtoul(start, NULL, 10);
assertTrue(this->sampleRate > 0);
assertTrue(this->sampleRate > 0, "AudioAsset::updateAsync: Sample rate must be greater than 0");
// Number of samples per channel
this->state = 0x05;
@ -57,7 +57,7 @@ void AudioAsset::updateAsync() {
end = strchr(start, '|');
*end = '\0';
this->samplesPerChannel = atoi(start);
assertTrue(this->samplesPerChannel > 0);
assertTrue(this->samplesPerChannel > 0, "AudioAsset::updateAsync: Samples per channel must be greater than 0");
// Total Data Length
this->state = 0x06;
@ -65,7 +65,7 @@ void AudioAsset::updateAsync() {
end = strchr(start, '|');
*end = '\0';
this->bufferSize = (size_t)atoll(start);
assertTrue(this->bufferSize > 0);
assertTrue(this->bufferSize > 0, "AudioAsset::updateAsync: Buffer size must be greater than 0");
// Determine frame size
this->frameSize = sizeof(int16_t) * this->channelCount;

View File

@ -19,7 +19,7 @@ void LanguageAsset::updateSync() {
}
void LanguageAsset::updateAsync() {
assertTrue(this->state == 0x00);
assertTrue(this->state == 0x00, "LanguageAsset::updateAsync: State must be 0x00");
// Open Asset
this->state = 0x01;
@ -71,9 +71,9 @@ void LanguageAsset::updateAsync() {
}
std::string LanguageAsset::getValue(std::string key) {
assertTrue(this->state == 0x07);
assertMapHasKey(this->values, key);
assertTrue(this->values[key].begin >= 0 && this->values[key].length > 0);
assertTrue(this->state == 0x07, "LanguageAsset::getValue: State must be 0x07");
assertMapHasKey(this->values, key, "LanguageAsset::getValue: Key does not exist");
assertTrue(this->values[key].begin >= 0 && this->values[key].length > 0, "LanguageAsset::getValue: Value is invalid");
// Get the positions
struct AssetLanguageValue value = this->values[key];

View File

@ -58,7 +58,7 @@ void TextureAsset::updateAsync() {
switch(parseState) {
case TEXTURE_ASSET_HEADER_PARSE_STATE_VERSION: {
auto compared = strcmp(integer, "DT_2.00");
assertTrue(compared == 0);
assertTrue(compared == 0, "TextureAsset::updateAsync: Invalid version");
j = 0;
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH;
break;
@ -66,7 +66,7 @@ void TextureAsset::updateAsync() {
case TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH: {
this->width = atoi(integer);
assertTrue(this->width > 0);
assertTrue(this->width > 0, "TextureAsset::updateAsync: Invalid width");
j = 0;
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_HEIGHT;
break;
@ -74,7 +74,7 @@ void TextureAsset::updateAsync() {
case TEXTURE_ASSET_HEADER_PARSE_STATE_HEIGHT: {
this->height = atoi(integer);
assertTrue(this->height > 0);
assertTrue(this->height > 0, "TextureAsset::updateAsync: Invalid height");
j = 0;
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_FORMAT;
break;
@ -116,7 +116,7 @@ void TextureAsset::updateAsync() {
}
default:
assertUnreachable();
assertUnreachable("TextureAsset::updateAsync: Invalid parse state");
}
}

View File

@ -20,7 +20,7 @@ void TilesetAsset::updateSync() {
void TilesetAsset::updateAsync() {
uint8_t *buffer;
assertTrue(this->state == 0x00);
assertTrue(this->state == 0x00, "TilesetAsset::updateAsync: Initial state should be 0x00");
this->state = 0x01;
this->loader.loadRaw(&buffer);
@ -34,28 +34,28 @@ void TilesetAsset::updateAsync() {
*strNext = '\0';
this->tileset.columns = atoi(strCurrent);
this->state = 0x03;
assertTrue(this->tileset.columns > 0);
assertTrue(this->tileset.columns > 0, "TilesetAsset::updateAsync: Count of columns needs to be greater than 0");
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.rows = atoi(strCurrent);
this->state = 0x04;
assertTrue(this->tileset.rows > 0);
assertTrue(this->tileset.rows > 0, "TilesetAsset::updateAsync: Count of rows needs to be greater than 0");
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.divX = atoi(strCurrent);
this->state = 0x05;
assertTrue(this->tileset.rows > 0);
assertTrue(this->tileset.divX > 0, "TilesetAsset::updateAsync: divX needs to be greater than 0");
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.divY = atoi(strCurrent);
this->state = 0x06;
assertTrue(this->tileset.rows > 0);
assertTrue(this->tileset.divY > 0, "TilesetAsset::updateAsync: divY needs to be greater than 0");
// Begin reading tiles.
int32_t done = 0;

View File

@ -15,20 +15,20 @@ TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) :
this->locks.onLockRemoved = [&](usagelockid_t id){
auto texture = this->textureByLock[id];
assertNotNull(texture);
assertNotNull(texture, "TrueTypeAsset::TrueTypeAsset: Texture cannot be null");
std::vector<usagelockid_t> &lbt = this->locksByTexture[texture];
auto it0 = std::find(lbt.begin(), lbt.end(), id);
assertTrue(it0 != lbt.end());
assertTrue(it0 != lbt.end(), "TrueTypeAsset::TrueTypeAsset: Could not remove locksByTexture[texture]");
lbt.erase(it0);
auto it1 = this->textureByLock.find(id);
assertTrue(it1 != this->textureByLock.end());
assertTrue(it1 != this->textureByLock.end(), "TrueTypeAsset::TrueTypeAsset: Could not remove textureByLock");
this->textureByLock.erase(it1);
if(lbt.empty()) {
if(lbt.empty()) {
auto it2 = locksByTexture.find(texture);
assertTrue(it2 != locksByTexture.end());
assertTrue(it2 != locksByTexture.end(), "TrueTypeAsset::TrueTypeAsset: Could not remove locksByTexture");
locksByTexture.erase(it2);
auto it3 = textureByStyle.begin();
@ -38,7 +38,7 @@ TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) :
}
auto it4 = std::find(textures.begin(), textures.end(), texture);
assertTrue(it4 != textures.end());
assertTrue(it4 != textures.end(), "TrueTypeAsset::TrueTypeAsset: Could not remove textureByStyle");
textures.erase(it4);
delete texture;
@ -61,40 +61,40 @@ void TrueTypeAsset::updateAsync() {
uint8_t buffer[64];
this->loader.rewind();
size_t read = this->loader.read(buffer, sizeof(char) * 6);
assertTrue(read == (6 * sizeof(char)));
assertTrue(read == (6 * sizeof(char)), "TrueTypeAsset::updateAsync: Could not read header");
buffer[6] = '\0';
// Confirm "DE_TTF"
assertTrue(std::string((char *)buffer) == "DE_TTF");
assertTrue(std::string((char *)buffer) == "DE_TTF", "TrueTypeAsset::updateAsync: Header is invalid (Missing DE_TTF)");
// Vertical bar
this->loader.read(buffer, 1);
assertTrue(buffer[0] == '|');
assertTrue(buffer[0] == '|', "TrueTypeAsset::updateAsync: Header is invalid (Missing first vertical bar)");
// Read version
this->state = TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION;
read = this->loader.read(buffer, sizeof(char) * 5);
assertTrue(buffer[0] == '3');
assertTrue(buffer[1] == '.');
assertTrue(buffer[2] == '0');
assertTrue(buffer[3] == '0');
assertTrue(buffer[4] == '|');
assertTrue(buffer[0] == '3', "TrueTypeAsset::updateAsync: Version is invalid 3");
assertTrue(buffer[1] == '.', "TrueTypeAsset::updateAsync: Version is invalid .");
assertTrue(buffer[2] == '0', "TrueTypeAsset::updateAsync: Version is invalid 0(1)");
assertTrue(buffer[3] == '0', "TrueTypeAsset::updateAsync: Version is invalid 0(2)");
assertTrue(buffer[4] == '|', "TrueTypeAsset::updateAsync: Version is invalid (Missing second vertical bar)");
// Read the count of font styles / variants.
size_t styleListBegin = this->loader.getPosition();
this->state = TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT;
read = this->loader.read(buffer, 64);
assertTrue(read > 0);
assertTrue(read > 0, "TrueTypeAsset::updateAsync: Could not read variant count");
// Get position of vertical bar.
size_t i = 0;
while(buffer[i] != '|' && i < 64) i++;
assertTrue(buffer[i] == '|');
assertTrue(buffer[i] == '|', "TrueTypeAsset::updateAsync: Could not find vertical bar");
styleListBegin += i + 1;
buffer[i] = '\0';
int32_t count = atoi((char *)buffer);
assertTrue(count > 0);
assertTrue(count > 0, "TrueTypeAsset::updateAsync: Invalid variant count");
// Now begin parsing each font style.
this->state = TRUE_TYPE_ASSET_STATE_READ_VARIANT;
@ -106,7 +106,7 @@ void TrueTypeAsset::updateAsync() {
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
assertTrue(read == 32, "TrueTypeAsset::updateAsync: Could not read variant");
// Read style
i = 0;
@ -119,7 +119,7 @@ void TrueTypeAsset::updateAsync() {
this->loader.rewind();
this->loader.setPosition(styleListBegin);
read = this->loader.read(buffer, 32);
assertTrue(read == 32);
assertTrue(read == 32, "TrueTypeAsset::updateAsync: Could not read variant style");
// Read length
i = 0;
@ -145,7 +145,7 @@ void TrueTypeAsset::updateAsync() {
// Init FreeType
this->state = TRUE_TYPE_ASSET_STATE_INIT_FREETYPE;
int32_t ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0);
assertTrue(ret == 0, "TrueTypeAsset::updateAsync: Could not init FreeType");
// Done parsing!
this->state = TRUE_TYPE_ASSET_STATE_READY;
@ -153,7 +153,7 @@ void TrueTypeAsset::updateAsync() {
}
usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
assertTrue(this->state == TRUE_TYPE_ASSET_STATE_READY);
assertTrue(this->state == TRUE_TYPE_ASSET_STATE_READY, "TrueTypeAsset::lock: Asset is not ready");
// Try and find an existing texture that matches this style
auto it = this->textureByStyle.find(style);
@ -169,19 +169,19 @@ usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
}
++itAssetStyle;
}
assertTrue(itAssetStyle != this->assetStyles.end());
assertTrue(itAssetStyle != this->assetStyles.end(), "TrueTypeAsset::lock: Could not find asset style");
// Create and read buffer.
uint8_t *dataBuffer = (uint8_t*)memoryAllocate(sizeof(uint8_t) * itAssetStyle->dataSize);
this->loader.rewind();
this->loader.setPosition(itAssetStyle->dataOffset);
auto read = this->loader.read(dataBuffer, itAssetStyle->dataSize);
assertTrue(read == itAssetStyle->dataSize);
assertTrue(read == itAssetStyle->dataSize, "TrueTypeAsset::lock: Could not read data");
// Create the face
FT_Face face;
auto ret = FT_New_Memory_Face(this->fontLibrary, (FT_Byte*)dataBuffer, itAssetStyle->dataSize, 0, &face);
assertTrue(ret == 0);
assertTrue(ret == 0, "TrueTypeAsset::lock: Could not create face");
texture = new TrueTypeFaceTexture(face, style);
memoryFree(dataBuffer);
@ -200,7 +200,7 @@ usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
TrueTypeFaceTexture * TrueTypeAsset::getTexture(usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end());
assertTrue(it != this->textureByLock.end(), "TrueTypeAsset::getTexture: Could not find texture");
return it->second;
}