combine win/nix error handler. simplify bitmap encoding a little bit

This commit is contained in:
UnknownShadow200 2018-08-15 16:39:21 +10:00
parent 79efdcf01a
commit 8fc1db2b68
12 changed files with 120 additions and 101 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -293,7 +293,6 @@
<ClCompile Include="BlockPhysics.c" />
<ClCompile Include="PickedPosRenderer.c" />
<ClCompile Include="Picking.c" />
<ClCompile Include="NixErrorHandler.c" />
<ClCompile Include="Platform.c" />
<ClCompile Include="Program.c" />
<ClCompile Include="Screens.c" />
@ -307,7 +306,7 @@
<ClCompile Include="Vectors.c" />
<ClCompile Include="Vorbis.c" />
<ClCompile Include="Widgets.c" />
<ClCompile Include="WinErrorHandler.c" />
<ClCompile Include="ErrorHandler.c" />
<ClCompile Include="WinWindow.c" />
<ClCompile Include="World.c" />
<ClCompile Include="NixWindow.c" />

View File

@ -374,9 +374,6 @@
<ClCompile Include="PackedCol.c">
<Filter>Source Files\2D\Utils</Filter>
</ClCompile>
<ClCompile Include="WinErrorHandler.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
<ClCompile Include="Drawer.c">
<Filter>Source Files\MeshBuilder</Filter>
</ClCompile>
@ -521,9 +518,6 @@
<ClCompile Include="EnvRenderer.c">
<Filter>Source Files\Rendering</Filter>
</ClCompile>
<ClCompile Include="NixErrorHandler.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
<ClCompile Include="NixWindow.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
@ -536,5 +530,8 @@
<ClCompile Include="Platform.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
<ClCompile Include="ErrorHandler.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -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");

View File

@ -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 <windows.h>
#include <dbghelp.h>
/* 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

View File

@ -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);

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);