From ddf7b10d912a3c22f5f1f9dbfabdeb420f66943d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 5 Sep 2020 16:04:08 +1000 Subject: [PATCH] Move towards using Http_CheckProgress instead of Http_GetCurrent --- src/Http.c | 17 +++++++++++++++-- src/Http.h | 6 +++++- src/LScreens.c | 6 +----- src/Screens.c | 21 ++++----------------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Http.c b/src/Http.c index 42fcbd6bb..f612616a1 100644 --- a/src/Http.c +++ b/src/Http.c @@ -99,7 +99,7 @@ static volatile cc_bool http_terminate; static struct RequestList pendingReqs; static struct RequestList processedReqs; static struct HttpRequest http_curRequest; -static volatile int http_curProgress = HTTP_PROGRESS_NOTHING; +static volatile int http_curProgress = HTTP_PROGRESS_NOT_WORKING_ON; #ifdef CC_BUILD_WEB static void Http_DownloadNextAsync(void); @@ -219,7 +219,7 @@ static void Http_FinishRequest(struct HttpRequest* req) { Mutex_Lock(curRequestMutex); { http_curRequest.id = 0; - http_curProgress = HTTP_PROGRESS_NOTHING; + http_curProgress = HTTP_PROGRESS_NOT_WORKING_ON; } Mutex_Unlock(curRequestMutex); } @@ -1064,6 +1064,19 @@ cc_bool Http_GetCurrent(struct HttpRequest* request, int* progress) { return request->id != 0; } +int Http_CheckProgress(int reqID) { + int curReqID, progress; + Mutex_Lock(curRequestMutex); + { + curReqID = http_curRequest.id; + progress = http_curProgress; + } + Mutex_Unlock(curRequestMutex); + + if (reqID != curReqID) progress = HTTP_PROGRESS_NOT_WORKING_ON; + return progress; +} + void Http_ClearPending(void) { Mutex_Lock(pendingMutex); { diff --git a/src/Http.h b/src/Http.h index e1d914bac..f330c72e9 100644 --- a/src/Http.h +++ b/src/Http.h @@ -14,7 +14,7 @@ extern struct IGameComponent Http_Component; enum HttpRequestType { REQUEST_TYPE_GET, REQUEST_TYPE_HEAD, REQUEST_TYPE_POST }; enum HttpProgress { - HTTP_PROGRESS_NOTHING = -3, + HTTP_PROGRESS_NOT_WORKING_ON = -3, HTTP_PROGRESS_MAKING_REQUEST = -2, HTTP_PROGRESS_FETCHING_DATA = -1 }; @@ -73,6 +73,10 @@ cc_bool Http_DescribeError(cc_result res, String* dst); cc_bool Http_GetResult(int reqID, struct HttpRequest* item); /* Retrieves information about the request currently being processed. */ cc_bool Http_GetCurrent(struct HttpRequest* request, int* progress); +/* Retrieves information about the download progress of the given request. */ +/* NOTE: This may return HTTP_PROGRESS_NOT_WORKING_ON if download has finished. */ +/* As such, this method should always be paired with a call to Http_GetResult. */ +int Http_CheckProgress(int reqID); /* Clears the list of pending requests. */ void Http_ClearPending(void); #endif diff --git a/src/LScreens.c b/src/LScreens.c index e178c1421..4a196872d 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -1529,11 +1529,7 @@ static void UpdatesScreen_CheckTick(struct UpdatesScreen* s) { static void UpdatesScreen_UpdateProgress(struct UpdatesScreen* s, struct LWebTask* task) { String str; char strBuffer[STRING_SIZE]; - struct HttpRequest item; - int progress; - if (!Http_GetCurrent(&item, &progress)) return; - - if (item.id != task->reqID) return; + int progress = Http_CheckProgress(task->reqID); if (progress == s->buildProgress) return; s->buildProgress = progress; diff --git a/src/Screens.c b/src/Screens.c index 107711a88..7df03feba 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -850,22 +850,9 @@ static void ChatScreen_EnterChatInput(struct ChatScreen* s, cc_bool close) { } static void ChatScreen_UpdateTexpackStatus(struct ChatScreen* s) { - static const String texPack = String_FromConst("texturePack"); - struct HttpRequest request; - int progress; - cc_bool hasRequest = Http_GetCurrent(&request, &progress); - - /* Is terrain/texture pack currently being downloaded? */ - if (!hasRequest || request.id != TexturePack_ReqID) { - if (s->status.textures[0].ID) { - Chat_Status[0].length = 0; - TextGroupWidget_Redraw(&s->status, 0); - } - s->lastDownloadStatus = Int32_MinValue; - return; - } - + int progress = Http_CheckProgress(TexturePack_ReqID); if (progress == s->lastDownloadStatus) return; + s->lastDownloadStatus = progress; Chat_Status[0].length = 0; @@ -1258,8 +1245,8 @@ static const struct ScreenVTABLE ChatScreen_VTABLE = { ChatScreen_Layout, ChatScreen_ContextLost, ChatScreen_ContextRecreated }; void ChatScreen_Show(void) { - struct ChatScreen* s = &ChatScreen_Instance; - s->lastDownloadStatus = Int32_MinValue; + struct ChatScreen* s = &ChatScreen_Instance; + s->lastDownloadStatus = HTTP_PROGRESS_NOT_WORKING_ON; s->VTABLE = &ChatScreen_VTABLE; Gui_Chat = s;