ryujinx-mirror/Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneManager.cs
Thomas Guillemard 1aba033ba7 Update time implementation to 9.0.0 (#783)
* Fix 9.0.0 related services bindings

This was wrong because of a mistake on switchbrew.

* Fix wronog cmdid for ISteadyClock::GetTestOffset/SetTestOffset

* Update ClockCore logics to 9.0.0

Also apply 9.0.0 permissions and comment time:u, and time:a (as those
are going to be moved)

* Move every clocks instances + timezone to a global manager

* Start implementing time:m

Also prepare the skeleton of the shared memory

* Implement SystemClockContextUpdateCallback and co

* Update StaticService to 9.0.0

* Update ISystemClock to 9.0.0

* Rename IStaticService and add glue's IStaticService

* Implement psc's ITimeZoneService

* Integrate psc layer into glue for TimeZoneService

* Rename TimeZoneManagerForPsc => TimeZoneManager

* Use correct TimeZoneService interface for both StaticService implementations

* Accurately implement time shared memory operations

* Fix two critical flaws in TimeZone logic

The first one was the month range being different fron Nintendo one
(0-11 instead of 1-12)

The other flaw was a bad incrementation order during days & months
computation.

* Follow Nintendo's abort logic for TimeManager

* Avoid crashing when timezone sysarchive isn't present

* Update Readme

* Address comments

* Correctly align fields in ISystemClock

* Fix code style and some typos

* Improve timezone system archive warning/error messages

* Rearrange using definitions in Horizon.cs

* Address comments
2019-10-08 14:48:49 +11:00

267 lines
7.5 KiB
C#

using Ryujinx.HLE.HOS.Services.Time.Clock;
using Ryujinx.HLE.Utilities;
using System.IO;
using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
{
class TimeZoneManager
{
private bool _isInitialized;
private TimeZoneRule _myRules;
private string _deviceLocationName;
private UInt128 _timeZoneRuleVersion;
private uint _totalLocationNameCount;
private SteadyClockTimePoint _timeZoneUpdateTimePoint;
private object _lock;
public TimeZoneManager()
{
_isInitialized = false;
_deviceLocationName = "UTC";
_timeZoneRuleVersion = new UInt128();
_lock = new object();
// Empty rules
_myRules = new TimeZoneRule
{
Ats = new long[TzMaxTimes],
Types = new byte[TzMaxTimes],
Ttis = new TimeTypeInfo[TzMaxTypes],
Chars = new char[TzCharsArraySize]
};
_timeZoneUpdateTimePoint = SteadyClockTimePoint.GetRandom();
}
public bool IsInitialized()
{
bool res;
lock (_lock)
{
res = _isInitialized;
}
return res;
}
public void MarkInitialized()
{
lock (_lock)
{
_isInitialized = true;
}
}
public ResultCode GetDeviceLocationName(out string deviceLocationName)
{
ResultCode result = ResultCode.UninitializedClock;
deviceLocationName = null;
lock (_lock)
{
if (_isInitialized)
{
deviceLocationName = _deviceLocationName;
result = ResultCode.Success;
}
}
return result;
}
public ResultCode SetDeviceLocationNameWithTimeZoneRule(string locationName, Stream timeZoneBinaryStream)
{
ResultCode result = ResultCode.TimeZoneConversionFailed;
lock (_lock)
{
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out TimeZoneRule rules, timeZoneBinaryStream);
if (timeZoneConversionSuccess)
{
_deviceLocationName = locationName;
_myRules = rules;
result = ResultCode.Success;
}
}
return result;
}
public void SetTotalLocationNameCount(uint totalLocationNameCount)
{
lock (_lock)
{
_totalLocationNameCount = totalLocationNameCount;
}
}
public ResultCode GetTotalLocationNameCount(out uint totalLocationNameCount)
{
ResultCode result = ResultCode.UninitializedClock;
totalLocationNameCount = 0;
lock (_lock)
{
if (_isInitialized)
{
totalLocationNameCount = _totalLocationNameCount;
result = ResultCode.Success;
}
}
return result;
}
public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint, bool bypassUninitialized = false)
{
ResultCode result = ResultCode.UninitializedClock;
lock (_lock)
{
if (_isInitialized || bypassUninitialized)
{
_timeZoneUpdateTimePoint = timeZoneUpdatedTimePoint;
result = ResultCode.Success;
}
}
return result;
}
public ResultCode GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdatedTimePoint)
{
ResultCode result;
lock (_lock)
{
if (_isInitialized)
{
timeZoneUpdatedTimePoint = _timeZoneUpdateTimePoint;
result = ResultCode.Success;
}
else
{
timeZoneUpdatedTimePoint = SteadyClockTimePoint.GetRandom();
result = ResultCode.UninitializedClock;
}
}
return result;
}
public ResultCode ParseTimeZoneRuleBinary(out TimeZoneRule outRules, Stream timeZoneBinaryStream)
{
ResultCode result = ResultCode.Success;
lock (_lock)
{
bool timeZoneConversionSuccess = TimeZone.ParseTimeZoneBinary(out outRules, timeZoneBinaryStream);
if (!timeZoneConversionSuccess)
{
result = ResultCode.TimeZoneConversionFailed;
}
}
return result;
}
public void SetTimeZoneRuleVersion(UInt128 timeZoneRuleVersion)
{
lock (_lock)
{
_timeZoneRuleVersion = timeZoneRuleVersion;
}
}
public ResultCode GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion)
{
ResultCode result;
lock (_lock)
{
if (_isInitialized)
{
timeZoneRuleVersion = _timeZoneRuleVersion;
result = ResultCode.Success;
}
else
{
timeZoneRuleVersion = new UInt128();
result = ResultCode.UninitializedClock;
}
}
return result;
}
public ResultCode ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
{
ResultCode result;
lock (_lock)
{
if (_isInitialized)
{
result = ToCalendarTime(_myRules, time, out calendar);
}
else
{
calendar = new CalendarInfo();
result = ResultCode.UninitializedClock;
}
}
return result;
}
public ResultCode ToCalendarTime(TimeZoneRule rules, long time, out CalendarInfo calendar)
{
ResultCode result;
lock (_lock)
{
result = TimeZone.ToCalendarTime(rules, time, out calendar);
}
return result;
}
public ResultCode ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
{
ResultCode result;
lock (_lock)
{
if (_isInitialized)
{
result = ToPosixTime(_myRules, calendarTime, out posixTime);
}
else
{
posixTime = 0;
result = ResultCode.UninitializedClock;
}
}
return result;
}
public ResultCode ToPosixTime(TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
{
ResultCode result;
lock (_lock)
{
result = TimeZone.ToPosixTime(rules, calendarTime, out posixTime);
}
return result;
}
}
}