From 0dd38028cbf54b672a41fcec0024740ff8ccea72 Mon Sep 17 00:00:00 2001 From: Thog Date: Wed, 25 Mar 2020 23:23:21 +0100 Subject: [PATCH] Make Device Location Name configuration (custom TZ) (#1031) This permit to use arbitrary timezone (instead of UTC). Useful for games like ACNH. --- .../Configuration/ConfigurationFileFormat.cs | 10 +++ .../Configuration/ConfigurationState.cs | 24 ++++++- .../Time/TimeZone/TimeZoneContentManager.cs | 70 +++++++++++++------ Ryujinx/Ui/MainWindow.cs | 2 +- Ryujinx/Ui/SwitchSettings.cs | 22 +++++- Ryujinx/Ui/SwitchSettings.glade | 47 ++++++++++++- 6 files changed, 148 insertions(+), 27 deletions(-) diff --git a/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs b/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs index a18db16ad..e4cd63d61 100644 --- a/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs +++ b/Ryujinx.Common/Configuration/ConfigurationFileFormat.cs @@ -16,6 +16,11 @@ namespace Ryujinx.Configuration { public class ConfigurationFileFormat { + /// + /// The current version of the file format + /// + public const int CurrentVersion = 3; + public int Version { get; set; } /// @@ -78,6 +83,11 @@ namespace Ryujinx.Configuration /// public Region SystemRegion { get; set; } + /// + /// Change System TimeZone + /// + public string SystemTimeZone { get; set; } + /// /// Enables or disables Docked Mode /// diff --git a/Ryujinx.Common/Configuration/ConfigurationState.cs b/Ryujinx.Common/Configuration/ConfigurationState.cs index 0f7b40841..e563008ac 100644 --- a/Ryujinx.Common/Configuration/ConfigurationState.cs +++ b/Ryujinx.Common/Configuration/ConfigurationState.cs @@ -153,6 +153,11 @@ namespace Ryujinx.Configuration /// public ReactiveObject Region { get; private set; } + /// + /// Change System TimeZone + /// + public ReactiveObject TimeZone { get; private set; } + /// /// Enables or disables Docked Mode /// @@ -182,6 +187,7 @@ namespace Ryujinx.Configuration { Language = new ReactiveObject(); Region = new ReactiveObject(); + TimeZone = new ReactiveObject(); EnableDockedMode = new ReactiveObject(); EnableMulticoreScheduling = new ReactiveObject(); EnableFsIntegrityChecks = new ReactiveObject(); @@ -295,7 +301,7 @@ namespace Ryujinx.Configuration { ConfigurationFileFormat configurationFile = new ConfigurationFileFormat { - Version = 2, + Version = ConfigurationFileFormat.CurrentVersion, GraphicsShadersDumpPath = Graphics.ShadersDumpPath, LoggingEnableDebug = Logger.EnableDebug, LoggingEnableStub = Logger.EnableStub, @@ -308,6 +314,7 @@ namespace Ryujinx.Configuration EnableFileLog = Logger.EnableFileLog, SystemLanguage = System.Language, SystemRegion = System.Region, + SystemTimeZone = System.TimeZone, DockedMode = System.EnableDockedMode, EnableDiscordIntegration = EnableDiscordIntegration, EnableVsync = Graphics.EnableVsync, @@ -354,6 +361,7 @@ namespace Ryujinx.Configuration Logger.EnableFileLog.Value = true; System.Language.Value = Language.AmericanEnglish; System.Region.Value = Region.USA; + System.TimeZone.Value = "UTC"; System.EnableDockedMode.Value = false; EnableDiscordIntegration.Value = true; Graphics.EnableVsync.Value = true; @@ -452,7 +460,7 @@ namespace Ryujinx.Configuration { bool configurationFileUpdated = false; - if (configurationFileFormat.Version < 0 || configurationFileFormat.Version > 2) + if (configurationFileFormat.Version < 0 || configurationFileFormat.Version > ConfigurationFileFormat.CurrentVersion) { Common.Logging.Logger.PrintWarning(LogClass.Application, $"Unsupported configuration version {configurationFileFormat.Version}, loading default."); @@ -463,13 +471,22 @@ namespace Ryujinx.Configuration if (configurationFileFormat.Version < 2) { - Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, needs to be updated."); + Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 2."); configurationFileFormat.SystemRegion = Region.USA; configurationFileUpdated = true; } + if (configurationFileFormat.Version < 3) + { + Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 3."); + + configurationFileFormat.SystemTimeZone = "UTC"; + + configurationFileUpdated = true; + } + Graphics.ShadersDumpPath.Value = configurationFileFormat.GraphicsShadersDumpPath; Logger.EnableDebug.Value = configurationFileFormat.LoggingEnableDebug; Logger.EnableStub.Value = configurationFileFormat.LoggingEnableStub; @@ -482,6 +499,7 @@ namespace Ryujinx.Configuration Logger.EnableFileLog.Value = configurationFileFormat.EnableFileLog; System.Language.Value = configurationFileFormat.SystemLanguage; System.Region.Value = configurationFileFormat.SystemRegion; + System.TimeZone.Value = configurationFileFormat.SystemTimeZone; System.EnableDockedMode.Value = configurationFileFormat.DockedMode; System.EnableDockedMode.Value = configurationFileFormat.DockedMode; EnableDiscordIntegration.Value = configurationFileFormat.EnableDiscordIntegration; diff --git a/Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs b/Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs index 06d34e79b..ab583c1ba 100644 --- a/Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs +++ b/Ryujinx.HLE/HOS/Services/Time/TimeZone/TimeZoneContentManager.cs @@ -4,8 +4,10 @@ using LibHac.Fs; using LibHac.FsSystem; using LibHac.FsSystem.NcaUtils; using Ryujinx.Common.Logging; +using Ryujinx.Configuration; using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.FileSystem; +using Ryujinx.HLE.FileSystem.Content; using Ryujinx.HLE.HOS.Services.Time.Clock; using Ryujinx.HLE.Utilities; using System.Collections.Generic; @@ -15,34 +17,62 @@ using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule; namespace Ryujinx.HLE.HOS.Services.Time.TimeZone { - class TimeZoneContentManager + public class TimeZoneContentManager { private const long TimeZoneBinaryTitleId = 0x010000000000080E; - private Switch _device; - private string[] _locationNameCache; + private VirtualFileSystem _virtualFileSystem; + private IntegrityCheckLevel _fsIntegrityCheckLevel; + private ContentManager _contentManager; - public TimeZoneManager Manager { get; private set; } + public string[] LocationNameCache { get; private set; } + + internal TimeZoneManager Manager { get; private set; } public TimeZoneContentManager() { Manager = new TimeZoneManager(); } - internal void Initialize(TimeManager timeManager, Switch device) + public void InitializeInstance(VirtualFileSystem virtualFileSystem, ContentManager contentManager, IntegrityCheckLevel fsIntegrityCheckLevel) { - _device = device; + _virtualFileSystem = virtualFileSystem; + _contentManager = contentManager; + _fsIntegrityCheckLevel = fsIntegrityCheckLevel; InitializeLocationNameCache(); + } + + public string SanityCheckDeviceLocationName() + { + string locationName = ConfigurationState.Instance.System.TimeZone; + + if (IsLocationNameValid(locationName)) + { + return locationName; + } + + Logger.PrintWarning(LogClass.ServiceTime, $"Invalid device TimeZone {locationName}, switching back to UTC"); + + ConfigurationState.Instance.System.TimeZone.Value = "UTC"; + + return "UTC"; + } + + internal void Initialize(TimeManager timeManager, Switch device) + { + InitializeInstance(device.FileSystem, device.System.ContentManager, device.System.FsIntegrityCheckLevel); SteadyClockTimePoint timeZoneUpdatedTimePoint = timeManager.StandardSteadyClock.GetCurrentTimePoint(null); - ResultCode result = GetTimeZoneBinary("UTC", out Stream timeZoneBinaryStream, out LocalStorage ncaFile); + string deviceLocationName = SanityCheckDeviceLocationName(); + + ResultCode result = GetTimeZoneBinary(deviceLocationName, out Stream timeZoneBinaryStream, out LocalStorage ncaFile); if (result == ResultCode.Success) { // TODO: Read TimeZoneVersion from sysarchive. - timeManager.SetupTimeZoneManager("UTC", timeZoneUpdatedTimePoint, (uint)_locationNameCache.Length, new UInt128(), timeZoneBinaryStream); + timeManager.SetupTimeZoneManager(deviceLocationName, timeZoneUpdatedTimePoint, (uint)LocationNameCache.Length, new UInt128(), timeZoneBinaryStream); ncaFile.Dispose(); } @@ -57,10 +87,10 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone { if (HasTimeZoneBinaryTitle()) { - using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open)) + using (IStorage ncaFileStream = new LocalStorage(_virtualFileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open)) { - Nca nca = new Nca(_device.System.KeySet, ncaFileStream); - IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel); + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFileStream); + IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _fsIntegrityCheckLevel); romfs.OpenFile(out IFile binaryListFile, "/binaryList.txt".ToU8Span(), OpenMode.Read).ThrowIfFailure(); @@ -74,12 +104,12 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone locationNameList.Add(locationName); } - _locationNameCache = locationNameList.ToArray(); + LocationNameCache = locationNameList.ToArray(); } } else { - _locationNameCache = new string[0]; + LocationNameCache = new string[] { "UTC" }; Logger.PrintWarning(LogClass.ServiceTime, "TimeZoneBinary system title not found! TimeZone conversions will not work, provide the system archive to fix this warning. (See https://github.com/Ryujinx/Ryujinx#requirements for more informations)"); } @@ -87,7 +117,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone private bool IsLocationNameValid(string locationName) { - foreach (string cachedLocationName in _locationNameCache) + foreach (string cachedLocationName in LocationNameCache) { if (cachedLocationName.Equals(locationName)) { @@ -116,14 +146,14 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone { List locationNameList = new List(); - for (int i = 0; i < _locationNameCache.Length && i < maxLength; i++) + for (int i = 0; i < LocationNameCache.Length && i < maxLength; i++) { if (i < index) { continue; } - string locationName = _locationNameCache[i]; + string locationName = LocationNameCache[i]; // If the location name is too long, error out. if (locationName.Length > 0x24) @@ -143,7 +173,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone public string GetTimeZoneBinaryTitleContentPath() { - return _device.System.ContentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, NcaContentType.Data); + return _contentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, NcaContentType.Data); } public bool HasTimeZoneBinaryTitle() @@ -161,10 +191,10 @@ namespace Ryujinx.HLE.HOS.Services.Time.TimeZone return ResultCode.TimeZoneNotFound; } - ncaFile = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open); + ncaFile = new LocalStorage(_virtualFileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open); - Nca nca = new Nca(_device.System.KeySet, ncaFile); - IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel); + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile); + IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _fsIntegrityCheckLevel); Result result = romfs.OpenFile(out IFile timeZoneBinaryFile, $"/zoneinfo/{locationName}".ToU8Span(), OpenMode.Read); diff --git a/Ryujinx/Ui/MainWindow.cs b/Ryujinx/Ui/MainWindow.cs index 05c9686db..5b75848d7 100644 --- a/Ryujinx/Ui/MainWindow.cs +++ b/Ryujinx/Ui/MainWindow.cs @@ -935,7 +935,7 @@ namespace Ryujinx.Ui private void Settings_Pressed(object sender, EventArgs args) { - SwitchSettings settingsWin = new SwitchSettings(); + SwitchSettings settingsWin = new SwitchSettings(_virtualFileSystem, _contentManager); settingsWin.Show(); } diff --git a/Ryujinx/Ui/SwitchSettings.cs b/Ryujinx/Ui/SwitchSettings.cs index 6fe490c80..777313a1c 100644 --- a/Ryujinx/Ui/SwitchSettings.cs +++ b/Ryujinx/Ui/SwitchSettings.cs @@ -2,6 +2,7 @@ using Ryujinx.Configuration; using Ryujinx.Configuration.Hid; using Ryujinx.Configuration.System; +using Ryujinx.HLE.HOS.Services.Time.TimeZone; using System; using System.Collections.Generic; using System.Diagnostics; @@ -40,6 +41,7 @@ namespace Ryujinx.Ui [GUI] CheckButton _directKeyboardAccess; [GUI] ComboBoxText _systemLanguageSelect; [GUI] ComboBoxText _systemRegionSelect; + [GUI] ComboBoxText _systemTimeZoneSelect; [GUI] CheckButton _custThemeToggle; [GUI] Entry _custThemePath; [GUI] ToggleButton _browseThemePath; @@ -80,9 +82,9 @@ namespace Ryujinx.Ui #pragma warning restore CS0649 #pragma warning restore IDE0044 - public SwitchSettings() : this(new Builder("Ryujinx.Ui.SwitchSettings.glade")) { } + public SwitchSettings(HLE.FileSystem.VirtualFileSystem virtualFileSystem, HLE.FileSystem.Content.ContentManager contentManager) : this(new Builder("Ryujinx.Ui.SwitchSettings.glade"), virtualFileSystem, contentManager) { } - private SwitchSettings(Builder builder) : base(builder.GetObject("_settingsWin").Handle) + private SwitchSettings(Builder builder, HLE.FileSystem.VirtualFileSystem virtualFileSystem, HLE.FileSystem.Content.ContentManager contentManager) : base(builder.GetObject("_settingsWin").Handle) { builder.Autoconnect(this); @@ -197,8 +199,22 @@ namespace Ryujinx.Ui _custThemeToggle.Click(); } + TimeZoneContentManager timeZoneContentManager = new TimeZoneContentManager(); + + timeZoneContentManager.InitializeInstance(virtualFileSystem, contentManager, LibHac.FsSystem.IntegrityCheckLevel.None); + + List locationNames = timeZoneContentManager.LocationNameCache.ToList(); + + locationNames.Sort(); + + foreach (string locationName in locationNames) + { + _systemTimeZoneSelect.Append(locationName, locationName); + } + _systemLanguageSelect.SetActiveId(ConfigurationState.Instance.System.Language.Value.ToString()); _systemRegionSelect .SetActiveId(ConfigurationState.Instance.System.Region.Value.ToString()); + _systemTimeZoneSelect.SetActiveId(timeZoneContentManager.SanityCheckDeviceLocationName()); _controller1Type .SetActiveId(ConfigurationState.Instance.Hid.ControllerType.Value.ToString()); Controller_Changed(null, null, _controller1Type.ActiveId, _controller1Image); @@ -448,6 +464,8 @@ namespace Ryujinx.Ui ConfigurationState.Instance.Ui.GameDirs.Value = gameDirs; ConfigurationState.Instance.System.FsGlobalAccessLogMode.Value = (int)_fsLogSpinAdjustment.Value; + ConfigurationState.Instance.System.TimeZone.Value = _systemTimeZoneSelect.ActiveId; + MainWindow.SaveConfig(); MainWindow.ApplyTheme(); MainWindow.UpdateGameTable(); diff --git a/Ryujinx/Ui/SwitchSettings.glade b/Ryujinx/Ui/SwitchSettings.glade index a23bc199c..fc9413ec0 100644 --- a/Ryujinx/Ui/SwitchSettings.glade +++ b/Ryujinx/Ui/SwitchSettings.glade @@ -210,6 +210,51 @@ 0 + + + True + False + vertical + + + True + False + + + True + False + Change System TimeZone + end + System TimeZone: + + + False + True + 1 + + + + + True + False + Change System TimeZone + 5 + + + False + True + 2 + + + + + + + False + True + 1 + + Enable Discord Rich Presence @@ -224,7 +269,7 @@ False True 5 - 1 + 2