metaforce/Runtime/CBasicsPC.cpp

161 lines
4.1 KiB
C++
Raw Normal View History

2017-01-08 03:08:02 +00:00
#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#if __APPLE__
#include <mach/mach_time.h>
#endif
#else
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
2015-08-17 05:26:58 +00:00
#include <stdio.h>
#include <stdarg.h>
2016-10-09 21:41:23 +00:00
#include <time.h>
2015-08-17 05:26:58 +00:00
#include "CBasics.hpp"
2017-01-08 03:08:02 +00:00
#if __APPLE__
static u64 MachToDolphinNum;
static u64 MachToDolphinDenom;
#elif _WIN32
static LARGE_INTEGER PerfFrequency;
#endif
2016-03-04 23:04:53 +00:00
namespace urde
{
2017-01-08 03:08:02 +00:00
void CBasics::Initialize()
2015-08-17 05:26:58 +00:00
{
2017-01-08 03:08:02 +00:00
#if __APPLE__
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
MachToDolphinNum = GetGCTicksPerSec() * timebase.numer;
MachToDolphinDenom = 1000000000ull * timebase.denom;
#elif _WIN32
QueryPerformanceFrequency(&PerfFrequency);
#endif
2015-08-17 05:26:58 +00:00
}
const char* CBasics::Stringize(const char* fmt, ...)
{
2015-11-10 23:19:36 +00:00
static char STRINGIZE_STR[2048] = {0};
2015-08-17 05:26:58 +00:00
va_list ap;
va_start(ap, fmt);
vsnprintf(STRINGIZE_STR, 2048, fmt, ap);
va_end(ap);
return STRINGIZE_STR;
}
2017-01-01 06:46:52 +00:00
u64 CBasics::GetGCTicks()
{
2017-01-08 03:08:02 +00:00
#if __APPLE__
return mach_absolute_time() * MachToDolphinNum / MachToDolphinDenom;
#elif __linux__ || __FreeBSD__
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return u64((tp.tv_sec * 1000000000ull) + tp.tv_nsec) * GetGCTicksPerSec() / 1000000000ull;
#elif _WIN32
LARGE_INTEGER perf;
QueryPerformanceCounter(&perf);
perf.QuadPart *= GetGCTicksPerSec();
perf.QuadPart /= PerfFrequency.QuadPart;
return perf.QuadPart;
#else
return 0;
#endif
2017-01-01 06:46:52 +00:00
}
2016-10-09 21:41:23 +00:00
const u64 CBasics::SECONDS_TO_2000 = 946684800LL;
const u64 CBasics::TICKS_PER_SECOND = 60750000LL;
2016-12-24 00:45:51 +00:00
#ifndef _WIN32
static struct tm* localtime_r(const time_t& time, struct tm& timeSt, long& gmtOff)
{
auto ret = ::localtime_r(&time, &timeSt);
2016-12-24 00:45:51 +00:00
if (!ret)
return nullptr;
gmtOff = ret->tm_gmtoff;
return ret;
}
#else
static struct tm* localtime_r(const time_t& time, struct tm& timeSt, long& gmtOff)
{
struct tm _gmSt;
auto reta = localtime_s(&timeSt, &time);
auto retb = gmtime_s(&_gmSt, &time);
if (reta || retb)
return nullptr;
gmtOff = mktime(&timeSt) - mktime(&_gmSt);
return &timeSt;
}
#endif
2016-10-09 21:41:23 +00:00
OSTime CBasics::ToWiiTime(std::chrono::system_clock::time_point time)
{
2016-12-24 00:45:51 +00:00
auto sec = std::chrono::time_point_cast<std::chrono::seconds>(time);
auto us = std::chrono::duration_cast<std::chrono::microseconds>((time - sec)).count();
time_t sysTime = std::chrono::system_clock::to_time_t(sec);
2016-10-09 21:41:23 +00:00
2016-12-24 00:45:51 +00:00
struct tm _timeSt;
long gmtOff;
struct tm* timeSt = localtime_r(sysTime, _timeSt, gmtOff);
if (!timeSt)
2016-10-09 21:41:23 +00:00
return 0;
2016-12-24 00:45:51 +00:00
/* Returning local */
return OSTime(TICKS_PER_SECOND * ((sysTime + gmtOff) - SECONDS_TO_2000) +
us * TICKS_PER_SECOND / 1000000);
2016-10-09 21:41:23 +00:00
}
std::chrono::system_clock::time_point CBasics::FromWiiTime(OSTime wiiTime)
{
2016-12-24 00:45:51 +00:00
auto div = std::lldiv(SECONDS_TO_2000 + wiiTime, TICKS_PER_SECOND);
time_t time = time_t(div.quot);
2016-10-09 21:41:23 +00:00
time_t sysTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
2016-12-24 00:45:51 +00:00
struct tm _timeSt;
long gmtOff;
struct tm* timeSt = localtime_r(sysTime, _timeSt, gmtOff);
if (!timeSt)
2016-10-09 21:41:23 +00:00
return std::chrono::system_clock::from_time_t(0);
2016-12-24 00:45:51 +00:00
/* Returning GMT */
return std::chrono::system_clock::from_time_t(time - gmtOff) +
std::chrono::microseconds(div.rem * 1000000 / TICKS_PER_SECOND);
2016-10-09 21:41:23 +00:00
}
2016-12-24 00:45:51 +00:00
OSCalendarTime CBasics::ToCalendarTime(std::chrono::system_clock::time_point time)
2016-12-23 06:41:39 +00:00
{
2016-12-24 00:45:51 +00:00
OSCalendarTime ret;
auto sec = std::chrono::time_point_cast<std::chrono::seconds>(time);
auto us = std::chrono::duration_cast<std::chrono::microseconds>((time - sec)).count();
time_t sysTime = std::chrono::system_clock::to_time_t(sec);
struct tm _timeSt;
long gmtOff;
struct tm* timeSt = localtime_r(sysTime, _timeSt, gmtOff);
if (!timeSt)
return {};
ret.x0_sec = timeSt->tm_sec;
ret.x4_min = timeSt->tm_min;
ret.x8_hour = timeSt->tm_hour;
ret.xc_mday = timeSt->tm_mday;
ret.x10_mon = timeSt->tm_mon;
ret.x14_year = timeSt->tm_year + 1900;
ret.x18_wday = timeSt->tm_wday;
ret.x1c_yday = timeSt->tm_yday;
auto div = std::ldiv(us, 1000);
ret.x20_msec = div.quot;
ret.x24_usec = div.rem;
2016-12-23 06:41:39 +00:00
return ret;
}
}