From 1d438067cece4c843090a8916f756e602f929aa6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 24 Jun 2019 12:25:28 +1000 Subject: [PATCH] Fix flags not showing in server list due to there being over 10 different countries --- src/LScreens.c | 2 +- src/LWeb.c | 49 ++++++++++++++++++++++++++----------------------- src/LWeb.h | 12 +++++++----- src/LWidgets.c | 2 +- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/LScreens.c b/src/LScreens.c index d02cc6359..7dd0c9089 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -1212,7 +1212,7 @@ static void ServersScreen_ReloadServers(struct ServersScreen* s) { LTable_Sort(&s->Table); for (i = 0; i < FetchServersTask.NumServers; i++) { - FetchFlagsTask_Add(&FetchServersTask.Servers[i].country); + FetchFlagsTask_Add(&FetchServersTask.Servers[i]); } } diff --git a/src/LWeb.c b/src/LWeb.c index 41aa05311..c351216e9 100644 --- a/src/LWeb.c +++ b/src/LWeb.c @@ -336,15 +336,15 @@ static void ServerInfo_Init(struct ServerInfo* info) { String_InitArray(info->hash, info->_hashBuffer); String_InitArray(info->name, info->_nameBuffer); String_InitArray(info->ip, info->_ipBuffer); - String_InitArray(info->mppass, info->_mppassBuffer); String_InitArray(info->software, info->_softBuffer); - String_InitArray(info->country, info->_countryBuffer); info->players = 0; info->maxPlayers = 0; info->uptime = 0; info->featured = false; + info->country[0] = 't'; + info->country[1] = '1'; /* 'T1' for unrecognised country */ info->_order = -100000; } @@ -372,7 +372,11 @@ static void ServerInfo_Parse(struct JsonContext* ctx, const String* val) { Convert_ParseBool(val, &info->featured); } else if (String_CaselessEqualsConst(&ctx->curKey, "country_abbr")) { /* Two letter country codes, see ISO 3166-1 alpha-2 */ - String_Copy(&info->country, val); + if (val->length < 2) return; + + /* classicube.net only works with lowercase flag urls */ + info->country[0] = val->buffer[0]; Char_MakeLower(info->country[0]); + info->country[1] = val->buffer[1]; Char_MakeLower(info->country[1]); } } @@ -576,10 +580,10 @@ void FetchUpdateTask_Run(bool release, bool d3d9) { *#########################################################################################################################*/ struct FetchFlagsData FetchFlagsTask; static int flagsCount, flagsCapacity; + struct Flag { - Bitmap Bmp; - String Name; - char _nameBuffer[2]; + Bitmap bmp; + char country[2]; }; static struct Flag* flags; @@ -589,7 +593,7 @@ static void FetchFlagsTask_Handle(uint8_t* data, uint32_t len) { ReturnCode res; Stream_ReadonlyMemory(&s, data, len); - res = Png_Decode(&flags[FetchFlagsTask.Count].Bmp, &s); + res = Png_Decode(&flags[FetchFlagsTask.Count].bmp, &s); if (res) Logger_Warn(res, "decoding flag"); FetchFlagsTask.Count++; @@ -605,7 +609,8 @@ static void FetchFlagsTask_DownloadNext(void) { if (FetchFlagsTask.Count == flagsCount) return; LWebTask_Reset(&FetchFlagsTask.Base); - String_Format1(&url, "http://static.classicube.net/img/flags/%s.png", &flags[FetchFlagsTask.Count].Name); + String_Format2(&url, "http://static.classicube.net/img/flags/%r%r.png", + &flags[FetchFlagsTask.Count].country[0], &flags[FetchFlagsTask.Count].country[1]); FetchFlagsTask.Base.Identifier = id; Http_AsyncGetData(&url, false, &id); @@ -623,34 +628,32 @@ static void FetchFlagsTask_Ensure(void) { } } -void FetchFlagsTask_Add(const String* name) { +void FetchFlagsTask_Add(const struct ServerInfo* server) { char c; int i; for (i = 0; i < flagsCount; i++) { - if (String_CaselessEquals(name, &flags[i].Name)) return; + if (flags[i].country[0] != server->country[0]) continue; + if (flags[i].country[1] != server->country[1]) continue; + /* flag is already or will be downloaded */ + return; } FetchFlagsTask_Ensure(); - - Bitmap_Init(flags[flagsCount].Bmp, 0, 0, NULL); - String_InitArray(flags[flagsCount].Name, flags[flagsCount]._nameBuffer); - /* classicube.net only works with lowercase flag urls */ - for (i = 0; i < name->length; i++) { - c = name->buffer[i]; - Char_MakeLower(c); - String_Append(&flags[flagsCount].Name, c); - } + Bitmap_Init(flags[flagsCount].bmp, 0, 0, NULL); + flags[flagsCount].country[0] = server->country[0]; + flags[flagsCount].country[1] = server->country[1]; flagsCount++; FetchFlagsTask_DownloadNext(); } -Bitmap* Flags_Get(const String* name) { +Bitmap* Flags_Get(const struct ServerInfo* server) { int i; for (i = 0; i < FetchFlagsTask.Count; i++) { - if (!String_CaselessEquals(name, &flags[i].Name)) continue; - return &flags[i].Bmp; + if (flags[i].country[0] != server->country[0]) continue; + if (flags[i].country[1] != server->country[1]) continue; + return &flags[i].bmp; } return NULL; } @@ -658,7 +661,7 @@ Bitmap* Flags_Get(const String* name) { void Flags_Free(void) { int i; for (i = 0; i < FetchFlagsTask.Count; i++) { - Mem_Free(flags[i].Bmp.Scan0); + Mem_Free(flags[i].bmp.Scan0); } } #endif diff --git a/src/LWeb.h b/src/LWeb.h index c9759ea80..2a9d35fbf 100644 --- a/src/LWeb.h +++ b/src/LWeb.h @@ -31,13 +31,14 @@ void Json_Parse(struct JsonContext* ctx); /* Represents all known details about a server. */ struct ServerInfo { - String hash, name, ip, mppass, software, country; + String hash, name, ip, mppass, software; int players, maxPlayers, port, uptime; bool featured; + char country[2]; int _order; /* (internal) order in servers table after filtering */ char _hashBuffer[32], _nameBuffer[STRING_SIZE]; char _ipBuffer[16], _mppassBuffer[STRING_SIZE]; - char _countryBuffer[2], _softBuffer[STRING_SIZE]; + char _softBuffer[STRING_SIZE]; }; struct LWebTask; @@ -113,9 +114,10 @@ extern struct FetchFlagsData { /* Number of flags downloaded. */ int Count; } FetchFlagsTask; -void FetchFlagsTask_Add(const String* name); -/* Gets the downloaded bitmap for the given flag. */ -Bitmap* Flags_Get(const String* name); +/* Asynchronously downloads the flag associated with the given server's country. */ +void FetchFlagsTask_Add(const struct ServerInfo* server); +/* Gets the bitmap for the flag associated with the given server's country. */ +Bitmap* Flags_Get(const struct ServerInfo* server); /* Frees all flag bitmaps. */ void Flags_Free(void); #endif diff --git a/src/LWidgets.c b/src/LWidgets.c index d224b8d54..10cdc746e 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -634,7 +634,7 @@ void LSlider_Init(struct LSlider* w, int width, int height) { *------------------------------------------------------TableWidget--------------------------------------------------------* *#########################################################################################################################*/ static void FlagColumn_Draw(struct ServerInfo* row, struct DrawTextArgs* args, int x, int y) { - Bitmap* bmp = Flags_Get(&row->country); + Bitmap* bmp = Flags_Get(row); if (bmp) Drawer2D_BmpCopy(&Launcher_Framebuffer, x + 2, y + 6, bmp); }