From d39ad388523510fbe1d36c6e8d5010d2838214c9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 1 Oct 2018 20:49:01 +1000 Subject: [PATCH] C client: Fix crashing when selecting font list --- src/Formats.c | 3 ++- src/PackedCol.c | 16 +++------------- src/PackedCol.h | 8 ++++---- src/Platform.c | 2 +- src/String.c | 40 +++++++++++++++++++++++----------------- src/String.h | 1 + 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/Formats.c b/src/Formats.c index d4326922a..f6521ad4c 100644 --- a/src/Formats.c +++ b/src/Formats.c @@ -883,12 +883,13 @@ NBT_END, static ReturnCode Cw_WriteBockDef(struct Stream* stream, Int32 b) { UInt8 tmp[512]; bool sprite = Block_Draw[b] == DRAW_SPRITE; + union IntAndFloat speed; Mem_Copy(tmp, cw_meta_def, sizeof(cw_meta_def)); { tmp[13] = b; tmp[28] = Block_Collide[b]; - union IntAndFloat speed; speed.f = Block_SpeedMultiplier[b]; + speed.f = Block_SpeedMultiplier[b]; Stream_SetU32_BE(&tmp[37], speed.u); tmp[56] = (UInt8)Block_GetTexLoc(b, FACE_YMAX); diff --git a/src/PackedCol.c b/src/PackedCol.c index 802620a26..c0f0ea3b0 100644 --- a/src/PackedCol.c +++ b/src/PackedCol.c @@ -52,19 +52,9 @@ bool PackedCol_Unhex(UInt8 hex, Int32* value) { } void PackedCol_ToHex(String* str, PackedCol value) { - UInt8 input[3] = { value.R, value.G, value.B }; - char hex[7]; - Int32 i; - - for (i = 0; i < 3; i++) { - Int32 cur = input[i], hi = cur >> 4, lo = cur & 0x0F; - /* 48 = index of 0, 55 = index of (A - 10) */ - hex[i * 2 + 0] = hi < 10 ? (hi + 48) : (hi + 55); - hex[i * 2 + 1] = lo < 10 ? (lo + 48) : (lo + 55); - } - - hex[6] = '\0'; - String_AppendConst(str, hex); + String_AppendHex(str, value.R); + String_AppendHex(str, value.G); + String_AppendHex(str, value.B); } bool PackedCol_TryParseHex(const String* str, PackedCol* value) { diff --git a/src/PackedCol.h b/src/PackedCol.h index b38413131..d54305491 100644 --- a/src/PackedCol.h +++ b/src/PackedCol.h @@ -29,14 +29,14 @@ PackedCol PackedCol_Create3(UInt8 r, UInt8 g, UInt8 b); UInt32 PackedCol_ToARGB(PackedCol col); PackedCol PackedCol_Scale(PackedCol value, float t); PackedCol PackedCol_Lerp(PackedCol a, PackedCol b, float t); -void PackedCol_ToHex(String* str, PackedCol value); -bool PackedCol_TryParseHex(const String* str, PackedCol* value); -bool PackedCol_Unhex(UInt8 hex, Int32* value); +NOINLINE_ void PackedCol_ToHex(String* str, PackedCol value); +NOINLINE_ bool PackedCol_TryParseHex(const String* str, PackedCol* value); +NOINLINE_ bool PackedCol_Unhex(UInt8 hex, Int32* value); #define PACKEDCOL_SHADE_X 0.6f #define PACKEDCOL_SHADE_Z 0.8f #define PACKEDCOL_SHADE_YMIN 0.5f -/* Retrieves shaded colours for ambient block face lighting. */ +/* Retrieves shaded colours for ambient block face lighting */ void PackedCol_GetShaded(PackedCol normal, PackedCol* xSide, PackedCol* zSide, PackedCol* yMin); #define PACKEDCOL_WHITE PACKEDCOL_CONST(255, 255, 255, 255) diff --git a/src/Platform.c b/src/Platform.c index 9e9b6c0f4..92682b27c 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -763,7 +763,7 @@ static void Font_DirCallback(const String* srcPath, void* obj) { if (error) return; bool styled = (face->style_flags & FT_STYLE_FLAG_BOLD) || (face->style_flags & FT_STYLE_FLAG_ITALIC); - if (!styled && face->family_name) { + if (!styled && face->family_name && (face->face_flags & FT_FACE_FLAG_SCALABLE)) { StringsBuffer_Add(&fonts_list, &path); path.length = 0; diff --git a/src/String.c b/src/String.c index 8973a6373..9da154b1d 100644 --- a/src/String.c +++ b/src/String.c @@ -226,30 +226,36 @@ bool String_AppendReal32(String* str, float num, Int32 fracDigits) { return true; } -NOINLINE_ static bool String_Hex32(String* str, UInt32 value) { - char hex[9]; hex[8] = '\0'; - Int32 i; +bool String_AppendHex(String* str, UInt8 value) { + /* 48 = index of 0, 55 = index of (A - 10) */ + UInt8 hi = (value >> 4) & 0xF; + char c_hi = hi < 10 ? (hi + 48) : (hi + 55); + UInt8 lo = value & 0xF; + char c_lo = lo < 10 ? (lo + 48) : (lo + 55); - for (i = 0; i < 8; i++) { - UInt32 nibble = value & 0x0F; - /* 48 = index of 0, 55 = index of (A - 10) */ - hex[7 - i] = nibble < 10 ? (nibble + 48) : (nibble + 55); - value >>= 4; + return String_Append(str, c_hi) && String_Append(str, c_lo); +} + +NOINLINE_ static bool String_Hex32(String* str, UInt32 value) { + bool appended; + Int32 shift; + + for (shift = 24; shift >= 0; shift -= 8) { + UInt8 part = (UInt8)(value >> shift); + appended = String_AppendHex(str, part); } - return String_AppendConst(str, hex); + return appended; } NOINLINE_ static bool String_Hex64(String* str, UInt64 value) { - char hex[17]; hex[16] = '\0'; - Int32 i; + bool appended; + Int32 shift; - for (i = 0; i < 16; i++) { - UInt32 nibble = (UInt32)(value & 0x0F); - /* 48 = index of 0, 55 = index of (A - 10) */ - hex[15 - i] = nibble < 10 ? (nibble + 48) : (nibble + 55); - value >>= 4; + for (shift = 56; shift >= 0; shift -= 8) { + UInt8 part = (UInt8)(value >> shift); + appended = String_AppendHex(str, part); } - return String_AppendConst(str, hex); + return appended; } bool String_AppendConst(String* str, const char* src) { diff --git a/src/String.h b/src/String.h index 5b7f5e27a..728344bda 100644 --- a/src/String.h +++ b/src/String.h @@ -61,6 +61,7 @@ NOINLINE_ bool String_AppendReal32(String* str, float num, Int32 fracDigits); /* NOINLINE_ bool String_AppendConst(String* str, const char* src); NOINLINE_ bool String_AppendString(String* str, const String* src); NOINLINE_ bool String_AppendColorless(String* str, const String* src); +NOINLINE_ bool String_AppendHex(String* str, UInt8 value); NOINLINE_ Int32 String_IndexOf(const String* str, char c, Int32 offset); NOINLINE_ Int32 String_LastIndexOf(const String* str, char c);