Tidy up http/texture pack code a little bit

This commit is contained in:
UnknownShadow200 2021-05-08 12:03:22 +10:00
parent 65a9cba5be
commit 75a645aacc
4 changed files with 30 additions and 32 deletions

View File

@ -62,25 +62,21 @@ static void RequestList_RemoveAt(struct RequestList* list, int i) {
}
/* Finds index of request whose id matches the given id */
static int RequestList_Find(struct RequestList* list, int id, struct HttpRequest* item) {
static int RequestList_Find(struct RequestList* list, int id) {
int i;
for (i = 0; i < list->count; i++) {
if (id != list->entries[i].id) continue;
*item = list->entries[i];
return i;
}
return -1;
}
/* Tries to remove and free given request. */
/* Tries to remove and free given request */
static void RequestList_TryFree(struct RequestList* list, int id) {
struct HttpRequest req;
int i = RequestList_Find(list, id, &req);
int i = RequestList_Find(list, id);
if (i < 0) return;
Mem_Free(req.data);
Mem_Free(list->entries[i].data);
RequestList_RemoveAt(list, i);
}
@ -1026,7 +1022,8 @@ cc_bool Http_GetResult(int reqID, struct HttpRequest* item) {
int i;
Mutex_Lock(processedMutex);
{
i = RequestList_Find(&processedReqs, reqID, item);
i = RequestList_Find(&processedReqs, reqID);
if (i >= 0) *item = processedReqs.entries[i];
if (i >= 0) RequestList_RemoveAt(&processedReqs, i);
}
Mutex_Unlock(processedMutex);

View File

@ -51,22 +51,6 @@ void Server_RetrieveTexturePack(const cc_string* url) {
}
}
static void Server_CheckAsyncResources(void) {
struct HttpRequest item;
if (!Http_GetResult(TexturePack_ReqID, &item)) return;
if (item.success) {
TexturePack_Apply(&item);
Mem_Free(item.data);
} else if (item.result) {
Logger_Warn(item.result, "trying to download texture pack", Http_DescribeError);
} else {
int status = item.statusCode;
if (status == 200 || status == 304) return;
Chat_Add1("&c%i error when trying to download texture pack", &status);
}
}
/*########################################################################################################################*
*--------------------------------------------------------PingList---------------------------------------------------------*
@ -199,7 +183,7 @@ static void SPConnection_Tick(struct ScheduledTask* task) {
if (Server.Disconnected) return;
if ((ticks % 3) == 0) { /* 60 -> 20 ticks a second */
Physics_Tick();
Server_CheckAsyncResources();
TexturePack_CheckPending();
}
ticks++;
}
@ -446,7 +430,7 @@ static void MPConnection_Tick(struct ScheduledTask* task) {
/* Network is ticked 60 times a second. We only send position updates 20 times a second */
if ((ticks % 3) == 0) {
Server_CheckAsyncResources();
TexturePack_CheckPending();
Protocol_Tick();
/* Have any packets been written? */
if (Server.WriteBuffer != net_writeBuffer) Net_SendPacket();

View File

@ -382,7 +382,8 @@ void TexturePack_ExtractCurrent(cc_bool forceReload) {
}
}
void TexturePack_Apply(struct HttpRequest* item) {
/* Extracts and updates cache for the downloaded texture pack */
static void ApplyDownloaded(struct HttpRequest* item) {
struct Stream mem;
cc_string url;
@ -396,6 +397,22 @@ void TexturePack_Apply(struct HttpRequest* item) {
usingDefault = false;
}
void TexturePack_CheckPending(void) {
struct HttpRequest item;
if (!Http_GetResult(TexturePack_ReqID, &item)) return;
if (item.success) {
ApplyDownloaded(&item);
Mem_Free(item.data);
} else if (item.result) {
Logger_Warn(item.result, "trying to download texture pack", Http_DescribeError);
} else {
int status = item.statusCode;
if (status == 200 || status == 304) return;
Chat_Add1("&c%i error when trying to download texture pack", &status);
}
}
/* Asynchronously downloads the given texture pack */
static void DownloadAsync(const cc_string* url) {
cc_string etag = String_Empty;

View File

@ -87,16 +87,16 @@ void TextureCache_Deny(const cc_string* url);
/* Clears the list of denied URLs, returning number removed. */
int TextureCache_ClearDenied(void);
/* ID of texture pack http request */
/* Request ID of texture pack currently being downloaded */
extern int TexturePack_ReqID;
/* Sets the filename of the default texture pack used. */
void TexturePack_SetDefault(const cc_string* texPack);
/* If TexturePack_Url is empty, extracts user's default texture pack. */
/* Otherwise extracts the cached texture pack for that URL. */
void TexturePack_ExtractCurrent(cc_bool forceReload);
/* Extracts a texture pack or terrain.png from given downloaded data. */
/* Also updates cached data associated with the given URL. */
void TexturePack_Apply(struct HttpRequest* item);
/* Checks if the texture pack currently being downloaded has completed. */
/* If completed, then applies the downloaded texture pack and updates cache */
void TexturePack_CheckPending(void);
/* If url is empty, extracts default texture pack. */
/* Else tries extracting cached texture pack for the given URL, */
/* then asynchronously downloads the texture pack from the given URL. */