Skip to content

Commit

Permalink
Merge branch 'main' of github.com:simul/Platform
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRichards-Code committed Nov 7, 2024
2 parents 514e758 + 06a7fbd commit c482678
Show file tree
Hide file tree
Showing 36 changed files with 170 additions and 296 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
16 changes: 7 additions & 9 deletions Applications/Test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface
crossplatform::Texture* depthTexture = nullptr;
crossplatform::HdrRenderer* hdrRenderer = nullptr;
crossplatform::Framebuffer* hdrFramebuffer = nullptr;
crossplatform::Effect* effect = nullptr;
crossplatform::Effect* test = nullptr;
std::shared_ptr<crossplatform::Effect> effect = nullptr;
std::shared_ptr<crossplatform::Effect> test = nullptr;
crossplatform::Texture* texture = nullptr;
crossplatform::ConstantBuffer<SceneConstants> sceneConstants;
crossplatform::ConstantBuffer<CameraConstants> cameraConstants;
Expand Down Expand Up @@ -272,8 +272,8 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface
rwSB.~StructuredBuffer();
roSB.~StructuredBuffer();
delete texture;
delete test;
delete effect;
test = nullptr;
effect = nullptr;
delete hdrFramebuffer;
delete hdrRenderer;
delete depthTexture;
Expand All @@ -288,11 +288,10 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface

hdrRenderer->RestoreDeviceObjects(renderPlatform);
hdrFramebuffer->RestoreDeviceObjects(renderPlatform);
effect = renderPlatform->CreateEffect();
effect->Load(renderPlatform, "solid");
effect = renderPlatform->GetOrCreateEffect("solid");
sceneConstants.RestoreDeviceObjects(renderPlatform);
cameraConstants.RestoreDeviceObjects(renderPlatform);
test = renderPlatform->CreateEffect("Test");
test = renderPlatform->GetOrCreateEffect("Test");
texture = renderPlatform->CreateTexture();
}

Expand All @@ -301,7 +300,6 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface
if (effect)
{
effect->InvalidateDeviceObjects();
delete effect;
effect = nullptr;
}
sceneConstants.InvalidateDeviceObjects();
Expand Down Expand Up @@ -423,7 +421,7 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface
hdrFramebuffer->Clear(deviceContext, 0.00f, 0.31f, 0.57f, 1.00f, reverseDepth ? 0.0f : 1.0f);
renderPlatform->GetDebugConstantBuffer().multiplier = vec4(0.0f, 0.33f, 1.0f, 1.0f);
renderPlatform->SetConstantBuffer(deviceContext, &(renderPlatform->GetDebugConstantBuffer()));
renderPlatform->DrawQuad(deviceContext, w / 4, h / 4, w / 2, h / 2, renderPlatform->GetDebugEffect(), renderPlatform->GetDebugEffect()->GetTechniqueByName("untextured"), "noblend");
renderPlatform->DrawQuad(deviceContext, w / 4, h / 4, w / 2, h / 2, renderPlatform->GetDebugEffect().get(), renderPlatform->GetDebugEffect()->GetTechniqueByName("untextured"), "noblend");
}

void Test_Text(crossplatform::GraphicsDeviceContext& deviceContext, int w, int h)
Expand Down
45 changes: 19 additions & 26 deletions Core/DefaultFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ void DefaultFileLoader::AcquireFileContents(void *&pointer, unsigned int &bytes,
filesLoaded.insert(filename_utf8);
}

double DefaultFileLoader::GetFileDate(const char *filename_utf8) const
uint64_t DefaultFileLoader::GetFileDate(const char *filename_utf8) const
{
if (!FileExists(filename_utf8))
return 0.0;
return 0;

std::wstring wstr = platform::core::Utf8ToWString(filename_utf8);
FILE *fp = nullptr;
Expand All @@ -168,44 +168,37 @@ double DefaultFileLoader::GetFileDate(const char *filename_utf8) const
if (!fp)
{
// std::cerr<<"Failed to find file "<<filename_utf8<<std::endl;
return 0.0;
return 0;
}
fclose(fp);

#if 0//PLATFORM_STD_FILESYSTEM
// https://stackoverflow.com/questions/61030383/how-to-convert-stdfilesystemfile-time-type-to-time-t
// https://stackoverflow.com/questions/51273205/how-to-compare-time-t-and-stdfilesystemfile-time-type
fs::file_time_type fileTime = fs::last_write_time(filename_utf8);
#if PLATFORM_CXX20
const std::chrono::system_clock systemTime = std::chrono::clock_cast<std::chrono::system_clock>(fileTime);
const time_t time = std::chrono::system_clock::to_time_t(systemTime);
#else
std::chrono::system_clock::time_point systemTime = std::chrono::time_point_cast<std::chrono::system_clock::duration>(fileTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now());
const time_t time = std::chrono::system_clock::to_time_t(systemTime);
#endif
return time;
#endif

#if defined(_MSC_VER) && defined(_WIN32)
struct _stat buf;
_wstat(wstr.c_str(), &buf);
buf.st_mtime;
time_t t = buf.st_mtime;
struct tm lt;
gmtime_s(&lt, &t);
// Note: bizarrely, the tm structure has MONTHS starting at ZERO, but DAYS start at 1.
double daynum = GetDayNumberFromDateTime(1900 + lt.tm_year, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec);
return daynum;
#elif PLATFORM_STD_FILESYSTEM
#ifdef CPP20
auto write_time = fs::last_write_time(filename_utf8);
const auto systemTime = std::chrono::clock_cast<std::chrono::system_clock>(fileTime);
const auto time = std::chrono::system_clock::to_time_t(systemTime);
return ((double)ns) / (3600.0 * 24.0 * 1000000.0);
return t;
#else
std::wstring filenamew = StringToWString(filename_utf8);
struct stat buf;
stat(filename_utf8, &buf);
buf.st_mtime;
time_t t = buf.st_mtime;
struct tm lt;
#if __COMMODORE__
gmtime_s(&t, &lt);
#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 t;
#endif
#else
return 0;
#endif
}

bool DefaultFileLoader::Save(const void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text)
Expand Down
2 changes: 1 addition & 1 deletion Core/DefaultFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace platform
~DefaultFileLoader() = default;
bool FileExists(const char *filename_utf8) const override;
void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text) override;
double GetFileDate(const char* filename_utf8) const override;
uint64_t GetFileDate(const char* filename_utf8) const override;
void ReleaseFileContents(void* pointer) override;
bool Save(const void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text) override;
};
Expand Down
14 changes: 2 additions & 12 deletions Core/FileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ int FileLoader::NextSlash(const std::string &str, int pos)
return slash;
}

double FileLoader::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;
}

#if defined(_MSC_VER) && !defined(_GAMING_XBOX) && !defined(__SWITCH__) && PLATFORM_STD_FILESYSTEM > 0
std::string FileLoader::FindParentFolder(const char *folder_utf8)
{
Expand Down Expand Up @@ -252,8 +242,8 @@ int FileLoader::FindIndexInPathStack(const char* filename_utf8, const char* cons
break;
}
}
double newest_date = 0.0;

uint64_t newest_date = 0;
int index = 0;
for (; i >= 0; i--)
{
Expand All @@ -268,7 +258,7 @@ int FileLoader::FindIndexInPathStack(const char* filename_utf8, const char* cons
f += filename_utf8;
if (FileExists(f.c_str()))
{
double filedate = GetFileDate(f.c_str());
uint64_t filedate = GetFileDate(f.c_str());
if (filedate >= newest_date)
{
fn = f;
Expand Down
6 changes: 2 additions & 4 deletions Core/FileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace platform

//! Get the position of the next slash in the string from the current position.
static int NextSlash(const std::string &str, int pos = 0);
//! Get the Day Number (decimal Julian) from DateTime. Useful for file time stamps.
static double GetDayNumberFromDateTime(int year, int month, int day, int hour, int min, int sec);

//! Get the parent folder of the current directory.
static std::string FindParentFolder(const char *folder_utf8);
Expand All @@ -43,8 +41,8 @@ namespace platform
//! The memory should later be freed by a call to ReleaseFileContents.
//! The filename should be unicode UTF8-encoded.
virtual void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text)=0;
//! Get the file date as a julian day number. Return zero if the file doesn't exist.
virtual double GetFileDate(const char* filename_utf8) const=0;
//! Get the unix timestamp. Return zero if the file doesn't exist.
virtual uint64_t GetFileDate(const char* filename_utf8) const=0;
//! Free the memory allocated by AcquireFileContents.
virtual void ReleaseFileContents(void* pointer)=0;
//! Save the chunk of memory to storage.
Expand Down
9 changes: 9 additions & 0 deletions Core/RuntimeError.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
#endif
#include <stdexcept> // for runtime_error

// C++ Versions
#if defined(_WIN64)
#define PLATFORM_CXX20 _HAS_CXX20
#define PLATFORM_CXX17 _HAS_CXX17
#elif defined(__linux__)
#define PLATFORM_CXX20 (__cplusplus == 202002L)
#define PLATFORM_CXX17 (__cplusplus == 201703L)
#endif

#define SIMUL_COUT \
std::cout << __FILE__ << "(" << std::dec << __LINE__ << "): info: "

Expand Down
13 changes: 13 additions & 0 deletions CrossPlatform/Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,19 @@ bool Effect::Load(crossplatform::RenderPlatform *r, const char *filename_utf8)
std::string sfxbFilenameUtf8 = binFilenameUtf8;
platform::core::find_and_replace(sfxbFilenameUtf8, ".sfxo", ".sfxb");

#if 0
uint64_t timestamp = platform::core::FileLoader::GetFileLoader()->GetFileDate(sfxbFilenameUtf8.c_str());
struct tm timeDate;
gmtime_s(&timeDate, (time_t *)&timestamp);
std::cout << sfxbFilenameUtf8 << " write time " <<
(timeDate.tm_year + 1900) << "-" <<
(timeDate.tm_mon + 1) << "-" <<
(timeDate.tm_mday) << " " <<
(timeDate.tm_hour) << ":" <<
(timeDate.tm_min) << ":" <<
(timeDate.tm_sec) << std::endl;
#endif

platform::core::FileLoader::GetFileLoader()->AcquireFileContents(ptr,num_bytes, binFilenameUtf8.c_str(),true);
filenameInUseUtf8=binFilenameUtf8;
void *bin_ptr=nullptr;
Expand Down
Loading

0 comments on commit c482678

Please sign in to comment.