diff --git a/src/Game.c b/src/Game.c index 42697075a..ee355a84d 100644 --- a/src/Game.c +++ b/src/Game.c @@ -53,7 +53,7 @@ cc_bool Game_ClassicMode, Game_ClassicHacks; cc_bool Game_AllowCustomBlocks; cc_bool Game_AllowServerTextures; -cc_bool Game_ViewBobbing, Game_HideGui, Game_DefaultZipMissing; +cc_bool Game_ViewBobbing, Game_HideGui; cc_bool Game_BreakableLiquids, Game_ScreenshotRequested; struct GameVersion Game_Version; @@ -419,11 +419,10 @@ static void Game_Load(void) { if (comp->Init) comp->Init(); } - Game_DefaultZipMissing = false; TexturePack_ExtractCurrent(true); - if (Game_DefaultZipMissing) { + if (TexturePack_DefaultMissing) { Window_ShowDialog("Missing file", - "default.zip is missing, try downloading resources first.\n\nThe game will still run, but without any textures"); + "Both default.zip and classicube.zip are missing,\n try downloading resources first.\n\nClassiCube will still run, but without any textures."); } entTaskI = ScheduledTask_Add(GAME_DEF_TICKS, Entities_Tick); diff --git a/src/Game.h b/src/Game.h index 5a1417c06..ba76f863c 100644 --- a/src/Game.h +++ b/src/Game.h @@ -45,7 +45,6 @@ extern cc_bool Game_BreakableLiquids; /* Whether a screenshot should be taken at the end of this frame */ extern cc_bool Game_ScreenshotRequested; extern cc_bool Game_HideGui; -extern cc_bool Game_DefaultZipMissing; enum GAME_VERSION_ { VERSION_0017 = 27, VERSION_0019 = 28, VERSION_0023 = 29, VERSION_0030 = 30, VERSION_CPE = 31 diff --git a/src/Launcher.c b/src/Launcher.c index 296bf5e52..5d96fe408 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -21,6 +21,7 @@ #include "LBackend.h" #include "PackedCol.h" #include "SystemFonts.h" +#include "TexturePack.h" struct LScreen* Launcher_Active; cc_bool Launcher_ShouldExit, Launcher_ShouldUpdate; @@ -249,6 +250,8 @@ void Launcher_Run(void) { Session_Load(); Launcher_LoadTheme(); Launcher_Init(); + + GameVersion_Load(); Launcher_TryLoadTexturePack(); Http_Component.Init(); @@ -443,13 +446,13 @@ static cc_result Launcher_ProcessZipEntry(const cc_string* path, struct Stream* return 0; } -static void ExtractTexturePack(const cc_string* path) { +static cc_result ExtractTexturePack(const cc_string* path) { struct Stream stream; cc_result res; res = Stream_OpenFile(&stream, path); - if (res == ReturnCode_FileNotFound) return; - if (res) { Logger_SysWarn(res, "opening texture pack"); return; } + if (res == ReturnCode_FileNotFound) return res; + if (res) { Logger_SysWarn(res, "opening texture pack"); return res; } res = Zip_Extract(&stream, Launcher_SelectZipEntry, Launcher_ProcessZipEntry); @@ -457,21 +460,24 @@ static void ExtractTexturePack(const cc_string* path) { if (res) { Logger_SysWarn(res, "extracting texture pack"); } /* No point logging error for closing readonly file */ (void)stream.Close(&stream); + return res; } void Launcher_TryLoadTexturePack(void) { - static const cc_string defZip = String_FromConst("texpacks/default.zip"); cc_string path; char pathBuffer[FILENAME_SIZE]; cc_string texPack; + /* TODO: Not duplicate TexturePack functionality */ if (Options_UNSAFE_Get(OPT_DEFAULT_TEX_PACK, &texPack)) { String_InitArray(path, pathBuffer); String_Format1(&path, "texpacks/%s", &texPack); - ExtractTexturePack(&path); + (void)ExtractTexturePack(&path); } /* user selected texture pack is missing some required .png files */ - if (!hasBitmappedFont || dirtBmp.scan0 == NULL) ExtractTexturePack(&defZip); + if (!hasBitmappedFont || dirtBmp.scan0 == NULL) + TexturePack_ExtractDefault(ExtractTexturePack); + LBackend_UpdateLogoFont(); } diff --git a/src/Resources.c b/src/Resources.c index 6fd828d19..699891f7e 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -1,5 +1,5 @@ #include "Resources.h" -#if !defined(CC_BUILD_WEB) && !defined(CC_BUILD_FLATPAK) +#if !defined CC_BUILD_WEB #include "Funcs.h" #include "String.h" #include "Constants.h" @@ -1064,7 +1064,6 @@ void Resources_CheckExistence(void) { int i; Resources_Count = 0; Resources_Size = 0; - GameVersion_Load(); for (i = 0; i < Array_Elems(asset_sets); i++) { diff --git a/src/TexturePack.c b/src/TexturePack.c index 6d34211ee..3ef7610f0 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -325,6 +325,7 @@ static char texpackPathBuffer[FILENAME_SIZE]; cc_string TexturePack_Url = String_FromArray(textureUrlBuffer); cc_string TexturePack_Path = String_FromArray(texpackPathBuffer); +cc_bool TexturePack_DefaultMissing; void TexturePack_SetDefault(const cc_string* texPack) { TexturePack_Path.length = 0; @@ -332,6 +333,26 @@ void TexturePack_SetDefault(const cc_string* texPack) { Options_Set(OPT_DEFAULT_TEX_PACK, texPack); } +cc_result TexturePack_ExtractDefault(DefaultZipCallback callback) { + cc_result res = ReturnCode_FileNotFound; + const char* defaults[3]; + cc_string path; + int i; + + defaults[0] = Game_Version.DefaultTexpack; + defaults[1] = "texpacks/default.zip"; + defaults[2] = "texpacks/classicube.zip"; + + for (i = 0; i < Array_Elems(defaults); i++) + { + path = String_FromReadonly(defaults[i]); + res = callback(&path); + if (!res) return 0; + } + return res; +} + + static cc_bool SelectZipEntry(const cc_string* path) { return true; } static cc_result ProcessZipEntry(const cc_string* path, struct Stream* stream, struct ZipEntry* source) { cc_string name = *path; @@ -371,17 +392,12 @@ static cc_result ExtractFrom(struct Stream* stream, const cc_string* path) { return res; } -static cc_result ExtractFromFile(const cc_string* path, cc_bool isDefault) { +static cc_result ExtractFromFile(const cc_string* path) { struct Stream stream; cc_result res; res = Stream_OpenFile(&stream, path); - if (res) { - /* Game shows a dialog if default.zip is missing */ - Game_DefaultZipMissing |= isDefault && res == ReturnCode_FileNotFound; - Logger_SysWarn2(res, "opening", path); - return res; - } + if (res) { Logger_SysWarn2(res, "opening", path); return res; } res = ExtractFrom(&stream, path); /* No point logging error for closing readonly file */ @@ -389,16 +405,21 @@ static cc_result ExtractFromFile(const cc_string* path, cc_bool isDefault) { return res; } -static cc_result ExtractDefault(void) { - cc_string defaultPath = String_FromReadonly(Game_Version.DefaultTexpack); - cc_string path = Game_ClassicMode ? defaultPath : TexturePack_Path; - cc_bool isDefault = String_CaselessEquals(&path, &defaultPath); - cc_result res = ExtractFromFile(&defaultPath, true); - /* TODO fallback */ +static cc_result ExtractUserTextures(void) { + cc_string path; + cc_result res; - /* override default.zip with user's default texture pack */ - if (!isDefault) res = ExtractFromFile(&path, false); - return res; + /* TODO: Log error for multiple default texture pack extract failure */ + res = TexturePack_ExtractDefault(ExtractFromFile); + /* Game shows a warning dialog if default textures are missing */ + TexturePack_DefaultMissing = res == ReturnCode_FileNotFound; + + path = TexturePack_Path; + if (String_CaselessEqualsConst(&path, "texpacks/default.zip")) path.length = 0; + if (Game_ClassicMode || path.length == 0) return res; + + /* override default textures with user's selected texture pack */ + return ExtractFromFile(&path); } static cc_bool usingDefault; @@ -409,7 +430,7 @@ cc_result TexturePack_ExtractCurrent(cc_bool forceReload) { /* don't pointlessly load default texture pack */ if (!usingDefault || forceReload) { - res = ExtractDefault(); + res = ExtractUserTextures(); usingDefault = true; } @@ -538,9 +559,7 @@ static void OnInit(void) { TexturePack_Path.length = 0; if (Options_UNSAFE_Get(OPT_DEFAULT_TEX_PACK, &file)) { - String_Format1(&TexturePack_Path, "texpacks/%s", &file); - } else { - String_AppendConst(&TexturePack_Path, Game_Version.DefaultTexpack); + String_Format1(&TexturePack_Path, "texpacks/%s", &file); } /* TODO temp hack to fix mobile, need to properly fix */ diff --git a/src/TexturePack.h b/src/TexturePack.h index 0bfe2dc85..66885cc51 100644 --- a/src/TexturePack.h +++ b/src/TexturePack.h @@ -53,8 +53,10 @@ CC_VAR extern struct _Atlas1DData { /* URL of the current custom texture pack, can be empty */ extern cc_string TexturePack_Url; -/* Path to the default texture pack to use */ +/* Path to the user selected custom texture pack to use */ extern cc_string TexturePack_Path; +/* Whether the default texture pack and its alternatives were all not found */ +extern cc_bool TexturePack_DefaultMissing; #define Atlas2D_TileX(texLoc) ((texLoc) & ATLAS2D_MASK) /* texLoc % ATLAS2D_TILES_PER_ROW */ #define Atlas2D_TileY(texLoc) ((texLoc) >> ATLAS2D_SHIFT) /* texLoc / ATLAS2D_TILES_PER_ROW */ @@ -100,6 +102,9 @@ void TexturePack_CheckPending(void); /* then asynchronously downloads the texture pack from the given URL. */ CC_API void TexturePack_Extract(const cc_string* url); +typedef cc_result (*DefaultZipCallback)(const cc_string* path); +cc_result TexturePack_ExtractDefault(DefaultZipCallback callback); + struct TextureEntry; struct TextureEntry { const char* filename;