diff --git a/src/LScreens.c b/src/LScreens.c index 9cef28d5a..a2f99abf4 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -6,6 +6,7 @@ #include "Game.h" #include "Drawer2D.h" #include "ExtMath.h" +#include "Platform.h" /*########################################################################################################################* *---------------------------------------------------------Screen base-----------------------------------------------------* @@ -995,14 +996,29 @@ static void UpdatesScreen_Draw(struct LScreen* s) { static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, TimeMS time) { String str; char buffer[STRING_SIZE]; - String_InitArray(str, buffer); + TimeMS now; + String_InitArray(str, buffer); String_AppendConst(&str, prefix); + if (!time) { String_AppendConst(&str, "&cCheck failed"); } else { - // do something here.. - String_AppendConst(&str, " ago"); + now = DateTime_CurrentUTC_MS(); + int delta = (int)(now - time) / 1000; + + if (delta < SECS_PER_MIN) { + String_Format1(&str, "%i seconds ago", &delta); + } else if (delta < SECS_PER_HOUR) { + delta /= SECS_PER_MIN; + String_Format1(&str, "%i minutes ago", &delta); + } else if (delta < SECS_PER_DAY) { + delta /= SECS_PER_HOUR; + String_Format1(&str, "%i hours ago", &delta); + } else { + delta /= SECS_PER_DAY; + String_Format1(&str, "%i days ago", &delta); + } } LLabel_SetText(lbl, &str); } @@ -1042,11 +1058,6 @@ static void UpdatesScreen_Get(bool release, bool d3d9) { //UpdateStatus(); } -void Init() { - //CheckUpdateTask_Run(); - //FIX ALL THE COMMENTED OUT // CODE -} - static void UpdatesScreen_CheckTick(struct UpdatesScreen* s) { if (!CheckUpdateTask.Base.Working) return; LWebTask_Tick(&CheckUpdateTask.Base); @@ -1101,10 +1112,12 @@ static void UpdatesScreen_DevD3D9(void* w, int x, int y) { UpdatesScreen_Get(f static void UpdatesScreen_DevOpenGL(void* w, int x, int y) { UpdatesScreen_Get(false, false); } static void UpdatesScreen_Init(struct LScreen* s_) { + static String exeName = String_FromConst(GAME_EXE_NAME); struct UpdatesScreen* s = (struct UpdatesScreen*)s_; - //DateTime writeTime = Platform.FileGetModifiedTime("ClassicalSharp.exe"); + TimeMS buildTime; + ReturnCode res; - if (s->NumWidgets) return; + if (s->NumWidgets) { CheckUpdateTask_Run(); return; } s->Widgets = s->_widgets; LScreen_Label(s_, &s->LblYour, "Your build: (unknown)"); @@ -1130,6 +1143,14 @@ static void UpdatesScreen_Init(struct LScreen* s_) { if (CheckUpdateTask.Base.Completed && CheckUpdateTask.Base.Success) { UpdatesScreen_FormatBoth(s); } + CheckUpdateTask_Run(); + + res = File_GetModifiedTime(&exeName, &buildTime); + if (res) { + Launcher_ShowError(res, "getting build time"); + } else { + UpdatesScreen_Format(&s->LblYour, "Your build: ", buildTime); + } } static void UpdatesScreen_Reposition(struct LScreen* s_) { diff --git a/src/Platform.c b/src/Platform.c index 892831b41..14e6964da 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -434,7 +434,7 @@ ReturnCode Directory_Enum(const String* dirPath, void* obj, Directory_EnumCallba return Win_Return(res == ERROR_NO_MORE_FILES); } -ReturnCode File_GetModifiedTime_MS(const String* path, TimeMS* time) { +ReturnCode File_GetModifiedTime(const String* path, TimeMS* time) { FileHandle file; ReturnCode res = File_Open(&file, path); if (res) return res; @@ -567,7 +567,7 @@ ReturnCode Directory_Enum(const String* dirPath, void* obj, Directory_EnumCallba return res; } -ReturnCode File_GetModifiedTime_MS(const String* path, TimeMS* time) { +ReturnCode File_GetModifiedTime(const String* path, TimeMS* time) { char str[600]; struct stat sb; Platform_ConvertString(str, path); diff --git a/src/Platform.h b/src/Platform.h index 406be047b..ef901e163 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -120,7 +120,7 @@ ReturnCode Directory_Enum(const String* path, void* obj, Directory_EnumCallback /* Returns whether the given file exists. */ bool File_Exists(const String* path); /* Returns the last time the file was modified, as number of milliseconds since 1/1/0001 */ -ReturnCode File_GetModifiedTime_MS(const String* path, TimeMS* ms); +ReturnCode File_GetModifiedTime(const String* path, TimeMS* ms); /* Attempts to create a new (or overwrite) file for writing. */ ReturnCode File_Create(FileHandle* file, const String* path); diff --git a/src/Screens.c b/src/Screens.c index 3f840ba20..3ca76dde2 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -1403,7 +1403,7 @@ static struct DisconnectScreen DisconnectScreen_Instance; static void DisconnectScreen_ReconnectMessage(struct DisconnectScreen* s, String* msg) { if (s->CanReconnect) { int elapsedMS = (int)(DateTime_CurrentUTC_MS() - s->InitTime); - int secsLeft = (DISCONNECT_DELAY_MS - elapsedMS) / DATETIME_MILLIS_PER_SEC; + int secsLeft = (DISCONNECT_DELAY_MS - elapsedMS) / MILLIS_PER_SEC; if (secsLeft > 0) { String_Format1(msg, "Reconnect in %i", &secsLeft); return; @@ -1417,7 +1417,7 @@ static void DisconnectScreen_UpdateDelayLeft(struct DisconnectScreen* s, double int elapsedMS, secsLeft; elapsedMS = (int)(DateTime_CurrentUTC_MS() - s->InitTime); - secsLeft = (DISCONNECT_DELAY_MS - elapsedMS) / DATETIME_MILLIS_PER_SEC; + secsLeft = (DISCONNECT_DELAY_MS - elapsedMS) / MILLIS_PER_SEC; if (secsLeft < 0) secsLeft = 0; if (s->LastSecsLeft == secsLeft && s->Reconnect.Active == s->LastActive) return; @@ -1465,7 +1465,7 @@ static void DisconnectScreen_Init(void* screen) { game_limitMs = 1000 / 5.0f; s->InitTime = DateTime_CurrentUTC_MS(); - s->LastSecsLeft = DISCONNECT_DELAY_MS / DATETIME_MILLIS_PER_SEC; + s->LastSecsLeft = DISCONNECT_DELAY_MS / MILLIS_PER_SEC; } static void DisconnectScreen_Render(void* screen, double delta) { diff --git a/src/TexturePack.c b/src/TexturePack.c index a0a5a2e70..a65aa18f9 100644 --- a/src/TexturePack.c +++ b/src/TexturePack.c @@ -572,7 +572,7 @@ void TextureCache_GetLastModified(const String* url, TimeMS* time) { String_InitArray(path, pathBuffer); TextureCache_MakePath(&path, url); - res = File_GetModifiedTime_MS(&path, time); + res = File_GetModifiedTime(&path, time); if (res) { Chat_LogError2(res, "getting last modified time of", url); *time = 0; } } } diff --git a/src/Utils.c b/src/Utils.c index 025169f79..a9b612c58 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -10,12 +10,6 @@ /*########################################################################################################################* *--------------------------------------------------------DateTime---------------------------------------------------------* *#########################################################################################################################*/ -#define DATETIME_SECONDS_PER_MINUTE 60 -#define DATETIME_SECONDS_PER_HOUR (60 * 60) -#define DATETIME_SECONDS_PER_DAY (60 * 60 * 24) -#define DATETIME_MINUTES_PER_HOUR 60 -#define DATETIME_HOURS_PER_DAY 24 -#define DATETIME_MILLISECS_PER_DAY (1000 * 60 * 60 * 24) #define DAYS_IN_400_YEARS 146097 /* (400*365) + 97 */ static uint16_t DateTime_DaysTotal[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; @@ -47,11 +41,11 @@ int DateTime_TotalDays(const struct DateTime* time) { TimeMS DateTime_TotalMs(const struct DateTime* time) { int days = DateTime_TotalDays(time); uint64_t seconds = - (uint64_t)days * DATETIME_SECONDS_PER_DAY + - time->Hour * DATETIME_SECONDS_PER_HOUR + - time->Minute * DATETIME_SECONDS_PER_MINUTE + + (uint64_t)days * SECS_PER_DAY + + time->Hour * SECS_PER_HOUR + + time->Minute * SECS_PER_MIN + time->Second; - return seconds * DATETIME_MILLIS_PER_SEC + time->Milli; + return seconds * MILLIS_PER_SEC + time->Milli; } void DateTime_FromTotalMs(struct DateTime* time, TimeMS ms) { @@ -61,16 +55,16 @@ void DateTime_FromTotalMs(struct DateTime* time, TimeMS ms) { bool leap; /* Work out time component for just this day */ - dayMS = (int)(ms % DATETIME_MILLISECS_PER_DAY); - time->Milli = dayMS % DATETIME_MILLIS_PER_SEC; dayMS /= DATETIME_MILLIS_PER_SEC; - time->Second = dayMS % DATETIME_SECONDS_PER_MINUTE; dayMS /= DATETIME_SECONDS_PER_MINUTE; - time->Minute = dayMS % DATETIME_MINUTES_PER_HOUR; dayMS /= DATETIME_MINUTES_PER_HOUR; - time->Hour = dayMS % DATETIME_HOURS_PER_DAY; dayMS /= DATETIME_HOURS_PER_DAY; + dayMS = (int)(ms % MILLIS_PER_DAY); + time->Milli = dayMS % MILLIS_PER_SEC; dayMS /= MILLIS_PER_SEC; + time->Second = dayMS % SECS_PER_MIN; dayMS /= SECS_PER_MIN; + time->Minute = dayMS % MINS_PER_HOUR; dayMS /= MINS_PER_HOUR; + time->Hour = dayMS % HOURS_PER_DAY; dayMS /= HOURS_PER_DAY; /* Then work out day/month/year component (inverse TotalDays operation) */ /* Probably not the most efficient way of doing this. But it passes my tests at */ /* https://gist.github.com/UnknownShadow200/30993c66464bb03ead01577f3ab2a653 */ - days = (int)(ms / DATETIME_MILLISECS_PER_DAY); + days = (int)(ms / MILLIS_PER_DAY); year = 1 + ((days / DAYS_IN_400_YEARS) * 400); days %= DAYS_IN_400_YEARS; for (; ; year++) { @@ -94,8 +88,8 @@ void DateTime_FromTotalMs(struct DateTime* time, TimeMS ms) { } void DateTime_HttpDate(TimeMS ms, String* str) { - static char* days_of_week[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; - static char* month_names[13] = { NULL, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + static const char* days_of_week[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; + static const char* month_names[13] = { NULL, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; struct DateTime t; int days; diff --git a/src/Utils.h b/src/Utils.h index 902bb3d3a..3051d6902 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -18,7 +18,14 @@ struct DateTime { int Milli; /* Milliseconds, ranges from 0 to 999 */ }; -#define DATETIME_MILLIS_PER_SEC 1000 +#define MILLIS_PER_SEC 1000 +#define SECS_PER_MIN 60 +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (60 * 60 * 24) +#define MINS_PER_HOUR 60 +#define HOURS_PER_DAY 24 +#define MILLIS_PER_DAY (1000 * 60 * 60 * 24) + int DateTime_TotalDays(const struct DateTime* time); TimeMS DateTime_TotalMs(const struct DateTime* time); void DateTime_FromTotalMs(struct DateTime* time, TimeMS ms);