From b29002a66f52448e1eeb7fe11eaec0ec99ea2d10 Mon Sep 17 00:00:00 2001 From: Ivan Mogilko Date: Fri, 11 Oct 2024 23:41:52 +0300 Subject: [PATCH] Engine: ScriptDateTime hides fields, supports init with C++11 tp --- Engine/ac/datetime.cpp | 24 +++---- Engine/ac/datetime.h | 1 - Engine/ac/dynobj/scriptdatetime.cpp | 79 ++++++++++++---------- Engine/ac/dynobj/scriptdatetime.h | 36 +++++++--- Engine/ac/file.cpp | 3 +- Engine/ac/game.cpp | 3 +- Engine/ac/global_datetime.cpp | 16 ++--- Engine/platform/base/agsplatformdriver.cpp | 6 -- Engine/platform/base/agsplatformdriver.h | 1 - 9 files changed, 90 insertions(+), 79 deletions(-) diff --git a/Engine/ac/datetime.cpp b/Engine/ac/datetime.cpp index f9138ec804..c3e5c69e79 100644 --- a/Engine/ac/datetime.cpp +++ b/Engine/ac/datetime.cpp @@ -16,46 +16,40 @@ #include "ac/dynobj/dynobj_manager.h" #include "platform/base/agsplatformdriver.h" -ScriptDateTime* DateTime_Now_Core() { - ScriptDateTime *sdt = new ScriptDateTime(); - - platform->GetSystemTime(sdt); - - return sdt; -} ScriptDateTime* DateTime_Now() { - ScriptDateTime *sdt = DateTime_Now_Core(); + ScriptDateTime *sdt = new ScriptDateTime(ScriptDateTime::SystemClock::now()); ccRegisterManagedObject(sdt, sdt); return sdt; } + int DateTime_GetYear(ScriptDateTime *sdt) { - return sdt->year; + return sdt->Year(); } int DateTime_GetMonth(ScriptDateTime *sdt) { - return sdt->month; + return sdt->Month(); } int DateTime_GetDayOfMonth(ScriptDateTime *sdt) { - return sdt->day; + return sdt->Day(); } int DateTime_GetHour(ScriptDateTime *sdt) { - return sdt->hour; + return sdt->Hour(); } int DateTime_GetMinute(ScriptDateTime *sdt) { - return sdt->minute; + return sdt->Minute(); } int DateTime_GetSecond(ScriptDateTime *sdt) { - return sdt->second; + return sdt->Second(); } int DateTime_GetRawTime(ScriptDateTime *sdt) { - return sdt->rawUnixTime; + return sdt->RawTime(); } //============================================================================= diff --git a/Engine/ac/datetime.h b/Engine/ac/datetime.h index 0a1b14a680..66029ceb50 100644 --- a/Engine/ac/datetime.h +++ b/Engine/ac/datetime.h @@ -20,7 +20,6 @@ #include "ac/dynobj/scriptdatetime.h" -ScriptDateTime* DateTime_Now_Core(); ScriptDateTime* DateTime_Now(); int DateTime_GetYear(ScriptDateTime *sdt); int DateTime_GetMonth(ScriptDateTime *sdt); diff --git a/Engine/ac/dynobj/scriptdatetime.cpp b/Engine/ac/dynobj/scriptdatetime.cpp index 567af544e6..3f0c30026d 100644 --- a/Engine/ac/dynobj/scriptdatetime.cpp +++ b/Engine/ac/dynobj/scriptdatetime.cpp @@ -12,33 +12,48 @@ // //============================================================================= #include "ac/dynobj/scriptdatetime.h" +#include #include "ac/dynobj/dynobj_manager.h" #include "util/stream.h" using namespace AGS::Common; -int ScriptDateTime::Dispose(void* /*address*/, bool /*force*/) { +int ScriptDateTime::Dispose(void* /*address*/, bool /*force*/) +{ // always dispose a DateTime delete this; return 1; } -const char *ScriptDateTime::GetType() { +const char *ScriptDateTime::GetType() +{ return "DateTime"; } -void ScriptDateTime::SetFromStdTime(time_t time) +ScriptDateTime::ScriptDateTime(const time_t &time) +{ + SetTime(SystemClock::from_time_t(time)); +} + +ScriptDateTime::ScriptDateTime(const ClockTimePoint &time) +{ + SetTime(time); +} + +void ScriptDateTime::SetTime(const ClockTimePoint &time) { - // NOTE: subject to year 2038 problem due to shoving time_t in an integer - rawUnixTime = static_cast(time); + // NOTE: subject to year 2038 problem due to shoving seconds since epoch into an int32 + _rawtime = static_cast( + std::chrono::duration_cast(time.time_since_epoch()).count()); - struct tm *newtime = localtime(&time); - hour = newtime->tm_hour; - minute = newtime->tm_min; - second = newtime->tm_sec; - day = newtime->tm_mday; - month = newtime->tm_mon + 1; - year = newtime->tm_year + 1900; + std::time_t ttime = SystemClock::to_time_t(time); + std::tm *newtime = std::localtime(&ttime); + _hour = newtime->tm_hour; + _minute = newtime->tm_min; + _second = newtime->tm_sec; + _day = newtime->tm_mday; + _month = newtime->tm_mon + 1; + _year = newtime->tm_year + 1900; } size_t ScriptDateTime::CalcSerializeSize(const void* /*address*/) @@ -46,29 +61,25 @@ size_t ScriptDateTime::CalcSerializeSize(const void* /*address*/) return sizeof(int32_t) * 7; } -void ScriptDateTime::Serialize(const void* /*address*/, Stream *out) { - out->WriteInt32(year); - out->WriteInt32(month); - out->WriteInt32(day); - out->WriteInt32(hour); - out->WriteInt32(minute); - out->WriteInt32(second); - out->WriteInt32(rawUnixTime); +void ScriptDateTime::Serialize(const void* /*address*/, Stream *out) +{ + out->WriteInt32(_year); + out->WriteInt32(_month); + out->WriteInt32(_day); + out->WriteInt32(_hour); + out->WriteInt32(_minute); + out->WriteInt32(_second); + out->WriteInt32(_rawtime); } -void ScriptDateTime::Unserialize(int index, Stream *in, size_t /*data_sz*/) { - year = in->ReadInt32(); - month = in->ReadInt32(); - day = in->ReadInt32(); - hour = in->ReadInt32(); - minute = in->ReadInt32(); - second = in->ReadInt32(); - rawUnixTime = in->ReadInt32(); +void ScriptDateTime::Unserialize(int index, Stream *in, size_t /*data_sz*/) +{ + _year = in->ReadInt32(); + _month = in->ReadInt32(); + _day = in->ReadInt32(); + _hour = in->ReadInt32(); + _minute = in->ReadInt32(); + _second = in->ReadInt32(); + _rawtime = in->ReadInt32(); ccRegisterUnserializedObject(index, this, this); } - -ScriptDateTime::ScriptDateTime() { - year = month = day = 0; - hour = minute = second = 0; - rawUnixTime = 0; -} diff --git a/Engine/ac/dynobj/scriptdatetime.h b/Engine/ac/dynobj/scriptdatetime.h index 7ec586f6c9..67b5094d7d 100644 --- a/Engine/ac/dynobj/scriptdatetime.h +++ b/Engine/ac/dynobj/scriptdatetime.h @@ -12,29 +12,47 @@ // //============================================================================= // -// +// ScriptDateTime is a script object that contains date/time definition. // //============================================================================= #ifndef __AGS_EE_DYNOBJ__SCRIPTDATETIME_H #define __AGS_EE_DYNOBJ__SCRIPTDATETIME_H -#include +#include #include "ac/dynobj/cc_agsdynamicobject.h" -struct ScriptDateTime final : AGSCCDynamicObject { - int year, month, day; - int hour, minute, second; - int rawUnixTime; +struct ScriptDateTime final : AGSCCDynamicObject +{ +public: + typedef std::chrono::system_clock SystemClock; + typedef std::chrono::time_point ClockTimePoint; + + // Constructs DateTime initialized with zero time + ScriptDateTime() = default; + // Constructs DateTime initialized using C time_t + ScriptDateTime(const time_t &time); + // Constructs DateTime initialized using chrono::time_point + ScriptDateTime(const ClockTimePoint &time); int Dispose(void *address, bool force) override; const char *GetType() override; void Unserialize(int index, AGS::Common::Stream *in, size_t data_sz) override; - void SetFromStdTime(time_t time); + inline int RawTime() const { return _rawtime; } + inline int Year() const { return _year; } + inline int Month() const { return _month; } + inline int Day() const { return _day; } + inline int Hour() const { return _hour; } + inline int Minute() const { return _minute; } + inline int Second() const { return _second; } + +private: + int _year = 0, _month = 0, _day = 0; + int _hour = 0, _minute = 0, _second = 0; + int _rawtime = 0; - ScriptDateTime(); + void SetTime(const ClockTimePoint &time); -protected: // Calculate and return required space for serialization, in bytes size_t CalcSerializeSize(const void *address) override; // Write object data into the provided stream diff --git a/Engine/ac/file.cpp b/Engine/ac/file.cpp index 9ec08739eb..ce02e6f043 100644 --- a/Engine/ac/file.cpp +++ b/Engine/ac/file.cpp @@ -70,8 +70,7 @@ ScriptDateTime* File_GetFileTime(const char *fnmm) { ft = File::GetFileTime(rp.FullPath); } - ScriptDateTime *sdt = new ScriptDateTime(); - sdt->SetFromStdTime(ft); + ScriptDateTime *sdt = new ScriptDateTime(ft); ccRegisterManagedObject(sdt, sdt); return sdt; } diff --git a/Engine/ac/game.cpp b/Engine/ac/game.cpp index f8c73cf16b..59d4cfb24e 100644 --- a/Engine/ac/game.cpp +++ b/Engine/ac/game.cpp @@ -420,8 +420,7 @@ const char* Game_GetSaveSlotDescription(int slnum) { ScriptDateTime* Game_GetSaveSlotTime(int slnum) { time_t ft = File::GetFileTime(get_save_game_path(slnum)); - ScriptDateTime *sdt = new ScriptDateTime(); - sdt->SetFromStdTime(ft); + ScriptDateTime *sdt = new ScriptDateTime(ft); ccRegisterManagedObject(sdt, sdt); return sdt; } diff --git a/Engine/ac/global_datetime.cpp b/Engine/ac/global_datetime.cpp index ed7c064daf..58e167ba9e 100644 --- a/Engine/ac/global_datetime.cpp +++ b/Engine/ac/global_datetime.cpp @@ -18,19 +18,17 @@ #include "ac/common.h" int sc_GetTime(int whatti) { - ScriptDateTime *sdt = DateTime_Now_Core(); + ScriptDateTime sdt(ScriptDateTime::SystemClock::now()); int returnVal = 0; - if (whatti == 1) returnVal = sdt->hour; - else if (whatti == 2) returnVal = sdt->minute; - else if (whatti == 3) returnVal = sdt->second; - else if (whatti == 4) returnVal = sdt->day; - else if (whatti == 5) returnVal = sdt->month; - else if (whatti == 6) returnVal = sdt->year; + if (whatti == 1) returnVal = sdt.Hour(); + else if (whatti == 2) returnVal = sdt.Minute(); + else if (whatti == 3) returnVal = sdt.Second(); + else if (whatti == 4) returnVal = sdt.Day(); + else if (whatti == 5) returnVal = sdt.Month(); + else if (whatti == 6) returnVal = sdt.Year(); else quit("!GetTime: invalid parameter passed"); - delete sdt; - return returnVal; } diff --git a/Engine/platform/base/agsplatformdriver.cpp b/Engine/platform/base/agsplatformdriver.cpp index a28f539cb3..f43d6ec061 100644 --- a/Engine/platform/base/agsplatformdriver.cpp +++ b/Engine/platform/base/agsplatformdriver.cpp @@ -78,12 +78,6 @@ const char *AGSPlatformDriver::GetDiskWriteAccessTroubleshootingText() return "Make sure you have write permissions, and also check the disk's free space."; } -void AGSPlatformDriver::GetSystemTime(ScriptDateTime *sdt) -{ - time_t t = time(nullptr); - sdt->SetFromStdTime(t); -} - void AGSPlatformDriver::DisplayAlert(const char *text, ...) { char displbuf[2048]; diff --git a/Engine/platform/base/agsplatformdriver.h b/Engine/platform/base/agsplatformdriver.h index f7b0c605b9..625a6f090a 100644 --- a/Engine/platform/base/agsplatformdriver.h +++ b/Engine/platform/base/agsplatformdriver.h @@ -104,7 +104,6 @@ class AGSPlatformDriver virtual uint64_t GetDiskFreeSpaceMB(const Common::String &path) = 0; virtual const char* GetBackendFailUserHint(); virtual eScriptSystemOSID GetSystemOSID() = 0; - virtual void GetSystemTime(ScriptDateTime*); virtual SetupReturnValue RunSetup(const Common::ConfigTree &cfg_in, Common::ConfigTree &cfg_out); // Formats message and writes to standard platform's output; // Always adds trailing '\n' after formatted string