From 8fc1db2b68589a30fd706a4466edde6d6541ee33 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 15 Aug 2018 16:39:21 +1000 Subject: [PATCH] combine win/nix error handler. simplify bitmap encoding a little bit --- src/Client/Bitmap.c | 32 +++-- src/Client/Bitmap.h | 2 +- src/Client/Client.vcxproj | 3 +- src/Client/Client.vcxproj.filters | 9 +- src/Client/D3D9Api.c | 4 +- .../{WinErrorHandler.c => ErrorHandler.c} | 115 +++++++++++------- src/Client/ErrorHandler.h | 6 + src/Client/NixErrorHandler.c | 27 ---- src/Client/OpenGLApi.c | 3 +- src/Client/ServerConnection.c | 14 +-- src/Client/Stream.h | 2 - src/Client/TexturePack.c | 4 +- 12 files changed, 120 insertions(+), 101 deletions(-) rename src/Client/{WinErrorHandler.c => ErrorHandler.c} (54%) delete mode 100644 src/Client/NixErrorHandler.c diff --git a/src/Client/Bitmap.c b/src/Client/Bitmap.c index 07aa625c7..aa4c8a659 100644 --- a/src/Client/Bitmap.c +++ b/src/Client/Bitmap.c @@ -609,12 +609,11 @@ static void Png_EncodeRow(UInt8* src, UInt8* cur, UInt8* prior, UInt8* best, Int best[0] = bestFilter; } -void Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream) { - Stream_Write(stream, png_sig, PNG_SIG_SIZE); - struct Stream* underlying = stream; - struct Stream crc32Stream; - Bitmap_Crc32Stream(&crc32Stream, underlying); +ReturnCode Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream) { + ReturnCode result; UInt8 tmp[32]; + result = Stream_TryWrite(stream, png_sig, PNG_SIG_SIZE); + if (result) return result; /* Write header chunk */ Stream_SetU32_BE(&tmp[0], PNG_IHDR_SIZE); @@ -629,7 +628,12 @@ void Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream) { tmp[20] = 0; /* Not using interlacing */ } Stream_SetU32_BE(&tmp[21], Utils_CRC32(&tmp[4], sizeof(UInt32) + PNG_IHDR_SIZE)); - Stream_Write(stream, tmp, PNG_IHDR_SIZE + 3 * sizeof(UInt32)); + result = Stream_TryWrite(stream, tmp, PNG_IHDR_SIZE + 3 * sizeof(UInt32)); + if (result) return result; + + struct Stream* underlying = stream; + struct Stream crc32Stream; + Bitmap_Crc32Stream(&crc32Stream, underlying); /* Write PNG body */ UInt8 prevLine[PNG_MAX_DIMS * 3]; @@ -655,19 +659,23 @@ void Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream) { } zlStream.Close(&zlStream); } - UInt32 dataEnd; ReturnCode result = underlying->Position(underlying, &dataEnd); - ErrorHandler_CheckOrFail(result, "PNG - getting position of data end"); stream = underlying; Stream_WriteU32_BE(stream, crc32Stream.Meta.CRC32.CRC32 ^ 0xFFFFFFFFUL); /* Write end chunk */ Stream_SetU32_BE(&tmp[0], 0); Stream_SetU32_BE(&tmp[4], PNG_FourCC('I','E','N','D')); - Stream_SetU32_BE(&tmp[8], 0xAE426082UL); /* crc32 of iend */ - Stream_Write(stream, tmp, 3 * sizeof(UInt32)); + Stream_SetU32_BE(&tmp[8], 0xAE426082UL); /* CRC32 of iend */ + result = Stream_TryWrite(stream, tmp, 3 * sizeof(UInt32)); + if (result) return result; /* Come back to write size of data chunk */ + UInt32 stream_len; + result = stream->Length(stream, &stream_len); + if (result) return result; result = stream->Seek(stream, 33, STREAM_SEEKFROM_BEGIN); - ErrorHandler_CheckOrFail(result, "PNG - seeking to write data size"); - Stream_WriteU32_BE(stream, dataEnd - 41); + if (result) return result; + + Stream_WriteU32_BE(stream, stream_len - 57); + return 0; } diff --git a/src/Client/Bitmap.h b/src/Client/Bitmap.h index 64a564488..364de77ff 100644 --- a/src/Client/Bitmap.h +++ b/src/Client/Bitmap.h @@ -27,5 +27,5 @@ bool Bitmap_DetectPng(UInt8* data, UInt32 len); https://github.com/nothings/stb/blob/master/stb_image.h */ ReturnCode Bitmap_DecodePng(struct Bitmap* bmp, struct Stream* stream); -void Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream); +ReturnCode Bitmap_EncodePng(struct Bitmap* bmp, struct Stream* stream); #endif diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 370023f3d..37567f49f 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -293,7 +293,6 @@ - @@ -307,7 +306,7 @@ - + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index da24920dc..bf8e1c15c 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -374,9 +374,6 @@ Source Files\2D\Utils - - Source Files\Platform - Source Files\MeshBuilder @@ -521,9 +518,6 @@ Source Files\Rendering - - Source Files\Platform - Source Files\Platform @@ -536,5 +530,8 @@ Source Files\Platform + + Source Files\Platform + \ No newline at end of file diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index 380b3d635..4d0943b91 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -583,7 +583,9 @@ void Gfx_TakeScreenshot(struct Stream* output, Int32 width, Int32 height) { ErrorHandler_CheckOrFail(hresult, "Gfx_TakeScreenshot - Lock temp surface"); struct Bitmap bmp; Bitmap_Create(&bmp, width, height, rect.pBits); - Bitmap_EncodePng(&bmp, output); + hresult = Bitmap_EncodePng(&bmp, output); + ErrorHandler_CheckOrFail(hresult, "Gfx_TakeScreenshot - Writing .png"); + hresult = IDirect3DSurface9_UnlockRect(temp); ErrorHandler_CheckOrFail(hresult, "Gfx_TakeScreenshot - Unlock temp surface"); diff --git a/src/Client/WinErrorHandler.c b/src/Client/ErrorHandler.c similarity index 54% rename from src/Client/WinErrorHandler.c rename to src/Client/ErrorHandler.c index ef7887d26..b97745a8d 100644 --- a/src/Client/WinErrorHandler.c +++ b/src/Client/ErrorHandler.c @@ -1,6 +1,57 @@ #include "ErrorHandler.h" -#if CC_BUILD_WIN #include "Platform.h" + +void ErrorHandler_Log(STRING_PURE String* msg) { + /* TODO: write to log file */ +} + +void ErrorHandler_Log1(const UChar* format, const void* a1) { + ErrorHandler_Log4(format, a1, NULL, NULL, NULL); +} +void ErrorHandler_Log2(const UChar* format, const void* a1, const void* a2) { + ErrorHandler_Log4(format, a1, a2, NULL, NULL); +} +void ErrorHandler_Log3(const UChar* format, const void* a1, const void* a2, const void* a3) { + ErrorHandler_Log4(format, a1, a2, a3, NULL); +} + +void ErrorHandler_Log4(const UChar* format, const void* a1, const void* a2, const void* a3, const void* a4) { + UChar msgBuffer[String_BufferSize(3071)]; + String msg = String_InitAndClearArray(msgBuffer); + String_Format4(&msg, format, a1, a2, a3, a4); + Chat_Add(&msg); + + String_AppendConst(&msg, Platform_NewLine); + ErrorHandler_Backtrace(&msg); + ErrorHandler_Log(&msg); +} + +void ErrorHandler_LogError(ReturnCode result, const UChar* place) { + ErrorHandler_Log4("&cError %y when %c", &result, place, NULL, NULL); +} + +void ErrorHandler_FailWithCode(ReturnCode result, const UChar* raw_msg) { + UChar logMsgBuffer[String_BufferSize(3071)]; + String logMsg = String_InitAndClearArray(logMsgBuffer); + + String_Format3(&logMsg, "ClassiCube crashed.%cMessge: %c%c", + Platform_NewLine, raw_msg, Platform_NewLine); + if (result) { + String_Format2(&logMsg, "%y%c", &result, Platform_NewLine); + } else { result = 1; } + + ErrorHandler_Backtrace(&logMsg); + /* TODO: write to log file */ + + String_AppendConst(&logMsg, + "Please report the crash to github.com/UnknownShadow200/ClassicalSharp/issues so we can fix it."); + ErrorHandler_ShowDialog("We're sorry", logMsg.buffer); + Platform_Exit(result); +} + +void ErrorHandler_Fail(const UChar* raw_msg) { ErrorHandler_FailWithCode(0, raw_msg); } + +#if CC_BUILD_WIN #define WIN32_LEAN_AND_MEAN #define NOSERVICE #define NOMCX @@ -8,18 +59,6 @@ #include #include -/* TODO: These might be better off as a function. */ -#define ErrorHandler_WriteLogBody(raw_msg)\ -UChar logMsgBuffer[String_BufferSize(3071)];\ -String logMsg = String_InitAndClearArray(logMsgBuffer);\ -String_AppendConst(&logMsg, "ClassiCube crashed.\r\n");\ -String_AppendConst(&logMsg, "Message: ");\ -String_AppendConst(&logMsg, raw_msg);\ -String_AppendConst(&logMsg, "\r\n"); - -#define ErrorHandler_WriteLogEnd()\ -String_AppendConst(&logMsg, "Please report the crash to github.com/UnknownShadow200/ClassicalSharp/issues so we can fix it."); - static LONG WINAPI ErrorHandler_UnhandledFilter(struct _EXCEPTION_POINTERS* pInfo) { /* TODO: Write processor state to file*/ /* TODO: Get address that caused the issue */ @@ -40,39 +79,11 @@ void ErrorHandler_Init(const UChar* logFile) { /* TODO: Open log file */ } -void ErrorHandler_Log(STRING_PURE String* msg) { - /* TODO: write to log file */ -} - -void ErrorHandler_Fail(const UChar* raw_msg) { - /* TODO: write to log file */ - ErrorHandler_WriteLogBody(raw_msg); - ErrorHandler_Backtrace(&logMsg); - ErrorHandler_WriteLogEnd(); - - ErrorHandler_ShowDialog("We're sorry", logMsg.buffer); - Platform_Exit(1); -} - -void ErrorHandler_FailWithCode(ReturnCode code, const UChar* raw_msg) { - /* TODO: write to log file */ - ErrorHandler_WriteLogBody(raw_msg); - String_AppendConst(&logMsg, "Return code: "); - String_Hex32(&logMsg, code); - String_AppendConst(&logMsg, "\r\n"); - ErrorHandler_Backtrace(&logMsg); - ErrorHandler_WriteLogEnd(); - - ErrorHandler_ShowDialog("We're sorry", logMsg.buffer); - Platform_Exit(code); -} - void ErrorHandler_ShowDialog(const UChar* title, const UChar* msg) { HWND win = GetActiveWindow(); /* TODO: It's probably wrong to use GetActiveWindow() here */ MessageBoxA(win, msg, title, 0); } - struct SymbolAndName { SYMBOL_INFO Symbol; UChar Name[256]; }; void ErrorHandler_Backtrace(STRING_TRANSIENT String* str) { HANDLE process = GetCurrentProcess(); @@ -100,4 +111,26 @@ void ErrorHandler_Backtrace(STRING_TRANSIENT String* str) { } String_AppendConst(str, "\r\n"); } +#elif CC_BUILD_NIX +void ErrorHandler_Init(const UChar* logFile) { + /* TODO: Implement this */ +} + +void ErrorHandler_Log(STRING_PURE String* msg) { + /* TODO: Implement this */ +} + +void ErrorHandler_Fail(const UChar* raw_msg) { + /* TODO: Implement this */ + Platform_Exit(1); +} + +void ErrorHandler_FailWithCode(ReturnCode code, const UChar* raw_msg) { + /* TODO: Implement this */ + Platform_Exit(code); +} + +void ErrorHandler_ShowDialog(const UChar* title, const UChar* msg) { + /* TODO: Implement this */ +} #endif diff --git a/src/Client/ErrorHandler.h b/src/Client/ErrorHandler.h index 3c3222db5..c8b8a26c2 100644 --- a/src/Client/ErrorHandler.h +++ b/src/Client/ErrorHandler.h @@ -8,6 +8,12 @@ void ErrorHandler_Init(const UChar* logFile); void ErrorHandler_Log(STRING_PURE String* msg); +void ErrorHandler_Log1(const UChar* format, const void* a1); +void ErrorHandler_Log2(const UChar* format, const void* a1, const void* a2); +void ErrorHandler_Log3(const UChar* format, const void* a1, const void* a2, const void* a3); +void ErrorHandler_Log4(const UChar* format, const void* a1, const void* a2, const void* a3, const void* a4); +void ErrorHandler_LogError(ReturnCode result, const UChar* place); + #define ErrorHandler_Check(returnCode) ((returnCode) == 0) void ErrorHandler_Fail(const UChar* raw_msg); void ErrorHandler_FailWithCode(ReturnCode returnCode, const UChar* raw_msg); diff --git a/src/Client/NixErrorHandler.c b/src/Client/NixErrorHandler.c deleted file mode 100644 index 0168d9917..000000000 --- a/src/Client/NixErrorHandler.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "ErrorHandler.h" -#if CC_BUILD_NIX -#include "Platform.h" - -void ErrorHandler_Init(const UChar* logFile) { - /* TODO: Implement this */ -} - -void ErrorHandler_Log(STRING_PURE String* msg) { - /* TODO: Implement this */ -} - -void ErrorHandler_Fail(const UChar* raw_msg) { - /* TODO: Implement this */ - Platform_Exit(1); -} - -void ErrorHandler_FailWithCode(ReturnCode code, const UChar* raw_msg) { - /* TODO: Implement this */ - Platform_Exit(code); -} - -void ErrorHandler_ShowDialog(const UChar* title, const UChar* msg) { - /* TODO: Implement this */ -} - -#endif diff --git a/src/Client/OpenGLApi.c b/src/Client/OpenGLApi.c index c1b900b5d..6d4ffa338 100644 --- a/src/Client/OpenGLApi.c +++ b/src/Client/OpenGLApi.c @@ -536,7 +536,8 @@ void Gfx_TakeScreenshot(struct Stream* output, Int32 width, Int32 height) { }*/ } - Bitmap_EncodePng(&bmp, output); + ReturnCode hresult = Bitmap_EncodePng(&bmp, output); + ErrorHandler_CheckOrFail(hresult, "Gfx_TakeScreenshot - Writing .png"); Mem_Free(&bmp.Scan0); } diff --git a/src/Client/ServerConnection.c b/src/Client/ServerConnection.c index a35affc9c..2812525f9 100644 --- a/src/Client/ServerConnection.c +++ b/src/Client/ServerConnection.c @@ -399,17 +399,17 @@ static void MPConnection_Tick(struct ScheduledTask* task) { } if (ServerConnection_Disconnected) return; - UInt32 modified = 0; - ReturnCode recvResult = Socket_Available(net_socket, &modified); - if (recvResult == 0 && modified > 0) { + UInt32 pending = 0; + ReturnCode recvResult = Socket_Available(net_socket, &pending); + if (recvResult == 0 && pending) { /* NOTE: Always using a read call that is a multiple of 4096 (appears to?) improve read performance */ UInt8* src = net_readBuffer + net_readStream.Meta.Mem.Left; - recvResult = Socket_Read(net_socket, src, 4096 * 4, &modified); - net_readStream.Meta.Mem.Left += modified; - net_readStream.Meta.Mem.Length += modified; + recvResult = Socket_Read(net_socket, src, 4096 * 4, &pending); + net_readStream.Meta.Mem.Left += pending; + net_readStream.Meta.Mem.Length += pending; } - if (recvResult != 0) { + if (recvResult) { UChar msgBuffer[String_BufferSize(STRING_SIZE * 2)]; String msg = String_InitAndClearArray(msgBuffer); String_Format3(&msg, "Error reading from %s:%i: %i", &Game_IPAddress, &Game_Port, &recvResult); diff --git a/src/Client/Stream.h b/src/Client/Stream.h index dd9d4b7c4..5115ac6c6 100644 --- a/src/Client/Stream.h +++ b/src/Client/Stream.h @@ -67,8 +67,6 @@ UInt32 Stream_ReadU32_BE(struct Stream* stream); void Stream_WriteU8(struct Stream* stream, UInt8 value); void Stream_WriteU16_BE(struct Stream* stream, UInt16 value); void Stream_WriteU32_BE(struct Stream* stream, UInt32 value); -#define Stream_WriteI16_BE(stream, value) Stream_WriteU16_BE(stream, (UInt16)(value)) -#define Stream_WriteI32_BE(stream, value) Stream_WriteU32_BE(stream, (UInt32)(value)) ReturnCode Stream_ReadUtf8Char(struct Stream* stream, UInt16* codepoint); bool Stream_ReadLine(struct Stream* stream, STRING_TRANSIENT String* text); diff --git a/src/Client/TexturePack.c b/src/Client/TexturePack.c index 1d268c419..de78c927a 100644 --- a/src/Client/TexturePack.c +++ b/src/Client/TexturePack.c @@ -125,8 +125,10 @@ void Zip_Init(struct ZipState* state, struct Stream* input) { ReturnCode Zip_Extract(struct ZipState* state) { state->EntriesCount = 0; struct Stream* stream = state->Input; - UInt32 sig = 0, stream_len = 0; + UInt32 sig = 0, stream_len; + ReturnCode result = stream->Length(stream, &stream_len); + if (result) return result; /* At -22 for nearly all zips, but try a bit further back in case of comment */ Int32 i, len = min(257, stream_len);