From 98d2ce3e31caf7ff11c985f76c7eb8f269808e8b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 9 Nov 2021 16:34:14 +1100 Subject: [PATCH] Refactor updater builds to be generic and not so tied to direct3d9/opengl and ifdef --- src/LScreens.c | 62 +++++++++++++++++++++--------------------- src/LWeb.c | 6 ++-- src/LWeb.h | 2 +- src/Platform.h | 13 +++++++-- src/Platform_Android.c | 5 ++-- src/Platform_Posix.c | 16 +++++------ src/Platform_Web.c | 3 +- src/Platform_WinApi.c | 13 ++++++--- 8 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/LScreens.c b/src/LScreens.c index 712825461..04bdbdbeb 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -1565,9 +1565,8 @@ static struct UpdatesScreen { struct LButton btnRel[2], btnDev[2], btnBack; struct LLabel lblYour, lblRel, lblDev, lblInfo, lblStatus; struct LWidget* _widgets[12]; - const char* buildName; - int buildProgress; - cc_bool pendingFetch, release, d3d9; + int buildProgress, buildIndex; + cc_bool pendingFetch, release; } UpdatesScreen_Instance; CC_NOINLINE static void UpdatesScreen_FormatTime(cc_string* str, int delta) { @@ -1618,29 +1617,33 @@ static void UpdatesScreen_FormatBoth(struct UpdatesScreen* s) { UpdatesScreen_Format(&s->lblDev, "Latest dev build: ", CheckUpdateTask.devTimestamp); } +static void UpdatesScreen_UpdateHeader(struct UpdatesScreen* s, cc_string* str) { + const char* message; + if ( s->release) message = "&eFetching latest release "; + if (!s->release) message = "&eFetching latest dev build "; + + String_Format2(str, "%c%c", message, Updater_Info.builds[s->buildIndex].name); +} + static void UpdatesScreen_DoFetch(struct UpdatesScreen* s) { cc_string str; char strBuffer[STRING_SIZE]; cc_uint64 time; time = s->release ? CheckUpdateTask.relTimestamp : CheckUpdateTask.devTimestamp; if (!time || FetchUpdateTask.Base.working) return; - FetchUpdateTask_Run(s->release, s->d3d9); - s->pendingFetch = false; - - if ( s->release && s->d3d9) s->buildName = "&eFetching latest release (Direct3D9)"; - if ( s->release && !s->d3d9) s->buildName = "&eFetching latest release (OpenGL)"; - if (!s->release && s->d3d9) s->buildName = "&eFetching latest dev build (Direct3D9)"; - if (!s->release && !s->d3d9) s->buildName = "&eFetching latest dev build (OpenGL)"; + FetchUpdateTask_Run(s->release, s->buildIndex); + s->pendingFetch = false; s->buildProgress = -1; String_InitArray(str, strBuffer); - String_Format1(&str, "%c..", s->buildName); + UpdatesScreen_UpdateHeader(s, &str); + String_AppendConst(&str, ".."); LLabel_SetText(&s->lblStatus, &str); LWidget_Redraw(&s->lblStatus); } -static void UpdatesScreen_Get(cc_bool release, cc_bool d3d9) { +static void UpdatesScreen_Get(cc_bool release, int buildIndex) { struct UpdatesScreen* s = &UpdatesScreen_Instance; /* This code is deliberately split up to handle this particular case: */ /* The user clicked this button before CheckUpdateTask completed, */ @@ -1648,7 +1651,7 @@ static void UpdatesScreen_Get(cc_bool release, cc_bool d3d9) { /* By storing requested build, it can be fetched later upon completion. */ s->pendingFetch = true; s->release = release; - s->d3d9 = d3d9; + s->buildIndex = buildIndex; UpdatesScreen_DoFetch(s); } @@ -1670,7 +1673,8 @@ static void UpdatesScreen_UpdateProgress(struct UpdatesScreen* s, struct LWebTas if (progress < 0 || progress > 100) return; String_InitArray(str, strBuffer); - String_Format2(&str, "%c &a%i%%", s->buildName, &s->buildProgress); + UpdatesScreen_UpdateHeader(s, &str); + String_Format1(&str, " &a%i%%", &s->buildProgress); LLabel_SetText(&s->lblStatus, &str); LWidget_Redraw(&s->lblStatus); } @@ -1695,10 +1699,10 @@ static void UpdatesScreen_FetchTick(struct UpdatesScreen* s) { } } -static void UpdatesScreen_RelD3D9(void* w, int idx) { UpdatesScreen_Get(true, true); } -static void UpdatesScreen_RelOpenGL(void* w, int idx) { UpdatesScreen_Get(true, false); } -static void UpdatesScreen_DevD3D9(void* w, int idx) { UpdatesScreen_Get(false, true); } -static void UpdatesScreen_DevOpenGL(void* w, int idx) { UpdatesScreen_Get(false, false); } +static void UpdatesScreen_RelD3D9(void* w, int idx) { UpdatesScreen_Get(true, 1); } +static void UpdatesScreen_RelOpenGL(void* w, int idx) { UpdatesScreen_Get(true, 0); } +static void UpdatesScreen_DevD3D9(void* w, int idx) { UpdatesScreen_Get(false, 1); } +static void UpdatesScreen_DevOpenGL(void* w, int idx) { UpdatesScreen_Get(false, 0); } static void UpdatesScreen_Init(struct LScreen* s_) { struct UpdatesScreen* s = (struct UpdatesScreen*)s_; @@ -1713,18 +1717,15 @@ static void UpdatesScreen_Init(struct LScreen* s_) { LLabel_Init(s_, &s->lblStatus, ""); LButton_Init(s_, &s->btnBack, 80, 35, "Back"); - if (Updater_D3D9) { - LButton_Init(s_, &s->btnRel[0], 130, 35, "Direct3D 9"); - LButton_Init(s_, &s->btnDev[0], 130, 35, "Direct3D 9"); - LLabel_Init(s_, &s->lblInfo, "&eDirect3D 9 is recommended"); + if (Updater_Info.numBuilds >= 2) { + LButton_Init(s_, &s->btnRel[0], 130, 35, Updater_Info.builds[1].name); + LButton_Init(s_, &s->btnDev[0], 130, 35, Updater_Info.builds[1].name); } - if (Updater_OGL) { - LButton_Init(s_, &s->btnRel[1], 130, 35, "OpenGL"); - LButton_Init(s_, &s->btnDev[1], 130, 35, "OpenGL"); + if (Updater_Info.numBuilds >= 1) { + LButton_Init(s_, &s->btnRel[1], 130, 35, Updater_Info.builds[0].name); + LButton_Init(s_, &s->btnDev[1], 130, 35, Updater_Info.builds[0].name); } -#ifdef CC_BUILD_MOBILE - LLabel_Init(s_, &s->lblInfo, "&eRedownload and reinstall to update"); -#endif + LLabel_Init(s_, &s->lblInfo, Updater_Info.info); s->btnRel[0].OnClick = UpdatesScreen_RelD3D9; s->btnRel[1].OnClick = UpdatesScreen_RelOpenGL; @@ -1759,7 +1760,7 @@ static void UpdatesScreen_Layout(struct LScreen* s_) { LWidget_SetLocation(&s->lblRel, ANCHOR_CENTRE, ANCHOR_CENTRE, -20, -75); LWidget_SetLocation(&s->btnRel[0], ANCHOR_CENTRE, ANCHOR_CENTRE, -80, -40); - if (Updater_D3D9) { + if (Updater_Info.numBuilds >= 2) { LWidget_SetLocation(&s->btnRel[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 80, -40); } else { LWidget_SetLocation(&s->btnRel[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -40); @@ -1768,7 +1769,7 @@ static void UpdatesScreen_Layout(struct LScreen* s_) { LWidget_SetLocation(&s->lblDev, ANCHOR_CENTRE, ANCHOR_CENTRE, -30, 20); LWidget_SetLocation(&s->btnDev[0], ANCHOR_CENTRE, ANCHOR_CENTRE, -80, 55); - if (Updater_D3D9) { + if (Updater_Info.numBuilds >= 2) { LWidget_SetLocation(&s->btnDev[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 80, 55); } else { LWidget_SetLocation(&s->btnDev[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 55); @@ -1790,7 +1791,6 @@ static void UpdatesScreen_Tick(struct LScreen* s_) { /* Aborts fetch if it is in progress */ static void UpdatesScreen_Free(struct LScreen* s_) { struct UpdatesScreen* s = (struct UpdatesScreen*)s_; - s->buildName = NULL; s->buildProgress = -1; FetchUpdateTask.Base.working = false; diff --git a/src/LWeb.c b/src/LWeb.c index 7763c853d..7d8e63137 100644 --- a/src/LWeb.c +++ b/src/LWeb.c @@ -548,13 +548,13 @@ static void FetchUpdateTask_Handle(cc_uint8* data, cc_uint32 len) { #endif } -void FetchUpdateTask_Run(cc_bool release, cc_bool d3d9) { +void FetchUpdateTask_Run(cc_bool release, int buildIndex) { cc_string url; char urlBuffer[URL_MAX_SIZE]; String_InitArray(url, urlBuffer); String_Format2(&url, UPDATES_SERVER "/%c/%c", - release ? "release" : "latest", - d3d9 ? Updater_D3D9 : Updater_OGL); + release ? "release" : "latest", + Updater_Info.builds[buildIndex].path); LWebTask_Reset(&FetchUpdateTask.Base); FetchUpdateTask.timestamp = release ? CheckUpdateTask.relTimestamp : CheckUpdateTask.devTimestamp; diff --git a/src/LWeb.h b/src/LWeb.h index a70c0a08f..8a9772444 100644 --- a/src/LWeb.h +++ b/src/LWeb.h @@ -107,7 +107,7 @@ extern struct FetchUpdateData { /* Unix timestamp downloaded build was originally built at. */ cc_uint64 timestamp; } FetchUpdateTask; -void FetchUpdateTask_Run(cc_bool release, cc_bool d3d9); +void FetchUpdateTask_Run(cc_bool release, int buildIndex); extern struct FetchFlagsData { diff --git a/src/Platform.h b/src/Platform.h index 9146166f3..dd24043d5 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -70,8 +70,17 @@ CC_API void Process_Exit(cc_result code); /* For example, provide a http:// url to open a website in the user's web browser. */ CC_API cc_result Process_StartOpen(const cc_string* args); -extern const char* const Updater_D3D9; -extern const char* const Updater_OGL; +struct UpdaterBuild { + const char* name; + const char* path; +}; +extern const struct UpdaterInfo { + const char* info; + /* Number of compiled builds available for this platform */ + int numBuilds; + /* Metadata for the compiled builds available for this platform */ + const struct UpdaterBuild builds[2]; // TODO name and path +} Updater_Info; /* Attempts to clean up any leftover files from an update */ cc_bool Updater_Clean(void); /* Starts the platform-specific method to update then start the game using the UPDATE_FILE file. */ diff --git a/src/Platform_Android.c b/src/Platform_Android.c index 608ba9052..690419a88 100644 --- a/src/Platform_Android.c +++ b/src/Platform_Android.c @@ -44,8 +44,9 @@ cc_result Process_StartOpen(const cc_string* args) { /*########################################################################################################################* *--------------------------------------------------------Updater----------------------------------------------------------* *#########################################################################################################################*/ -const char* const Updater_OGL = NULL; -const char* const Updater_D3D9 = NULL; +const struct UpdaterInfo Updater_Info = { + "&eRedownload and reinstall to update", 0 +}; cc_bool Updater_Clean(void) { return true; } cc_result Updater_GetBuildTime(cc_uint64* t) { diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 0397a0e54..87776c6f7 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -783,24 +783,24 @@ cc_bool Updater_Clean(void) { return true; } #if defined CC_BUILD_LINUX #if __x86_64__ -const char* const Updater_OGL = "ClassiCube"; +const struct UpdaterInfo Updater_Info = { "", 1, { { "OpenGL", "ClassiCube" } } }; #elif __i386__ -const char* const Updater_OGL = "ClassiCube.32"; +const struct UpdaterInfo Updater_Info = { "", 1, { { "OpenGL", "ClassiCube.32" } } }; #elif CC_BUILD_RPI -const char* const Updater_OGL = "ClassiCube.rpi"; +const struct UpdaterInfo Updater_Info = { "", 1, { { "OpenGL", "ClassiCube.rpi" } } }; #else -const char* const Updater_OGL = NULL; +const struct UpdaterInfo Updater_Info = { "&eCompile latest source code to update", 0 }; #endif #elif defined CC_BUILD_MACOS #if __x86_64__ -const char* const Updater_OGL = "ClassiCube.64.osx"; +const struct UpdaterInfo Updater_Info = { "", 1, { { "OpenGL", "ClassiCube.64.osx" } } }; #elif __i386__ -const char* const Updater_OGL = "ClassiCube.osx"; +const struct UpdaterInfo Updater_Info = { "", 1, { { "OpenGL", "ClassiCube.osx" } } }; #else -const char* const Updater_OGL = NULL; +const struct UpdaterInfo Updater_Info = { "&eCompile latest source code to update", 0 }; #endif #else -const char* const Updater_OGL = NULL; +const struct UpdaterInfo Updater_Info = { "&eCompile latest source code to update", 0 }; #endif cc_result Updater_Start(const char** action) { diff --git a/src/Platform_Web.c b/src/Platform_Web.c index 60e0512c6..0451b8333 100644 --- a/src/Platform_Web.c +++ b/src/Platform_Web.c @@ -376,8 +376,7 @@ cc_result Process_StartOpen(const cc_string* args) { /*########################################################################################################################* *--------------------------------------------------------Updater----------------------------------------------------------* *#########################################################################################################################*/ -const char* const Updater_D3D9 = NULL; -const char* const Updater_OGL = NULL; +const struct UpdaterInfo Updater_Info = { "", 0 }; cc_result Updater_GetBuildTime(cc_uint64* t) { return ERR_NOT_SUPPORTED; } cc_bool Updater_Clean(void) { return true; } diff --git a/src/Platform_WinApi.c b/src/Platform_WinApi.c index e1ccbf181..dc0e9c43f 100644 --- a/src/Platform_WinApi.c +++ b/src/Platform_WinApi.c @@ -600,13 +600,18 @@ cc_result Process_StartOpen(const cc_string* args) { #define UPDATE_TMP TEXT("CC_prev.exe") #define UPDATE_SRC TEXT(UPDATE_FILE) +const struct UpdaterInfo Updater_Info = { + "&eDirect3D 9 is recommended", 2, + { #if _WIN64 -const char* const Updater_D3D9 = "ClassiCube.64.exe"; -const char* const Updater_OGL = "ClassiCube.64-opengl.exe"; + { "OpenGL", "ClassiCube.64-opengl.exe" }, + { "Direct3D9", "ClassiCube.64.exe" } #else -const char* const Updater_D3D9 = "ClassiCube.exe"; -const char* const Updater_OGL = "ClassiCube.opengl.exe"; + { "OpenGL", "ClassiCube.opengl.exe" }, + { "Direct3D9", "ClassiCube.exe" } #endif + } +}; cc_bool Updater_Clean(void) { return DeleteFile(UPDATE_TMP) || GetLastError() == ERROR_FILE_NOT_FOUND;