diff --git a/src/Animations.c b/src/Animations.c index 55b937f10..4f38f561f 100644 --- a/src/Animations.c +++ b/src/Animations.c @@ -156,18 +156,19 @@ bool anims_validated, anims_useLavaAnim, anims_useWaterAnim; #define ANIM_MIN_ARGS 7 static void Animations_ReadDescription(struct Stream* stream, const String* path) { - char lineBuffer[STRING_SIZE * 2]; - String line = String_FromArray(lineBuffer); + String line; char lineBuffer[STRING_SIZE * 2]; String parts[ANIM_MIN_ARGS]; - int count; struct AnimationData data = { 0 }; uint8_t tileX, tileY; - /* ReadLine reads single byte at a time */ - uint8_t buffer[2048]; struct Stream buffered; + uint8_t buffer[2048]; + struct Stream buffered; ReturnCode res; - Stream_ReadonlyBuffered(&buffered, stream, buffer, sizeof(buffer)); + + String_InitArray(line, lineBuffer); + /* ReadLine reads single byte at a time */ + Stream_ReadonlyBuffered(&buffered, stream, buffer, sizeof(buffer)); for (;;) { res = Stream_ReadLine(&buffered, &line); @@ -244,7 +245,7 @@ static void Animations_Draw(struct AnimationData* data, TextureLoc texLoc, int s } static void Animations_Apply(struct AnimationData* data) { - TextureLoc texLoc; + TextureLoc loc; data->Tick--; if (data->Tick >= 0) return; @@ -252,17 +253,17 @@ static void Animations_Apply(struct AnimationData* data) { data->State %= data->StatesCount; data->Tick = data->TickDelay; - texLoc = data->TexLoc; - if (texLoc == 30 && anims_useLavaAnim) return; - if (texLoc == 14 && anims_useWaterAnim) return; - Animations_Draw(data, texLoc, data->FrameSize); + loc = data->TexLoc; + if (loc == 30 && anims_useLavaAnim) return; + if (loc == 14 && anims_useWaterAnim) return; + Animations_Draw(data, loc, data->FrameSize); } static bool Animations_IsDefaultZip(void) { - char texPackBuffer[STRING_SIZE]; - String texPack = String_FromArray(texPackBuffer); - + String texPack; char texPackBuffer[STRING_SIZE]; if (World_TextureUrl.length) return false; + + String_InitArray(texPack, texPackBuffer); Options_Get(OPT_DEFAULT_TEX_PACK, &texPack, "default.zip"); return String_CaselessEqualsConst(&texPack, "default.zip"); } diff --git a/src/AsyncDownloader.c b/src/AsyncDownloader.c index cf1783a56..076f00ab7 100644 --- a/src/AsyncDownloader.c +++ b/src/AsyncDownloader.c @@ -81,21 +81,23 @@ bool KeepAlive; static void AsyncDownloader_Add(const String* url, bool priority, const String* id, uint8_t type, TimeMS* lastModified, const String* etag, const String* data) { struct AsyncRequest req = { 0 }; - String reqUrl = String_FromArray(req.URL); - String reqID = String_FromArray(req.ID); - String reqEtag = String_FromArray(req.Etag); + String reqUrl, reqID, reqEtag; + + String_InitArray(reqUrl, req.URL); + String_Copy(&reqUrl, url); + String_InitArray(reqID, req.ID); + String_Copy(&reqID, id); + + req.RequestType = type; + Platform_Log2("Adding %s (type %b)", &reqUrl, &type); + + String_InitArray(reqEtag, req.Etag); + if (lastModified) { req.LastModified = *lastModified; } + if (etag) { String_Copy(&reqEtag, etag); } + /* request.Data = data; TODO: Implement this. do we need to copy or expect caller to malloc it? */ Mutex_Lock(async_pendingMutex); { - String_Copy(&reqUrl, url); - String_Copy(&reqID, id); - req.RequestType = type; - Platform_Log2("Adding %s (type %b)", &reqUrl, &type); - - if (lastModified) { req.LastModified = *lastModified; } - if (etag) { String_Copy(&reqEtag, etag); } - /* request.Data = data; TODO: Implement this. do we need to copy or expect caller to malloc it? */ - req.TimeAdded = DateTime_CurrentUTC_MS(); if (priority) { AsyncRequestList_Prepend(&async_pending, &req); diff --git a/src/Audio.c b/src/Audio.c index d1413e127..2c97328fd 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -100,12 +100,13 @@ static ReturnCode Sound_ReadWaveData(struct Stream* stream, struct Sound* snd) { } static ReturnCode Sound_ReadWave(const String* filename, struct Sound* snd) { - char pathBuffer[FILENAME_SIZE]; - String path = String_FromArray(pathBuffer); struct Stream stream; ReturnCode res; + String path; char pathBuffer[FILENAME_SIZE]; + String_InitArray(path, pathBuffer); String_Format2(&path, "audio%r%s", &Directory_Separator, filename); + res = Stream_OpenFile(&stream, &path); if (res) return res; diff --git a/src/Block.c b/src/Block.c index 00cc41176..311b56d83 100644 --- a/src/Block.c +++ b/src/Block.c @@ -473,14 +473,14 @@ void Block_UpdateCulling(BlockID block) { *-------------------------------------------------------AutoRotate--------------------------------------------------------* *#########################################################################################################################*/ static BlockID AutoRotate_Find(BlockID block, const String* name, const char* suffix) { - char buffer[STRING_SIZE * 2]; - String temp = String_FromArray(buffer); int rotated; + String str; char strBuffer[STRING_SIZE * 2]; - String_AppendString(&temp, name); - String_AppendConst(&temp, suffix); + String_InitArray(str, strBuffer); + String_AppendString(&str, name); + String_AppendConst(&str, suffix); - rotated = Block_FindID(&temp); + rotated = Block_FindID(&str); return rotated == -1 ? block : (BlockID)rotated; } diff --git a/src/Chat.c b/src/Chat.c index cc8964fff..64b8582b0 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -118,14 +118,13 @@ static void Chat_OpenLog(DateTime* now) { } static void Chat_AppendLog(const String* text) { - char strBuffer[STRING_SIZE * 2]; - String str = String_FromArray(strBuffer); - DateTime now; ReturnCode res; - if (!Chat_LogName.length || !Game_ChatLogging) return; + String str; char strBuffer[STRING_SIZE * 2]; + if (!Chat_LogName.length || !Game_ChatLogging) return; DateTime_CurrentLocal(&now); + if (now.Day != ChatLog_LastLogDate.Day || now.Month != ChatLog_LastLogDate.Month || now.Year != ChatLog_LastLogDate.Year) { Chat_CloseLog(); Chat_OpenLog(&now); @@ -135,6 +134,7 @@ static void Chat_AppendLog(const String* text) { if (!Chat_LogStream.Meta.File) return; /* [HH:mm:ss] text */ + String_InitArray(str, strBuffer); String_Format3(&str, "[%p2:%p2:%p2] ", &now.Hour, &now.Minute, &now.Second); String_AppendColorless(&str, text); @@ -166,8 +166,9 @@ void Chat_Add3(const char* format, const void* a1, const void* a2, const void* a Chat_Add4(format, a1, a2, a3, NULL); } void Chat_Add4(const char* format, const void* a1, const void* a2, const void* a3, const void* a4) { - char msgBuffer[STRING_SIZE * 2]; - String msg = String_FromArray(msgBuffer); + String msg; char msgBuffer[STRING_SIZE * 2]; + String_InitArray(msg, msgBuffer); + String_Format4(&msg, format, a1, a2, a3, a4); Chat_AddOf(&msg, MSG_TYPE_NORMAL); } @@ -473,8 +474,8 @@ static void CuboidCommand_DoCuboid(void) { } static void CuboidCommand_BlockChanged(void* obj, Vector3I coords, BlockID old, BlockID now) { - char msgBuffer[STRING_SIZE]; - String msg = String_FromArray(msgBuffer); + String msg; char msgBuffer[STRING_SIZE]; + String_InitArray(msg, msgBuffer); if (cuboid_mark1.X == Int32_MaxValue) { cuboid_mark1 = coords; diff --git a/src/Entity.c b/src/Entity.c index ab4b5beca..ea9ac9dee 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -864,7 +864,7 @@ void LocalPlayer_Tick(struct Entity* e, double delta) { AnimatedComp_Update(e, p->Interp.Prev.Pos, p->Interp.Next.Pos, delta); TiltComp_Update(&p->Tilt, delta); - Player_CheckSkin(&p->Base); + Player_CheckSkin((struct Player*)p); SoundComp_Tick(wasOnGround); } @@ -931,7 +931,7 @@ struct EntityVTABLE localPlayer_VTABLE = { void LocalPlayer_Init(void) { struct LocalPlayer* p = &LocalPlayer_Instance; Player_Init(&p->Base); - Player_SetName(&p->Base, &Game_Username, &Game_Username); + Player_SetName((struct Player*)p, &Game_Username, &Game_Username); p->Collisions.Entity = &p->Base; HacksComp_Init(&p->Hacks); @@ -1070,7 +1070,7 @@ static void NetPlayer_SetLocation(struct Entity* e, struct LocationUpdate* updat static void NetPlayer_Tick(struct Entity* e, double delta) { struct NetPlayer* p = (struct NetPlayer*)e; - Player_CheckSkin(&p->Base); + Player_CheckSkin((struct Player*)p); NetInterpComp_AdvanceState(&p->Interp); AnimatedComp_Update(e, p->Interp.Prev.Pos, p->Interp.Next.Pos, delta); } @@ -1093,7 +1093,7 @@ static void NetPlayer_RenderName(struct Entity* e) { distance = Model_RenderDistance(e); threshold = Entities_NameMode == NAME_MODE_ALL_UNSCALED ? 8192 * 8192 : 32 * 32; - if (distance <= (float)threshold) Player_DrawName(&p->Base); + if (distance <= (float)threshold) Player_DrawName((struct Player*)p); } struct EntityVTABLE netPlayer_VTABLE = { @@ -1103,6 +1103,6 @@ struct EntityVTABLE netPlayer_VTABLE = { void NetPlayer_Init(struct NetPlayer* p, const String* displayName, const String* skinName) { Mem_Set(p, 0, sizeof(struct NetPlayer)); Player_Init(&p->Base); - Player_SetName(&p->Base, displayName, skinName); + Player_SetName((struct Player*)p, displayName, skinName); p->Base.VTABLE = &netPlayer_VTABLE; } diff --git a/src/ErrorHandler.c b/src/ErrorHandler.c index 26c18bac0..cacb2dce4 100644 --- a/src/ErrorHandler.c +++ b/src/ErrorHandler.c @@ -145,11 +145,12 @@ static void ErrorHandler_DumpCommon(String* str, void* ctx) { } static void ErrorHandler_DumpRegisters(void* ctx) { + CONTEXT* r; + String str; char strBuffer[STRING_SIZE * 8]; if (!ctx) return; - CONTEXT* r = (CONTEXT*)ctx; - char strBuffer[STRING_SIZE * 8]; - String str = String_FromArray(strBuffer); + r = (CONTEXT*)ctx; + String_InitArray(str, strBuffer); String_AppendConst(&str, "-- registers --\r\n"); #ifdef _M_IX86 @@ -577,11 +578,12 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) { *-------------------------------------------------------Info dumping------------------------------------------------------* *#########################################################################################################################*/ static void ErrorHandler_DumpRegisters(void* ctx) { + mcontext_t r; + String str; char strBuffer[STRING_SIZE * 8]; if (!ctx) return; - mcontext_t r = ((ucontext_t*)ctx)->uc_mcontext; - char strBuffer[STRING_SIZE * 8]; - String str = String_FromArray(strBuffer); + r = ((ucontext_t*)ctx)->uc_mcontext; + String_InitArray(str, strBuffer); String_AppendConst(&str, "-- registers --\n"); /* TODO: There must be a better way of getting these.. */ @@ -637,11 +639,12 @@ void ErrorHandler_ShowDialog(const char* title, const char* msg) { #endif #ifdef CC_BUILD_OSX static void ErrorHandler_DumpRegisters(void* ctx) { + mcontext_t r; + String str; char strBuffer[STRING_SIZE * 8]; if (!ctx) return; - mcontext_t r = ((ucontext_t*)ctx)->uc_mcontext; - char strBuffer[STRING_SIZE * 8]; - String str = String_FromArray(strBuffer); + r = ((ucontext_t*)ctx)->uc_mcontext; + String_InitArray(str, strBuffer); String_AppendConst(&str, "-- registers --\n"); /* You can find these definitions at /usr/include/mach/i386/_structs.h */ diff --git a/src/Game.c b/src/Game.c index b1978a8ad..7b3c3b7a7 100644 --- a/src/Game.c +++ b/src/Game.c @@ -105,8 +105,9 @@ char game_defTexPackBuffer[STRING_SIZE]; String game_defTexPack = String_FromArray(game_defTexPackBuffer); void Game_GetDefaultTexturePack(String* texPack) { - char texPathBuffer[STRING_SIZE]; - String texPath = String_FromArray(texPathBuffer); + String texPath; char texPathBuffer[STRING_SIZE]; + + String_InitArray(texPath, texPathBuffer); String_Format2(&texPath, "texpacks%r%s", &Directory_Separator, &game_defTexPack); if (File_Exists(&texPath) && !Game_ClassicMode) { @@ -412,13 +413,10 @@ static void Game_InitScheduledTasks(void) { void Game_Free(void* obj); void Game_Load(void) { - char renderTypeBuffer[STRING_SIZE]; - String renderType = String_FromArray(renderTypeBuffer); - char titleBuffer[STRING_SIZE]; - String title = String_FromArray(titleBuffer); - struct IGameComponent comp; int i, flags; + String renderType; char renderTypeBuffer[STRING_SIZE]; + String title; char titleBuffer[STRING_SIZE]; Game_ViewDistance = 512; Game_MaxViewDistance = 32768; @@ -478,6 +476,7 @@ void Game_Load(void) { ChunkUpdater_Init(); EnvRenderer_MakeComponent(&comp); Game_AddComponent(&comp); + String_InitArray(renderType, renderTypeBuffer); Options_Get(OPT_RENDER_TYPE, &renderType, "normal"); flags = Game_CalcRenderType(&renderType); @@ -533,6 +532,7 @@ void Game_Load(void) { }*/ if (Gfx_WarnIfNecessary()) EnvRenderer_UseLegacyMode(true); + String_InitArray(title, titleBuffer); String_Format2(&title, "Connecting to %s:%i..", &Game_IPAddress, &Game_Port); Gui_FreeActive(); diff --git a/src/Input.c b/src/Input.c index b9c34be08..a96997003 100644 --- a/src/Input.c +++ b/src/Input.c @@ -331,18 +331,18 @@ void Hotkeys_Init(void) { } void Hotkeys_UserRemovedHotkey(Key trigger, HotkeyFlags flags) { - char keyBuffer[STRING_SIZE]; - String key = String_FromArray(keyBuffer); + String key; char keyBuffer[STRING_SIZE]; + String_InitArray(key, keyBuffer); String_Format2(&key, "hotkey-%c&%i", Key_Names[trigger], &flags); Options_SetString(&key, NULL); } void Hotkeys_UserAddedHotkey(Key trigger, HotkeyFlags flags, bool moreInput, const String* text) { - char keyBuffer[STRING_SIZE]; - String key = String_FromArray(keyBuffer); - char valueBuffer[STRING_SIZE * 2]; - String value = String_FromArray(valueBuffer); + String key; char keyBuffer[STRING_SIZE]; + String value; char valueBuffer[STRING_SIZE * 2]; + String_InitArray(key, keyBuffer); + String_InitArray(value, valueBuffer); String_Format2(&key, "hotkey-%c&%i", Key_Names[trigger], &flags); String_Format2(&value, "%t&%s", &moreInput, text); diff --git a/src/Menus.c b/src/Menus.c index 269a90690..02e592812 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -795,8 +795,8 @@ static void EditHotkeyScreen_MakeFlags(int flags, String* str) { } static void EditHotkeyScreen_MakeBaseKey(struct EditHotkeyScreen* s, Widget_LeftClick onClick) { - char textBuffer[STRING_SIZE]; - String text = String_FromArray(textBuffer); + String text; char textBuffer[STRING_SIZE]; + String_InitArray(text, textBuffer); String_AppendConst(&text, "Key: "); String_AppendConst(&text, Key_Names[s->CurHotkey.Trigger]); @@ -804,8 +804,8 @@ static void EditHotkeyScreen_MakeBaseKey(struct EditHotkeyScreen* s, Widget_Left } static void EditHotkeyScreen_MakeModifiers(struct EditHotkeyScreen* s, Widget_LeftClick onClick) { - char textBuffer[STRING_SIZE]; - String text = String_FromArray(textBuffer); + String text; char textBuffer[STRING_SIZE]; + String_InitArray(text, textBuffer); String_AppendConst(&text, "Modifiers:"); EditHotkeyScreen_MakeFlags(s->CurHotkey.Flags, &text); @@ -813,8 +813,8 @@ static void EditHotkeyScreen_MakeModifiers(struct EditHotkeyScreen* s, Widget_Le } static void EditHotkeyScreen_MakeLeaveOpen(struct EditHotkeyScreen* s, Widget_LeftClick onClick) { - char textBuffer[STRING_SIZE]; - String text = String_FromArray(textBuffer); + String text; char textBuffer[STRING_SIZE]; + String_InitArray(text, textBuffer); String_AppendConst(&text, "Input stays open: "); String_AppendConst(&text, s->CurHotkey.StaysOpen ? "ON" : "OFF"); @@ -2075,13 +2075,15 @@ static bool MenuOptionsScreen_MouseMove(void* screen, int x, int y) { } static void MenuOptionsScreen_Make(struct MenuOptionsScreen* s, int i, int dir, int y, const char* optName, Widget_LeftClick onClick, Button_Get getter, Button_Set setter) { - char titleBuffer[STRING_SIZE]; - String title = String_FromArray(titleBuffer); + struct ButtonWidget* btn; + String title; char titleBuffer[STRING_SIZE]; + + String_InitArray(title, titleBuffer); String_AppendConst(&title, optName); String_AppendConst(&title, ": "); getter(&title); - struct ButtonWidget* btn = &s->Buttons[i]; + btn = &s->Buttons[i]; Menu_Button(s, i, btn, 300, &title, &s->TitleFont, onClick, ANCHOR_CENTRE, ANCHOR_CENTRE, 160 * dir, y); diff --git a/src/Options.c b/src/Options.c index 7ada22526..2445a602f 100644 --- a/src/Options.c +++ b/src/Options.c @@ -130,10 +130,10 @@ void Options_SetBool(const char* keyRaw, bool value) { } void Options_SetInt(const char* keyRaw, int value) { - char numBuffer[STRING_INT_CHARS]; - String numStr = String_FromArray(numBuffer); - String_AppendInt(&numStr, value); - Options_Set(keyRaw, &numStr); + String str; char strBuffer[STRING_INT_CHARS]; + String_InitArray(str, strBuffer); + String_AppendInt(&str, value); + Options_Set(keyRaw, &str); } void Options_Set(const char* keyRaw, const String* value) { @@ -156,14 +156,13 @@ void Options_SetString(const String* key, const String* value) { void Options_Load(void) { static String path = String_FromConst("options.txt"); - char lineBuffer[768]; - String line = String_FromArray(lineBuffer); String key, value; uint8_t buffer[2048]; struct Stream stream, buffered; - ReturnCode res; int i; + ReturnCode res; + String line; char lineBuffer[768]; res = Stream_OpenFile(&stream, &path); if (res == ReturnCode_FileNotFound) return; @@ -178,6 +177,7 @@ void Options_Load(void) { /* ReadLine reads single byte at a time */ Stream_ReadonlyBuffered(&buffered, &stream, buffer, sizeof(buffer)); + String_InitArray(line, lineBuffer); for (;;) { res = Stream_ReadLine(&buffered, &line); @@ -198,13 +198,13 @@ void Options_Load(void) { void Options_Save(void) { static String path = String_FromConst("options.txt"); - char lineBuffer[1024]; + char lineBuffer[768]; String line = String_FromArray(lineBuffer); String key, value; struct Stream stream; - ReturnCode res; int i; + ReturnCode res; res = Stream_CreateFile(&stream, &path); if (res) { Chat_LogError2(res, "creating", &path); return; } diff --git a/src/PacketHandlers.c b/src/PacketHandlers.c index c4eec6abc..ccffb456a 100644 --- a/src/PacketHandlers.c +++ b/src/PacketHandlers.c @@ -219,15 +219,14 @@ static void WoM_CheckMotd(void) { static String cfg = String_FromConst("cfg="); String motd = ServerConnection_ServerMOTD, host; int index; - - char urlBuffer[STRING_SIZE]; - String url = String_FromArray(urlBuffer); + String url; char urlBuffer[STRING_SIZE]; if (!motd.length) return; index = String_IndexOfString(&motd, &cfg); if (Game_PureClassic || index == -1) return; host = String_UNSAFE_SubstringAt(&motd, index + cfg.length); + String_InitArray(url, urlBuffer); String_Format1(&url, "http://%s", &host); /* TODO: Replace $U with username */ /*url = url.Replace("$U", game.Username); */ @@ -646,10 +645,9 @@ static void Classic_Message(uint8_t* data) { static void Classic_Kick(uint8_t* data) { static String title = String_FromConst("&eLost connection to the server"); + String reason; char reasonBuffer[STRING_SIZE]; - char reasonBuffer[STRING_SIZE]; - String reason = String_FromArray(reasonBuffer); - + String_InitArray(reason, reasonBuffer); Handlers_ReadString(&data, &reason); Game_Disconnect(&title, &reason); } @@ -856,9 +854,9 @@ static void CPE_SendCpeExtInfoReply(void) { static void CPE_ExtInfo(uint8_t* data) { static String d3Server = String_FromConst("D3 server"); - char appNameBuffer[STRING_SIZE]; - String appName = String_FromArray(appNameBuffer); + String appName; char appNameBuffer[STRING_SIZE]; + String_InitArray(appName, appNameBuffer); Handlers_ReadString(&data, &appName); Chat_Add1("Server software: %s", &appName); cpe_needD3Fix = String_CaselessStarts(&appName, &d3Server); @@ -870,10 +868,10 @@ static void CPE_ExtInfo(uint8_t* data) { } static void CPE_ExtEntry(uint8_t* data) { - char extNameBuffer[STRING_SIZE]; - String ext = String_FromArray(extNameBuffer); + String ext; char extNameBuffer[STRING_SIZE]; int extVersion; + String_InitArray(ext, extNameBuffer); Handlers_ReadString(&data, &ext); extVersion = Stream_GetU32_BE(data); Platform_Log2("cpe ext: %s, %i", &ext, &extVersion); @@ -1215,8 +1213,9 @@ static void CPE_SetTextColor(uint8_t* data) { } static void CPE_SetMapEnvUrl(uint8_t* data) { - char urlBuffer[STRING_SIZE]; - String url = String_FromArray(urlBuffer); + String url; char urlBuffer[STRING_SIZE]; + + String_InitArray(url, urlBuffer); Handlers_ReadString(&data, &url); if (!Game_AllowServerTextures) return; diff --git a/src/Platform.c b/src/Platform.c index 39c333af5..9b3a0cda3 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -215,8 +215,9 @@ void Platform_Log3(const char* format, const void* a1, const void* a2, const voi } void Platform_Log4(const char* format, const void* a1, const void* a2, const void* a3, const void* a4) { - char msgBuffer[512]; - String msg = String_FromArray(msgBuffer); + String msg; char msgBuffer[512]; + String_InitArray(msg, msgBuffer); + String_Format4(&msg, format, a1, a2, a3, a4); Platform_Log(&msg); } @@ -922,16 +923,17 @@ void Font_Free(FontDesc* desc) { } static void Font_Add(const String* path, FT_Face face, StringsBuffer* entries, const char* defStyle) { + String name; char nameBuffer[STRING_SIZE]; + String style; + if (!face->family_name || !(face->face_flags & FT_FACE_FLAG_SCALABLE)) return; StringsBuffer_Add(entries, path); - - char nameBuffer[STRING_SIZE]; - String name = String_FromArray(nameBuffer); + String_InitArray(name, nameBuffer); String_AppendConst(&name, face->family_name); /* don't want 'Arial Regular' or 'Arial Bold' */ if (face->style_name) { - String style = String_FromReadonly(face->style_name); + style = String_FromReadonly(face->style_name); if (!String_CaselessEqualsConst(&style, defStyle)) { String_Format1(&name, " %c", face->style_name); } @@ -1918,9 +1920,9 @@ int Platform_GetCommandLineArgs(int argc, STRING_REF const char** argv, String* static ReturnCode Platform_RunOpen(const char* format, const String* args) { char str[300]; FILE* fp; - char pathBuffer[FILENAME_SIZE + 10]; - String path = String_FromArray(pathBuffer); + String path; char pathBuffer[FILENAME_SIZE + 10]; + String_InitArray(path, pathBuffer); String_Format1(&path, format, args); Platform_ConvertString(str, &path); diff --git a/src/Program.c b/src/Program.c index 9bbb6e38d..2c573f483 100644 --- a/src/Program.c +++ b/src/Program.c @@ -47,10 +47,8 @@ int main(int argc, char** argv) { /* String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); */ /* argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); */ - char defPathBuffer[STRING_SIZE]; - String defPath = String_FromArray(defPathBuffer); - char titleBuffer[STRING_SIZE]; - String title = String_FromArray(titleBuffer); + String defPath; char defPathBuffer[STRING_SIZE]; + String title; char titleBuffer[STRING_SIZE]; uint8_t ip[4]; uint16_t port; @@ -68,6 +66,8 @@ int main(int argc, char** argv) { Utils_EnsureDirectory("maps"); Utils_EnsureDirectory("texpacks"); Utils_EnsureDirectory("texturecache"); + + String_InitArray(defPath, defPathBuffer); String_Format1(&defPath, "texpacks%rdefault.zip", &Directory_Separator); if (!File_Exists(&defPath)) { @@ -111,6 +111,7 @@ int main(int argc, char** argv) { if (device.Bounds.Width < 854) width = 640; } + String_InitArray(title, titleBuffer); String_Format2(&title, "%c (%s)", PROGRAM_APP_NAME, &Game_Username); Game_Run(width, height, &title, &device); diff --git a/src/Screens.c b/src/Screens.c index 395cd0e54..330477a28 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -316,6 +316,8 @@ static bool StatusScreen_HacksChanged(struct StatusScreen* s) { static void StatusScreen_UpdateHackState(struct StatusScreen* s) { struct HacksComp* hacks = &LocalPlayer_Instance.Hacks; + bool speeding; + s->Speed = hacks->Speeding; s->HalfSpeed = hacks->HalfSpeeding; s->Fly = hacks->Flying; s->Noclip = hacks->Noclip; s->LastFov = Game_Fov; s->CanSpeed = hacks->CanSpeed; @@ -327,7 +329,7 @@ static void StatusScreen_UpdateHackState(struct StatusScreen* s) { } if (hacks->Flying) String_AppendConst(&status, "Fly ON "); - bool speeding = (hacks->Speeding || hacks->HalfSpeeding) && hacks->CanSpeed; + speeding = (hacks->Speeding || hacks->HalfSpeeding) && hacks->CanSpeed; if (speeding) String_AppendConst(&status, "Speed ON "); if (hacks->Noclip) String_AppendConst(&status, "Noclip ON "); @@ -335,12 +337,13 @@ static void StatusScreen_UpdateHackState(struct StatusScreen* s) { } static void StatusScreen_Update(struct StatusScreen* s, double delta) { + String status; char statusBuffer[STRING_SIZE * 2]; + s->Frames++; s->Accumulator += delta; if (s->Accumulator < 1.0) return; - char statusBuffer[STRING_SIZE * 2]; - String status = String_FromArray(statusBuffer); + String_InitArray(status, statusBuffer); StatusScreen_MakeText(s, &status); TextWidget_Set(&s->Status, &status, &s->Font); @@ -365,8 +368,8 @@ static void StatusScreen_ContextLost(void* screen) { static void StatusScreen_ContextRecreated(void* screen) { static String chars = String_FromConst("0123456789-, ()"); static String prefix = String_FromConst("Position: "); - struct StatusScreen* s = screen; + struct StatusScreen* s = screen; struct TextWidget* status = &s->Status; struct TextWidget* hacks = &s->HackStates; int y; @@ -1275,14 +1278,13 @@ static bool HUDScreen_MouseScroll(void* screen, float delta) { static bool HUDScreen_MouseDown(void* screen, int x, int y, MouseButton btn) { struct HUDScreen* s = screen; struct ChatScreen* chat; + String name; char nameBuffer[STRING_SIZE + 1]; if (btn != MouseButton_Left || !s->HandlesAllInput) return false; chat = (struct ChatScreen*)s->Chat; if (!s->ShowingList) { return Elem_HandlesMouseDown(chat, x, y, btn); } - char nameBuffer[STRING_SIZE + 1]; - String name = String_FromArray(nameBuffer); - + String_InitArray(name, nameBuffer); PlayerListWidget_GetNameUnder(&s->PlayerList, x, y, &name); if (!name.length) { return Elem_HandlesMouseDown(chat, x, y, btn); } @@ -1428,6 +1430,7 @@ static void DisconnectScreen_ContextLost(void* screen) { static void DisconnectScreen_ContextRecreated(void* screen) { struct DisconnectScreen* s = screen; + String msg; char msgBuffer[STRING_SIZE]; TextWidget_Create(&s->Title, &s->TitleStr, &s->TitleFont); Widget_SetLocation(&s->Title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -30); @@ -1435,8 +1438,7 @@ static void DisconnectScreen_ContextRecreated(void* screen) { TextWidget_Create(&s->Message, &s->MessageStr, &s->MessageFont); Widget_SetLocation(&s->Message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 10); - char msgBuffer[STRING_SIZE]; - String msg = String_FromArray(msgBuffer); + String_InitArray(msg, msgBuffer); DisconnectScreen_ReconnectMessage(s, &msg); ButtonWidget_Create(&s->Reconnect, 300, &msg, &s->TitleFont, NULL); @@ -1494,14 +1496,13 @@ static bool DisconnectScreen_KeyPress(void* s, char keyChar) { return true; } static bool DisconnectScreen_KeyUp(void* s, Key key) { return true; } static bool DisconnectScreen_MouseDown(void* screen, int x, int y, MouseButton btn) { - char titleBuffer[STRING_SIZE]; - String title = String_FromArray(titleBuffer); - struct DisconnectScreen* s = screen; - struct ButtonWidget* w = &s->Reconnect; + struct ButtonWidget* w = &s->Reconnect; + String title; char titleBuffer[STRING_SIZE]; if (btn != MouseButton_Left) return true; - if (!w->Disabled && Widget_Contains(w, x, y)) { + if (!w->Disabled && Widget_Contains(w, x, y)) { + String_InitArray(title, titleBuffer); String_Format2(&title, "Connecting to %s:%i..", &Game_IPAddress, &Game_Port); Gui_FreeActive(); @@ -1531,7 +1532,9 @@ struct ScreenVTABLE DisconnectScreen_VTABLE = { struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* message) { static String kick = String_FromConst("Kicked "); static String ban = String_FromConst("Banned "); + struct DisconnectScreen* s = &DisconnectScreen_Instance; + String why; char whyBuffer[STRING_SIZE]; s->HandlesAllInput = true; s->BlocksWorld = true; @@ -1542,8 +1545,7 @@ struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* String_InitArray(s->MessageStr, s->__MessageBuffer); String_AppendString(&s->MessageStr, message); - char whyBuffer[STRING_SIZE]; - String why = String_FromArray(whyBuffer); + String_InitArray(why, whyBuffer); String_AppendColorless(&why, message); s->CanReconnect = !(String_CaselessStarts(&why, &kick) || String_CaselessStarts(&why, &ban)); diff --git a/src/TexturePack.c b/src/TexturePack.c index 04c32e58c..073d48bfd 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -234,11 +234,12 @@ static void EntryList_Load(struct EntryList* list) { static void EntryList_Save(struct EntryList* list) { struct Stream stream; - ReturnCode res; + String entry; int i; - char pathBuffer[FILENAME_SIZE]; - String path = String_FromArray(pathBuffer); + ReturnCode res; + String path; char pathBuffer[FILENAME_SIZE]; + String_InitArray(path, pathBuffer); String_Format3(&path, "%c%r%c", list->Folder, &Directory_Separator, list->Filename); if (!Utils_EnsureDirectory(list->Folder)) return; @@ -246,8 +247,8 @@ static void EntryList_Save(struct EntryList* list) { if (res) { Chat_LogError2(res, "creating", &path); return; } for (i = 0; i < list->Entries.Count; i++) { - String entry = StringsBuffer_UNSAFE_Get(&list->Entries, i); - res = Stream_WriteLine(&stream, &entry); + entry = StringsBuffer_UNSAFE_Get(&list->Entries, i); + res = Stream_WriteLine(&stream, &entry); if (res) { Chat_LogError2(res, "writing to", &path); break; } } @@ -261,16 +262,18 @@ static void EntryList_Add(struct EntryList* list, const String* entry) { } static bool EntryList_Has(struct EntryList* list, const String* entry) { + String curEntry; int i; + for (i = 0; i < list->Entries.Count; i++) { - String curEntry = StringsBuffer_UNSAFE_Get(&list->Entries, i); + curEntry = StringsBuffer_UNSAFE_Get(&list->Entries, i); if (String_Equals(&curEntry, entry)) return true; } return false; } static void EntryList_UNSAFE_Make(struct EntryList* list, STRING_REF const char* folder, STRING_REF const char* file) { - list->Folder = folder; + list->Folder = folder; list->Filename = file; EntryList_Load(list); } @@ -325,8 +328,8 @@ bool TextureCache_Get(const String* url, struct Stream* stream) { void TexturePack_GetFromTags(const String* url, String* result, struct EntryList* list) { TexCache_Crc32(url); - int i; String line, key, value; + int i; for (i = 0; i < list->Entries.Count; i++) { line = StringsBuffer_UNSAFE_Get(&list->Entries, i); @@ -372,7 +375,7 @@ void TextureCache_Set(const String* url, uint8_t* data, uint32_t length) { if (res) { Chat_LogError2(res, "closing cache for", url); } } -void TextureCache_AddToTags(const String* url, const String* data, struct EntryList* list) { +static void TextureCache_SetEntry(const String* url, const String* data, struct EntryList* list) { TexCache_Crc32(url); char entryBuffer[2048]; String entry = String_FromArray(entryBuffer); @@ -391,17 +394,17 @@ void TextureCache_AddToTags(const String* url, const String* data, struct EntryL void TextureCache_SetETag(const String* url, const String* etag) { if (!etag->length) return; - TextureCache_AddToTags(url, etag, &cache_eTags); + TextureCache_SetEntry(url, etag, &cache_eTags); } void TextureCache_SetLastModified(const String* url, const TimeMS* lastModified) { - if (!lastModified) return; uint64_t ticks = (*lastModified) * TEXCACHE_TICKS_PER_MS; + if (!ticks) return; char dataBuffer[STRING_SIZE]; String data = String_FromArray(dataBuffer); String_AppendUInt64(&data, ticks); - TextureCache_AddToTags(url, &data, &cache_lastModified); + TextureCache_SetEntry(url, &data, &cache_lastModified); } @@ -426,10 +429,11 @@ static ReturnCode TexturePack_ExtractZip(struct Stream* stream) { void TexturePack_ExtractZip_File(const String* filename) { struct Stream stream; ReturnCode res; - char pathBuffer[FILENAME_SIZE]; - String path = String_FromArray(pathBuffer); + String path; char pathBuffer[FILENAME_SIZE]; + String_InitArray(path, pathBuffer); String_Format2(&path, "texpacks%r%s", &Directory_Separator, filename); + res = Stream_OpenFile(&stream, &path); if (res) { Chat_LogError2(res, "opening", &path); return; } diff --git a/src/Widgets.c b/src/Widgets.c index 35f40b584..ba99380d3 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -580,11 +580,11 @@ static void TableWidget_RecreateDescTex(struct TableWidget* w) { void TableWidget_MakeDescTex(struct TableWidget* w, BlockID block) { struct DrawTextArgs args; - char descBuffer[STRING_SIZE * 2]; - String desc = String_FromArray(descBuffer); + String desc; char descBuffer[STRING_SIZE * 2]; Gfx_DeleteTexture(&w->DescTex.ID); - if (block == BLOCK_AIR) return; + if (block == BLOCK_AIR) return; + String_InitArray(desc, descBuffer); TableWidget_MakeBlockDesc(&desc, block); DrawTextArgs_Make(&args, &desc, &w->Font, true); @@ -1133,12 +1133,14 @@ static void InputWidget_EndKey(struct InputWidget* w) { } static bool InputWidget_OtherKey(struct InputWidget* w, Key key) { - int maxChars = w->GetMaxLines() * INPUTWIDGET_LEN; + int maxChars; + String text; char textBuffer[INPUTWIDGET_MAX_LINES * STRING_SIZE]; + + maxChars = w->GetMaxLines() * INPUTWIDGET_LEN; if (!InputWidget_ControlDown()) return false; if (key == Key_V && w->Text.length < maxChars) { - char textBuffer[INPUTWIDGET_MAX_LINES * STRING_SIZE]; - String text = String_FromArray(textBuffer); + String_InitArray(text, textBuffer); Window_GetClipboardText(&text); String_TrimStart(&text); @@ -1457,13 +1459,13 @@ static void MenuInputWidget_RemakeTexture(void* widget) { struct DrawTextArgs args; Size2D size; Bitmap bmp; + String range; char rangeBuffer[STRING_SIZE]; DrawTextArgs_Make(&args, &w->Base.Lines[0], &w->Base.Font, false); size = Drawer2D_MeasureText(&args); w->Base.CaretAccumulator = 0.0; - char rangeBuffer[STRING_SIZE]; - String range = String_FromArray(rangeBuffer); + String_InitArray(range, rangeBuffer); v = &w->Validator; v->VTABLE->GetRange(v, &range); @@ -1806,11 +1808,11 @@ void ChatInputWidget_Create(struct ChatInputWidget* w, const FontDesc* font) { #define LIST_NAMES_PER_COLUMN 16 static void PlayerListWidget_DrawName(struct Texture* tex, struct PlayerListWidget* w, const String* name) { - char tmpBuffer[STRING_SIZE]; - String tmp = String_FromArray(tmpBuffer); + String tmp; char tmpBuffer[STRING_SIZE]; struct DrawTextArgs args; if (Game_PureClassic) { + String_InitArray(tmp, tmpBuffer); String_AppendColorless(&tmp, name); } else { tmp = *name; @@ -1982,18 +1984,21 @@ static int PlayerListWidget_GetGroupCount(struct PlayerListWidget* w, int id, in } static int PlayerListWidget_PlayerCompare(int x, int y) { - uint8_t xRank = TabList_GroupRanks[x]; - uint8_t yRank = TabList_GroupRanks[y]; - if (xRank != yRank) return (xRank < yRank ? -1 : 1); + uint8_t xRank, yRank; + String xNameRaw, yNameRaw; + String xName; char xNameBuffer[STRING_SIZE]; + String yName; char yNameBuffer[STRING_SIZE]; - char xNameBuffer[STRING_SIZE]; - String xName = String_FromArray(xNameBuffer); - String xNameRaw = TabList_UNSAFE_GetList(x); + xRank = TabList_GroupRanks[x]; + yRank = TabList_GroupRanks[y]; + if (xRank != yRank) return (xRank < yRank ? -1 : 1); + + String_InitArray(xName, xNameBuffer); + xNameRaw = TabList_UNSAFE_GetList(x); String_AppendColorless(&xName, &xNameRaw); - char yNameBuffer[STRING_SIZE]; - String yName = String_FromArray(yNameBuffer); - String yNameRaw = TabList_UNSAFE_GetList(y); + String_InitArray(yName, yNameBuffer); + yNameRaw = TabList_UNSAFE_GetList(y); String_AppendColorless(&yName, &yNameRaw); return String_Compare(&xName, &yName);