finish texture functions rewrite

This commit is contained in:
UnknownShadow200 2019-06-21 19:49:56 +10:00
parent cbee786c99
commit bd3ceb1393
9 changed files with 54 additions and 91 deletions

View File

@ -110,18 +110,14 @@ float Game_GetChatScale(void) {
static char game_defTexPackBuffer[STRING_SIZE]; static char game_defTexPackBuffer[STRING_SIZE];
static String game_defTexPack = String_FromArray(game_defTexPackBuffer); static String game_defTexPack = String_FromArray(game_defTexPackBuffer);
static const String defaultZip = String_FromConst("default.zip");
void Game_GetDefaultTexturePack(String* texPack) { String Game_UNSAFE_GetDefaultTexturePack(void) {
String texPath; char texPathBuffer[STRING_SIZE]; String texPath; char texPathBuffer[STRING_SIZE];
String_InitArray(texPath, texPathBuffer); String_InitArray(texPath, texPathBuffer);
String_Format1(&texPath, "texpacks/%s", &game_defTexPack);
if (File_Exists(&texPath) && !Game_ClassicMode) { String_Format1(&texPath, "texpacks/%s", &game_defTexPack);
String_AppendString(texPack, &game_defTexPack); return File_Exists(&texPath) && !Game_ClassicMode ? game_defTexPack : defaultZip;
} else {
String_AppendConst(texPack, "default.zip");
}
} }
void Game_SetDefaultTexturePack(const String* texPack) { void Game_SetDefaultTexturePack(const String* texPack) {
@ -190,7 +186,11 @@ void Game_Reset(void) {
struct IGameComponent* comp; struct IGameComponent* comp;
World_Reset(); World_Reset();
Event_RaiseVoid(&WorldEvents.NewMap); Event_RaiseVoid(&WorldEvents.NewMap);
if (World_TextureUrl.length) TexturePack_ExtractDefault();
if (World_TextureUrl.length) {
World_TextureUrl.length = 0;
TexturePack_ExtractCurrent(false);
}
for (comp = comps_head; comp; comp = comp->next) { for (comp = comps_head; comp; comp = comp->next) {
if (comp->Reset) comp->Reset(); if (comp->Reset) comp->Reset();
@ -331,18 +331,13 @@ static void Game_WarnFunc(const String* msg) {
} }
static void Game_ExtractInitialTexturePack(void) { static void Game_ExtractInitialTexturePack(void) {
String texPack; char texPackBuffer[STRING_SIZE]; String texPack;
Options_Get(OPT_DEFAULT_TEX_PACK, &game_defTexPack, "default.zip"); Options_Get(OPT_DEFAULT_TEX_PACK, &game_defTexPack, "default.zip");
String_InitArray(texPack, texPackBuffer); TexturePack_ExtractZip_File(&defaultZip);
String_AppendConst(&texPack, "default.zip");
TexturePack_ExtractZip_File(&texPack);
texPack.length = 0;
/* in case the user's default texture pack doesn't have all required textures */ /* in case the user's default texture pack doesn't have all required textures */
Game_GetDefaultTexturePack(&texPack); texPack = Game_UNSAFE_GetDefaultTexturePack();
if (!String_CaselessEqualsConst(&texPack, "default.zip")) { if (!String_CaselessEquals(&texPack, "default.zip")) {
TexturePack_ExtractZip_File(&texPack); TexturePack_ExtractZip_File(&texPack);
} }
} }

View File

@ -61,7 +61,7 @@ float Game_GetChatScale(void);
/* Retrieves the filename of the default texture pack used. */ /* Retrieves the filename of the default texture pack used. */
/* NOTE: Returns default.zip if classic mode or selected pack does not exist. */ /* NOTE: Returns default.zip if classic mode or selected pack does not exist. */
void Game_GetDefaultTexturePack(String* texPack); String Game_UNSAFE_GetDefaultTexturePack(void);
/* Sets the filename of the default texture pack used. */ /* Sets the filename of the default texture pack used. */
void Game_SetDefaultTexturePack(const String* texPack); void Game_SetDefaultTexturePack(const String* texPack);

View File

@ -1375,7 +1375,9 @@ static void TexturePackScreen_EntryClick(void* screen, void* widget) {
idx = s->currentIndex; idx = s->currentIndex;
Game_SetDefaultTexturePack(&filename); Game_SetDefaultTexturePack(&filename);
TexturePack_ExtractDefault(); World_TextureUrl.length = 0;
TexturePack_ExtractCurrent(true);
Elem_Recreate(s); Elem_Recreate(s);
ListScreen_SetCurrentIndex(s, idx); ListScreen_SetCurrentIndex(s, idx);
} }
@ -2407,18 +2409,8 @@ static void GraphicsOptionsScreen_SetShadows(const String* v) {
static void GraphicsOptionsScreen_GetMipmaps(String* v) { Menu_GetBool(v, Gfx.Mipmaps); } static void GraphicsOptionsScreen_GetMipmaps(String* v) { Menu_GetBool(v, Gfx.Mipmaps); }
static void GraphicsOptionsScreen_SetMipmaps(const String* v) { static void GraphicsOptionsScreen_SetMipmaps(const String* v) {
String url; char urlBuffer[STRING_SIZE];
Gfx.Mipmaps = Menu_SetBool(v, OPT_MIPMAPS); Gfx.Mipmaps = Menu_SetBool(v, OPT_MIPMAPS);
String_InitArray(url, urlBuffer); TexturePack_ExtractCurrent(true);
String_Copy(&url, &World_TextureUrl);
/* always force a reload from cache */
World_TextureUrl.length = 0;
String_AppendConst(&World_TextureUrl, "~`#$_^*()@");
TexturePack_ExtractCurrent(&url);
String_Copy(&World_TextureUrl, &url);
} }
static void GraphicsOptionsScreen_ContextRecreated(void* screen) { static void GraphicsOptionsScreen_ContextRecreated(void* screen) {
@ -3194,7 +3186,7 @@ static void TexPackOverlay_YesClick(void* screen, void* widget) {
struct TexPackOverlay* s = (struct TexPackOverlay*)screen; struct TexPackOverlay* s = (struct TexPackOverlay*)screen;
String url = String_UNSAFE_SubstringAt(&s->identifier, 3); String url = String_UNSAFE_SubstringAt(&s->identifier, 3);
Server_DownloadTexturePack(&url); World_ApplyTexturePack(&url);
if (WarningOverlay_IsAlways(s, widget)) TextureCache_Accept(&url); if (WarningOverlay_IsAlways(s, widget)) TextureCache_Accept(&url);
Elem_Free(s); Elem_Free(s);
} }

View File

@ -43,23 +43,13 @@ static void Server_ResetState(void) {
void Server_RetrieveTexturePack(const String* url) { void Server_RetrieveTexturePack(const String* url) {
if (!Game_AllowServerTextures || TextureCache_HasDenied(url)) return; if (!Game_AllowServerTextures || TextureCache_HasDenied(url)) return;
if (!url->length) { if (!url->length || TextureCache_HasAccepted(url)) {
/* Only extract default texture pack when necessary */ World_ApplyTexturePack(url);
if (World_TextureUrl.length) TexturePack_ExtractDefault();
} else if (TextureCache_HasAccepted(url)) {
Server_DownloadTexturePack(url);
} else { } else {
Gui_ShowOverlay(TexPackOverlay_MakeInstance(url)); Gui_ShowOverlay(TexPackOverlay_MakeInstance(url));
} }
} }
void Server_DownloadTexturePack(const String* url) {
static const String texPack = String_FromConst("texturePack");
TexturePack_ExtractCurrent(url);
TexturePack_DownloadAsync(url, &texPack);
String_Copy(&World_TextureUrl, url);
}
static void Server_CheckAsyncResources(void) { static void Server_CheckAsyncResources(void) {
static const String texPack = String_FromConst("texturePack"); static const String texPack = String_FromConst("texturePack");
struct HttpRequest item; struct HttpRequest item;

View File

@ -96,13 +96,9 @@ CC_VAR extern struct _ServerConnectionData {
int Port; int Port;
} Server; } Server;
/* If the user hasn't previously accepted url, displays a dialog asking to confirm downloading it. */ /* If user hasn't previously accepted url, displays a dialog asking to confirm downloading it. */
/* Otherwise just calls Server_DownloadTexturePack. */ /* Otherwise just calls World_ApplyTexturePack. */
void Server_RetrieveTexturePack(const String* url); void Server_RetrieveTexturePack(const String* url);
/* Queues the given URL to be asynchronously downloaded. */
/* The texture pack will later be loaded in Server.Tick, once it has finished downloading. */
/* NOTE: If a cached texture pack exists for the given URL, that gets immediately loaded. */
void Server_DownloadTexturePack(const String* url);
typedef void (*Net_Handler)(uint8_t* data); typedef void (*Net_Handler)(uint8_t* data);
extern uint16_t Net_PacketSizes[OPCODE_COUNT]; extern uint16_t Net_PacketSizes[OPCODE_COUNT];

View File

@ -656,7 +656,7 @@ static ReturnCode TexturePack_ExtractZip(struct Stream* stream) {
/* Changes the current terrain atlas from a stream representing a .png image */ /* Changes the current terrain atlas from a stream representing a .png image */
/* Raises TextureEvents.PackChanged, so behaves as a .zip with only terrain.png in it */ /* Raises TextureEvents.PackChanged, so behaves as a .zip with only terrain.png in it */
static ReturnCode TexturePack_ExtractTerrainPng(struct Stream* stream) { static ReturnCode TexturePack_ExtractPng(struct Stream* stream) {
Bitmap bmp; Bitmap bmp;
ReturnCode res = Png_Decode(&bmp, stream); ReturnCode res = Png_Decode(&bmp, stream);
@ -701,39 +701,29 @@ void TexturePack_ExtractZip_File(const String* filename) {
#endif #endif
} }
void TexturePack_ExtractDefault(void) { static bool texturePackDefault = true;
String texPack; char texPackBuffer[STRING_SIZE]; void TexturePack_ExtractCurrent(bool forceReload) {
String_InitArray(texPack, texPackBuffer);
Game_GetDefaultTexturePack(&texPack);
TexturePack_ExtractZip_File(&texPack);
World_TextureUrl.length = 0;
}
void TexturePack_ExtractCurrent(const String* url) {
static const String zipExt = String_FromConst(".zip"); static const String zipExt = String_FromConst(".zip");
String url = World_TextureUrl, file;
struct Stream stream; struct Stream stream;
bool zip; bool zip;
ReturnCode res = 0; ReturnCode res;
if (!url->length) { if (!url.length || !TextureCache_Get(&url, &stream)) {
TexturePack_ExtractDefault(); /* don't pointlessly load default texture pack */
} else if (!TextureCache_Get(url, &stream)) { if (texturePackDefault && !forceReload) return;
/* e.g. 404 errors */ file = Game_UNSAFE_GetDefaultTexturePack();
if (World_TextureUrl.length) TexturePack_ExtractDefault();
TexturePack_ExtractZip_File(&file);
texturePackDefault = true;
} else { } else {
if (!String_Equals(url, &World_TextureUrl)) { zip = String_ContainsString(&url, &zipExt);
zip = String_ContainsString(url, &zipExt); res = zip ? TexturePack_ExtractZip(&stream) : TexturePack_ExtractPng(&stream);
String_Copy(&World_TextureUrl, url); if (res) Logger_Warn2(res, zip ? "extracting" : "decoding", &url);
res = zip ? TexturePack_ExtractZip(&stream) :
TexturePack_ExtractTerrainPng(&stream);
if (res) Logger_Warn2(res, zip ? "extracting" : "decoding", url);
}
res = stream.Close(&stream); res = stream.Close(&stream);
if (res) Logger_Warn2(res, "closing cache for", url); if (res) Logger_Warn2(res, "closing cache for", &url);
texturePackDefault = false;
} }
} }
@ -748,13 +738,13 @@ void TexturePack_Extract_Req(struct HttpRequest* item) {
String_Copy(&World_TextureUrl, &url); String_Copy(&World_TextureUrl, &url);
data = item->Data; data = item->Data;
len = item->Size; len = item->Size;
Stream_ReadonlyMemory(&mem, data, len); Stream_ReadonlyMemory(&mem, data, len);
png = Png_Detect(data, len); png = Png_Detect(data, len);
res = png ? TexturePack_ExtractTerrainPng(&mem) res = png ? TexturePack_ExtractPng(&mem) : TexturePack_ExtractZip(&mem);
: TexturePack_ExtractZip(&mem);
if (res) Logger_Warn2(res, png ? "decoding" : "extracting", &url); if (res) Logger_Warn2(res, png ? "decoding" : "extracting", &url);
texturePackDefault = false;
} }
void TexturePack_DownloadAsync(const String* url, const String* id) { void TexturePack_DownloadAsync(const String* url, const String* id) {

View File

@ -92,13 +92,9 @@ void TextureCache_Update(struct HttpRequest* req);
/* Extracts a texture pack .zip from the given file. */ /* Extracts a texture pack .zip from the given file. */
void TexturePack_ExtractZip_File(const String* filename); void TexturePack_ExtractZip_File(const String* filename);
/* Extracts user's default texture pack, then resets World_TextureUrl. */ /* If World_TextureUrl is empty, extracts user's default texture pack. */
void TexturePack_ExtractDefault(void); /* Otherwise extracts the cached texture pack for that URL. */
/* Extracts the current texture pack or terrain.png. 3 cases: */ void TexturePack_ExtractCurrent(bool forceReload);
/* - Server has not set a URL, so just extract default */
/* - URL is in texture cache, so extract cached version */
/* - URL is not cached, so extract default for now */
void TexturePack_ExtractCurrent(const String* url);
/* Asynchronously downloads a texture pack. */ /* Asynchronously downloads a texture pack. */
/* Sends ETag and Last-Modified to webserver to avoid redundant downloads. */ /* Sends ETag and Last-Modified to webserver to avoid redundant downloads. */
/* NOTE: This does not load cached textures - use TexturePack_ExtractCurrent for that. */ /* NOTE: This does not load cached textures - use TexturePack_ExtractCurrent for that. */

View File

@ -138,8 +138,11 @@ String World_TextureUrl = String_FromArray(World_TextureUrlBuffer);
void World_ApplyTexturePack(const String* url) { void World_ApplyTexturePack(const String* url) {
static const String texPack = String_FromConst("texturePack"); static const String texPack = String_FromConst("texturePack");
TexturePack_ExtractCurrent(url); if (url->length) TexturePack_DownloadAsync(url, &texPack);
TexturePack_DownloadAsync(url, &texPack);
if (String_Equals(url, &World_TextureUrl)) return;
String_Copy(&World_TextureUrl, url);
TexturePack_ExtractCurrent(false);
} }
void Env_Reset(void) { void Env_Reset(void) {

View File

@ -123,8 +123,9 @@ extern const PackedCol Env_DefaultSunCol, Env_DefaultShadowCol;
#define ENV_DEFAULT_SUNCOL_HEX "FFFFFF" #define ENV_DEFAULT_SUNCOL_HEX "FFFFFF"
#define ENV_DEFAULT_SHADOWCOL_HEX "9B9B9B" #define ENV_DEFAULT_SHADOWCOL_HEX "9B9B9B"
/* Extracts texture pack cached for the given URL (or default.zip if not), */ /* If url is empty, extracts default texture pack. */
/* then asynchronously downloads the data for the given URL. */ /* Else tries extracting cached texture pack for the given URL, */
/* then asynchronously downloads the texture pack from the given URL. */
CC_API void World_ApplyTexturePack(const String* url); CC_API void World_ApplyTexturePack(const String* url);
/* Resets all environment settings to default. */ /* Resets all environment settings to default. */
/* NOTE: Unlike Env_Set functions, DOES NOT raise EnvVarChanged event. */ /* NOTE: Unlike Env_Set functions, DOES NOT raise EnvVarChanged event. */