mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-14 10:05:44 -04:00
Tidy up http/texture pack code a little bit
This commit is contained in:
parent
65a9cba5be
commit
75a645aacc
15
src/Http.c
15
src/Http.c
@ -62,25 +62,21 @@ static void RequestList_RemoveAt(struct RequestList* list, int i) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Finds index of request whose id matches the given id */
|
/* 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;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < list->count; i++) {
|
for (i = 0; i < list->count; i++) {
|
||||||
if (id != list->entries[i].id) continue;
|
if (id != list->entries[i].id) continue;
|
||||||
|
|
||||||
*item = list->entries[i];
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;
|
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) {
|
static void RequestList_TryFree(struct RequestList* list, int id) {
|
||||||
struct HttpRequest req;
|
int i = RequestList_Find(list, id);
|
||||||
int i = RequestList_Find(list, id, &req);
|
|
||||||
if (i < 0) return;
|
if (i < 0) return;
|
||||||
|
|
||||||
Mem_Free(req.data);
|
Mem_Free(list->entries[i].data);
|
||||||
RequestList_RemoveAt(list, i);
|
RequestList_RemoveAt(list, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1026,7 +1022,8 @@ cc_bool Http_GetResult(int reqID, struct HttpRequest* item) {
|
|||||||
int i;
|
int i;
|
||||||
Mutex_Lock(processedMutex);
|
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);
|
if (i >= 0) RequestList_RemoveAt(&processedReqs, i);
|
||||||
}
|
}
|
||||||
Mutex_Unlock(processedMutex);
|
Mutex_Unlock(processedMutex);
|
||||||
|
20
src/Server.c
20
src/Server.c
@ -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---------------------------------------------------------*
|
*--------------------------------------------------------PingList---------------------------------------------------------*
|
||||||
@ -199,7 +183,7 @@ static void SPConnection_Tick(struct ScheduledTask* task) {
|
|||||||
if (Server.Disconnected) return;
|
if (Server.Disconnected) return;
|
||||||
if ((ticks % 3) == 0) { /* 60 -> 20 ticks a second */
|
if ((ticks % 3) == 0) { /* 60 -> 20 ticks a second */
|
||||||
Physics_Tick();
|
Physics_Tick();
|
||||||
Server_CheckAsyncResources();
|
TexturePack_CheckPending();
|
||||||
}
|
}
|
||||||
ticks++;
|
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 */
|
/* Network is ticked 60 times a second. We only send position updates 20 times a second */
|
||||||
if ((ticks % 3) == 0) {
|
if ((ticks % 3) == 0) {
|
||||||
Server_CheckAsyncResources();
|
TexturePack_CheckPending();
|
||||||
Protocol_Tick();
|
Protocol_Tick();
|
||||||
/* Have any packets been written? */
|
/* Have any packets been written? */
|
||||||
if (Server.WriteBuffer != net_writeBuffer) Net_SendPacket();
|
if (Server.WriteBuffer != net_writeBuffer) Net_SendPacket();
|
||||||
|
@ -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;
|
struct Stream mem;
|
||||||
cc_string url;
|
cc_string url;
|
||||||
|
|
||||||
@ -396,6 +397,22 @@ void TexturePack_Apply(struct HttpRequest* item) {
|
|||||||
usingDefault = false;
|
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 */
|
/* Asynchronously downloads the given texture pack */
|
||||||
static void DownloadAsync(const cc_string* url) {
|
static void DownloadAsync(const cc_string* url) {
|
||||||
cc_string etag = String_Empty;
|
cc_string etag = String_Empty;
|
||||||
|
@ -87,16 +87,16 @@ void TextureCache_Deny(const cc_string* url);
|
|||||||
/* Clears the list of denied URLs, returning number removed. */
|
/* Clears the list of denied URLs, returning number removed. */
|
||||||
int TextureCache_ClearDenied(void);
|
int TextureCache_ClearDenied(void);
|
||||||
|
|
||||||
/* ID of texture pack http request */
|
/* Request ID of texture pack currently being downloaded */
|
||||||
extern int TexturePack_ReqID;
|
extern int TexturePack_ReqID;
|
||||||
/* Sets the filename of the default texture pack used. */
|
/* Sets the filename of the default texture pack used. */
|
||||||
void TexturePack_SetDefault(const cc_string* texPack);
|
void TexturePack_SetDefault(const cc_string* texPack);
|
||||||
/* If TexturePack_Url is empty, extracts user's default texture pack. */
|
/* If TexturePack_Url is empty, extracts user's default texture pack. */
|
||||||
/* Otherwise extracts the cached texture pack for that URL. */
|
/* Otherwise extracts the cached texture pack for that URL. */
|
||||||
void TexturePack_ExtractCurrent(cc_bool forceReload);
|
void TexturePack_ExtractCurrent(cc_bool forceReload);
|
||||||
/* Extracts a texture pack or terrain.png from given downloaded data. */
|
/* Checks if the texture pack currently being downloaded has completed. */
|
||||||
/* Also updates cached data associated with the given URL. */
|
/* If completed, then applies the downloaded texture pack and updates cache */
|
||||||
void TexturePack_Apply(struct HttpRequest* item);
|
void TexturePack_CheckPending(void);
|
||||||
/* If url is empty, extracts default texture pack. */
|
/* If url is empty, extracts default texture pack. */
|
||||||
/* Else tries extracting cached texture pack for the given URL, */
|
/* Else tries extracting cached texture pack for the given URL, */
|
||||||
/* then asynchronously downloads the texture pack from the given URL. */
|
/* then asynchronously downloads the texture pack from the given URL. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user