perf: 根据 qodana 优化代码

This commit is contained in:
uye
2023-10-07 14:09:12 +08:00
parent 62821c451f
commit 283f12c9e7
13 changed files with 91 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
@@ -14,11 +14,12 @@ using Serilog;
namespace MaaWpfGui.Configuration
{
public class ConfigFactory
public static class ConfigFactory
{
private static readonly string _configurationFile = Path.Combine(Environment.CurrentDirectory, "config/gui.new.json");
// TODO: write backup method. WIP: https://github.com/Cryolitia/MaaAssistantArknights/tree/config
// ReSharper disable once UnusedMember.Local
private static readonly string _configurationBakFile = Path.Combine(Environment.CurrentDirectory, "config/gui.new.json.bak");
private static readonly ILogger _logger = Log.ForContext<ConfigurationHelper>();
@@ -27,6 +28,7 @@ namespace MaaWpfGui.Configuration
public delegate void ConfigurationUpdateEventHandler(string key, object oldValue, object newValue);
// ReSharper disable once EventNeverSubscribedTo.Global
public static event ConfigurationUpdateEventHandler ConfigurationUpdateEvent;
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions {WriteIndented = true, Converters = {new JsonStringEnumConverter()}};
@@ -59,10 +61,7 @@ namespace MaaWpfGui.Configuration
parsed = new Root();
}
if (parsed.CurrentConfig is null)
{
parsed.CurrentConfig = new SpecificConfig();
}
parsed.CurrentConfig ??= new SpecificConfig();
parsed.PropertyChanged += OnPropertyChangedFactory("Configurations.");
parsed.Configurations.CollectionChanged += (in NotifyCollectionChangedEventArgs<KeyValuePair<string, SpecificConfig>> args) =>
@@ -84,6 +83,12 @@ namespace MaaWpfGui.Configuration
}
break;
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Move:
case NotifyCollectionChangedAction.Reset:
break;
default:
throw new ArgumentOutOfRangeException();
}
OnPropertyChangedFactory("Configurations");
@@ -103,9 +108,9 @@ namespace MaaWpfGui.Configuration
return (o, args) =>
{
var after = newValue;
if (after == null && args is PropertyChangedEventDetailArgs)
if (after == null && args is PropertyChangedEventDetailArgs detailArgs)
{
after = (args as PropertyChangedEventDetailArgs).NewValue;
after = detailArgs.NewValue;
}
OnPropertyChanged(key + args.PropertyName, oldValue, after);
@@ -117,20 +122,20 @@ namespace MaaWpfGui.Configuration
return (o, args) =>
{
object after = null;
if (args is PropertyChangedEventDetailArgs)
if (args is PropertyChangedEventDetailArgs detailArgs)
{
after = (args as PropertyChangedEventDetailArgs).NewValue;
after = detailArgs.NewValue;
}
OnPropertyChanged(key + args.PropertyName, null, after);
};
}
public static Root Root => _rootConfig.Value;
private static Root Root => _rootConfig.Value;
public static readonly SpecificConfig CurrentConfig = Root.CurrentConfig;
public static async void OnPropertyChanged(string key, object oldValue, object newValue)
private static async void OnPropertyChanged(string key, object oldValue, object newValue)
{
var result = await Save();
if (result)

View File

@@ -1,4 +1,4 @@
// <copyright file="GUI.cs" company="MaaAssistantArknights">
// <copyright file="GUI.cs" company="MaaAssistantArknights">
// MaaWpfGui - A part of the MaaCoreArknights project
// Copyright (C) 2021 MistEO and Contributors
//
@@ -23,6 +23,7 @@ namespace MaaWpfGui.Configuration
public bool UseNotify { get; set; } = true;
// ReSharper disable once UnusedMember.Global
public void OnPropertyChanged(string propertyName, object before, object after)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventDetailArgs(propertyName, before, after));

View File

@@ -1,4 +1,4 @@
using System.ComponentModel;
using System.ComponentModel;
namespace MaaWpfGui.Configuration
{
@@ -12,6 +12,7 @@ namespace MaaWpfGui.Configuration
NewValue = newValue;
}
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public object OldValue { get; private set; }
public object NewValue { get; private set; }

View File

@@ -1,5 +1,16 @@
using System;
using System.Collections.ObjectModel;
// <copyright file="Root.cs" company="MaaAssistantArknights">
// MaaWpfGui - A part of the MaaCoreArknights project
// Copyright (C) 2021 MistEO and Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
using System.ComponentModel;
using System.Text.Json.Serialization;
using ObservableCollections;
@@ -19,14 +30,14 @@ namespace MaaWpfGui.Configuration
{
get
{
SpecificConfig result = null;
Configurations.TryGetValue(Current, out result);
Configurations.TryGetValue(Current, out var result);
return result;
}
set => Configurations[Current] = value;
}
// ReSharper disable once UnusedMember.Global
public void OnPropertyChanged(string propertyName, object before, object after)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventDetailArgs(propertyName, before, after));

View File

@@ -1,11 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
// <copyright file="SpecificConfig.cs" company="MaaAssistantArknights">
// MaaWpfGui - A part of the MaaCoreArknights project
// Copyright (C) 2021 MistEO and Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
namespace MaaWpfGui.Configuration
{
public class SpecificConfig
{
public GUI GUI { get; set; } = new GUI();
public GUI GUI { get; } = new GUI();
}
}

View File

@@ -57,6 +57,7 @@ namespace MaaWpfGui.Helper
File.WriteAllText(_cacheFile, jsonStr);
}
// ReSharper disable once MemberCanBePrivate.Global
public static string Get(string url)
{
if (url is null)
@@ -67,6 +68,7 @@ namespace MaaWpfGui.Helper
return _cache.TryGetValue(url, out string ret) ? ret : string.Empty;
}
// ReSharper disable once MemberCanBePrivate.Global
public static void Set(string url, string etag)
{
_cache[url] = etag;

View File

@@ -23,7 +23,6 @@ using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MaaWpfGui.Configuration;
using MaaWpfGui.Constants;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.Win32;
using Notification.Wpf;

View File

@@ -465,7 +465,10 @@ namespace MaaWpfGui.Main
break;
}
AsstStop();
if (!AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
Execute.OnUIThread(async () =>
{
@@ -534,7 +537,10 @@ namespace MaaWpfGui.Main
// 如果启用战斗列表,需要中止掉剩余的任务
if (Instances.CopilotViewModel.UseCopilotList)
{
AsstStop(false);
if (!AsstStop(false))
{
_logger.Warning("Failed to stop Asst");
}
}
_runningState.SetIdle(true);
@@ -1581,7 +1587,11 @@ namespace MaaWpfGui.Main
public bool AsstAppendCloseDown()
{
AsstStop();
if (!AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
AsstTaskId id = AsstAppendTaskWithEncoding("CloseDown");
_latestTaskId[TaskType.CloseDown] = id;
return id != 0;

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;

View File

@@ -11,10 +11,7 @@
// but WITHOUT ANY WARRANTY
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;

View File

@@ -43,6 +43,7 @@ namespace MaaWpfGui.ViewModels.UI
public class CopilotViewModel : Screen
{
private readonly RunningState _runningState;
private static readonly ILogger _logger = Log.ForContext<CopilotViewModel>();
/// <summary>
/// Gets the view models of log items.
@@ -807,7 +808,11 @@ namespace MaaWpfGui.ViewModels.UI
if (!startAny)
{
// 一个都没启动,怎会有如此无聊之人
Instances.AsstProxy.AsstStop();
if (!Instances.AsstProxy.AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
_runningState.SetIdle(true);
return;
}
@@ -823,7 +828,11 @@ namespace MaaWpfGui.ViewModels.UI
}
else
{
Instances.AsstProxy.AsstStop();
if (!Instances.AsstProxy.AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
_runningState.SetIdle(true);
AddLog(LocalizationHelper.GetString("CopilotFileReadError"), UiLogColor.Error);
}
@@ -841,7 +850,11 @@ namespace MaaWpfGui.ViewModels.UI
// ReSharper disable once UnusedMember.Global
public void Stop()
{
Instances.AsstProxy.AsstStop();
if (!Instances.AsstProxy.AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
_runningState.SetIdle(true);
}

View File

@@ -2860,8 +2860,8 @@ namespace MaaWpfGui.ViewModels.UI
{
JObject 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"];
var poolTime = (ulong)versionJson?["gacha"]?["time"];
var activityTime = (ulong)versionJson?["activity"]?["time"];
if ((currentTime < poolTime) && (currentTime < activityTime))
{

View File

@@ -937,7 +937,13 @@ namespace MaaWpfGui.ViewModels.UI
{
Stopping = true;
AddLog(LocalizationHelper.GetString("Stopping"));
await Task.Run(() => { Instances.AsstProxy.AsstStop(); });
await Task.Run(() =>
{
if (!Instances.AsstProxy.AsstStop())
{
_logger.Warning("Failed to stop Asst");
}
});
int count = 0;
while (Instances.AsstProxy.AsstRunning() && count <= 600)
@@ -2105,6 +2111,8 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public bool Waiting
{
// UI 会根据这个值来改变 Visibility
// ReSharper disable once UnusedMember.Global
get => _waiting;
private set => SetAndNotify(ref _waiting, value);
}