2019-10-17 02:17:44 -04:00
using LibHac ;
2020-03-25 04:14:35 -04:00
using LibHac.Common ;
2019-10-17 02:17:44 -04:00
using LibHac.Fs ;
2020-09-01 16:08:59 -04:00
using LibHac.Fs.Fsa ;
2019-10-17 02:17:44 -04:00
using LibHac.FsSystem ;
2022-03-22 15:46:16 -04:00
using LibHac.Ncm ;
2022-01-12 06:22:19 -05:00
using LibHac.Tools.FsSystem ;
using LibHac.Tools.FsSystem.NcaUtils ;
2019-10-07 23:48:49 -04:00
using Ryujinx.Common.Logging ;
2022-05-31 15:29:35 -04:00
using Ryujinx.Cpu ;
2019-10-15 20:30:36 -04:00
using Ryujinx.HLE.Exceptions ;
2019-10-07 23:48:49 -04:00
using Ryujinx.HLE.FileSystem ;
using Ryujinx.HLE.HOS.Services.Time.Clock ;
using Ryujinx.HLE.Utilities ;
2020-07-21 00:14:42 -04:00
using System ;
2019-10-07 23:48:49 -04:00
using System.Collections.Generic ;
using System.IO ;
2022-06-29 16:08:30 -04:00
using System.Text ;
2022-06-24 13:04:57 -04:00
using TimeZoneRuleBox = Ryujinx . Common . Memory . Box < Ryujinx . HLE . HOS . Services . Time . TimeZone . TimeZoneRule > ;
2019-10-07 23:48:49 -04:00
namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
{
2020-03-25 18:23:21 -04:00
public class TimeZoneContentManager
2019-10-07 23:48:49 -04:00
{
private const long TimeZoneBinaryTitleId = 0x010000000000080E ;
2020-08-30 12:35:42 -04:00
private readonly string TimeZoneSystemTitleMissingErrorMessage = "TimeZoneBinary system title not found! TimeZone conversions will not work, provide the system archive to fix this error. (See https://github.com/Ryujinx/Ryujinx/wiki/Ryujinx-Setup-&-Configuration-Guide#initial-setup-continued---installation-of-firmware for more information)" ;
2020-03-29 17:23:05 -04:00
2020-03-25 18:23:21 -04:00
private VirtualFileSystem _virtualFileSystem ;
private IntegrityCheckLevel _fsIntegrityCheckLevel ;
private ContentManager _contentManager ;
2019-10-07 23:48:49 -04:00
2020-03-25 18:23:21 -04:00
public string [ ] LocationNameCache { get ; private set ; }
internal TimeZoneManager Manager { get ; private set ; }
2019-10-07 23:48:49 -04:00
public TimeZoneContentManager ( )
{
Manager = new TimeZoneManager ( ) ;
}
2020-03-25 18:23:21 -04:00
public void InitializeInstance ( VirtualFileSystem virtualFileSystem , ContentManager contentManager , IntegrityCheckLevel fsIntegrityCheckLevel )
2019-10-07 23:48:49 -04:00
{
2020-03-25 18:23:21 -04:00
_virtualFileSystem = virtualFileSystem ;
_contentManager = contentManager ;
_fsIntegrityCheckLevel = fsIntegrityCheckLevel ;
2019-10-07 23:48:49 -04:00
InitializeLocationNameCache ( ) ;
2020-03-25 18:23:21 -04:00
}
2021-05-16 11:12:14 -04:00
public string SanityCheckDeviceLocationName ( string locationName )
2020-03-25 18:23:21 -04:00
{
if ( IsLocationNameValid ( locationName ) )
{
return locationName ;
}
2020-08-03 19:32:53 -04:00
Logger . Warning ? . Print ( LogClass . ServiceTime , $"Invalid device TimeZone {locationName}, switching back to UTC" ) ;
2020-03-25 18:23:21 -04:00
return "UTC" ;
}
internal void Initialize ( TimeManager timeManager , Switch device )
{
InitializeInstance ( device . FileSystem , device . System . ContentManager , device . System . FsIntegrityCheckLevel ) ;
2019-10-07 23:48:49 -04:00
2022-05-31 15:29:35 -04:00
ITickSource tickSource = device . System . TickSource ;
SteadyClockTimePoint timeZoneUpdatedTimePoint = timeManager . StandardSteadyClock . GetCurrentTimePoint ( tickSource ) ;
2019-10-07 23:48:49 -04:00
2021-05-16 11:12:14 -04:00
string deviceLocationName = SanityCheckDeviceLocationName ( device . Configuration . TimeZone ) ;
2020-03-25 18:23:21 -04:00
ResultCode result = GetTimeZoneBinary ( deviceLocationName , out Stream timeZoneBinaryStream , out LocalStorage ncaFile ) ;
2019-10-07 23:48:49 -04:00
if ( result = = ResultCode . Success )
{
// TODO: Read TimeZoneVersion from sysarchive.
2020-03-25 18:23:21 -04:00
timeManager . SetupTimeZoneManager ( deviceLocationName , timeZoneUpdatedTimePoint , ( uint ) LocationNameCache . Length , new UInt128 ( ) , timeZoneBinaryStream ) ;
2019-10-11 12:05:10 -04:00
ncaFile . Dispose ( ) ;
2019-10-07 23:48:49 -04:00
}
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 ( ) )
{
2020-03-25 18:23:21 -04:00
using ( IStorage ncaFileStream = new LocalStorage ( _virtualFileSystem . SwitchPathToSystemPath ( GetTimeZoneBinaryTitleContentPath ( ) ) , FileAccess . Read , FileMode . Open ) )
2019-10-07 23:48:49 -04:00
{
2020-03-25 18:23:21 -04:00
Nca nca = new Nca ( _virtualFileSystem . KeySet , ncaFileStream ) ;
IFileSystem romfs = nca . OpenFileSystem ( NcaSectionType . Data , _fsIntegrityCheckLevel ) ;
2019-10-07 23:48:49 -04:00
2021-12-23 11:55:50 -05:00
using var binaryListFile = new UniqueRef < IFile > ( ) ;
2019-10-17 02:17:44 -04:00
2023-03-01 21:42:27 -05:00
romfs . OpenFile ( ref binaryListFile . Ref , "/binaryList.txt" . ToU8Span ( ) , OpenMode . Read ) . ThrowIfFailure ( ) ;
2021-12-23 11:55:50 -05:00
StreamReader reader = new StreamReader ( binaryListFile . Get . AsStream ( ) ) ;
2019-10-07 23:48:49 -04:00
List < string > locationNameList = new List < string > ( ) ;
string locationName ;
while ( ( locationName = reader . ReadLine ( ) ) ! = null )
{
locationNameList . Add ( locationName ) ;
}
2020-03-25 18:23:21 -04:00
LocationNameCache = locationNameList . ToArray ( ) ;
2019-10-07 23:48:49 -04:00
}
}
else
{
2020-03-25 18:23:21 -04:00
LocationNameCache = new string [ ] { "UTC" } ;
2019-10-07 23:48:49 -04:00
2020-08-03 19:32:53 -04:00
Logger . Error ? . Print ( LogClass . ServiceTime , TimeZoneSystemTitleMissingErrorMessage ) ;
2019-10-07 23:48:49 -04:00
}
}
2020-07-21 00:14:42 -04:00
public IEnumerable < ( int Offset , string Location , string Abbr ) > ParseTzOffsets ( )
{
var tzBinaryContentPath = GetTimeZoneBinaryTitleContentPath ( ) ;
if ( string . IsNullOrEmpty ( tzBinaryContentPath ) )
{
return new [ ] { ( 0 , "UTC" , "UTC" ) } ;
}
List < ( int Offset , string Location , string Abbr ) > outList = new List < ( int Offset , string Location , string Abbr ) > ( ) ;
2022-12-10 15:21:13 -05:00
var now = DateTimeOffset . Now . ToUnixTimeSeconds ( ) ;
2020-07-21 00:14:42 -04:00
using ( IStorage ncaStorage = new LocalStorage ( _virtualFileSystem . SwitchPathToSystemPath ( tzBinaryContentPath ) , FileAccess . Read , FileMode . Open ) )
using ( IFileSystem romfs = new Nca ( _virtualFileSystem . KeySet , ncaStorage ) . OpenFileSystem ( NcaSectionType . Data , _fsIntegrityCheckLevel ) )
{
foreach ( string locName in LocationNameCache )
{
if ( locName . StartsWith ( "Etc" ) )
{
continue ;
}
2021-12-23 11:55:50 -05:00
using var tzif = new UniqueRef < IFile > ( ) ;
2023-03-01 21:42:27 -05:00
if ( romfs . OpenFile ( ref tzif . Ref , $"/zoneinfo/{locName}" . ToU8Span ( ) , OpenMode . Read ) . IsFailure ( ) )
2020-07-21 00:14:42 -04:00
{
2020-08-03 19:32:53 -04:00
Logger . Error ? . Print ( LogClass . ServiceTime , $"Error opening /zoneinfo/{locName}" ) ;
2020-07-21 00:14:42 -04:00
continue ;
}
2022-06-24 13:04:57 -04:00
TimeZoneRuleBox tzRuleBox = new TimeZoneRuleBox ( ) ;
ref TimeZoneRule tzRule = ref tzRuleBox . Data ;
TimeZone . ParseTimeZoneBinary ( ref tzRule , tzif . Get . AsStream ( ) ) ;
2020-07-21 00:14:42 -04:00
2021-12-23 11:55:50 -05:00
TimeTypeInfo ttInfo ;
if ( tzRule . TimeCount > 0 ) // Find the current transition period
{
int fin = 0 ;
for ( int i = 0 ; i < tzRule . TimeCount ; + + i )
2020-07-21 00:14:42 -04:00
{
2021-12-23 11:55:50 -05:00
if ( tzRule . Ats [ i ] < = now )
2020-07-21 00:14:42 -04:00
{
2021-12-23 11:55:50 -05:00
fin = i ;
2020-07-21 00:14:42 -04:00
}
}
2021-12-23 11:55:50 -05:00
ttInfo = tzRule . Ttis [ tzRule . Types [ fin ] ] ;
}
else if ( tzRule . TypeCount > = 1 ) // Otherwise, use the first offset in TTInfo
{
ttInfo = tzRule . Ttis [ 0 ] ;
}
else
{
Logger . Error ? . Print ( LogClass . ServiceTime , $"Couldn't find UTC offset for zone {locName}" ) ;
continue ;
}
2020-07-21 00:14:42 -04:00
2022-06-24 13:04:57 -04:00
var abbrStart = tzRule . Chars [ ttInfo . AbbreviationListIndex . . ] ;
int abbrEnd = abbrStart . IndexOf ( ( byte ) 0 ) ;
2020-07-21 00:14:42 -04:00
2022-06-29 16:08:30 -04:00
outList . Add ( ( ttInfo . GmtOffset , locName , Encoding . UTF8 . GetString ( abbrStart [ . . abbrEnd ] ) ) ) ;
2020-07-21 00:14:42 -04:00
}
}
outList . Sort ( ) ;
return outList ;
}
2019-10-07 23:48:49 -04:00
private bool IsLocationNameValid ( string locationName )
{
2020-03-25 18:23:21 -04:00
foreach ( string cachedLocationName in LocationNameCache )
2019-10-07 23:48:49 -04:00
{
if ( cachedLocationName . Equals ( locationName ) )
{
return true ;
}
}
return false ;
}
public ResultCode SetDeviceLocationName ( string locationName )
{
2019-10-11 12:05:10 -04:00
ResultCode result = GetTimeZoneBinary ( locationName , out Stream timeZoneBinaryStream , out LocalStorage ncaFile ) ;
2019-10-07 23:48:49 -04:00
if ( result = = ResultCode . Success )
{
result = Manager . SetDeviceLocationNameWithTimeZoneRule ( locationName , timeZoneBinaryStream ) ;
2019-10-11 12:05:10 -04:00
ncaFile . Dispose ( ) ;
2019-10-07 23:48:49 -04:00
}
return result ;
}
public ResultCode LoadLocationNameList ( uint index , out string [ ] outLocationNameArray , uint maxLength )
{
List < string > locationNameList = new List < string > ( ) ;
2020-03-25 18:23:21 -04:00
for ( int i = 0 ; i < LocationNameCache . Length & & i < maxLength ; i + + )
2019-10-07 23:48:49 -04:00
{
if ( i < index )
{
continue ;
}
2020-03-25 18:23:21 -04:00
string locationName = LocationNameCache [ i ] ;
2019-10-07 23:48:49 -04:00
// If the location name is too long, error out.
if ( locationName . Length > 0x24 )
{
2023-03-11 15:05:48 -05:00
outLocationNameArray = Array . Empty < string > ( ) ;
2019-10-07 23:48:49 -04:00
return ResultCode . LocationNameTooLong ;
}
locationNameList . Add ( locationName ) ;
}
outLocationNameArray = locationNameList . ToArray ( ) ;
return ResultCode . Success ;
}
public string GetTimeZoneBinaryTitleContentPath ( )
{
2022-03-22 15:46:16 -04:00
return _contentManager . GetInstalledContentPath ( TimeZoneBinaryTitleId , StorageId . BuiltInSystem , NcaContentType . Data ) ;
2019-10-07 23:48:49 -04:00
}
public bool HasTimeZoneBinaryTitle ( )
{
return ! string . IsNullOrEmpty ( GetTimeZoneBinaryTitleContentPath ( ) ) ;
}
2019-10-11 12:05:10 -04:00
internal ResultCode GetTimeZoneBinary ( string locationName , out Stream timeZoneBinaryStream , out LocalStorage ncaFile )
2019-10-07 23:48:49 -04:00
{
timeZoneBinaryStream = null ;
2019-10-11 12:05:10 -04:00
ncaFile = null ;
2019-10-07 23:48:49 -04:00
2020-03-29 17:23:05 -04:00
if ( ! HasTimeZoneBinaryTitle ( ) | | ! IsLocationNameValid ( locationName ) )
2019-10-07 23:48:49 -04:00
{
return ResultCode . TimeZoneNotFound ;
}
2020-03-25 18:23:21 -04:00
ncaFile = new LocalStorage ( _virtualFileSystem . SwitchPathToSystemPath ( GetTimeZoneBinaryTitleContentPath ( ) ) , FileAccess . Read , FileMode . Open ) ;
2019-10-07 23:48:49 -04:00
2020-03-25 18:23:21 -04:00
Nca nca = new Nca ( _virtualFileSystem . KeySet , ncaFile ) ;
IFileSystem romfs = nca . OpenFileSystem ( NcaSectionType . Data , _fsIntegrityCheckLevel ) ;
2019-10-11 12:05:10 -04:00
2021-12-23 11:55:50 -05:00
using var timeZoneBinaryFile = new UniqueRef < IFile > ( ) ;
2023-03-01 21:42:27 -05:00
Result result = romfs . OpenFile ( ref timeZoneBinaryFile . Ref , $"/zoneinfo/{locationName}" . ToU8Span ( ) , OpenMode . Read ) ;
2019-10-07 23:48:49 -04:00
2021-12-23 11:55:50 -05:00
timeZoneBinaryStream = timeZoneBinaryFile . Release ( ) . AsStream ( ) ;
2019-10-17 02:17:44 -04:00
return ( ResultCode ) result . Value ;
2019-10-07 23:48:49 -04:00
}
2022-06-24 13:04:57 -04:00
internal ResultCode LoadTimeZoneRule ( ref TimeZoneRule rules , string locationName )
2019-10-07 23:48:49 -04:00
{
2022-06-24 13:04:57 -04:00
rules = default ;
2019-10-07 23:48:49 -04:00
if ( ! HasTimeZoneBinaryTitle ( ) )
{
2020-03-29 17:23:05 -04:00
throw new InvalidSystemResourceException ( TimeZoneSystemTitleMissingErrorMessage ) ;
2019-10-07 23:48:49 -04:00
}
2019-10-11 12:05:10 -04:00
ResultCode result = GetTimeZoneBinary ( locationName , out Stream timeZoneBinaryStream , out LocalStorage ncaFile ) ;
2019-10-07 23:48:49 -04:00
if ( result = = ResultCode . Success )
{
2022-06-24 13:04:57 -04:00
result = Manager . ParseTimeZoneRuleBinary ( ref rules , timeZoneBinaryStream ) ;
2019-10-11 12:05:10 -04:00
ncaFile . Dispose ( ) ;
2019-10-07 23:48:49 -04:00
}
return result ;
}
}
2020-08-30 12:35:42 -04:00
}