Skip to content

Commit

Permalink
Updated Sfx's FileLoader::GetFileDate() to use unix timestamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRichards-Code committed Nov 7, 2024
1 parent 631e596 commit 06a7fbd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 62 deletions.
20 changes: 2 additions & 18 deletions Applications/Sfx/FileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,8 @@ void FileLoader::AcquireFileContents(void*& pointer, unsigned int& bytes, const

fclose(fp);
}
static double GetDayNumberFromDateTime(int year,int month,int day,int hour,int min,int sec)
{
int D = 367*year - (7*(year + ((month+9)/12)))/4 + (275*month)/9 + day - 730531;//was +2451545
double d=(double)D;
d+=(double)hour/24.0;
d+=(double)min/24.0/60.0;
d+=(double)sec/24.0/3600.0;
return d;
}

double FileLoader::GetFileDate(const char* filename_utf8)
uint64_t FileLoader::GetFileDate(const char *filename_utf8)
{
std::wstring filenamew=StringToWString(filename_utf8);
#ifdef _MSC_VER
Expand All @@ -218,14 +209,7 @@ double FileLoader::GetFileDate(const char* filename_utf8)
#endif
buf.st_mtime;
time_t t = buf.st_mtime;
struct tm lt;
#ifdef _MSC_VER
gmtime_s(&lt,&t);
#else
gmtime_r(&t,&lt);
#endif
double datetime=GetDayNumberFromDateTime(1900+lt.tm_year,lt.tm_mon,lt.tm_mday,lt.tm_hour,lt.tm_min,lt.tm_sec);
return datetime;
return static_cast<uint64_t>(t);
}

void FileLoader::ReleaseFileContents(void* pointer)
Expand Down
2 changes: 1 addition & 1 deletion Applications/Sfx/FileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FileLoader
bool FileExists(const char *filename_utf8) const;
const char *FindFileInPathStack(const char *filename_utf8,const std::vector<std::string> &path_stack_utf8) const;
void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text);
double GetFileDate(const char* filename_utf8);
static uint64_t GetFileDate(const char* filename_utf8);
void ReleaseFileContents(void* pointer);
void Save(void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text);
};
Expand Down
4 changes: 2 additions & 2 deletions Applications/Sfx/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ struct MacroDefinition
};
// prevent echoing of unprocessed chars:
#define ECHO
extern double latest_datetime;
extern uint64_t latest_datetime;
extern std::string latest_file;
#undef PREPRO_STYPE
#define PREPRO_STYPE PreprocessorType
#undef PREPRO_DEBUG
#define PREPRO_DEBUG 1
// These are the callback functions for file handling that we will send to the preprocessor.
extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,double &datetime);
extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,uint64_t &datetime);
extern void (*prepro_close)(FILE *f);
extern void Unput(int c);
extern std::ostringstream preproOutput;
Expand Down
4 changes: 2 additions & 2 deletions Applications/Sfx/Preprocessor.lpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
extern void prepro_warning(const char *s);
extern void prepro_warning(const char *s,const char *file,int line);
extern int prepro_get_lineno ();
FILE* (*prepro_open)(const char *filename_utf8,string &fullPathName,double &filedate)=NULL;
FILE* (*prepro_open)(const char *filename_utf8,string &fullPathName,uint64_t &filedate)=NULL;

void (*prepro_close)(FILE *f)=NULL;
std::ostringstream preproOutput;
Expand Down Expand Up @@ -621,7 +621,7 @@ void Preprocess(int argc, char **argv)
int newfile(const char *fn)
{
std::string fullPathName;
double datetime=0.0;
uint64_t datetime=0;
FILE *f = prepro_open(fn,fullPathName,datetime);

/* die if no file or no room */
Expand Down
2 changes: 1 addition & 1 deletion Applications/Sfx/Preprocessor.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
vector<bool> activeBlockStack;
vector<string> blockTestStack;
extern int preproBracket;
double latest_datetime = 0.0;
uint64_t latest_datetime = 0;
std::string latest_file;
void ClearParserState()
{
Expand Down
45 changes: 7 additions & 38 deletions Applications/Sfx/Sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,45 +79,14 @@ typedef int errno_t;
using namespace std;

// These are the callback functions for file handling that we will send to the preprocessor.
//extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,double &time);
//extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,uint64_t &time);
//extern void (*prepro_close)(FILE *f);

vector<string> shaderPathsUtf8;
FileLoader fileLoader;
extern std::ostringstream preproOutput;

static double GetDayNumberFromDateTime(int year,int month,int day,int hour,int min,int sec)
{
int D = 367*year - (7*(year + ((month+9)/12)))/4 + (275*month)/9 + day - 730531;//was +2451545
double d=(double)D;
d+=(double)hour/24.0;
d+=(double)min/24.0/60.0;
d+=(double)sec/24.0/3600.0;
return d;
}

double GetFileDate(const std::string &fullPathNameUtf8)
{
wstring filenamew=StringToWString(fullPathNameUtf8);
#ifdef _MSC_VER
struct _stat64i32 buf;
_wstat(filenamew.c_str(), &buf);
#else
struct stat buf;
stat(fullPathNameUtf8.c_str(), &buf);
#endif
buf.st_mtime;
time_t t = buf.st_mtime;
struct tm lt;
#ifdef _MSC_VER
gmtime_s(&lt,&t);
#else
gmtime_r(&t,&lt);
#endif
double datetime=GetDayNumberFromDateTime(1900+lt.tm_year,lt.tm_mon,lt.tm_mday,lt.tm_hour,lt.tm_min,lt.tm_sec);
return datetime;
}

FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,double &datetime)
FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,uint64_t &datetime)
{
fullPathNameUtf8 =fileLoader.FindFileInPathStack(filename_utf8,shaderPathsUtf8);
if(!fullPathNameUtf8.length())
Expand All @@ -137,7 +106,7 @@ FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,double &d
if(last_slash>0)
path=path.substr(0,last_slash);
shaderPathsUtf8.push_back(path);
datetime=GetFileDate(fullPathNameUtf8);
datetime=FileLoader::GetFileDate(fullPathNameUtf8.c_str());
return f;
}

Expand Down Expand Up @@ -626,13 +595,13 @@ bool sfxParseEffectFromFile(int effect, const char *file, const std::vector<std:
readlink("/proc/self/exe",exeNameUtf8,_MAX_PATH);
#endif
// start with the date that this exe was made, so new exe's rebuild the shaders.
double exe_datetime=GetFileDate(exeNameUtf8);
double platformfile_datetime=GetFileDate(config->platformFilename);
uint64_t exe_datetime=FileLoader::GetFileDate(exeNameUtf8);
uint64_t platformfile_datetime=FileLoader::GetFileDate(config->platformFilename.c_str());
latest_datetime= std::max(exe_datetime,platformfile_datetime);
latest_file=file;
if (!preprocess(file, config->define, sfxOptions->disableLineWrites))
return false;
double output_filedatetime=GetFileDate(sfxoFilename);
uint64_t output_filedatetime = FileLoader::GetFileDate(sfxoFilename.c_str());
bool recompile=false;
if(sfxOptions->force)
{
Expand Down

0 comments on commit 06a7fbd

Please sign in to comment.