From 19f1ff3aa70966085b430fc87bb07bfcd350ffa1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 20 May 2020 18:30:44 +1000 Subject: [PATCH] Change Updater_GetBuildTime/SetNewBuildTime to use unix timestamp instead of milliseconds since 1/1/0001 timestamp --- src/LScreens.c | 8 ++++---- src/LWeb.c | 9 ++++----- src/LWeb.h | 8 ++++---- src/Platform.c | 32 +++++++++++++++++--------------- src/Platform.h | 8 ++++---- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/LScreens.c b/src/LScreens.c index 229794735..e778a90ce 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -1381,7 +1381,7 @@ CC_NOINLINE static void UpdatesScreen_FormatTime(String* str, char* type, int de String_AppendConst(str, " ago"); } -static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, TimeMS time) { +static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, cc_uint64 timestamp) { String str; char buffer[STRING_SIZE]; TimeMS now; int delta; @@ -1389,12 +1389,12 @@ static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, TimeMS String_InitArray(str, buffer); String_AppendConst(&str, prefix); - if (!time) { + if (!timestamp) { String_AppendConst(&str, "&cCheck failed"); } else { - now = DateTime_CurrentUTC_MS(); + now = DateTime_CurrentUTC_MS() - UNIX_EPOCH; /* must divide as cc_uint64, int delta overflows after 26 days */ - delta = (int)((now - time) / 1000); + delta = (int)((now / 1000) - timestamp); if (delta < SECS_PER_MIN) { UpdatesScreen_FormatTime(&str, "second", delta, 1); diff --git a/src/LWeb.c b/src/LWeb.c index 6c4a013e0..091e61d6d 100644 --- a/src/LWeb.c +++ b/src/LWeb.c @@ -451,16 +451,15 @@ void FetchServersTask_ResetOrder(void) { struct CheckUpdateData CheckUpdateTask; static char relVersionBuffer[16]; -CC_NOINLINE static TimeMS CheckUpdateTask_ParseTime(const String* str) { +CC_NOINLINE static cc_uint64 CheckUpdateTask_ParseTime(const String* str) { String time, fractional; - TimeMS ms; + cc_uint64 secs; /* timestamp is in form of "seconds.fractional" */ /* But only need to care about the seconds here */ String_UNSAFE_Separate(str, '.', &time, &fractional); - Convert_ParseUInt64(&time, &ms); - if (!ms) return 0; - return ms * 1000 + UNIX_EPOCH; + Convert_ParseUInt64(&time, &secs); + return secs; } static void CheckUpdateTask_OnValue(struct JsonContext* ctx, const String* str) { diff --git a/src/LWeb.h b/src/LWeb.h index ce816b2aa..e6e07760b 100644 --- a/src/LWeb.h +++ b/src/LWeb.h @@ -91,8 +91,8 @@ void FetchServersTask_ResetOrder(void); extern struct CheckUpdateData { struct LWebTask Base; - /* Timestamp latest commit/dev build and release were at. */ - TimeMS devTimestamp, relTimestamp; + /* Unix timestamp latest commit/dev build and release were at. */ + cc_uint64 devTimestamp, relTimestamp; /* Version of latest release. */ String latestRelease; } CheckUpdateTask; /* TODO: Work out the JSON for this.. */ @@ -101,8 +101,8 @@ void CheckUpdateTask_Run(void); extern struct FetchUpdateData { struct LWebTask Base; - /* Timestamp downloaded build was originally built at. */ - TimeMS timestamp; + /* Unix timestamp downloaded build was originally built at. */ + cc_uint64 timestamp; } FetchUpdateTask; void FetchUpdateTask_Run(cc_bool release, cc_bool d3d9); diff --git a/src/Platform.c b/src/Platform.c index d50325703..87098fe9e 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -221,7 +221,9 @@ void Platform_Log(const String* message) { } #define FILETIME_EPOCH 50491123200000ULL -#define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH) +#define FILETIME_UNIX_EPOCH 11644473600LL +#define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH) +#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH) TimeMS DateTime_CurrentUTC_MS(void) { FILETIME ft; cc_uint64 raw; @@ -1233,7 +1235,7 @@ cc_result Updater_Start(void) { return Process_RawStart(path, args); } -cc_result Updater_GetBuildTime(TimeMS* ms) { +cc_result Updater_GetBuildTime(cc_uint64* timestamp) { TCHAR path[NATIVE_STR_LEN + 1]; FileHandle file; FILETIME ft; @@ -1248,8 +1250,8 @@ cc_result Updater_GetBuildTime(TimeMS* ms) { if (file == INVALID_HANDLE_VALUE) return GetLastError(); if (GetFileTime(file, NULL, NULL, &ft)) { - raw = ft.dwLowDateTime | ((cc_uint64)ft.dwHighDateTime << 32); - *ms = FileTime_TotalMS(raw); + raw = ft.dwLowDateTime | ((cc_uint64)ft.dwHighDateTime << 32); + *timestamp = FileTime_UnixTime(raw); } else { res = GetLastError(); } @@ -1260,7 +1262,7 @@ cc_result Updater_GetBuildTime(TimeMS* ms) { /* Don't need special execute permission on windows */ cc_result Updater_MarkExecutable(void) { return 0; } -cc_result Updater_SetNewBuildTime(TimeMS ms) { +cc_result Updater_SetNewBuildTime(cc_uint64 timestamp) { static const String path = String_FromConst(UPDATE_FILE); FileHandle file; FILETIME ft; @@ -1268,7 +1270,7 @@ cc_result Updater_SetNewBuildTime(TimeMS ms) { cc_result res = File_OpenOrCreate(&file, &path); if (res) return res; - raw = 10000 * (ms - FILETIME_EPOCH); + raw = 10000000 * (timestamp + FILETIME_UNIX_EPOCH); ft.dwLowDateTime = (cc_uint32)raw; ft.dwHighDateTime = (cc_uint32)(raw >> 32); @@ -1281,11 +1283,11 @@ cc_result Updater_SetNewBuildTime(TimeMS ms) { const char* const Updater_D3D9 = NULL; const char* const Updater_OGL = NULL; -cc_bool Updater_Clean(void) { return true; } -cc_result Updater_Start(void) { return ERR_NOT_SUPPORTED; } -cc_result Updater_GetBuildTime(TimeMS* ms) { return ERR_NOT_SUPPORTED; } -cc_result Updater_MarkExecutable(void) { return 0; } -cc_result Updater_SetNewBuildTime(TimeMS ms) { return ERR_NOT_SUPPORTED; } +cc_bool Updater_Clean(void) { return true; } +cc_result Updater_Start(void) { return ERR_NOT_SUPPORTED; } +cc_result Updater_GetBuildTime(cc_uint64* t) { return ERR_NOT_SUPPORTED; } +cc_result Updater_MarkExecutable(void) { return 0; } +cc_result Updater_SetNewBuildTime(cc_uint64 t) { return ERR_NOT_SUPPORTED; } #elif defined CC_BUILD_POSIX cc_bool Updater_Clean(void) { return true; } @@ -1330,7 +1332,7 @@ cc_result Updater_Start(void) { return Process_RawStart(path, argv); } -cc_result Updater_GetBuildTime(TimeMS* ms) { +cc_result Updater_GetBuildTime(cc_uint64* timestamp) { char path[NATIVE_STR_LEN + 1]; struct stat sb; int len = 0; @@ -1340,7 +1342,7 @@ cc_result Updater_GetBuildTime(TimeMS* ms) { path[len] = '\0'; if (stat(path, &sb) == -1) return errno; - *ms = (cc_uint64)sb.st_mtime * 1000 + UNIX_EPOCH; + *timestamp = (cc_uint64)sb.st_mtime; return 0; } @@ -1352,9 +1354,9 @@ cc_result Updater_MarkExecutable(void) { return chmod(UPDATE_FILE, st.st_mode) == -1 ? errno : 0; } -cc_result Updater_SetNewBuildTime(TimeMS ms) { +cc_result Updater_SetNewBuildTime(cc_uint64 timestamp) { struct utimbuf times = { 0 }; - times.modtime = (ms - UNIX_EPOCH) / 1000; + times.modtime = timestamp; return utime(UPDATE_FILE, ×) == -1 ? errno : 0; } #endif diff --git a/src/Platform.h b/src/Platform.h index 364983ae9..06a3ec641 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -69,12 +69,12 @@ extern const char* const Updater_OGL; cc_bool Updater_Clean(void); /* Starts the platform-specific method to update then start the game using the UPDATE_FILE file. */ cc_result Updater_Start(void); -/* Returns the last time the application was modified, as number of milliseconds since 1/1/0001 */ -cc_result Updater_GetBuildTime(TimeMS* ms); +/* Returns the last time the application was modified, as a unix timestamp. */ +cc_result Updater_GetBuildTime(cc_uint64* timestamp); /* Marks the UPDATE_FILE file as being executable. (Needed for some platforms) */ cc_result Updater_MarkExecutable(void); -/* Sets the last time UPDATE_FILE file was modified, as number of milliseconds since 1/1/0001 */ -cc_result Updater_SetNewBuildTime(TimeMS ms); +/* Sets the last time UPDATE_FILE file was modified, as a unix timestamp. */ +cc_result Updater_SetNewBuildTime(cc_uint64 timestamp); /* The default file extension used for dynamic libraries on this platform. */ extern const String DynamicLib_Ext;