diff --git a/storm/Crypto.cpp b/storm/Crypto.cpp index 0b8cae9..53fc925 100644 --- a/storm/Crypto.cpp +++ b/storm/Crypto.cpp @@ -3,8 +3,9 @@ #include void SARC4PrepareKey(const void* data, uint32_t len, SARC4Key* key) { - STORM_ASSERT(data); - STORM_VALIDATE(data, ERROR_INVALID_PARAMETER); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(data); + STORM_VALIDATE_END_VOID; key->x = 0; key->y = 0; diff --git a/storm/Error.cpp b/storm/Error.cpp index 9c8b239..79c67be 100644 --- a/storm/Error.cpp +++ b/storm/Error.cpp @@ -24,7 +24,7 @@ int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linen printf("\n=========================================================\n"); - if (linenumber == -5) { + if (linenumber == SERR_LINECODE_EXCEPTION) { printf("Exception Raised!\n\n"); printf(" App: %s\n", "GenericBlizzardApp"); diff --git a/storm/Error.hpp b/storm/Error.hpp index 75e8b9b..e8681c5 100644 --- a/storm/Error.hpp +++ b/storm/Error.hpp @@ -9,27 +9,100 @@ #if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX) #define ERROR_SUCCESS 0x0 +#define ERROR_INVALID_HANDLE 0x6 +#define ERROR_NOT_ENOUGH_MEMORY 0x8 #define ERROR_INVALID_PARAMETER 0x57 #endif -#if defined(NDEBUG) -#define STORM_ASSERT(x) \ - (void)0 -#else -#define STORM_ASSERT(x) \ + +#ifdef _DEBUG +#ifndef ASSERTIONS_ENABLED +#define ASSERTIONS_ENABLED +#endif +#endif + + +#if defined(ASSERTIONS_ENABLED) +#define STORM_ASSERT(x) \ + if (!(x)) { \ + SErrDisplayError(STORM_ERROR_ASSERTION, __FILE__, __LINE__, #x, 0, 1, 0x11111111); \ + } + +#define STORM_ASSERT_FATAL(x) \ if (!(x)) { \ SErrPrepareAppFatal(__FILE__, __LINE__); \ SErrDisplayAppFatal(#x); \ - } \ - (void)0 + } +#else +#define STORM_ASSERT(x) +#define STORM_ASSERT_FATAL(x) #endif -#define STORM_VALIDATE(x, y, ...) \ - if (!(x)) { \ - SErrSetLastError(y); \ - return __VA_ARGS__; \ - } \ - (void)0 + +#if defined(NDEBUG) +#define STORM_VALIDATE_BEGIN { bool __storm_result = true +#define STORM_VALIDATE(x) __storm_result &= !!(x); STORM_ASSERT(x) +#define STORM_VALIDATE_END \ + if (!__storm_result) { \ + SErrSetLastError(ERROR_INVALID_PARAMETER); \ + return 0; \ + } } +#define STORM_VALIDATE_END_VOID \ + if (!__storm_result) { \ + SErrSetLastError(ERROR_INVALID_PARAMETER); \ + return; \ + } } +#else +#define STORM_VALIDATE_BEGIN { +#define STORM_VALIDATE(x) STORM_ASSERT_FATAL(x) +#define STORM_VALIDATE_END } +#define STORM_VALIDATE_END_VOID } +#endif + + +#define STORM_ERROR_ASSERTION 0x85100000 +#define STORM_ERROR_BAD_ARGUMENT 0x85100065 +#define STORM_ERROR_GAME_ALREADY_STARTED 0x85100066 +#define STORM_ERROR_GAME_FULL 0x85100067 +#define STORM_ERROR_GAME_NOT_FOUND 0x85100068 +#define STORM_ERROR_GAME_TERMINATED 0x85100069 +#define STORM_ERROR_INVALID_PLAYER 0x8510006A +#define STORM_ERROR_NO_MESSAGES_WAITING 0x8510006B +#define STORM_ERROR_NOT_ARCHIVE 0x8510006C +#define STORM_ERROR_NOT_ENOUGH_ARGUMENTS 0x8510006D +#define STORM_ERROR_NOT_IMPLEMENTED 0x8510006E +#define STORM_ERROR_NOT_IN_ARCHIVE 0x8510006F +#define STORM_ERROR_NOT_IN_GAME 0x85100070 +#define STORM_ERROR_NOT_INITIALIZED 0x85100071 +#define STORM_ERROR_NOT_PLAYING 0x85100072 +#define STORM_ERROR_NOT_REGISTERED 0x85100073 +#define STORM_ERROR_REQUIRES_CODEC 0x85100074 +#define STORM_ERROR_REQUIRES_DDRAW 0x85100075 +#define STORM_ERROR_REQUIRES_DSOUND 0x85100076 +#define STORM_ERROR_REQUIRES_UPGRADE 0x85100077 +#define STORM_ERROR_STILL_ACTIVE 0x85100078 +#define STORM_ERROR_VERSION_MISMATCH 0x85100079 +#define STORM_ERROR_MEMORY_ALREADY_FREED 0x8510007A +#define STORM_ERROR_MEMORY_CORRUPT 0x8510007B +#define STORM_ERROR_MEMORY_INVALID_BLOCK 0x8510007C +#define STORM_ERROR_MEMORY_MANAGER_INACTIVE 0x8510007D +#define STORM_ERROR_MEMORY_NEVER_RELEASED 0x8510007E +#define STORM_ERROR_HANDLE_NEVER_RELEASED 0x8510007F +#define STORM_ERROR_ACCESS_OUT_OF_BOUNDS 0x85100080 +#define STORM_ERROR_MEMORY_NULL_POINTER 0x85100081 +#define STORM_ERROR_CDKEY_MISMATCH 0x85100082 +#define STORM_ERROR_DATA_FILE_CORRUPT 0x85100083 +#define STORM_ERROR_FATAL_EXCEPTION 0x85100084 +#define STORM_ERROR_GAME_TYPE_UNAVAILABLE 0x85100085 +#define STORM_ERROR_FATAL_CONDITION 0x85100086 + + +#define SERR_LINECODE_FUNCTION -1 +#define SERR_LINECODE_OBJECT -2 +#define SERR_LINECODE_HANDLE -3 +#define SERR_LINECODE_FILE -4 +#define SERR_LINECODE_EXCEPTION -5 // exception handler + [[noreturn]] void SErrDisplayAppFatal(const char* format, ...); diff --git a/storm/Region.cpp b/storm/Region.cpp index 9ff47bc..1d96db2 100644 --- a/storm/Region.cpp +++ b/storm/Region.cpp @@ -159,10 +159,12 @@ void ProcessBooleanOperation(TSGrowableArray* sourceArray, int32_t combi } void SRgnCombineRectf(HSRGN handle, RECTF* rect, void* param, int32_t combineMode) { - STORM_ASSERT(handle); - STORM_ASSERT(rect); - STORM_ASSERT(combineMode >= 1); - STORM_ASSERT(combineMode <= 6); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(handle); + STORM_VALIDATE(rect); + STORM_VALIDATE(combineMode >= 1); + STORM_VALIDATE(combineMode <= 6); + STORM_VALIDATE_END_VOID; HLOCKEDRGN lockedHandle; auto rgn = s_rgntable.Lock(handle, &lockedHandle, 0); @@ -190,8 +192,10 @@ void SRgnCombineRectf(HSRGN handle, RECTF* rect, void* param, int32_t combineMod } void SRgnCreate(HSRGN* handlePtr, uint32_t reserved) { - STORM_ASSERT(handlePtr); - STORM_ASSERT(!reserved); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(handlePtr); + STORM_VALIDATE(!reserved); + STORM_VALIDATE_END_VOID; HLOCKEDRGN lockedHandle = nullptr; auto rgn = s_rgntable.NewLock(handlePtr, &lockedHandle); @@ -202,14 +206,18 @@ void SRgnCreate(HSRGN* handlePtr, uint32_t reserved) { } void SRgnDelete(HSRGN handle) { - STORM_ASSERT(handle); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(handle); + STORM_VALIDATE_END_VOID; s_rgntable.Delete(handle); } void SRgnGetBoundingRectf(HSRGN handle, RECTF* rect) { - STORM_ASSERT(handle); - STORM_ASSERT(rect); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(handle); + STORM_VALIDATE(rect); + STORM_VALIDATE_END_VOID; rect->left = std::numeric_limits::max(); rect->bottom = std::numeric_limits::max(); diff --git a/storm/String.cpp b/storm/String.cpp index 806e3ab..610fc65 100644 --- a/storm/String.cpp +++ b/storm/String.cpp @@ -207,8 +207,9 @@ void SStrInitialize() { } char* SStrChr(char* string, char search) { - STORM_ASSERT(string); - STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; if (!*string) { return nullptr; @@ -226,8 +227,9 @@ char* SStrChr(char* string, char search) { } const char* SStrChr(const char* string, char search) { - STORM_ASSERT(string); - STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; if (!*string) { return nullptr; @@ -245,8 +247,9 @@ const char* SStrChr(const char* string, char search) { } char* SStrChrR(char* string, char search) { - STORM_ASSERT(string); - STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; char* result; @@ -260,8 +263,9 @@ char* SStrChrR(char* string, char search) { } const char* SStrChrR(const char* string, char search) { - STORM_ASSERT(string); - STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; const char* result; @@ -275,10 +279,20 @@ const char* SStrChrR(const char* string, char search) { } int32_t SStrCmp(const char* string1, const char* string2, size_t maxchars) { + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string1); + STORM_VALIDATE(string2); + STORM_VALIDATE_END; + return strncmp(string1, string2, maxchars); } int32_t SStrCmpI(const char* string1, const char* string2, size_t maxchars) { + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string1); + STORM_VALIDATE(string2); + STORM_VALIDATE_END; + #if defined(WHOA_SYSTEM_WIN) return _strnicmp(string1, string2, maxchars); #endif @@ -289,9 +303,10 @@ int32_t SStrCmpI(const char* string1, const char* string2, size_t maxchars) { } size_t SStrCopy(char* dest, const char* source, size_t destsize) { - STORM_ASSERT(dest); - STORM_ASSERT(source); - STORM_VALIDATE(dest && source, ERROR_INVALID_PARAMETER, 0); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(dest); + STORM_VALIDATE(source); + STORM_VALIDATE_END; char* destbuf = dest; @@ -323,6 +338,10 @@ size_t SStrCopy(char* dest, const char* source, size_t destsize) { } char* SStrDupA(const char* string, const char* filename, uint32_t linenumber) { + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; + size_t len = SStrLen(string) + 1; char* dup = static_cast(SMemAlloc(len, filename, linenumber, 0x0)); memcpy(dup, string, len); @@ -362,7 +381,9 @@ uint32_t SStrHashHT(const char* string) { } size_t SStrLen(const char* string) { - STORM_ASSERT(string); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; auto stringEnd = string; while (*stringEnd) { @@ -380,8 +401,10 @@ void SStrLower(char* string) { } uint32_t SStrPack(char* dest, const char* source, uint32_t destsize) { - STORM_ASSERT(dest); - STORM_ASSERT(source); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(dest); + STORM_VALIDATE(source); + STORM_VALIDATE_END; if (!destsize) { return 0; @@ -426,15 +449,19 @@ size_t SStrPrintf(char* dest, size_t maxchars, const char* format, ...) { va_list va; va_start(va, format); - STORM_ASSERT(dest); - STORM_ASSERT(format); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(dest); + STORM_VALIDATE(format); + STORM_VALIDATE_END; return ISStrVPrintf(format, va, dest, maxchars); } const char* SStrStr(const char* string, const char* search) { - STORM_ASSERT(string); - STORM_ASSERT(search); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE(search); + STORM_VALIDATE_END; if (!*string) { return nullptr; @@ -460,14 +487,12 @@ const char* SStrStr(const char* string, const char* search) { } void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const char* whitespace, int32_t* quoted) { - STORM_ASSERT(string); - STORM_VALIDATE(string, ERROR_INVALID_PARAMETER); - STORM_ASSERT(*string); - STORM_VALIDATE(*string, ERROR_INVALID_PARAMETER); - STORM_ASSERT(buffer || !bufferchars); - STORM_VALIDATE(buffer || !bufferchars, ERROR_INVALID_PARAMETER); - STORM_ASSERT(whitespace); - STORM_VALIDATE(whitespace, ERROR_INVALID_PARAMETER); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE(*string); + STORM_VALIDATE(buffer || bufferchars == 0); + STORM_VALIDATE(whitespace); + STORM_VALIDATE_END_VOID; int32_t checkquotes = SStrChr(whitespace, '"') != nullptr; @@ -529,7 +554,9 @@ void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const c } float SStrToFloat(const char* string) { - STORM_ASSERT(string); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; SStrInitialize(); @@ -626,7 +653,9 @@ float SStrToFloat(const char* string) { } int32_t SStrToInt(const char* string) { - STORM_ASSERT(string); + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; int32_t result = 0; bool negative = false; diff --git a/storm/array/TSBaseArray.hpp b/storm/array/TSBaseArray.hpp index 417ee9c..23e99dd 100644 --- a/storm/array/TSBaseArray.hpp +++ b/storm/array/TSBaseArray.hpp @@ -40,7 +40,7 @@ void TSBaseArray::CheckArrayBounds(uint32_t index) const { } SErrDisplayErrorFmt( - 0x85100080, + STORM_ERROR_ACCESS_OUT_OF_BOUNDS, this->MemFileName(), this->MemLineNo(), 1,