refactor: replace POSIX/Win32 APIs with standard C++ equivalents

Replaces platform-specific APIs across 21 files with std::chrono, std::fstream, std::filesystem, and std::this_thread. Adds PlatformTime.h utility header wrapping std::chrono::steady_clock for GetTickCount/QueryPerformanceCounter patterns.
This commit is contained in:
MatthewBeshay
2026-04-01 05:01:37 +11:00
parent 2f92b07c1f
commit ee31bfcec5
21 changed files with 196 additions and 306 deletions

View File

@@ -85,11 +85,11 @@ File::File(const std::wstring& pathname) {
for (const char* base : bases) {
std::string tryFull = exeDir + base + request;
std::string tryFile = exeDir + base + fileName;
if (access(tryFull.c_str(), F_OK) != -1) {
if (std::filesystem::exists(tryFull)) {
m_abstractPathName = convStringToWstring(tryFull);
return;
}
if (access(tryFile.c_str(), F_OK) != -1) {
if (std::filesystem::exists(tryFile)) {
m_abstractPathName = convStringToWstring(tryFile);
return;
}

View File

@@ -1,4 +1,4 @@
#include <ctime>
#include <chrono>
#include <cstdint> // for int64_t
#include "java/Random.h"
@@ -9,10 +9,8 @@ Random::Random() {
// only millisecond accuate, so use QueryPerformanceCounter here instead
int64_t seed;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
seed = ts.tv_sec * 1000000000LL + ts.tv_nsec;
auto now = std::chrono::steady_clock::now().time_since_epoch();
seed = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
seed += 8682522807148012LL;

View File

@@ -1,7 +1,4 @@
#if defined(__linux__)
#include <sys/time.h>
#include <time.h>
#endif
#include <chrono>
#include <assert.h>
#include <stdint.h>
@@ -69,13 +66,8 @@ void System::arraycopy(const std::vector<int>& src, unsigned int srcPos,
// Returns:
// The current value of the system timer, in nanoseconds.
int64_t System::nanoTime() {
#if !defined(__linux__)
return GetTickCount() * 1000000LL;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<int64_t>(ts.tv_sec) * 1000000000LL + ts.tv_nsec;
#endif
auto now = std::chrono::steady_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
}
// Returns the current time in milliseconds. Note that while the unit of time of
@@ -89,28 +81,8 @@ int64_t System::nanoTime() {
// the difference, measured in milliseconds, between the current time and
// midnight, January 1, 1970 UTC.
int64_t System::currentTimeMillis() {
#if defined(__linux__)
struct timeval tv;
gettimeofday(&tv, nullptr);
// Convert to milliseconds since unix epoch instead of windows file time
// time is expecting calculation to be between 10-30 ms.
return (int64_t)tv.tv_sec * 1000LL + tv.tv_usec / 1000;
#else
SYSTEMTIME UTCSysTime;
GetSystemTime(&UTCSysTime);
// Represents as a 64-bit value the number of 100-nanosecond intervals since
// January 1, 1601
FILETIME UTCFileTime;
SystemTimeToFileTime(&UTCSysTime, &UTCFileTime);
LARGE_INTEGER li;
li.HighPart = UTCFileTime.dwHighDateTime;
li.LowPart = UTCFileTime.dwLowDateTime;
return li.QuadPart / 10000;
#endif
auto now = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
}
// 4J Stu - Added this so that we can use real-world timestamps in PSVita saves.