From 11211e95261ab6fdc53e35c942960e313670fb34 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 19 Apr 2020 17:43:00 +1000 Subject: [PATCH] Move extracting initial texture pack out of Game.c --- src/Game.c | 31 +------------------------------ src/Game.h | 6 ------ src/Menus.c | 2 +- src/TexturePack.c | 36 ++++++++++++++++++++++++++++++++++-- src/TexturePack.h | 6 ++++-- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/Game.c b/src/Game.c index 9eb89341e..909daa28f 100644 --- a/src/Game.c +++ b/src/Game.c @@ -109,23 +109,6 @@ float Game_GetChatScale(void) { return Game_Scale(Game_GetWindowScale() * Game_RawChatScale); } -static char game_defTexPackBuffer[STRING_SIZE]; -static String game_defTexPack = String_FromArray(game_defTexPackBuffer); -static const String defaultZip = String_FromConst("default.zip"); - -String Game_UNSAFE_GetDefaultTexturePack(void) { - String texPath; char texPathBuffer[STRING_SIZE]; - String_InitArray(texPath, texPathBuffer); - - String_Format1(&texPath, "texpacks/%s", &game_defTexPack); - return File_Exists(&texPath) && !Game_ClassicMode ? game_defTexPack : defaultZip; -} - -void Game_SetDefaultTexturePack(const String* texPack) { - String_Copy(&game_defTexPack, texPack); - Options_Set(OPT_DEFAULT_TEX_PACK, texPack); -} - cc_bool Game_ChangeTerrainAtlas(Bitmap* atlas) { static const String terrain = String_FromConst("terrain.png"); if (!Game_ValidateBitmap(&terrain, atlas)) return false; @@ -329,18 +312,6 @@ static void Game_WarnFunc(const String* msg) { } } -static void ExtractInitialTexturePack(void) { - String texPack; - Options_Get(OPT_DEFAULT_TEX_PACK, &game_defTexPack, "default.zip"); - TexturePack_ExtractZip_File(&defaultZip); - - /* in case the user's default texture pack doesn't have all required textures */ - texPack = Game_UNSAFE_GetDefaultTexturePack(); - if (!String_CaselessEqualsConst(&texPack, "default.zip")) { - TexturePack_ExtractZip_File(&texPack); - } -} - static void LoadOptions(void) { Game_ClassicMode = Options_GetBool(OPT_CLASSIC_MODE, false); Game_ClassicHacks = Options_GetBool(OPT_CLASSIC_HACKS, false); @@ -475,7 +446,7 @@ static void Game_Load(void) { if (comp->Init) comp->Init(); } - ExtractInitialTexturePack(); + TexturePack_ExtractInitial(); entTaskI = ScheduledTask_Add(GAME_DEF_TICKS, Entities_Tick); /* set vsync after because it causes a context loss depending on backend */ diff --git a/src/Game.h b/src/Game.h index b92ba8ec3..da78c273c 100644 --- a/src/Game.h +++ b/src/Game.h @@ -59,12 +59,6 @@ float Game_GetHotbarScale(void); float Game_GetInventoryScale(void); float Game_GetChatScale(void); -/* Retrieves the filename of the default texture pack used. */ -/* NOTE: Returns default.zip if classic mode or selected pack does not exist. */ -String Game_UNSAFE_GetDefaultTexturePack(void); -/* Sets the filename of the default texture pack used. */ -void Game_SetDefaultTexturePack(const String* texPack); - /* Attempts to change the terrain atlas. (bitmap containing textures for all blocks) */ cc_bool Game_ChangeTerrainAtlas(Bitmap* atlas); void Game_SetViewDistance(int distance); diff --git a/src/Menus.c b/src/Menus.c index c452ef83d..adf2f3653 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1502,7 +1502,7 @@ static void TexturePackScreen_EntryClick(void* screen, void* widget) { String_Format1(&path, "texpacks/%s", &relPath); if (!File_Exists(&path)) return; - Game_SetDefaultTexturePack(&relPath); + TexturePack_SetDefault(&relPath); World_TextureUrl.length = 0; TexturePack_ExtractCurrent(true); } diff --git a/src/TexturePack.c b/src/TexturePack.c index dacf695cd..4397420ed 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -640,6 +640,25 @@ static void TextureCache_Update(struct HttpRequest* req) { /*########################################################################################################################* *-------------------------------------------------------TexturePack-------------------------------------------------------* *#########################################################################################################################*/ +static char defTexPackBuffer[STRING_SIZE]; +static String defTexPack = String_FromArray(defTexPackBuffer); +static const String defaultZip = String_FromConst("default.zip"); + +/* Retrieves the filename of the default texture pack used. */ +/* NOTE: Returns default.zip if classic mode or selected pack does not exist. */ +static String TexturePack_UNSAFE_GetDefault(void) { + String texPath; char texPathBuffer[STRING_SIZE]; + String_InitArray(texPath, texPathBuffer); + + String_Format1(&texPath, "texpacks/%s", &defTexPack); + return File_Exists(&texPath) && !Game_ClassicMode ? defTexPack : defaultZip; +} + +void TexturePack_SetDefault(const String* texPack) { + String_Copy(&defTexPack, texPack); + Options_Set(OPT_DEFAULT_TEX_PACK, texPack); +} + static cc_result TexturePack_ProcessZipEntry(const String* path, struct Stream* stream, struct ZipState* s) { String name = *path; Utils_UNSAFE_GetFilename(&name); @@ -673,7 +692,8 @@ static cc_result TexturePack_ExtractPng(struct Stream* stream) { return res; } -void TexturePack_ExtractZip_File(const String* filename) { +/* Extracts a .zip texture pack from the given file */ +static void TexturePack_ExtractZip_File(const String* filename) { String path; char pathBuffer[FILENAME_SIZE]; struct Stream stream; cc_result res; @@ -704,6 +724,18 @@ void TexturePack_ExtractZip_File(const String* filename) { #endif } +void TexturePack_ExtractInitial(void) { + String texPack; + Options_Get(OPT_DEFAULT_TEX_PACK, &defTexPack, "default.zip"); + TexturePack_ExtractZip_File(&defaultZip); + + /* in case the user's default texture pack doesn't have all required textures */ + texPack = TexturePack_UNSAFE_GetDefault(); + if (!String_CaselessEqualsConst(&texPack, "default.zip")) { + TexturePack_ExtractZip_File(&texPack); + } +} + static cc_bool texturePackDefault = true; void TexturePack_ExtractCurrent(cc_bool forceReload) { String url = World_TextureUrl, file; @@ -714,7 +746,7 @@ void TexturePack_ExtractCurrent(cc_bool forceReload) { if (!url.length || !TextureCache_Get(&url, &stream)) { /* don't pointlessly load default texture pack */ if (texturePackDefault && !forceReload) return; - file = Game_UNSAFE_GetDefaultTexturePack(); + file = TexturePack_UNSAFE_GetDefault(); TexturePack_ExtractZip_File(&file); texturePackDefault = true; diff --git a/src/TexturePack.h b/src/TexturePack.h index 92ed20da0..50f1ade85 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -85,8 +85,10 @@ void TextureCache_Deny(const String* url); /* Clears the list of denied URLs, returning number removed. */ int TextureCache_ClearDenied(void); -/* Extracts a texture pack .zip from the given file. */ -void TexturePack_ExtractZip_File(const String* filename); +/* Sets the filename of the default texture pack used. */ +void TexturePack_SetDefault(const String* texPack); +/* Gets filename of default texture pack and then extracts it. */ +void TexturePack_ExtractInitial(void); /* If World_TextureUrl is empty, extracts user's default texture pack. */ /* Otherwise extracts the cached texture pack for that URL. */ void TexturePack_ExtractCurrent(cc_bool forceReload);