From 2532940c49d6f8f1abddc977f8d06e5dddf83da2 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 23 Oct 2022 12:44:24 +1100 Subject: [PATCH] Refactor texture pack entry extraction to be more uniform --- src/Animations.c | 51 +++++++++++++++++++++++++++-------------------- src/Drawer2D.c | 34 ++++++++++++++++--------------- src/EnvRenderer.c | 39 ++++++++++++++++++++++++------------ src/Gui.c | 40 +++++++++++++++++++++++++------------ src/Particle.c | 16 +++++++-------- src/Protocol.c | 13 ++---------- src/TexturePack.c | 29 ++++++++++++++++++++++----- src/TexturePack.h | 8 ++++++++ 8 files changed, 142 insertions(+), 88 deletions(-) diff --git a/src/Animations.c b/src/Animations.c index bc2fc90ff..033b9827f 100644 --- a/src/Animations.c +++ b/src/Animations.c @@ -351,6 +351,30 @@ static void Animations_Tick(struct ScheduledTask* task) { /*########################################################################################################################* *--------------------------------------------------Animations component---------------------------------------------------* *#########################################################################################################################*/ +static void AnimationsPngProcess(struct Stream* stream, const cc_string* name) { + cc_result res = Png_Decode(&anims_bmp, stream); + if (!res) return; + + Logger_SysWarn2(res, "decoding", name); + Mem_Free(anims_bmp.scan0); + anims_bmp.scan0 = NULL; +} +static struct TextureEntry animations_entry = { "animations.png", AnimationsPngProcess }; +static struct TextureEntry animations_txt = { "animations.txt", Animations_ReadDescription }; + +static void UseWaterProcess(struct Stream* stream, const cc_string* name) { + useWaterAnim = true; + alwaysWaterAnim = true; +} +static struct TextureEntry water_entry = { "usewateranim", UseWaterProcess }; + +static void UseLavaProcess(struct Stream* stream, const cc_string* name) { + useLavaAnim = true; + alwaysLavaAnim = true; +} +static struct TextureEntry lava_entry = { "uselavaanim", UseLavaProcess }; + + static void OnPackChanged(void* obj) { Animations_Clear(); useLavaAnim = Animations_IsDefaultZip(); @@ -358,31 +382,14 @@ static void OnPackChanged(void* obj) { alwaysLavaAnim = false; alwaysWaterAnim = false; } - -static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* name) { - cc_result res; - if (String_CaselessEqualsConst(name, "animations.png")) { - res = Png_Decode(&anims_bmp, stream); - if (!res) return; - - Logger_SysWarn2(res, "decoding", name); - Mem_Free(anims_bmp.scan0); - anims_bmp.scan0 = NULL; - } else if (String_CaselessEqualsConst(name, "animations.txt")) { - Animations_ReadDescription(stream, name); - } else if (String_CaselessEqualsConst(name, "uselavaanim")) { - useLavaAnim = true; - alwaysLavaAnim = true; - } else if (String_CaselessEqualsConst(name, "usewateranim")) { - useWaterAnim = true; - alwaysWaterAnim = true; - } -} - static void OnInit(void) { + TextureEntry_Register(&animations_entry); + TextureEntry_Register(&animations_txt); + TextureEntry_Register(&water_entry); + TextureEntry_Register(&lava_entry); + ScheduledTask_Add(GAME_DEF_TICKS, Animations_Tick); Event_Register_(&TextureEvents.PackChanged, NULL, OnPackChanged); - Event_Register_(&TextureEvents.FileChanged, NULL, OnFileChanged); } struct IGameComponent Animations_Component = { diff --git a/src/Drawer2D.c b/src/Drawer2D.c index f66b9024a..401e40cd9 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -13,6 +13,7 @@ #include "Errors.h" #include "Window.h" #include "Options.h" +#include "TexturePack.h" struct _Drawer2DData Drawer2D; #define Font_IsBitmap(font) (!(font)->handle) @@ -633,6 +634,22 @@ void Drawer2D_DrawClippedText(struct Context2D* ctx, struct DrawTextArgs* args, /*########################################################################################################################* *---------------------------------------------------Drawer2D component----------------------------------------------------* *#########################################################################################################################*/ +static void DefaultPngProcess(struct Stream* stream, const cc_string* name) { + struct Bitmap bmp; + cc_result res; + + if ((res = Png_Decode(&bmp, stream))) { + Logger_SysWarn2(res, "decoding", name); + Mem_Free(bmp.scan0); + } else if (Font_SetBitmapAtlas(&bmp)) { + Event_RaiseVoid(&ChatEvents.FontChanged); + } else { + Mem_Free(bmp.scan0); + } +} +static struct TextureEntry default_entry = { "default.png", DefaultPngProcess }; + + static void InitHexEncodedColor(int i, int hex, cc_uint8 lo, cc_uint8 hi) { Drawer2D.Colors[i] = BitmapColor_RGB( lo * ((hex >> 2) & 1) + hi * (hex >> 3), @@ -655,21 +672,6 @@ static void OnReset(void) { } } -static void OnFileChanged(void* obj, struct Stream* src, const cc_string* name) { - struct Bitmap bmp; - cc_result res; - if (!String_CaselessEqualsConst(name, "default.png")) return; - - if ((res = Png_Decode(&bmp, src))) { - Logger_SysWarn2(res, "decoding", name); - Mem_Free(bmp.scan0); - } else if (Font_SetBitmapAtlas(&bmp)) { - Event_RaiseVoid(&ChatEvents.FontChanged); - } else { - Mem_Free(bmp.scan0); - } -} - static void OnInit(void) { OnReset(); Drawer2D.BitmappedText = Game_ClassicMode || !Options_GetBool(OPT_USE_CHAT_FONT, false); @@ -677,7 +679,7 @@ static void OnInit(void) { Options_Get(OPT_FONT_NAME, &font_default, ""); if (Game_ClassicMode) font_default.length = 0; - Event_Register_(&TextureEvents.FileChanged, NULL, OnFileChanged); + TextureEntry_Register(&default_entry); } static void OnFree(void) { diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index fec4c87e2..58b0b2e43 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -745,6 +745,27 @@ static void UpdateMapEdges(void) { /*########################################################################################################################* *---------------------------------------------------------General---------------------------------------------------------* *#########################################################################################################################*/ +static void CloudsPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&clouds_tex, stream, name, NULL); +} +static struct TextureEntry clouds_entry = { "clouds.png", CloudsPngProcess }; + +static void SkyboxPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&skybox_tex, stream, name, NULL); +} +static struct TextureEntry skybox_entry = { "skybox.png", SkyboxPngProcess }; + +static void SnowPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&snow_tex, stream, name, NULL); +} +static struct TextureEntry snow_entry = { "snow.png", SnowPngProcess }; + +static void RainPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&rain_tex, stream, name, NULL); +} +static struct TextureEntry rain_entry = { "rain.png", RainPngProcess }; + + static void DeleteVbs(void) { Gfx_DeleteVb(&sky_vb); Gfx_DeleteVb(&clouds_vb); @@ -803,18 +824,6 @@ int EnvRenderer_CalcFlags(const cc_string* mode) { } -static void OnFileChanged(void* obj, struct Stream* src, const cc_string* name) { - if (String_CaselessEqualsConst(name, "clouds.png")) { - Game_UpdateTexture(&clouds_tex, src, name, NULL); - } else if (String_CaselessEqualsConst(name, "skybox.png")) { - Game_UpdateTexture(&skybox_tex, src, name, NULL); - } else if (String_CaselessEqualsConst(name, "snow.png")) { - Game_UpdateTexture(&snow_tex, src, name, NULL); - } else if (String_CaselessEqualsConst(name, "rain.png")) { - Game_UpdateTexture(&rain_tex, src, name, NULL); - } -} - static void OnTexturePackChanged(void* obj) { /* TODO: Find better way, really should delete them all here */ Gfx_DeleteTexture(&skybox_tex); @@ -864,7 +873,11 @@ static void OnInit(void) { EnvRenderer_Legacy = flags & ENV_LEGACY; EnvRenderer_Minimal = flags & ENV_MINIMAL; - Event_Register_(&TextureEvents.FileChanged, NULL, OnFileChanged); + TextureEntry_Register(&clouds_entry); + TextureEntry_Register(&skybox_entry); + TextureEntry_Register(&snow_entry); + TextureEntry_Register(&rain_entry); + Event_Register_(&TextureEvents.PackChanged, NULL, OnTexturePackChanged); Event_Register_(&TextureEvents.AtlasChanged, NULL, OnTerrainAtlasChanged); diff --git a/src/Gui.c b/src/Gui.c index 0961b79fc..209bf507f 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -16,6 +16,7 @@ #include "Menus.h" #include "Funcs.h" #include "Server.h" +#include "TexturePack.h" struct _GuiData Gui; struct Screen* Gui_Screens[GUI_MAX_SCREENS]; @@ -495,19 +496,28 @@ void Screen_PointerUp(void* s, int id, int x, int y) { } /*########################################################################################################################* *------------------------------------------------------Gui component------------------------------------------------------* *#########################################################################################################################*/ -static void OnFontChanged(void* obj) { Gui_RefreshAll(); } - -static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* name) { - if (String_CaselessEqualsConst(name, "gui.png")) { - Game_UpdateTexture(&Gui.GuiTex, stream, name, NULL); - } else if (String_CaselessEqualsConst(name, "gui_classic.png")) { - Game_UpdateTexture(&Gui.GuiClassicTex, stream, name, NULL); - } else if (String_CaselessEqualsConst(name, "icons.png")) { - Game_UpdateTexture(&Gui.IconsTex, stream, name, NULL); - } else if (String_CaselessEqualsConst(name, "touch.png")) { - Game_UpdateTexture(&Gui.TouchTex, stream, name, NULL); - } +static void GuiPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&Gui.GuiTex, stream, name, NULL); } +static struct TextureEntry gui_entry = { "gui.png", GuiPngProcess }; + +static void GuiClassicPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&Gui.GuiClassicTex, stream, name, NULL); +} +static struct TextureEntry guiClassic_entry = { "gui_classic.png", GuiClassicPngProcess }; + +static void IconsPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&Gui.IconsTex, stream, name, NULL); +} +static struct TextureEntry icons_entry = { "icons.png", IconsPngProcess }; + +static void TouchPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&Gui.TouchTex, stream, name, NULL); +} +static struct TextureEntry touch_entry = { "touch.png", TouchPngProcess }; + + +static void OnFontChanged(void* obj) { Gui_RefreshAll(); } static void OnKeyPress(void* obj, int cp) { struct Screen* s; @@ -545,8 +555,12 @@ static void OnContextLost(void* obj) { static void OnInit(void) { Gui.Screens = Gui_Screens; /* for plugins */ + TextureEntry_Register(&gui_entry); + TextureEntry_Register(&guiClassic_entry); + TextureEntry_Register(&icons_entry); + TextureEntry_Register(&touch_entry); + Event_Register_(&ChatEvents.FontChanged, NULL, OnFontChanged); - Event_Register_(&TextureEvents.FileChanged, NULL, OnFileChanged); Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); Event_Register_(&InputEvents.Press, NULL, OnKeyPress); diff --git a/src/Particle.c b/src/Particle.c index fb12e2a0b..41adf9ac8 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -570,6 +570,12 @@ void Particles_CustomEffect(int effectID, float x, float y, float z, float origi /*########################################################################################################################* *---------------------------------------------------Particles component---------------------------------------------------* *#########################################################################################################################*/ +static void ParticlesPngProcess(struct Stream* stream, const cc_string* name) { + Game_UpdateTexture(&Particles_TexId, stream, name, NULL); +} +static struct TextureEntry particles_entry = { "particles.png", ParticlesPngProcess }; + + static void OnContextLost(void* obj) { Gfx_DeleteDynamicVb(&Particles_VB); @@ -583,19 +589,13 @@ static void OnBreakBlockEffect_Handler(void* obj, IVec3 coords, BlockID old, Blo Particles_BreakBlockEffect(coords, old, now); } -static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* name) { - if (String_CaselessEqualsConst(name, "particles.png")) { - Game_UpdateTexture(&Particles_TexId, stream, name, NULL); - } -} - static void OnInit(void) { ScheduledTask_Add(GAME_DEF_TICKS, Particles_Tick); Random_SeedFromCurrentTime(&rnd); - OnContextRecreated(NULL); + OnContextRecreated(NULL); + TextureEntry_Register(&particles_entry); Event_Register_(&UserEvents.BlockChanged, NULL, OnBreakBlockEffect_Handler); - Event_Register_(&TextureEvents.FileChanged, NULL, OnFileChanged); Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); } diff --git a/src/Protocol.c b/src/Protocol.c index 5e5758343..e90cb9603 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -30,8 +30,6 @@ #include "Input.h" #include "Utils.h" -#define QUOTE(x) #x -#define STRINGIFY(val) QUOTE(val) struct _ProtocolData Protocol; /* Classic state */ @@ -1461,15 +1459,8 @@ static void CPE_DefineModel(cc_uint8* data) { numParts = data[114]; if (numParts > MAX_CUSTOM_MODEL_PARTS) { - cc_string msg; char msgBuffer[256]; - String_InitArray(msg, msgBuffer); - - String_Format1( - &msg, - "&cCustom Model '%s' exceeds parts limit of " STRINGIFY(MAX_CUSTOM_MODEL_PARTS), - &name - ); - Logger_WarnFunc(&msg); + int maxParts = MAX_CUSTOM_MODEL_PARTS; + Chat_Add2("&cCustom Model '%s' exceeds parts limit of %i", &name, &maxParts); return; } diff --git a/src/TexturePack.c b/src/TexturePack.c index 80f5261a3..0635ebe8b 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -440,16 +440,20 @@ void TexturePack_Extract(const cc_string* url) { TexturePack_ExtractCurrent(false); } +static struct TextureEntry* entries_head; +static struct TextureEntry* entries_tail; + +void TextureEntry_Register(struct TextureEntry* entry) { + LinkedList_Append(entry, entries_head, entries_tail); +} + /*########################################################################################################################* *---------------------------------------------------Textures component----------------------------------------------------* *#########################################################################################################################*/ -static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* name) { +static void TerrainPngProcess(struct Stream* stream, const cc_string* name) { struct Bitmap bmp; - cc_result res; - - if (!String_CaselessEqualsConst(name, "terrain.png")) return; - res = Png_Decode(&bmp, stream); + cc_result res = Png_Decode(&bmp, stream); if (res) { Logger_SysWarn2(res, "decoding", name); @@ -458,6 +462,19 @@ static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* nam Mem_Free(bmp.scan0); } } +static struct TextureEntry terrain_entry = { "terrain.png", TerrainPngProcess }; + + +static void OnFileChanged(void* obj, struct Stream* stream, const cc_string* name) { + struct TextureEntry* e; + + for (e = entries_head; e; e = e->next) { + if (!String_CaselessEqualsConst(name, e->filename)) continue; + + e->Callback(stream, name); + return; + } +} static void OnContextLost(void* obj) { if (!Gfx.ManagedTextures) Atlas1D_Free(); @@ -482,6 +499,7 @@ static void OnInit(void) { String_AppendString(&TexturePack_Path, &defaultPath); } + TextureEntry_Register(&terrain_entry); Utils_EnsureDirectory("texpacks"); Utils_EnsureDirectory("texturecache"); TextureCache_Init(); @@ -497,6 +515,7 @@ static void OnFree(void) { OnContextLost(NULL); Atlas2D_Free(); TexturePack_Url.length = 0; + entries_head = NULL; } struct IGameComponent Textures_Component = { diff --git a/src/TexturePack.h b/src/TexturePack.h index ec5206b1c..0bfe2dc85 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -99,4 +99,12 @@ void TexturePack_CheckPending(void); /* Else tries extracting cached texture pack for the given URL, */ /* then asynchronously downloads the texture pack from the given URL. */ CC_API void TexturePack_Extract(const cc_string* url); + +struct TextureEntry; +struct TextureEntry { + const char* filename; + void (*Callback)(struct Stream* stream, const cc_string* name); + struct TextureEntry* next; +}; +void TextureEntry_Register(struct TextureEntry* entry); #endif