Integrate psc layer into glue for TimeZoneService
This commit is contained in:
parent
dc95f7535f
commit
812387a307
9 changed files with 273 additions and 391 deletions
|
@ -143,7 +143,7 @@ namespace Ryujinx.HLE.FileSystem.Content
|
|||
}
|
||||
}
|
||||
|
||||
TimeManager.Instance.TimeZone.Initialize(_device);
|
||||
TimeManager.Instance.InitializeTimeZone(_device);
|
||||
}
|
||||
|
||||
public void ClearEntry(long titleId, ContentType contentType, StorageId storageId)
|
||||
|
|
|
@ -339,18 +339,25 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
clockSnapshot.UserContext = userContext;
|
||||
clockSnapshot.NetworkContext = networkContext;
|
||||
|
||||
char[] tzName = _timeManager.TimeZone.GetDeviceLocationName().ToCharArray();
|
||||
ResultCode result = _timeManager.TimeZone.Manager.GetDeviceLocationName(out string deviceLocationName);
|
||||
|
||||
if (result != ResultCode.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
char[] tzName = deviceLocationName.ToCharArray();
|
||||
char[] locationName = new char[0x24];
|
||||
|
||||
Array.Copy(tzName, locationName, tzName.Length);
|
||||
|
||||
clockSnapshot.LocationName = locationName;
|
||||
|
||||
ResultCode result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
|
||||
result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
result = _timeManager.TimeZone.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
|
||||
result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
|
@ -362,7 +369,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
clockSnapshot.NetworkTime = 0;
|
||||
}
|
||||
|
||||
result = _timeManager.TimeZone.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
|
||||
result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
|
|
|
@ -5,6 +5,8 @@ using Ryujinx.HLE.HOS.Kernel.Common;
|
|||
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
||||
using Ryujinx.HLE.Utilities;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Time
|
||||
{
|
||||
|
@ -108,11 +110,22 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
}
|
||||
|
||||
[Command(14)]
|
||||
// SetupTimeZoneManager(nn::time::LocationName location_name, u32 total_location_name_count, nn::time::TimeZoneRuleVersion timezone_rule_version, buffer<nn::time::TimeZoneBinary, 0x21> timezone_binary)
|
||||
// SetupTimeZoneManager(nn::time::LocationName location_name, nn::time::SteadyClockTimePoint timezone_update_timepoint, u32 total_location_name_count, nn::time::TimeZoneRuleVersion timezone_rule_version, buffer<nn::time::TimeZoneBinary, 0x21> timezone_binary)
|
||||
public ResultCode SetupTimeZoneManager(ServiceCtx context)
|
||||
{
|
||||
// TODO
|
||||
return ResultCode.NotImplemented;
|
||||
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
|
||||
SteadyClockTimePoint timeZoneUpdateTimePoint = context.RequestData.ReadStruct<SteadyClockTimePoint>();
|
||||
uint totalLocationNameCount = context.RequestData.ReadUInt32();
|
||||
UInt128 timeZoneRuleVersion = context.RequestData.ReadStruct<UInt128>();
|
||||
|
||||
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
|
||||
|
||||
using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize)))
|
||||
{
|
||||
_timeManager.SetupTimeZoneManager(locationName, timeZoneUpdateTimePoint, totalLocationNameCount, timeZoneRuleVersion, timeZoneBinaryStream);
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[Command(15)]
|
||||
|
|
|
@ -9,36 +9,22 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
{
|
||||
class ITimeZoneService : IpcService
|
||||
{
|
||||
private TimeZoneManager _timeZoneManager;
|
||||
private bool _writePermission;
|
||||
private TimeZoneContentManager _timeZoneContentManager;
|
||||
private ITimeZoneServiceForPsc _inner;
|
||||
private bool _writePermission;
|
||||
|
||||
public ITimeZoneService(TimeZoneManager timeZoneManager, bool writePermission)
|
||||
public ITimeZoneService(TimeZoneContentManager timeZoneContentManager, bool writePermission)
|
||||
{
|
||||
_timeZoneManager = timeZoneManager;
|
||||
_writePermission = writePermission;
|
||||
_timeZoneContentManager = timeZoneContentManager;
|
||||
_writePermission = writePermission;
|
||||
_inner = new ITimeZoneServiceForPsc(timeZoneContentManager.Manager, writePermission);
|
||||
}
|
||||
|
||||
[Command(0)]
|
||||
// GetDeviceLocationName() -> nn::time::LocationName
|
||||
public ResultCode GetDeviceLocationName(ServiceCtx context)
|
||||
{
|
||||
char[] tzName = _timeZoneManager.GetDeviceLocationName().ToCharArray();
|
||||
|
||||
int padding = 0x24 - tzName.Length;
|
||||
|
||||
if (padding < 0)
|
||||
{
|
||||
return ResultCode.LocationNameTooLong;
|
||||
}
|
||||
|
||||
context.ResponseData.Write(tzName);
|
||||
|
||||
for (int index = 0; index < padding; index++)
|
||||
{
|
||||
context.ResponseData.Write((byte)0);
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
return _inner.GetDeviceLocationName(context);
|
||||
}
|
||||
|
||||
[Command(1)]
|
||||
|
@ -52,16 +38,14 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
|
||||
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
|
||||
|
||||
return _timeZoneManager.SetDeviceLocationName(locationName);
|
||||
return _timeZoneContentManager.SetDeviceLocationName(locationName);
|
||||
}
|
||||
|
||||
[Command(2)]
|
||||
// GetTotalLocationNameCount() -> u32
|
||||
public ResultCode GetTotalLocationNameCount(ServiceCtx context)
|
||||
{
|
||||
context.ResponseData.Write(_timeZoneManager.GetTotalLocationNameCount());
|
||||
|
||||
return ResultCode.Success;
|
||||
return _inner.GetTotalLocationNameCount(context);
|
||||
}
|
||||
|
||||
[Command(3)]
|
||||
|
@ -72,7 +56,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
long bufferPosition = context.Request.ReceiveBuff[0].Position;
|
||||
long bufferSize = context.Request.ReceiveBuff[0].Size;
|
||||
|
||||
ResultCode errorCode = _timeZoneManager.LoadLocationNameList(index, out string[] locationNameArray, (uint)bufferSize / 0x24);
|
||||
ResultCode errorCode = _timeZoneContentManager.LoadLocationNameList(index, out string[] locationNameArray, (uint)bufferSize / 0x24);
|
||||
|
||||
if (errorCode == 0)
|
||||
{
|
||||
|
@ -114,13 +98,12 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
|
||||
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
|
||||
|
||||
ResultCode resultCode = _timeZoneManager.LoadTimeZoneRules(out TimeZoneRule rules, locationName);
|
||||
ResultCode resultCode = _timeZoneContentManager.LoadTimeZoneRule(out TimeZoneRule rules, locationName);
|
||||
|
||||
// Write TimeZoneRule if success
|
||||
if (resultCode == 0)
|
||||
if (resultCode == ResultCode.Success)
|
||||
{
|
||||
MemoryHelper.Write(context.Memory, bufferPosition, rules);
|
||||
}
|
||||
|
@ -132,99 +115,28 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
// ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
||||
public ResultCode ToCalendarTime(ServiceCtx context)
|
||||
{
|
||||
long posixTime = context.RequestData.ReadInt64();
|
||||
long bufferPosition = context.Request.SendBuff[0].Position;
|
||||
long bufferSize = context.Request.SendBuff[0].Size;
|
||||
|
||||
if (bufferSize != 0x4000)
|
||||
{
|
||||
// TODO: find error code here
|
||||
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
|
||||
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, bufferPosition);
|
||||
|
||||
ResultCode resultCode = TimeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
context.ResponseData.WriteStruct(calendar);
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
return _inner.ToCalendarTime(context);
|
||||
}
|
||||
|
||||
[Command(101)]
|
||||
// ToCalendarTimeWithMyRule(nn::time::PosixTime) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
||||
public ResultCode ToCalendarTimeWithMyRule(ServiceCtx context)
|
||||
{
|
||||
long posixTime = context.RequestData.ReadInt64();
|
||||
|
||||
ResultCode resultCode = _timeZoneManager.ToCalendarTimeWithMyRules(posixTime, out CalendarInfo calendar);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
context.ResponseData.WriteStruct(calendar);
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
return _inner.ToCalendarTimeWithMyRule(context);
|
||||
}
|
||||
|
||||
[Command(201)]
|
||||
// ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
||||
public ResultCode ToPosixTime(ServiceCtx context)
|
||||
{
|
||||
long inBufferPosition = context.Request.SendBuff[0].Position;
|
||||
long inBufferSize = context.Request.SendBuff[0].Size;
|
||||
|
||||
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
||||
|
||||
if (inBufferSize != 0x4000)
|
||||
{
|
||||
// TODO: find error code here
|
||||
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");
|
||||
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
|
||||
|
||||
ResultCode resultCode = TimeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
long outBufferPosition = context.Request.RecvListBuff[0].Position;
|
||||
long outBufferSize = context.Request.RecvListBuff[0].Size;
|
||||
|
||||
context.Memory.WriteInt64(outBufferPosition, posixTime);
|
||||
context.ResponseData.Write(1);
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
return _inner.ToPosixTime(context);
|
||||
}
|
||||
|
||||
[Command(202)]
|
||||
// ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
||||
public ResultCode ToPosixTimeWithMyRule(ServiceCtx context)
|
||||
{
|
||||
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
||||
|
||||
ResultCode resultCode = _timeZoneManager.ToPosixTimeWithMyRules(calendarTime, out long posixTime);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
long outBufferPosition = context.Request.RecvListBuff[0].Position;
|
||||
long outBufferSize = context.Request.RecvListBuff[0].Size;
|
||||
|
||||
context.Memory.WriteInt64(outBufferPosition, posixTime);
|
||||
|
||||
// There could be only one result on one calendar as leap seconds aren't supported.
|
||||
context.ResponseData.Write(1);
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
return _inner.ToPosixTimeWithMyRule(context);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@ using Ryujinx.HLE.HOS.Services.Time.Clock;
|
|||
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
|
||||
using Ryujinx.HLE.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
@ -195,7 +194,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
|
||||
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, bufferPosition);
|
||||
|
||||
ResultCode resultCode = TimeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
|
||||
ResultCode resultCode = _timeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
|
@ -240,7 +239,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
|
||||
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
|
||||
|
||||
ResultCode resultCode = TimeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
|
||||
ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
|
@ -282,7 +281,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|||
|
||||
int padding = 0x24 - locationNameArray.Length;
|
||||
|
||||
Debug.Assert(padding < 0, "LocationName exceeded limit (0x24 bytes)");
|
||||
Debug.Assert(padding >= 0, "LocationName exceeded limit (0x24 bytes)");
|
||||
|
||||
context.ResponseData.Write(locationNameArray);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Ryujinx.HLE.HOS.Kernel.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
||||
|
@ -29,7 +30,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
public StandardLocalSystemClockCore StandardLocalSystemClock { get; private set; }
|
||||
public StandardNetworkSystemClockCore StandardNetworkSystemClock { get; private set; }
|
||||
public StandardUserSystemClockCore StandardUserSystemClock { get; private set; }
|
||||
public TimeZoneManager TimeZone { get; private set; }
|
||||
public TimeZoneContentManager TimeZone { get; private set; }
|
||||
public EphemeralNetworkSystemClockCore EphemeralNetworkSystemClock { get; private set; }
|
||||
public TimeSharedMemory SharedMemory { get; private set; }
|
||||
// TODO: 9.0.0+ power states and alarms
|
||||
|
@ -44,7 +45,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
StandardLocalSystemClock = new StandardLocalSystemClockCore(StandardSteadyClock);
|
||||
StandardNetworkSystemClock = new StandardNetworkSystemClockCore(StandardSteadyClock);
|
||||
StandardUserSystemClock = new StandardUserSystemClockCore(StandardLocalSystemClock, StandardNetworkSystemClock);
|
||||
TimeZone = new TimeZoneManager();
|
||||
TimeZone = new TimeZoneContentManager();
|
||||
EphemeralNetworkSystemClock = new EphemeralNetworkSystemClockCore(StandardSteadyClock);
|
||||
SharedMemory = new TimeSharedMemory();
|
||||
LocalClockContextWriter = new LocalSystemClockContextWriter(SharedMemory);
|
||||
|
@ -60,6 +61,11 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
StandardUserSystemClock.CreateAutomaticCorrectionEvent(system);
|
||||
}
|
||||
|
||||
public void InitializeTimeZone(Switch device)
|
||||
{
|
||||
TimeZone.Initialize(this, device);
|
||||
}
|
||||
|
||||
|
||||
public ResultCode SetupStandardSteadyClock(KThread thread, UInt128 clockSourceId, TimeSpanType setupValue, TimeSpanType internalOffset, TimeSpanType testOffset, bool isRtcResetDetected)
|
||||
{
|
||||
|
@ -125,6 +131,20 @@ namespace Ryujinx.HLE.HOS.Services.Time
|
|||
|
||||
}
|
||||
|
||||
public void SetupTimeZoneManager(string locationName, SteadyClockTimePoint timeZoneUpdatedTimePoint, uint totalLocationNameCount, UInt128 timeZoneRuleVersion, Stream timeZoneBinaryStream)
|
||||
{
|
||||
// TODO: if the result of this is wrong, abort
|
||||
TimeZone.Manager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
|
||||
|
||||
TimeZone.Manager.SetUpdatedTime(timeZoneUpdatedTimePoint, true);
|
||||
TimeZone.Manager.SetTotalLocationNameCount(totalLocationNameCount);
|
||||
TimeZone.Manager.SetTimeZoneRuleVersion(timeZoneRuleVersion);
|
||||
TimeZone.Manager.MarkInitialized();
|
||||
|
||||
// TODO: propagate IPC late binding of "time:s" and "time:p"
|
||||
|
||||
}
|
||||
|
||||
public void SetupEphemeralNetworkSystemClock()
|
||||
{
|
||||
EphemeralNetworkSystemClock.SetUpdateCallbackInstance(EphemeralClockContextWriter);
|
||||
|
|
188
Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs
Normal file
188
Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs
Normal file
|
@ -0,0 +1,188 @@
|
|||
using LibHac.Fs;
|
||||
using LibHac.Fs.NcaUtils;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.FileSystem;
|
||||
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
||||
using Ryujinx.HLE.Resource;
|
||||
using Ryujinx.HLE.Utilities;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
||||
{
|
||||
class TimeZoneContentManager
|
||||
{
|
||||
private const long TimeZoneBinaryTitleId = 0x010000000000080E;
|
||||
|
||||
private Switch _device;
|
||||
private string[] _locationNameCache;
|
||||
|
||||
public TimeZoneManagerForPsc Manager { get; private set; }
|
||||
|
||||
public TimeZoneContentManager()
|
||||
{
|
||||
Manager = new TimeZoneManagerForPsc();
|
||||
}
|
||||
|
||||
internal void Initialize(TimeManager timeManager, Switch device)
|
||||
{
|
||||
_device = device;
|
||||
|
||||
InitializeLocationNameCache();
|
||||
|
||||
SteadyClockTimePoint timeZoneUpdatedTimePoint = timeManager.StandardSteadyClock.GetCurrentTimePoint(null);
|
||||
|
||||
ResultCode result = GetTimeZoneBinary("UTC", out Stream timeZoneBinaryStream);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
// TODO: Read TimeZoneVersion from sysarchive.
|
||||
timeManager.SetupTimeZoneManager("UTC", timeZoneUpdatedTimePoint, (uint)_locationNameCache.Length, new UInt128(), timeZoneBinaryStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
// In the case the user don't have the timezone system archive, we just mark the manager as initialized.
|
||||
Manager.MarkInitialized();
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeLocationNameCache()
|
||||
{
|
||||
if (HasTimeZoneBinaryTitle())
|
||||
{
|
||||
using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
|
||||
{
|
||||
Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
|
||||
IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
|
||||
Stream binaryListStream = romfs.OpenFile("binaryList.txt", OpenMode.Read).AsStream();
|
||||
|
||||
StreamReader reader = new StreamReader(binaryListStream);
|
||||
|
||||
List<string> locationNameList = new List<string>();
|
||||
|
||||
string locationName;
|
||||
while ((locationName = reader.ReadLine()) != null)
|
||||
{
|
||||
locationNameList.Add(locationName);
|
||||
}
|
||||
|
||||
_locationNameCache = locationNameList.ToArray();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_locationNameCache = new string[0];
|
||||
Logger.PrintWarning(LogClass.ServiceTime, "TimeZoneBinary system archive not found! TimeZone conversions will not work, provide the system archive to fix.");
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsLocationNameValid(string locationName)
|
||||
{
|
||||
foreach (string cachedLocationName in _locationNameCache)
|
||||
{
|
||||
if (cachedLocationName.Equals(locationName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public ResultCode SetDeviceLocationName(string locationName)
|
||||
{
|
||||
ResultCode result = GetTimeZoneBinary(locationName, out Stream timeZoneBinaryStream);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
result = Manager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public ResultCode LoadLocationNameList(uint index, out string[] outLocationNameArray, uint maxLength)
|
||||
{
|
||||
List<string> locationNameList = new List<string>();
|
||||
|
||||
for (int i = 0; i < _locationNameCache.Length && i < maxLength; i++)
|
||||
{
|
||||
if (i < index)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string locationName = _locationNameCache[i];
|
||||
|
||||
// If the location name is too long, error out.
|
||||
if (locationName.Length > 0x24)
|
||||
{
|
||||
outLocationNameArray = new string[0];
|
||||
|
||||
return ResultCode.LocationNameTooLong;
|
||||
}
|
||||
|
||||
locationNameList.Add(locationName);
|
||||
}
|
||||
|
||||
outLocationNameArray = locationNameList.ToArray();
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
public string GetTimeZoneBinaryTitleContentPath()
|
||||
{
|
||||
return _device.System.ContentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, ContentType.Data);
|
||||
}
|
||||
|
||||
public bool HasTimeZoneBinaryTitle()
|
||||
{
|
||||
return !string.IsNullOrEmpty(GetTimeZoneBinaryTitleContentPath());
|
||||
}
|
||||
|
||||
internal ResultCode GetTimeZoneBinary(string locationName, out Stream timeZoneBinaryStream)
|
||||
{
|
||||
timeZoneBinaryStream = null;
|
||||
|
||||
if (!IsLocationNameValid(locationName))
|
||||
{
|
||||
return ResultCode.TimeZoneNotFound;
|
||||
}
|
||||
|
||||
using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
|
||||
{
|
||||
Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
|
||||
IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
|
||||
timeZoneBinaryStream = romfs.OpenFile($"zoneinfo/{locationName}", OpenMode.Read).AsStream();
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
internal ResultCode LoadTimeZoneRule(out TimeZoneRule outRules, string locationName)
|
||||
{
|
||||
outRules = new TimeZoneRule
|
||||
{
|
||||
Ats = new long[TzMaxTimes],
|
||||
Types = new byte[TzMaxTimes],
|
||||
Ttis = new TimeTypeInfo[TzMaxTypes],
|
||||
Chars = new char[TzCharsArraySize]
|
||||
};
|
||||
|
||||
if (!HasTimeZoneBinaryTitle())
|
||||
{
|
||||
throw new InvalidSystemResourceException($"TimeZoneBinary system archive not found! Please provide it.");
|
||||
}
|
||||
|
||||
ResultCode result = GetTimeZoneBinary(locationName, out Stream timeZoneBinaryStream);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
result = Manager.ParseTimeZoneRuleBinary(out outRules, timeZoneBinaryStream);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,269 +0,0 @@
|
|||
using LibHac.Fs;
|
||||
using LibHac.Fs.NcaUtils;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.FileSystem;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using TimeZoneConverter;
|
||||
using TimeZoneConverter.Posix;
|
||||
|
||||
using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
||||
{
|
||||
// TODO: rewrite it for psc/glue changes + readd a correct locking around this
|
||||
public sealed class TimeZoneManager
|
||||
{
|
||||
private const long TimeZoneBinaryTitleId = 0x010000000000080E;
|
||||
|
||||
private Switch _device;
|
||||
private TimeZoneRule _myRules;
|
||||
private string _deviceLocationName;
|
||||
private string[] _locationNameCache;
|
||||
|
||||
public TimeZoneManager()
|
||||
{
|
||||
// Empty rules (UTC)
|
||||
_myRules = new TimeZoneRule
|
||||
{
|
||||
Ats = new long[TzMaxTimes],
|
||||
Types = new byte[TzMaxTimes],
|
||||
Ttis = new TimeTypeInfo[TzMaxTypes],
|
||||
Chars = new char[TzCharsArraySize]
|
||||
};
|
||||
|
||||
_deviceLocationName = "UTC";
|
||||
}
|
||||
|
||||
internal void Initialize(Switch device)
|
||||
{
|
||||
_device = device;
|
||||
|
||||
InitializeLocationNameCache();
|
||||
}
|
||||
|
||||
private void InitializeLocationNameCache()
|
||||
{
|
||||
if (HasTimeZoneBinaryTitle())
|
||||
{
|
||||
using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
|
||||
{
|
||||
Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
|
||||
IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
|
||||
Stream binaryListStream = romfs.OpenFile("binaryList.txt", OpenMode.Read).AsStream();
|
||||
|
||||
StreamReader reader = new StreamReader(binaryListStream);
|
||||
|
||||
List<string> locationNameList = new List<string>();
|
||||
|
||||
string locationName;
|
||||
while ((locationName = reader.ReadLine()) != null)
|
||||
{
|
||||
locationNameList.Add(locationName);
|
||||
}
|
||||
|
||||
_locationNameCache = locationNameList.ToArray();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadOnlyCollection<TimeZoneInfo> timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
|
||||
_locationNameCache = new string[timeZoneInfos.Count];
|
||||
|
||||
int i = 0;
|
||||
|
||||
foreach (TimeZoneInfo timeZoneInfo in timeZoneInfos)
|
||||
{
|
||||
bool needConversion = TZConvert.TryWindowsToIana(timeZoneInfo.Id, out string convertedName);
|
||||
if (needConversion)
|
||||
{
|
||||
_locationNameCache[i] = convertedName;
|
||||
}
|
||||
else
|
||||
{
|
||||
_locationNameCache[i] = timeZoneInfo.Id;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// As we aren't using the system archive, "UTC" might not exist on the host system.
|
||||
// Load from C# TimeZone APIs UTC id.
|
||||
string utcId = TimeZoneInfo.Utc.Id;
|
||||
bool utcNeedConversion = TZConvert.TryWindowsToIana(utcId, out string utcConvertedName);
|
||||
if (utcNeedConversion)
|
||||
{
|
||||
utcId = utcConvertedName;
|
||||
}
|
||||
|
||||
_deviceLocationName = utcId;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsLocationNameValid(string locationName)
|
||||
{
|
||||
foreach (string cachedLocationName in _locationNameCache)
|
||||
{
|
||||
if (cachedLocationName.Equals(locationName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public string GetDeviceLocationName()
|
||||
{
|
||||
return _deviceLocationName;
|
||||
}
|
||||
|
||||
public ResultCode SetDeviceLocationName(string locationName)
|
||||
{
|
||||
ResultCode resultCode = LoadTimeZoneRules(out TimeZoneRule rules, locationName);
|
||||
|
||||
if (resultCode == 0)
|
||||
{
|
||||
_myRules = rules;
|
||||
_deviceLocationName = locationName;
|
||||
}
|
||||
|
||||
return resultCode;
|
||||
}
|
||||
|
||||
public ResultCode LoadLocationNameList(uint index, out string[] outLocationNameArray, uint maxLength)
|
||||
{
|
||||
List<string> locationNameList = new List<string>();
|
||||
|
||||
for (int i = 0; i < _locationNameCache.Length && i < maxLength; i++)
|
||||
{
|
||||
if (i < index)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string locationName = _locationNameCache[i];
|
||||
|
||||
// If the location name is too long, error out.
|
||||
if (locationName.Length > 0x24)
|
||||
{
|
||||
outLocationNameArray = new string[0];
|
||||
|
||||
return ResultCode.LocationNameTooLong;
|
||||
}
|
||||
|
||||
locationNameList.Add(locationName);
|
||||
}
|
||||
|
||||
outLocationNameArray = locationNameList.ToArray();
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
public uint GetTotalLocationNameCount()
|
||||
{
|
||||
return (uint)_locationNameCache.Length;
|
||||
}
|
||||
|
||||
public string GetTimeZoneBinaryTitleContentPath()
|
||||
{
|
||||
return _device.System.ContentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, ContentType.Data);
|
||||
}
|
||||
|
||||
public bool HasTimeZoneBinaryTitle()
|
||||
{
|
||||
return !string.IsNullOrEmpty(GetTimeZoneBinaryTitleContentPath());
|
||||
}
|
||||
|
||||
internal ResultCode LoadTimeZoneRules(out TimeZoneRule outRules, string locationName)
|
||||
{
|
||||
outRules = new TimeZoneRule
|
||||
{
|
||||
Ats = new long[TzMaxTimes],
|
||||
Types = new byte[TzMaxTimes],
|
||||
Ttis = new TimeTypeInfo[TzMaxTypes],
|
||||
Chars = new char[TzCharsArraySize]
|
||||
};
|
||||
|
||||
if (!IsLocationNameValid(locationName))
|
||||
{
|
||||
return ResultCode.TimeZoneNotFound;
|
||||
}
|
||||
|
||||
if (!HasTimeZoneBinaryTitle())
|
||||
{
|
||||
// If the user doesn't have the system archives, we generate a POSIX rule string and parse it to generate a incomplete TimeZoneRule
|
||||
// TODO: As for now not having system archives is fine, we should enforce the usage of system archives later.
|
||||
Logger.PrintWarning(LogClass.ServiceTime, "TimeZoneBinary system archive not found! Time conversions will not be accurate!");
|
||||
try
|
||||
{
|
||||
TimeZoneInfo info = TZConvert.GetTimeZoneInfo(locationName);
|
||||
string posixRule = PosixTimeZone.FromTimeZoneInfo(info);
|
||||
|
||||
if (!TimeZone.ParsePosixName(posixRule, out outRules))
|
||||
{
|
||||
return ResultCode.TimeZoneConversionFailed;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (TimeZoneNotFoundException)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {locationName})");
|
||||
|
||||
return ResultCode.TimeZoneNotFound;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
|
||||
{
|
||||
Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
|
||||
IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
|
||||
Stream tzIfStream = romfs.OpenFile($"zoneinfo/{locationName}", OpenMode.Read).AsStream();
|
||||
|
||||
if (!TimeZone.ParseTimeZoneBinary(out outRules, tzIfStream))
|
||||
{
|
||||
return ResultCode.TimeZoneConversionFailed;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal ResultCode ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
|
||||
{
|
||||
return ToCalendarTime(_myRules, time, out calendar);
|
||||
}
|
||||
|
||||
internal static ResultCode ToCalendarTime(TimeZoneRule rules, long time, out CalendarInfo calendar)
|
||||
{
|
||||
ResultCode error = TimeZone.ToCalendarTime(rules, time, out calendar);
|
||||
|
||||
if (error != ResultCode.Success)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
internal ResultCode ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
|
||||
{
|
||||
return ToPosixTime(_myRules, calendarTime, out posixTime);
|
||||
}
|
||||
|
||||
internal static ResultCode ToPosixTime(TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
|
||||
{
|
||||
ResultCode error = TimeZone.ToPosixTime(rules, calendarTime, out posixTime);
|
||||
|
||||
if (error != ResultCode.Success)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,12 +37,22 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
|||
|
||||
public bool IsInitialized()
|
||||
{
|
||||
return _isInitialized;
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
bool res = _isInitialized;
|
||||
|
||||
Monitor.Exit(_lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public void MarkInitialized()
|
||||
{
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
_isInitialized = true;
|
||||
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
public ResultCode GetDeviceLocationName(out string deviceLocationName)
|
||||
|
@ -87,7 +97,9 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
|||
public void SetTotalLocationNameCount(uint totalLocationNameCount)
|
||||
{
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
_totalLocationNameCount = totalLocationNameCount;
|
||||
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
|
@ -110,13 +122,13 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
|
|||
return result;
|
||||
}
|
||||
|
||||
public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint)
|
||||
public ResultCode SetUpdatedTime(SteadyClockTimePoint timeZoneUpdatedTimePoint, bool bypassUninitialized = false)
|
||||
{
|
||||
ResultCode result = ResultCode.UninitializedClock;
|
||||
|
||||
Monitor.Enter(_lock);
|
||||
|
||||
if (_isInitialized)
|
||||
if (_isInitialized || bypassUninitialized)
|
||||
{
|
||||
_timeZoneUpdateTimePoint = timeZoneUpdatedTimePoint;
|
||||
result = ResultCode.Success;
|
||||
|
|
Loading…
Add table
Reference in a new issue