diff --git a/src/AsyncDownloader.c b/src/AsyncDownloader.c index 076f00ab7..5a8566684 100644 --- a/src/AsyncDownloader.c +++ b/src/AsyncDownloader.c @@ -110,8 +110,8 @@ static void AsyncDownloader_Add(const String* url, bool priority, const String* } void AsyncDownloader_GetSkin(const String* id, const String* skinName) { - char urlBuffer[STRING_SIZE]; - String url = String_FromArray(urlBuffer); + String url; char urlBuffer[STRING_SIZE]; + String_InitArray(url, urlBuffer); if (Utils_IsUrlPrefix(skinName, 0)) { String_Copy(&url, skinName); diff --git a/src/Chat.c b/src/Chat.c index 64b8582b0..2a680119b 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -262,14 +262,14 @@ static struct ChatCommand* Commands_GetMatch(const String* cmdName) { } static void Commands_PrintDefault(void) { - char strBuffer[STRING_SIZE]; - String str = String_FromArray(strBuffer); - String name; struct ChatCommand* cmd; int i; + String str; char strBuffer[STRING_SIZE]; Chat_AddRaw("&eList of client commands:"); + String_InitArray(str, strBuffer); + for (i = 0; i < commands_count; i++) { cmd = &commands_list[i]; name = String_FromReadonly(cmd->Name); diff --git a/src/Entity.c b/src/Entity.c index ea9ac9dee..166ddd1e4 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -444,12 +444,13 @@ bool TabList_Remove(EntityID id) { } void TabList_Set(EntityID id, const String* player, const String* list, const String* group, uint8_t rank) { - char playerNameBuffer[STRING_SIZE]; - String playerName = String_FromArray(playerNameBuffer); - String_AppendColorless(&playerName, player); + String colorlessName; char colorlessBuffer[STRING_SIZE]; + + String_InitArray(colorlessName, colorlessBuffer); + String_AppendColorless(&colorlessName, player); TabList_Remove(id); - TabList_PlayerNames[id] = TabList_Buffer.Count; StringsBuffer_Add(&TabList_Buffer, &playerName); + TabList_PlayerNames[id] = TabList_Buffer.Count; StringsBuffer_Add(&TabList_Buffer, &colorlessName); TabList_ListNames[id] = TabList_Buffer.Count; StringsBuffer_Add(&TabList_Buffer, list); TabList_GroupNames[id] = TabList_Buffer.Count; StringsBuffer_Add(&TabList_Buffer, group); TabList_GroupRanks[id] = rank; diff --git a/src/Game.c b/src/Game.c index 7b3c3b7a7..f4abf740a 100644 --- a/src/Game.c +++ b/src/Game.c @@ -324,9 +324,10 @@ static void Game_OnLowVRAMDetected(void* obj) { } static void Game_ExtractInitialTexturePack(void) { - char texPackBuffer[STRING_SIZE]; - String texPack = String_FromArray(texPackBuffer); + String texPack; char texPackBuffer[STRING_SIZE]; + Options_Get(OPT_DEFAULT_TEX_PACK, &game_defTexPack, "default.zip"); + String_InitArray(texPack, texPackBuffer); String_AppendConst(&texPack, "default.zip"); TexturePack_ExtractZip_File(&texPack); @@ -641,21 +642,20 @@ static void Game_DoScheduledTasks(double time) { } void Game_TakeScreenshot(void) { - char fileBuffer[STRING_SIZE]; - String filename = String_FromArray(fileBuffer); - char pathBuffer[FILENAME_SIZE]; - String path = String_FromArray(pathBuffer); - DateTime now; struct Stream stream; ReturnCode res; + String filename; char fileBuffer[STRING_SIZE]; + String path; char pathBuffer[FILENAME_SIZE]; Game_ScreenshotRequested = false; if (!Utils_EnsureDirectory("screenshots")) return; - DateTime_CurrentLocal(&now); + + String_InitArray(filename, fileBuffer); String_Format3(&filename, "screenshot_%p2-%p2-%p4", &now.Day, &now.Month, &now.Year); String_Format3(&filename, "-%p2-%p2-%p2.png", &now.Hour, &now.Minute, &now.Second); + String_InitArray(path, pathBuffer); String_Format2(&path, "screenshots%r%s", &Directory_Separator, &filename); res = Stream_CreateFile(&stream, &path); diff --git a/src/Input.c b/src/Input.c index a96997003..42248f06d 100644 --- a/src/Input.c +++ b/src/Input.c @@ -155,11 +155,11 @@ void KeyBind_Load(void) { } void KeyBind_Save(void) { - char nameBuffer[STRING_SIZE]; - String name = String_FromArray(nameBuffer); String value; int i; + String name; char nameBuffer[STRING_SIZE]; + String_InitArray(name, nameBuffer); for (i = 0; i < KeyBind_Count; i++) { name.length = 0; String_Format1(&name, "key-%c", KeyBind_Names[i]); diff --git a/src/Menus.c b/src/Menus.c index 02e592812..15eebf144 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -382,17 +382,21 @@ static void ListScreen_Make(struct ListScreen* s, int i, int x, const String* te } static void ListScreen_UpdatePage(struct ListScreen* s) { - int start = LIST_SCREEN_ITEMS, end = s->Entries.Count - LIST_SCREEN_ITEMS; - s->Buttons[5].Disabled = s->CurrentIndex < start; - s->Buttons[6].Disabled = s->CurrentIndex >= end; - if (Game_ClassicMode) return; + int beg, end; + int num, pages; + String page; char pageBuffer[STRING_SIZE]; - char pageBuffer[STRING_SIZE]; - String page = String_FromArray(pageBuffer); - int num = (s->CurrentIndex / LIST_SCREEN_ITEMS) + 1; - int pages = Math_CeilDiv(s->Entries.Count, LIST_SCREEN_ITEMS); + beg = LIST_SCREEN_ITEMS; + end = s->Entries.Count - LIST_SCREEN_ITEMS; + s->Buttons[5].Disabled = s->CurrentIndex < beg; + s->Buttons[6].Disabled = s->CurrentIndex >= end; + + if (Game_ClassicMode) return; + num = (s->CurrentIndex / LIST_SCREEN_ITEMS) + 1; + pages = Math_CeilDiv(s->Entries.Count, LIST_SCREEN_ITEMS); if (pages == 0) pages = 1; + String_InitArray(page, pageBuffer); String_Format2(&page, "&7Page %i of %i", &num, &pages); TextWidget_Set(&s->Page, &page, &s->Font); } @@ -443,8 +447,7 @@ static void ListScreen_ContextRecreated(void* screen) { ListScreen_Make(s, 5, -220, &lArrow, ListScreen_MoveBackwards); ListScreen_Make(s, 6, 220, &rArrow, ListScreen_MoveForwards); - Menu_Back(s, 7, &s->Buttons[7], "Done", &s->Font, Menu_SwitchPause); - + Menu_Back(s, 7, &s->Buttons[7], "Done", &s->Font, Menu_SwitchPause); Menu_Label(s, 8, &s->Title, &s->TitleText, &s->Font, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -155); Menu_Label(s, 9, &s->Page, &String_Empty, &s->Font, @@ -646,7 +649,7 @@ static void PauseScreen_ContextRecreated(void* screen) { Menu_Button(s, 6, &s->Buttons[6], 120, &quitMsg, &s->TitleFont, PauseScreen_Quit, ANCHOR_MAX, ANCHOR_MAX, 5, 5); - Menu_Back(s, 7, &s->Buttons[7], "Back to game", &s->TitleFont, PauseScreen_Game); + Menu_Back(s, 7, &s->Buttons[7], "Back to game",&s->TitleFont, PauseScreen_Game); } if (!ServerConnection_IsSinglePlayer) { @@ -1106,10 +1109,10 @@ static void GenLevelScreen_ContextRecreated(void* screen) { static String title = String_FromConst("Generate new level"); static String flat = String_FromConst("Flatgrass"); static String norm = String_FromConst("Vanilla"); - struct GenLevelScreen* s = screen; - char tmpBuffer[STRING_SIZE]; - String tmp = String_FromArray(tmpBuffer); + + String tmp; char tmpBuffer[STRING_SIZE]; + String_InitArray(tmp, tmpBuffer); String_AppendInt(&tmp, World_Width); GenLevelScreen_Input(s, 0, -80, false, &tmp); @@ -1124,14 +1127,13 @@ static void GenLevelScreen_ContextRecreated(void* screen) { GenLevelScreen_Label(s, 2, -150, 0, "Length:"); GenLevelScreen_Label(s, 3, -140, 40, "Seed:"); - Menu_Label(s, 8, &s->Labels[4], &title, &s->TextFont, + Menu_Label(s, 8, &s->Labels[4], &title, &s->TextFont, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -130); Menu_Button(s, 9, &s->Buttons[0], 200, &flat, &s->TitleFont, GenLevelScreen_Flatgrass, ANCHOR_CENTRE, ANCHOR_CENTRE, -120, 100); Menu_Button(s, 10, &s->Buttons[1], 200, &norm, &s->TitleFont, GenLevelScreen_Notchy, ANCHOR_CENTRE, ANCHOR_CENTRE, 120, 100); - - Menu_Back(s, 11, &s->Buttons[2], "Cancel", &s->TitleFont, Menu_SwitchPause); + Menu_Back(s, 11, &s->Buttons[2], "Cancel", &s->TitleFont, Menu_SwitchPause); } struct ScreenVTABLE GenLevelScreen_VTABLE = { @@ -1361,7 +1363,7 @@ static void SaveLevelScreen_ContextRecreated(void* screen) { ANCHOR_CENTRE, ANCHOR_CENTRE, 110, 120); Menu_Back(s, 3, &s->Buttons[2], "Cancel", &s->TitleFont, Menu_SwitchPause); - Menu_Input(s, 4, &s->Input, 500, &String_Empty, &s->TextFont, &validator, + Menu_Input(s, 4, &s->Input, 500, &String_Empty, &s->TextFont, &validator, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -30); s->Widgets[5] = NULL; /* description widget placeholder */ } @@ -1416,10 +1418,10 @@ static void TexturePackScreen_FilterFiles(const String* path, void* obj) { } struct Screen* TexturePackScreen_MakeInstance(void) { - static String title = String_FromConst("Select a texture pack zip"); - static String path = String_FromConst("texpacks"); - + static String title = String_FromConst("Select a texture pack zip"); + static String path = String_FromConst("texpacks"); struct ListScreen* s = ListScreen_MakeInstance(); + s->TitleText = title; s->EntryClick = TexturePackScreen_EntryClick; @@ -1533,19 +1535,20 @@ static void HotkeyListScreen_MakeFlags(int flags, String* str) { } struct Screen* HotkeyListScreen_MakeInstance(void) { - static String title = String_FromConst("Modify hotkeys"); - static String empty = String_FromConst(LIST_SCREEN_EMPTY); - - char textBuffer[STRING_SIZE]; - String text = String_FromArray(textBuffer); - int i; - + static String title = String_FromConst("Modify hotkeys"); + static String empty = String_FromConst(LIST_SCREEN_EMPTY); struct ListScreen* s = ListScreen_MakeInstance(); + + struct HotkeyData hKey; + int i; + String text; char textBuffer[STRING_SIZE]; + + String_InitArray(text, textBuffer); s->TitleText = title; s->EntryClick = HotkeyListScreen_EntryClick; for (i = 0; i < HotkeysText.Count; i++) { - struct HotkeyData hKey = HotkeysList[i]; + hKey = HotkeysList[i]; text.length = 0; String_AppendConst(&text, Key_Names[hKey.Trigger]); @@ -1928,9 +1931,10 @@ static void MenuOptionsScreen_SetFPS(const String* v) { } static void MenuOptionsScreen_Set(struct MenuOptionsScreen* s, int i, const String* text) { - char titleBuffer[STRING_SIZE]; - String title = String_FromArray(titleBuffer); + String title; char titleBuffer[STRING_SIZE]; + s->Buttons[i].SetValue(text); + String_InitArray(title, titleBuffer); /* need to get btn again here (e.g. changing FPS invalidates all widgets) */ String_AppendConst(&title, s->Buttons[i].OptName); @@ -2147,19 +2151,20 @@ static void MenuOptionsScreen_Enum(void* screen, void* widget) { static void MenuOptionsScreen_Input(void* screen, void* widget) { static String okay = String_FromConst("OK"); static String def = String_FromConst("Default value"); - struct MenuOptionsScreen* s = screen; + struct ButtonWidget* btn = widget; int i; - char valueBuffer[STRING_SIZE]; - String value = String_FromArray(valueBuffer); + String value; char valueBuffer[STRING_SIZE]; s->ActiveI = Menu_Index(s, btn); MenuOptionsScreen_FreeExtHelp(s); MenuOptionsScreen_FreeInput(s); - btn->GetValue(&value); + String_InitArray(value, valueBuffer); + btn->GetValue(&value); i = s->WidgetsCount; + Menu_Input(s, i - 1, &s->Input, 400, &value, &s->TextFont, &s->Validators[s->ActiveI], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 110); Menu_Button(s, i - 2, &s->OK, 40, &okay, &s->TitleFont, MenuOptionsScreen_OK, @@ -2282,7 +2287,7 @@ static void ClassicOptionsScreen_ContextRecreated(void* screen) { Menu_Button(s, 9, &s->Buttons[9], 400, &title, &s->TitleFont, Menu_SwitchKeysClassic, ANCHOR_CENTRE, ANCHOR_MAX, 0, 95); - Menu_Back(s, 10, &s->Buttons[10], "Done", &s->TitleFont, Menu_SwitchPause); + Menu_Back(s, 10, &s->Buttons[10], "Done", &s->TitleFont, Menu_SwitchPause); /* Disable certain options */ if (!ServerConnection_IsSinglePlayer) Menu_Remove(s, 3); @@ -2435,9 +2440,10 @@ static void GraphicsOptionsScreen_SetShadows(const String* v) { static void GraphicsOptionsScreen_GetMipmaps(String* v) { Menu_GetBool(v, Gfx_Mipmaps); } static void GraphicsOptionsScreen_SetMipmaps(const String* v) { + String url; char urlBuffer[STRING_SIZE]; + Gfx_Mipmaps = Menu_SetBool(v, OPT_MIPMAPS); - char urlBuffer[STRING_SIZE]; - String url = String_FromArray(urlBuffer); + String_InitArray(url, urlBuffer); String_Copy(&url, &World_TextureUrl); /* always force a reload from cache */ @@ -2626,12 +2632,12 @@ static void HacksSettingsScreen_SetClipping(const String* v) { static void HacksSettingsScreen_GetJump(String* v) { String_AppendFloat(v, LocalPlayer_JumpHeight(), 3); } static void HacksSettingsScreen_SetJump(const String* v) { struct PhysicsComp* physics = &LocalPlayer_Instance.Physics; - char strBuffer[STRING_SIZE]; - String str = String_FromArray(strBuffer); + String str; char strBuffer[STRING_SIZE]; PhysicsComp_CalculateJumpVelocity(physics, Menu_Float(v)); physics->UserJumpVel = physics->JumpVel; + String_InitArray(str, strBuffer); String_AppendFloat(&str, physics->JumpVel, 8); Options_Set(OPT_JUMP_VELOCITY, &str); } diff --git a/src/Options.c b/src/Options.c index 2445a602f..920df9427 100644 --- a/src/Options.c +++ b/src/Options.c @@ -198,16 +198,16 @@ void Options_Load(void) { void Options_Save(void) { static String path = String_FromConst("options.txt"); - char lineBuffer[768]; - String line = String_FromArray(lineBuffer); String key, value; struct Stream stream; int i; ReturnCode res; + String line; char lineBuffer[768]; res = Stream_CreateFile(&stream, &path); if (res) { Chat_LogError2(res, "creating", &path); return; } + String_InitArray(line, lineBuffer); for (i = 0; i < Options_Keys.Count; i++) { key = StringsBuffer_UNSAFE_Get(&Options_Keys, i); diff --git a/src/PacketHandlers.c b/src/PacketHandlers.c index ccffb456a..dc572d83a 100644 --- a/src/PacketHandlers.c +++ b/src/PacketHandlers.c @@ -132,19 +132,20 @@ static void Handlers_RemoveTablistEntry(EntityID id) { TabList_Remove(id); } -static void Handlers_CheckName(EntityID id, String* displayName, String* skinName) { - String_StripCols(skinName); - Handlers_RemoveEndPlus(displayName); - Handlers_RemoveEndPlus(skinName); +static void Handlers_CheckName(EntityID id, String* name, String* skin) { + String colorlessName; char colorlessBuffer[STRING_SIZE]; + + String_StripCols(skin); + Handlers_RemoveEndPlus(name); + Handlers_RemoveEndPlus(skin); /* Server is only allowed to change our own name colours. */ if (id != ENTITIES_SELF_ID) return; - char nameNoColsBuffer[STRING_SIZE]; - String nameNoCols = String_FromArray(nameNoColsBuffer); - String_AppendColorless(&nameNoCols, displayName); + String_InitArray(colorlessName, colorlessBuffer); + String_AppendColorless(&colorlessName, name); - if (!String_Equals(&nameNoCols, &Game_Username)) { String_Copy(displayName, &Game_Username); } - if (!skinName->length) { String_Copy(skinName, &Game_Username); } + if (!String_Equals(&colorlessName, &Game_Username)) String_Copy(name, &Game_Username); + if (!skin->length) String_Copy(skin, &Game_Username); } static void Classic_ReadAbsoluteLocation(uint8_t* data, EntityID id, bool interpolate); diff --git a/src/Platform.c b/src/Platform.c index 9b3a0cda3..3c488ca35 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -383,16 +383,19 @@ bool File_Exists(const String* path) { } ReturnCode Directory_Enum(const String* dirPath, void* obj, Directory_EnumCallback callback) { - char pathBuffer[MAX_PATH + 10]; - String path = String_FromArray(pathBuffer); - /* Need to append \* to search for files in directory */ - String_Format1(&path, "%s\\*", dirPath); - WCHAR str[300]; Platform_ConvertString(str, &path); - + WCHAR str[300]; WIN32_FIND_DATAW entry; - HANDLE find = FindFirstFileW(str, &entry); - if (find == INVALID_HANDLE_VALUE) return GetLastError(); + HANDLE find; ReturnCode res; + String path; char pathBuffer[MAX_PATH + 10]; + + /* Need to append \* to search for files in directory */ + String_InitArray(path, pathBuffer); + String_Format1(&path, "%s\\*", dirPath); + Platform_ConvertString(str, &path); + + find = FindFirstFileW(str, &entry); + if (find == INVALID_HANDLE_VALUE) return GetLastError(); do { path.length = 0; @@ -513,7 +516,7 @@ bool File_Exists(const String* path) { ReturnCode Directory_Enum(const String* dirPath, void* obj, Directory_EnumCallback callback) { char str[600]; DIR* dirPtr; - struct dirent* entry; + struct dirent* entry; char* src; int len, res; char pathBuffer[FILENAME_SIZE]; diff --git a/src/Screens.c b/src/Screens.c index 330477a28..9d32f49e6 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -806,11 +806,15 @@ static void ChatScreen_CheckOtherStatuses(struct ChatScreen* s) { static String texPack = String_FromConst("texturePack"); struct AsyncRequest request; int progress; - bool hasRequest = AsyncDownloader_GetCurrent(&request, &progress); - String id = String_FromRawArray(request.ID); + bool hasRequest; + String identifier; + String str; char strBuffer[STRING_SIZE]; + + hasRequest = AsyncDownloader_GetCurrent(&request, &progress); + identifier = String_FromRawArray(request.ID); /* Is terrain / texture pack currently being downloaded? */ - if (!hasRequest || !String_Equals(&id, &texPack)) { + if (!hasRequest || !String_Equals(&identifier, &texPack)) { if (s->Status.Textures[1].ID) { TextGroupWidget_SetText(&s->Status, 1, &String_Empty); } @@ -820,8 +824,7 @@ static void ChatScreen_CheckOtherStatuses(struct ChatScreen* s) { if (progress == s->LastDownloadStatus) return; s->LastDownloadStatus = progress; - char strBuffer[STRING_SIZE]; - String str = String_FromArray(strBuffer); + String_InitArray(str, strBuffer); if (progress == ASYNC_PROGRESS_MAKING_REQUEST) { String_AppendConst(&str, "&eRetrieving texture pack.."); diff --git a/src/ServerConnection.c b/src/ServerConnection.c index 7515626bb..e7411947c 100644 --- a/src/ServerConnection.c +++ b/src/ServerConnection.c @@ -277,10 +277,11 @@ static void MPConnection_FinishConnect(void) { static void MPConnection_FailConnect(ReturnCode result) { static String reason = String_FromConst("You failed to connect to the server. It's probably down!"); - char msgBuffer[STRING_SIZE * 2]; - String msg = String_FromArray(msgBuffer); + String msg; char msgBuffer[STRING_SIZE * 2]; net_connecting = false; + String_InitArray(msg, msgBuffer); + if (result) { String_Format3(&msg, "Error connecting to %s:%i: %i", &Game_IPAddress, &Game_Port, &result); ErrorHandler_Log(&msg); diff --git a/src/TexturePack.c b/src/TexturePack.c index 073d48bfd..526ef8651 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -199,14 +199,13 @@ struct EntryList { }; static void EntryList_Load(struct EntryList* list) { - char pathBuffer[FILENAME_SIZE]; - String path = String_FromArray(pathBuffer); - char lineBuffer[FILENAME_SIZE]; - String line = String_FromArray(lineBuffer); - uint8_t buffer[2048]; struct Stream stream, buffered; ReturnCode res; + String line; char lineBuffer[FILENAME_SIZE]; + String path; char pathBuffer[FILENAME_SIZE]; + + String_InitArray(path, pathBuffer); String_Format3(&path, "%c%r%c", list->Folder, &Directory_Separator, list->Filename); res = Stream_OpenFile(&stream, &path); @@ -215,6 +214,7 @@ static void EntryList_Load(struct EntryList* list) { /* ReadLine reads single byte at a time */ Stream_ReadonlyBuffered(&buffered, &stream, buffer, sizeof(buffer)); + String_InitArray(line, lineBuffer); for (;;) { res = Stream_ReadLine(&buffered, &line); @@ -377,7 +377,7 @@ void TextureCache_Set(const String* url, uint8_t* data, uint32_t length) { static void TextureCache_SetEntry(const String* url, const String* data, struct EntryList* list) { TexCache_Crc32(url); - char entryBuffer[2048]; + char entryBuffer[1024]; String entry = String_FromArray(entryBuffer); String_Format2(&entry, "%s %s", &crc32, data); @@ -458,8 +458,9 @@ ReturnCode TexturePack_ExtractTerrainPng(struct Stream* stream) { } void TexturePack_ExtractDefault(void) { - char texPackBuffer[STRING_SIZE]; - String texPack = String_FromArray(texPackBuffer); + String texPack; char texPackBuffer[STRING_SIZE]; + + String_InitArray(texPack, texPackBuffer); Game_GetDefaultTexturePack(&texPack); TexturePack_ExtractZip_File(&texPack); diff --git a/src/Widgets.c b/src/Widgets.c index ba99380d3..0f8e61455 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -880,9 +880,7 @@ static void InputWidget_CalculateLineSizes(struct InputWidget* w) { struct DrawTextArgs args; Size2D size; int y; - - char lineBuffer[STRING_SIZE]; - String line = String_FromArray(lineBuffer); + String line; char lineBuffer[STRING_SIZE]; for (y = 0; y < INPUTWIDGET_MAX_LINES; y++) { w->LineSizes[y] = Size2D_Empty; @@ -890,11 +888,12 @@ static void InputWidget_CalculateLineSizes(struct InputWidget* w) { w->LineSizes[0].Width = w->PrefixWidth; DrawTextArgs_MakeEmpty(&args, &w->Font, true); + String_InitArray(line, lineBuffer); for (y = 0; y < w->GetMaxLines(); y++) { line.length = 0; InputWidget_FormatLine(w, y, &line); - args.Text = line; + args.Text = line; size = Drawer2D_MeasureText(&args); w->LineSizes[y].Width += size.Width; w->LineSizes[y].Height = size.Height;