From 1b28a870857467235acb0f600bb406384dd43441 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 1 Mar 2019 00:19:16 +1100 Subject: [PATCH] fix not showing command line args when failing to start due 'missing IP and/or port' also cleanup logger code in general --- ClassicalSharp/known_bugs.txt | 3 ++- src/Game.c | 15 +++---------- src/LWeb.c | 2 +- src/Logger.c | 40 ++++++++++++++++++++--------------- src/Logger.h | 18 ++++++++-------- src/Platform.c | 4 ++-- src/Platform.h | 2 +- src/Program.c | 35 +++++++++++++++++++++--------- src/Resources.c | 2 +- src/String.c | 4 +--- 10 files changed, 68 insertions(+), 57 deletions(-) diff --git a/ClassicalSharp/known_bugs.txt b/ClassicalSharp/known_bugs.txt index a97185f8f..487851f31 100644 --- a/ClassicalSharp/known_bugs.txt +++ b/ClassicalSharp/known_bugs.txt @@ -1,4 +1,5 @@ * Blocks over 256 are not saved or loaded at all. * Custom block information for blocks over 256 is not saved or loaded at all. * /hold 0 prevents you deleting blocks until you change to another. -* Sometimes when holding air and your own model is a block model, you crash. \ No newline at end of file +* Sometimes when holding air and your own model is a block model, you crash. +* Labels and buttons overlap in launcher with some fonts. (e.g. Lucida Console) \ No newline at end of file diff --git a/src/Game.c b/src/Game.c index 29902b457..d2c1d1081 100644 --- a/src/Game.c +++ b/src/Game.c @@ -314,13 +314,7 @@ static void Game_OnLowVRAMDetected(void* obj) { Chat_AddRaw("&cOut of VRAM! Halving view distance.."); } -static void Game_Warn(ReturnCode result, const char* place) { - Chat_Add4("&cError %h when %c", &result, place, NULL, NULL); -} -static void Game_Warn2(ReturnCode result, const char* place, const String* path) { - Chat_Add4("&cError %h when %c '%s'", &result, place, path, NULL); -} - +static void Game_WarnFunc(const String* msg) { Chat_Add1("&c%s", msg); } static void Game_ExtractInitialTexturePack(void) { String texPack; char texPackBuffer[STRING_SIZE]; @@ -418,9 +412,7 @@ void Game_Free(void* obj); static void Game_Load(void) { String title; char titleBuffer[STRING_SIZE]; struct IGameComponent* comp; - - Logger_Warn = Game_Warn; - Logger_Warn2 = Game_Warn2; + Logger_WarnFunc = Game_WarnFunc; Game_ViewDistance = 512; Game_MaxViewDistance = 32768; @@ -693,8 +685,7 @@ void Game_Free(void* obj) { if (comp->Free) comp->Free(); } - Logger_Warn = Logger_DialogWarn; - Logger_Warn2 = Logger_DialogWarn2; + Logger_WarnFunc = Logger_DialogWarn; Gfx_Free(); if (!Options_ChangedCount()) return; diff --git a/src/LWeb.c b/src/LWeb.c index d8ca6271f..c40e48df1 100644 --- a/src/LWeb.c +++ b/src/LWeb.c @@ -294,7 +294,7 @@ static void SignInTask_Append(String* dst, const char* key, const String* value) void SignInTask_Run(const String* user, const String* pass) { const static String id = String_FromConst("CC post login"); const static String url = String_FromConst("https://www.classicube.net/api/login"); - String tmp; char tmpBuffer[STRING_SIZE * 6]; + String tmp; char tmpBuffer[384]; if (SignInTask.Base.Working) return; LWebTask_Reset(&SignInTask.Base); diff --git a/src/Logger.c b/src/Logger.c index e32efd5e1..b676a8225 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -37,7 +37,7 @@ struct SymbolAndName { IMAGEHLP_SYMBOL Symbol; char Name[256]; }; *-------------------------------------------------------Info dumping------------------------------------------------------* *#########################################################################################################################*/ static void Logger_DumpRegisters(void* ctx) { - String str; char strBuffer[STRING_SIZE * 8]; + String str; char strBuffer[512]; CONTEXT* r; if (!ctx) return; @@ -131,7 +131,7 @@ static void Logger_Backtrace(String* backtrace, void* ctx) { int number = i + 1; uintptr_t addr = pointers[i].Instruction; - char strBuffer[STRING_SIZE * 10]; + char strBuffer[512]; String str = String_FromArray(strBuffer); /* instruction pointer */ @@ -173,7 +173,7 @@ static void Logger_DumpBacktrace(String* str, void* ctx) { } static BOOL CALLBACK Logger_DumpModule(const char* name, ULONG_PTR base, ULONG size, void* ctx) { - String str; char strBuffer[STRING_SIZE * 4]; + String str; char strBuffer[256]; uintptr_t beg, end; beg = base; end = base + (size - 1); @@ -279,7 +279,7 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { *-------------------------------------------------------Info dumping------------------------------------------------------* *#########################################################################################################################*/ static void Logger_Backtrace(String* backtrace_, void* ctx) { - String str; char strBuffer[STRING_SIZE * 5]; + String str; char strBuffer[384]; void* addrs[40]; int i, frames, num; char** strings; @@ -367,7 +367,7 @@ void Logger_Abort2(ReturnCode result, const char* raw_msg) { *#########################################################################################################################*/ #ifdef CC_BUILD_POSIX static void Logger_DumpRegisters(void* ctx) { - String str; char strBuffer[STRING_SIZE * 8]; + String str; char strBuffer[512]; mcontext_t r; if (!ctx) return; @@ -459,26 +459,32 @@ static void Logger_DumpMisc(void* ctx) { } /*########################################################################################################################* *----------------------------------------------------------Common---------------------------------------------------------* *#########################################################################################################################*/ -void Logger_DialogWarn(ReturnCode res, const char* place) { - String msg; char msgBuffer[STRING_SIZE * 2]; - String_InitArray_NT(msg, msgBuffer); +void Logger_Warn(ReturnCode res, const char* place) { + String msg; char msgBuffer[128]; + String_InitArray(msg, msgBuffer); String_Format2(&msg, "Error %h when %c", &res, place); - msg.buffer[msg.length] = '\0'; - Window_ShowDialog("Error", msg.buffer); + Logger_WarnFunc(&msg); } -void Logger_DialogWarn2(ReturnCode res, const char* place, const String* path) { - String msg; char msgBuffer[STRING_SIZE * 4]; - String_InitArray_NT(msg, msgBuffer); +void Logger_Warn2(ReturnCode res, const char* place, const String* path) { + String msg; char msgBuffer[256]; + String_InitArray(msg, msgBuffer); String_Format3(&msg, "Error %h when %c '%s'", &res, place, path); - msg.buffer[msg.length] = '\0'; - Window_ShowDialog("Error", msg.buffer); + Logger_WarnFunc(&msg); } -Logger_WarnFunc Logger_Warn = Logger_DialogWarn; -Logger_Warn2Func Logger_Warn2 = Logger_DialogWarn2; +void Logger_DialogWarn(const String* msg) { + String dst; char dstBuffer[256]; + String_InitArray_NT(dst, dstBuffer); + + String_AppendString(&dst, msg); + dst.buffer[dst.length] = '\0'; + Window_ShowDialog(Logger_DialogTitle, dst.buffer); +} +const char* Logger_DialogTitle = "Error"; +Logger_DoWarn Logger_WarnFunc = Logger_DialogWarn; static FileHandle logFile; static struct Stream logStream; diff --git a/src/Logger.h b/src/Logger.h index 40e99177e..52f06a97b 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -5,18 +5,18 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3 */ -typedef void (*Logger_WarnFunc)(ReturnCode result, const char* place); -typedef void (*Logger_Warn2Func)(ReturnCode result, const char* place, const String* path); +typedef void (*Logger_DoWarn)(const String* msg); +/* Informs the user about a non-fatal error. */ +/* By default this shows a message box, but changes to in-game chat when game is running. */ +extern Logger_DoWarn Logger_WarnFunc; +/* The title shown for warning dialogs. */ +extern const char* Logger_DialogTitle; +void Logger_DialogWarn(const String* msg); /* Informs the user about a non-fatal error, with a message of form: "Error [result] when [place] */ -/* By default this shows a message box, but changes to in-game chat when game is running. */ -extern Logger_WarnFunc Logger_Warn; +void Logger_Warn(ReturnCode res, const char* place); /* Informs the user about a non-fatal error, with a message of form: "Error [result] when [place] 'path' */ -/* By default this shows a message box, but changes to in-game chat when game is running. */ -extern Logger_Warn2Func Logger_Warn2; - -void Logger_DialogWarn(ReturnCode res, const char* place); -void Logger_DialogWarn2(ReturnCode res, const char* place, const String* path); +void Logger_Warn2(ReturnCode res, const char* place, const String* path); /* Hooks the operating system's unhandled error callback/signal. */ /* This is used to attempt to log some information about a crash due to invalid memory read, etc. */ diff --git a/src/Platform.c b/src/Platform.c index 7825ac717..5388dba28 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -41,7 +41,7 @@ void* DisplayDevice_Meta; #define Socket__Error() WSAGetLastError() static HANDLE heap; -char* Platform_NewLine = "\r\n"; +const char* Platform_NewLine = "\r\n"; const ReturnCode ReturnCode_FileShareViolation = ERROR_SHARING_VIOLATION; const ReturnCode ReturnCode_FileNotFound = ERROR_FILE_NOT_FOUND; @@ -73,7 +73,7 @@ const ReturnCode ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK; #include #define Socket__Error() errno -char* Platform_NewLine = "\n"; +const char* Platform_NewLine = "\n"; const ReturnCode ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ const ReturnCode ReturnCode_FileNotFound = ENOENT; diff --git a/src/Platform.h b/src/Platform.h index 6d7bf8f16..3b33c6986 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -24,7 +24,7 @@ enum File_SeekFrom { FILE_SEEKFROM_BEGIN, FILE_SEEKFROM_CURRENT, FILE_SEEKFROM_E #define UNIX_EPOCH 62135596800000ULL /* Newline for console and text files. */ -extern char* Platform_NewLine; +extern const char* Platform_NewLine; extern const ReturnCode ReturnCode_FileShareViolation; extern const ReturnCode ReturnCode_FileNotFound; extern const ReturnCode ReturnCode_NotSupported; diff --git a/src/Program.c b/src/Program.c index 4d5113c40..bb9e0461e 100644 --- a/src/Program.c +++ b/src/Program.c @@ -82,13 +82,29 @@ static void Program_SetCurrentDirectory(void) { if (res) { Logger_Warn(res, "setting current directory"); return; } } -static void Program_FailInvalidArg(const char* name, const String* arg) { - char tmpBuffer[256]; - String tmp = String_NT_Array(tmpBuffer); - +CC_NOINLINE static void Exit_InvalidArg(const char* name, const String* arg) { + String tmp; char tmpBuffer[256]; + String_InitArray(tmp, tmpBuffer); String_Format2(&tmp, "%c '%s'", name, arg); - tmp.buffer[tmp.length] = '\0'; - Window_ShowDialog("Failed to start", tmpBuffer); + + Logger_DialogTitle = "Failed to start"; + Logger_DialogWarn(&tmp); + Platform_Exit(1); +} + +CC_NOINLINE static void Exit_MissingArgs(int argsCount, const String* args) { + String tmp; char tmpBuffer[256]; + int i; + String_InitArray(tmp, tmpBuffer); + + String_AppendConst(&tmp, "Missing IP and/or port - "); + for (i = 0; i < argsCount; i++) { + String_AppendString(&tmp, &args[i]); + String_Append(&tmp, ' '); + } + + Logger_DialogTitle = "Failed to start"; + Logger_DialogWarn(&tmp); Platform_Exit(1); } @@ -124,8 +140,7 @@ int main(int argc, char** argv) { String_Copy(&Game_Username, &args[0]); Program_RunGame(); } else if (argsCount < 4) { - Window_ShowDialog("Failed to start", "Missing IP and/or port"); - Platform_Exit(1); + Exit_MissingArgs(argsCount, args); return 1; } else { String_Copy(&Game_Username, &args[0]); @@ -133,11 +148,11 @@ int main(int argc, char** argv) { String_Copy(&Game_IPAddress, &args[2]); if (!Utils_ParseIP(&args[2], ip)) { - Program_FailInvalidArg("Invalid IP", &args[2]); + Exit_InvalidArg("Invalid IP", &args[2]); return 1; } if (!Convert_ParseUInt16(&args[3], &port)) { - Program_FailInvalidArg("Invalid port", &args[3]); + Exit_InvalidArg("Invalid port", &args[3]); return 1; } Game_Port = port; diff --git a/src/Resources.c b/src/Resources.c index 3be683bdd..a58f944ee 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -677,7 +677,7 @@ int Fetcher_StatusCode, Fetcher_Downloaded; ReturnCode Fetcher_Error; CC_NOINLINE static void Fetcher_DownloadAudio(const char* name, const char* hash) { - String url; char urlBuffer[STRING_SIZE * 2]; + String url; char urlBuffer[URL_MAX_SIZE]; String id = String_FromReadonly(name); String_InitArray(url, urlBuffer); diff --git a/src/String.c b/src/String.c index e39ff72ef..3e00ba1fe 100644 --- a/src/String.c +++ b/src/String.c @@ -131,9 +131,7 @@ bool String_CaselessEqualsConst(const String* a, const char* b) { void String_Append(String* str, char c) { if (str->length == str->capacity) return; - - str->buffer[str->length] = c; - str->length++; + str->buffer[str->length++] = c; } void String_AppendBool(String* str, bool value) {