Skip to content

Commit

Permalink
Engine: ScriptDateTime hides fields, supports init with C++11 tp
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-mogilko committed Oct 11, 2024
1 parent 0d6194b commit b29002a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 79 deletions.
24 changes: 9 additions & 15 deletions Engine/ac/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

//=============================================================================
Expand Down
1 change: 0 additions & 1 deletion Engine/ac/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
79 changes: 45 additions & 34 deletions Engine/ac/dynobj/scriptdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,74 @@
//
//=============================================================================
#include "ac/dynobj/scriptdatetime.h"
#include <ctime>
#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<int>(time);
// NOTE: subject to year 2038 problem due to shoving seconds since epoch into an int32
_rawtime = static_cast<int32_t>(
std::chrono::duration_cast<std::chrono::seconds>(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*/)
{
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;
}
36 changes: 27 additions & 9 deletions Engine/ac/dynobj/scriptdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <time.h>
#include <chrono>
#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<SystemClock> 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
Expand Down
3 changes: 1 addition & 2 deletions Engine/ac/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions Engine/ac/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 7 additions & 9 deletions Engine/ac/global_datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 0 additions & 6 deletions Engine/platform/base/agsplatformdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 0 additions & 1 deletion Engine/platform/base/agsplatformdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b29002a

Please sign in to comment.