Change Updater_GetBuildTime/SetNewBuildTime to use unix timestamp instead of milliseconds since 1/1/0001 timestamp

This commit is contained in:
UnknownShadow200 2020-05-20 18:30:44 +10:00
parent 1095418d41
commit 19f1ff3aa7
5 changed files with 33 additions and 32 deletions

View File

@ -1381,7 +1381,7 @@ CC_NOINLINE static void UpdatesScreen_FormatTime(String* str, char* type, int de
String_AppendConst(str, " ago"); 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]; String str; char buffer[STRING_SIZE];
TimeMS now; TimeMS now;
int delta; int delta;
@ -1389,12 +1389,12 @@ static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, TimeMS
String_InitArray(str, buffer); String_InitArray(str, buffer);
String_AppendConst(&str, prefix); String_AppendConst(&str, prefix);
if (!time) { if (!timestamp) {
String_AppendConst(&str, "&cCheck failed"); String_AppendConst(&str, "&cCheck failed");
} else { } else {
now = DateTime_CurrentUTC_MS(); now = DateTime_CurrentUTC_MS() - UNIX_EPOCH;
/* must divide as cc_uint64, int delta overflows after 26 days */ /* 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) { if (delta < SECS_PER_MIN) {
UpdatesScreen_FormatTime(&str, "second", delta, 1); UpdatesScreen_FormatTime(&str, "second", delta, 1);

View File

@ -451,16 +451,15 @@ void FetchServersTask_ResetOrder(void) {
struct CheckUpdateData CheckUpdateTask; struct CheckUpdateData CheckUpdateTask;
static char relVersionBuffer[16]; 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; String time, fractional;
TimeMS ms; cc_uint64 secs;
/* timestamp is in form of "seconds.fractional" */ /* timestamp is in form of "seconds.fractional" */
/* But only need to care about the seconds here */ /* But only need to care about the seconds here */
String_UNSAFE_Separate(str, '.', &time, &fractional); String_UNSAFE_Separate(str, '.', &time, &fractional);
Convert_ParseUInt64(&time, &ms); Convert_ParseUInt64(&time, &secs);
if (!ms) return 0; return secs;
return ms * 1000 + UNIX_EPOCH;
} }
static void CheckUpdateTask_OnValue(struct JsonContext* ctx, const String* str) { static void CheckUpdateTask_OnValue(struct JsonContext* ctx, const String* str) {

View File

@ -91,8 +91,8 @@ void FetchServersTask_ResetOrder(void);
extern struct CheckUpdateData { extern struct CheckUpdateData {
struct LWebTask Base; struct LWebTask Base;
/* Timestamp latest commit/dev build and release were at. */ /* Unix timestamp latest commit/dev build and release were at. */
TimeMS devTimestamp, relTimestamp; cc_uint64 devTimestamp, relTimestamp;
/* Version of latest release. */ /* Version of latest release. */
String latestRelease; String latestRelease;
} CheckUpdateTask; /* TODO: Work out the JSON for this.. */ } CheckUpdateTask; /* TODO: Work out the JSON for this.. */
@ -101,8 +101,8 @@ void CheckUpdateTask_Run(void);
extern struct FetchUpdateData { extern struct FetchUpdateData {
struct LWebTask Base; struct LWebTask Base;
/* Timestamp downloaded build was originally built at. */ /* Unix timestamp downloaded build was originally built at. */
TimeMS timestamp; cc_uint64 timestamp;
} FetchUpdateTask; } FetchUpdateTask;
void FetchUpdateTask_Run(cc_bool release, cc_bool d3d9); void FetchUpdateTask_Run(cc_bool release, cc_bool d3d9);

View File

@ -221,7 +221,9 @@ void Platform_Log(const String* message) {
} }
#define FILETIME_EPOCH 50491123200000ULL #define FILETIME_EPOCH 50491123200000ULL
#define FILETIME_UNIX_EPOCH 11644473600LL
#define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH) #define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH)
#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH)
TimeMS DateTime_CurrentUTC_MS(void) { TimeMS DateTime_CurrentUTC_MS(void) {
FILETIME ft; FILETIME ft;
cc_uint64 raw; cc_uint64 raw;
@ -1233,7 +1235,7 @@ cc_result Updater_Start(void) {
return Process_RawStart(path, args); return Process_RawStart(path, args);
} }
cc_result Updater_GetBuildTime(TimeMS* ms) { cc_result Updater_GetBuildTime(cc_uint64* timestamp) {
TCHAR path[NATIVE_STR_LEN + 1]; TCHAR path[NATIVE_STR_LEN + 1];
FileHandle file; FileHandle file;
FILETIME ft; FILETIME ft;
@ -1249,7 +1251,7 @@ cc_result Updater_GetBuildTime(TimeMS* ms) {
if (GetFileTime(file, NULL, NULL, &ft)) { if (GetFileTime(file, NULL, NULL, &ft)) {
raw = ft.dwLowDateTime | ((cc_uint64)ft.dwHighDateTime << 32); raw = ft.dwLowDateTime | ((cc_uint64)ft.dwHighDateTime << 32);
*ms = FileTime_TotalMS(raw); *timestamp = FileTime_UnixTime(raw);
} else { } else {
res = GetLastError(); res = GetLastError();
} }
@ -1260,7 +1262,7 @@ cc_result Updater_GetBuildTime(TimeMS* ms) {
/* Don't need special execute permission on windows */ /* Don't need special execute permission on windows */
cc_result Updater_MarkExecutable(void) { return 0; } 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); static const String path = String_FromConst(UPDATE_FILE);
FileHandle file; FileHandle file;
FILETIME ft; FILETIME ft;
@ -1268,7 +1270,7 @@ cc_result Updater_SetNewBuildTime(TimeMS ms) {
cc_result res = File_OpenOrCreate(&file, &path); cc_result res = File_OpenOrCreate(&file, &path);
if (res) return res; if (res) return res;
raw = 10000 * (ms - FILETIME_EPOCH); raw = 10000000 * (timestamp + FILETIME_UNIX_EPOCH);
ft.dwLowDateTime = (cc_uint32)raw; ft.dwLowDateTime = (cc_uint32)raw;
ft.dwHighDateTime = (cc_uint32)(raw >> 32); ft.dwHighDateTime = (cc_uint32)(raw >> 32);
@ -1283,9 +1285,9 @@ const char* const Updater_OGL = NULL;
cc_bool Updater_Clean(void) { return true; } cc_bool Updater_Clean(void) { return true; }
cc_result Updater_Start(void) { return ERR_NOT_SUPPORTED; } cc_result Updater_Start(void) { return ERR_NOT_SUPPORTED; }
cc_result Updater_GetBuildTime(TimeMS* ms) { 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_MarkExecutable(void) { return 0; }
cc_result Updater_SetNewBuildTime(TimeMS ms) { return ERR_NOT_SUPPORTED; } cc_result Updater_SetNewBuildTime(cc_uint64 t) { return ERR_NOT_SUPPORTED; }
#elif defined CC_BUILD_POSIX #elif defined CC_BUILD_POSIX
cc_bool Updater_Clean(void) { return true; } cc_bool Updater_Clean(void) { return true; }
@ -1330,7 +1332,7 @@ cc_result Updater_Start(void) {
return Process_RawStart(path, argv); return Process_RawStart(path, argv);
} }
cc_result Updater_GetBuildTime(TimeMS* ms) { cc_result Updater_GetBuildTime(cc_uint64* timestamp) {
char path[NATIVE_STR_LEN + 1]; char path[NATIVE_STR_LEN + 1];
struct stat sb; struct stat sb;
int len = 0; int len = 0;
@ -1340,7 +1342,7 @@ cc_result Updater_GetBuildTime(TimeMS* ms) {
path[len] = '\0'; path[len] = '\0';
if (stat(path, &sb) == -1) return errno; if (stat(path, &sb) == -1) return errno;
*ms = (cc_uint64)sb.st_mtime * 1000 + UNIX_EPOCH; *timestamp = (cc_uint64)sb.st_mtime;
return 0; return 0;
} }
@ -1352,9 +1354,9 @@ cc_result Updater_MarkExecutable(void) {
return chmod(UPDATE_FILE, st.st_mode) == -1 ? errno : 0; 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 }; struct utimbuf times = { 0 };
times.modtime = (ms - UNIX_EPOCH) / 1000; times.modtime = timestamp;
return utime(UPDATE_FILE, &times) == -1 ? errno : 0; return utime(UPDATE_FILE, &times) == -1 ? errno : 0;
} }
#endif #endif

View File

@ -69,12 +69,12 @@ extern const char* const Updater_OGL;
cc_bool Updater_Clean(void); cc_bool Updater_Clean(void);
/* Starts the platform-specific method to update then start the game using the UPDATE_FILE file. */ /* Starts the platform-specific method to update then start the game using the UPDATE_FILE file. */
cc_result Updater_Start(void); cc_result Updater_Start(void);
/* Returns the last time the application was modified, as number of milliseconds since 1/1/0001 */ /* Returns the last time the application was modified, as a unix timestamp. */
cc_result Updater_GetBuildTime(TimeMS* ms); cc_result Updater_GetBuildTime(cc_uint64* timestamp);
/* Marks the UPDATE_FILE file as being executable. (Needed for some platforms) */ /* Marks the UPDATE_FILE file as being executable. (Needed for some platforms) */
cc_result Updater_MarkExecutable(void); cc_result Updater_MarkExecutable(void);
/* Sets the last time UPDATE_FILE file was modified, as number of milliseconds since 1/1/0001 */ /* Sets the last time UPDATE_FILE file was modified, as a unix timestamp. */
cc_result Updater_SetNewBuildTime(TimeMS ms); cc_result Updater_SetNewBuildTime(cc_uint64 timestamp);
/* The default file extension used for dynamic libraries on this platform. */ /* The default file extension used for dynamic libraries on this platform. */
extern const String DynamicLib_Ext; extern const String DynamicLib_Ext;