From 12005f0e1a30aa91b14078a6c4bc10bb10de525e Mon Sep 17 00:00:00 2001 From: status102 <102887808+status102@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:58:00 +0800 Subject: [PATCH] perf: null check [skip changelog] --- src/MaaWpfGui/Main/AsstProxy.cs | 4 ++-- .../ViewModels/UI/SettingsViewModel.cs | 17 ++++++++--------- .../ViewModels/UI/TaskQueueViewModel.cs | 3 +-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index 5a35d1d777..d6b7f6ca88 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -1688,7 +1688,7 @@ namespace MaaWpfGui.Main /// /// 连接地址。 /// 是否有效。 - private static bool IfPortEstablished(string address) + private static bool IfPortEstablished(string? address) { if (string.IsNullOrEmpty(address) || !address.Contains(':')) { @@ -1745,7 +1745,7 @@ namespace MaaWpfGui.Main if (Instances.SettingsViewModel.AutoDetectConnection) { - string bsHvAddress = Instances.SettingsViewModel.TryToSetBlueStacksHyperVAddress(); + string? bsHvAddress = Instances.SettingsViewModel.TryToSetBlueStacksHyperVAddress(); if (string.Equals(Instances.SettingsViewModel.ConnectAddress, bsHvAddress)) { diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index 2a25df78c5..00f8dd71d5 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -1943,7 +1943,7 @@ namespace MaaWpfGui.ViewModels.UI } var jsonStr = File.ReadAllText(filePath); - var json = (JObject)JsonConvert.DeserializeObject(jsonStr); + var json = (JObject?)JsonConvert.DeserializeObject(jsonStr); var roguelikeCoreCharList = new ObservableCollection(); @@ -1964,7 +1964,7 @@ namespace MaaWpfGui.ViewModels.UI continue; } - var name = (string)operItem["name"]; + var name = (string?)operItem["name"]; if (string.IsNullOrEmpty(name)) { continue; @@ -3419,7 +3419,7 @@ namespace MaaWpfGui.ViewModels.UI return versionName; } - JObject versionJson = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(jsonPath)); + var versionJson = (JObject?)JsonConvert.DeserializeObject(File.ReadAllText(jsonPath)); var currentTime = (ulong)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); var poolTime = (ulong?)versionJson?["gacha"]?["time"]; // 卡池的开始时间 var activityTime = (ulong?)versionJson?["activity"]?["time"]; // 活动的开始时间 @@ -4172,7 +4172,7 @@ namespace MaaWpfGui.ViewModels.UI /// Get the path of bluestacks.conf /// /// path - private static string GetBluestacksConfig() + private static string? GetBluestacksConfig() { var conf = ConfigurationHelper.GetValue(ConfigurationKeys.BluestacksConfigPath, string.Empty); if (!string.IsNullOrEmpty(conf)) @@ -4180,8 +4180,8 @@ namespace MaaWpfGui.ViewModels.UI return conf; } - using RegistryKey key = Registry.LocalMachine.OpenSubKey(BluestacksNxtRegistryKey); - object value = key?.GetValue(BluestacksNxtValueName); + using var key = Registry.LocalMachine.OpenSubKey(BluestacksNxtRegistryKey); + var value = key?.GetValue(BluestacksNxtValueName); if (value != null) { return (string)value + @"\bluestacks.conf"; @@ -4334,14 +4334,14 @@ namespace MaaWpfGui.ViewModels.UI rvm.WindowTitle = $"{prefix}MAA{currentConfiguration} - {CoreVersion}{resourceVersion}{connectConfigName}{connectAddress}{clientName}"; } - private readonly string _bluestacksConfig = GetBluestacksConfig(); + private readonly string? _bluestacksConfig = GetBluestacksConfig(); private string _bluestacksKeyWord = ConfigurationHelper.GetValue(ConfigurationKeys.BluestacksConfigKeyword, string.Empty); /// /// Tries to set BlueStack Hyper V address. /// /// success - public string TryToSetBlueStacksHyperVAddress() + public string? TryToSetBlueStacksHyperVAddress() { if (string.IsNullOrEmpty(_bluestacksConfig)) { @@ -4574,7 +4574,6 @@ namespace MaaWpfGui.ViewModels.UI } } - private bool _minimizeToTray = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeToTray, bool.FalseString)); /// diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 5b5bfaa45e..f6dc757848 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -116,7 +116,6 @@ namespace MaaWpfGui.ViewModels.UI await Task.Run(() => Instances.SettingsViewModel.RunScript("EndsWithScript")); var actions = TaskSettingDataContext.PostActionSetting; - if (actions.BackToAndroidHome) { Instances.AsstProxy.AsstBackToHome(); @@ -2536,7 +2535,7 @@ namespace MaaWpfGui.ViewModels.UI if (value >= CustomInfrastPlanInfoList.Count || value < 0) { var count = CustomInfrastPlanInfoList.Count; - value = (value % count + count) % count; + value = ((value % count) + count) % count; _logger.Warning($"CustomInfrastPlanIndex out of range, reset to Index % Count: {value}"); }