Move extracting initial texture pack out of Game.c

This commit is contained in:
UnknownShadow200 2020-04-19 17:43:00 +10:00
parent 13ad241412
commit 11211e9526
5 changed files with 40 additions and 41 deletions

View File

@ -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 */

View File

@ -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);

View File

@ -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);
}

View File

@ -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;

View File

@ -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);