From 98b2786879bc1a06398803b840e05a5523711d12 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Fri, 4 Oct 2024 11:27:01 +0300 Subject: [PATCH] Using collection expression --- .../DistanceMatrix/DistanceMatrixSubSystem.cs | 8 ++--- .../HotKeys/HotKeyCategoryContainer.cs | 6 ++-- .../HotKeys/HotKeyManagerImplementation.cs | 8 ++--- .../Patches/MBGameManagerPatch.cs | 16 ++++++--- .../Patches/ModulePatch.cs | 5 +-- .../Patches/DefinitionContextPatch.cs | 10 +++--- .../SaveSystem/Patches/TypeExtensionsPatch.cs | 2 +- .../ButterLibSubModule.Services.cs | 2 +- .../DynamicAPI/DynamicAPIProvider.cs | 18 +++++----- .../ExceptionHandler/BEWPatch.cs | 4 +-- .../ExceptionHandler/ExceptionReporter.cs | 34 +++++++++---------- .../ImplementationLoaderSubModule.cs | 2 +- .../Logger/Extensions/LoggerExtensions.cs | 4 +-- .../Logger/ReverseTextReader.cs | 2 +- .../TaleWorldsContractResolverBase.cs | 2 +- .../MBSubModuleBaseListWrapper.cs | 2 +- .../DependencyInjectionTests.cs | 2 +- .../SaveSystem/JsonSerializationTests.cs | 4 +-- 18 files changed, 69 insertions(+), 62 deletions(-) diff --git a/src/Bannerlord.ButterLib.Implementation/DistanceMatrix/DistanceMatrixSubSystem.cs b/src/Bannerlord.ButterLib.Implementation/DistanceMatrix/DistanceMatrixSubSystem.cs index 36f89a3f..a019a14a 100644 --- a/src/Bannerlord.ButterLib.Implementation/DistanceMatrix/DistanceMatrixSubSystem.cs +++ b/src/Bannerlord.ButterLib.Implementation/DistanceMatrix/DistanceMatrixSubSystem.cs @@ -51,11 +51,11 @@ public void Disable() IsEnabled = false; } - public IReadOnlyCollection> Declarations { get; } = new SubSystemSettingsDeclaration[] - { + public IReadOnlyCollection> Declarations { get; } = + [ new SubSystemSettingsPropertyBool( "{=MSJe8ih4yp}Consider Villages", "{=kvR54SiOFn}Allow villages to be used for built-in distance matrix calculations. May negatively affect performance.", - x => x.ConsiderVillages), - }; + x => x.ConsiderVillages) + ]; } \ No newline at end of file diff --git a/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyCategoryContainer.cs b/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyCategoryContainer.cs index 239a9b93..8197eac8 100644 --- a/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyCategoryContainer.cs +++ b/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyCategoryContainer.cs @@ -24,15 +24,15 @@ internal sealed class HotKeyCategoryContainer : GameKeyContext public HotKeyCategoryContainer(string categoryId, string categoryName, IEnumerable keys) : base(categoryId, ListCapacity) { var keyCategoryName = Module.CurrentModule.GlobalTextManager.AddGameText("str_key_category_name"); - keyCategoryName.AddVariationWithId(categoryId, new TextObject(categoryName), new List()); + keyCategoryName.AddVariationWithId(categoryId, new TextObject(categoryName), []); var keyName = Module.CurrentModule.GlobalTextManager.AddGameText("str_key_name"); var keyDesc = Module.CurrentModule.GlobalTextManager.AddGameText("str_key_description"); foreach (var key in keys) { var variationId = $"{categoryId}_{(GameKeyDefinition) key.Id}"; - keyName.AddVariationWithId(variationId, new TextObject(key.DisplayName), new List()); - keyDesc.AddVariationWithId(variationId, new TextObject(key.Description), new List()); + keyName.AddVariationWithId(variationId, new TextObject(key.DisplayName), []); + keyDesc.AddVariationWithId(variationId, new TextObject(key.Description), []); RegisterGameKey(new GameKey(key.Id, key.Uid, categoryId, key.DefaultKey, key.Category)); } } diff --git a/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyManagerImplementation.cs b/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyManagerImplementation.cs index f6be56f0..3f581428 100644 --- a/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyManagerImplementation.cs +++ b/src/Bannerlord.ButterLib.Implementation/HotKeys/HotKeyManagerImplementation.cs @@ -17,13 +17,13 @@ namespace Bannerlord.ButterLib.Implementation.HotKeys; internal sealed class HotKeyManagerImplementation : HotKeyManager { - internal static readonly List GlobalContainerStorage = new(); - internal static readonly List GlobalHotKeyStorage = new(); + internal static readonly List GlobalContainerStorage = []; + internal static readonly List GlobalHotKeyStorage = []; private int _currentId = 0; // To prevent collision with the game private readonly string _modName; private readonly string _categoryName; - private readonly List _instanceHotKeys = new(); + private readonly List _instanceHotKeys = []; internal HotKeyManagerImplementation(string modName, string categoryName = "") { @@ -54,7 +54,7 @@ public override IReadOnlyList Build() #if v100 || v101 || v102 || v103 || v110 || v111 || v112 || v113 || v114 || v115 || v116 TWHotKeyManager.RegisterInitialContexts(new[] { hotKeyCategoryContainer }, true); #elif v120 || v121 || v122 || v123 || v124 || v125 || v126 || v127 || v128 || v129 || v1210 || v1211 - TWHotKeyManager.RegisterInitialContexts(TWHotKeyManager.GetAllCategories().ToList().Concat(new[] { hotKeyCategoryContainer }), true); + TWHotKeyManager.RegisterInitialContexts(TWHotKeyManager.GetAllCategories().ToList().Concat([hotKeyCategoryContainer]), true); #else #error DEFINE #endif diff --git a/src/Bannerlord.ButterLib.Implementation/MBSubModuleBaseExtended/Patches/MBGameManagerPatch.cs b/src/Bannerlord.ButterLib.Implementation/MBSubModuleBaseExtended/Patches/MBGameManagerPatch.cs index 9774c13f..9e703b04 100644 --- a/src/Bannerlord.ButterLib.Implementation/MBSubModuleBaseExtended/Patches/MBGameManagerPatch.cs +++ b/src/Bannerlord.ButterLib.Implementation/MBSubModuleBaseExtended/Patches/MBGameManagerPatch.cs @@ -68,14 +68,20 @@ public static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable("TaleWorlds.SaveSystem.TypeExtensions:IsContainer", new[] { typeof(Type) }); + AccessTools2.GetDelegate("TaleWorlds.SaveSystem.TypeExtensions:IsContainer", [typeof(Type)]); private static MethodInfo? TargetTypeMethod(string name) => AccessTools2.Method($"TaleWorlds.SaveSystem.Definition.DefinitionContext:{name}"); - private static readonly Patch[] Patches = new Patch[] - { + private static readonly Patch[] Patches = + [ new PrefixPatch(nameof(AddBasicTypeDefinitionPrefix), TargetTypeMethod("AddBasicTypeDefinition")), new PrefixPatch(nameof(AddClassDefinitionPrefix), TargetTypeMethod("AddClassDefinition")), new PrefixPatch(nameof(AddContainerDefinitionPrefix), TargetTypeMethod("AddContainerDefinition")), @@ -70,8 +70,8 @@ internal static bool Disable(Harmony harmony) new PrefixPatch(nameof(AddInterfaceDefinitionPrefix), TargetTypeMethod("AddInterfaceDefinition")), new PrefixPatch(nameof(AddRootClassDefinitionPrefix), TargetTypeMethod("AddRootClassDefinition")), new PrefixPatch(nameof(AddStructDefinitionPrefix), TargetTypeMethod("AddStructDefinition")), - new ConstructContainerDefinitionPrefixPatch(), - }; + new ConstructContainerDefinitionPrefixPatch() + ]; // PATCH METHODS diff --git a/src/Bannerlord.ButterLib.Implementation/SaveSystem/Patches/TypeExtensionsPatch.cs b/src/Bannerlord.ButterLib.Implementation/SaveSystem/Patches/TypeExtensionsPatch.cs index 2e8bc8cb..5010e4d1 100644 --- a/src/Bannerlord.ButterLib.Implementation/SaveSystem/Patches/TypeExtensionsPatch.cs +++ b/src/Bannerlord.ButterLib.Implementation/SaveSystem/Patches/TypeExtensionsPatch.cs @@ -51,7 +51,7 @@ internal static bool Disable(Harmony harmony) } private static readonly Type? TargetType = AccessTools2.TypeByName("TaleWorlds.SaveSystem.TypeExtensions"); - private static readonly Type[] TargetMethodParams = { typeof(Type), typeof(ContainerType).MakeByRefType() }; + private static readonly Type[] TargetMethodParams = [typeof(Type), typeof(ContainerType).MakeByRefType()]; private static readonly MethodInfo? TargetMethod = AccessTools2.Method(TargetType!, "IsContainer", TargetMethodParams); private static readonly MethodInfo? PatchMethod = SymbolExtensions2.GetMethodInfo((Type x, ContainerType y, bool z) => IsContainerPrefix(x, out y, ref z)); diff --git a/src/Bannerlord.ButterLib/ButterLibSubModule.Services.cs b/src/Bannerlord.ButterLib/ButterLibSubModule.Services.cs index 64842877..5ce36fdd 100644 --- a/src/Bannerlord.ButterLib/ButterLibSubModule.Services.cs +++ b/src/Bannerlord.ButterLib/ButterLibSubModule.Services.cs @@ -10,7 +10,7 @@ namespace Bannerlord.ButterLib; public sealed partial class ButterLibSubModule { public static bool CanBeConfigured { get; private set; } = true; - private static List> BeforeInitialization { get; } = new(); + private static List> BeforeInitialization { get; } = []; /// /// The only way to inject your stuff before ButterLib will start it's initialization diff --git a/src/Bannerlord.ButterLib/DynamicAPI/DynamicAPIProvider.cs b/src/Bannerlord.ButterLib/DynamicAPI/DynamicAPIProvider.cs index 22628b11..ffda8ef3 100644 --- a/src/Bannerlord.ButterLib/DynamicAPI/DynamicAPIProvider.cs +++ b/src/Bannerlord.ButterLib/DynamicAPI/DynamicAPIProvider.cs @@ -24,16 +24,16 @@ private record MethodInfoWithAttribute(MethodInfo MethodInfo, CustomAttributeDat private delegate object DynamicAPIObjectActivator(); - private static readonly HashSet PossibleClassNames = new() - { + private static readonly HashSet PossibleClassNames = + [ "Bannerlord.ButterLib.DynamicAPI.DynamicAPIClassAttribute", - "Bannerlord.DynamicAPI.DynamicAPIClassAttribute", - }; - private static readonly HashSet PossibleMethodNames = new() - { + "Bannerlord.DynamicAPI.DynamicAPIClassAttribute" + ]; + private static readonly HashSet PossibleMethodNames = + [ "Bannerlord.ButterLib.DynamicAPI.DynamicAPIMethodAttribute", - "Bannerlord.DynamicAPI.DynamicAPIMethodAttribute", - }; + "Bannerlord.DynamicAPI.DynamicAPIMethodAttribute" + ]; private static readonly ConcurrentDictionary CachedActivators = new(); internal static Dictionary? APIClasses; @@ -104,7 +104,7 @@ internal static void Initialize() } catch (Exception) { - return Enumerable.Empty(); + return []; } }) .OfType() diff --git a/src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs b/src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs index a9a3097a..a584a065 100644 --- a/src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs +++ b/src/Bannerlord.ButterLib/ExceptionHandler/BEWPatch.cs @@ -49,9 +49,9 @@ internal record ExceptionIdentifier(Type Type, string? StackTrace, string Messag public static ExceptionIdentifier FromException(Exception e) => new(e.GetType(), e.StackTrace, e.Message); } - internal static readonly HashSet SuppressedExceptions = new(); + internal static readonly HashSet SuppressedExceptions = []; - private static readonly string[] BEW = { "org.calradia.admiralnelson.betterexceptionwindow" }; + private static readonly string[] BEW = ["org.calradia.admiralnelson.betterexceptionwindow"]; private static readonly MethodInfo? ManagedApplicationTickMethod = AccessTools2.Method("TaleWorlds.DotNet.Managed:ApplicationTick"); diff --git a/src/Bannerlord.ButterLib/ExceptionHandler/ExceptionReporter.cs b/src/Bannerlord.ButterLib/ExceptionHandler/ExceptionReporter.cs index cd57f550..819de1cc 100644 --- a/src/Bannerlord.ButterLib/ExceptionHandler/ExceptionReporter.cs +++ b/src/Bannerlord.ButterLib/ExceptionHandler/ExceptionReporter.cs @@ -53,35 +53,35 @@ public static void Show(Exception exception) var harmonyProvider = new HarmonyProvider(); var crashReportRendererUtilities = new CrashReportRendererUtilities(); - var crashReport = CrashReportInfo.Create(exception, metadata, filter, helper, helper, helper, harmonyProvider); - var crashReportModel = CrashReportInfo.ToModel(crashReport, helper, helper, helper, helper, helper, helper); - var logSources = GetLogSources().ToArray(); - try - { - CrashReportImGui.ShowAndWait(crashReportModel, logSources, crashReportRendererUtilities); - } - catch (Exception ex) - { + var crashReport = CrashReportInfo.Create(exception, metadata, filter, helper, helper, helper, harmonyProvider); + var crashReportModel = CrashReportInfo.ToModel(crashReport, helper, helper, helper, helper, helper, helper); + var logSources = GetLogSources().ToArray(); try { + CrashReportImGui.ShowAndWait(crashReportModel, logSources, crashReportRendererUtilities); + } + catch (Exception ex) + { + try + { #if NET472 || (NET6_0 && WINDOWS) - var forms = new CrashReportWinForms(crashReportModel, logSources, crashReportRendererUtilities); - forms.ShowDialog(); + var forms = new CrashReportWinForms(crashReportModel, logSources, crashReportRendererUtilities); + forms.ShowDialog(); #endif - } - catch (Exception ex2) - { - throw new AggregateException(ex, ex2); + } + catch (Exception ex2) + { + throw new AggregateException(ex, ex2); + } } } - } private static IEnumerable GetLogSources() { var now = DateTimeOffset.Now; - foreach (var logSource in ButterLibSubModule.ServiceProvider?.GetService>() ?? Enumerable.Empty()) + foreach (var logSource in ButterLibSubModule.ServiceProvider?.GetService>() ?? []) { switch (logSource) { diff --git a/src/Bannerlord.ButterLib/ImplementationLoaderSubModule.cs b/src/Bannerlord.ButterLib/ImplementationLoaderSubModule.cs index 59718672..e59fbaa0 100644 --- a/src/Bannerlord.ButterLib/ImplementationLoaderSubModule.cs +++ b/src/Bannerlord.ButterLib/ImplementationLoaderSubModule.cs @@ -125,7 +125,7 @@ private static IEnumerable LoadAllImplementations(ILogger? logg catch (Exception e) when (e is ReflectionTypeLoadException) { logger?.LogError(e, "Implementation {Name} is not compatible with the current game!", Path.GetFileName(a.Location)); - return Enumerable.Empty(); + return []; } }).ToList(); diff --git a/src/Bannerlord.ButterLib/Logger/Extensions/LoggerExtensions.cs b/src/Bannerlord.ButterLib/Logger/Extensions/LoggerExtensions.cs index 3b8aa985..fd08c4f0 100644 --- a/src/Bannerlord.ButterLib/Logger/Extensions/LoggerExtensions.cs +++ b/src/Bannerlord.ButterLib/Logger/Extensions/LoggerExtensions.cs @@ -18,9 +18,9 @@ public static class LoggerExtensions private class LogValuesFormatter { private const string NullValue = "(null)"; - private static readonly object[] EmptyArray = Array.Empty(); + private static readonly object[] EmptyArray = []; private readonly string _format; - private readonly List _valueNames = new(); + private readonly List _valueNames = []; public LogValuesFormatter(string format) { diff --git a/src/Bannerlord.ButterLib/Logger/ReverseTextReader.cs b/src/Bannerlord.ButterLib/Logger/ReverseTextReader.cs index 97a6e12f..6cb06827 100644 --- a/src/Bannerlord.ButterLib/Logger/ReverseTextReader.cs +++ b/src/Bannerlord.ButterLib/Logger/ReverseTextReader.cs @@ -44,7 +44,7 @@ public ReverseTextReader(Stream stream, Encoding encoding) throw new InvalidOperationException("The specified stream needs to support reading to be read backwards."); // Set the current position to the end of the file _stream.Position = _stream.Length; - _leftoverBuffer = Array.Empty(); + _leftoverBuffer = []; } #endregion diff --git a/src/Bannerlord.ButterLib/SaveSystem/TaleWorldsContractResolverBase.cs b/src/Bannerlord.ButterLib/SaveSystem/TaleWorldsContractResolverBase.cs index 70a0512f..6715aecf 100644 --- a/src/Bannerlord.ButterLib/SaveSystem/TaleWorldsContractResolverBase.cs +++ b/src/Bannerlord.ButterLib/SaveSystem/TaleWorldsContractResolverBase.cs @@ -11,7 +11,7 @@ public class TaleWorldsContractResolverBase : DefaultContractResolver { protected delegate bool IsContainerDelegate(Type type); protected static readonly IsContainerDelegate? _isContainerDelegate = - AccessTools2.GetDelegate("TaleWorlds.SaveSystem.TypeExtensions:IsContainer", new[] { typeof(Type) }); + AccessTools2.GetDelegate("TaleWorlds.SaveSystem.TypeExtensions:IsContainer", [typeof(Type)]); protected static bool IsContainerFallback(Type type) { diff --git a/src/Bannerlord.ButterLib/SubModuleWrappers2/MBSubModuleBaseListWrapper.cs b/src/Bannerlord.ButterLib/SubModuleWrappers2/MBSubModuleBaseListWrapper.cs index 39e78102..bf67025e 100644 --- a/src/Bannerlord.ButterLib/SubModuleWrappers2/MBSubModuleBaseListWrapper.cs +++ b/src/Bannerlord.ButterLib/SubModuleWrappers2/MBSubModuleBaseListWrapper.cs @@ -12,7 +12,7 @@ namespace Bannerlord.ButterLib.SubModuleWrappers2; /// public class MBSubModuleBaseListWrapper : MBSubModuleBase { - protected readonly List _subModules = new(); + protected readonly List _subModules = []; public IReadOnlyList SubModules => _subModules.AsReadOnly(); public new virtual void OnSubModuleLoad() diff --git a/tests/Bannerlord.ButterLib.Implementation.Tests/DependencyInjectionTests.cs b/tests/Bannerlord.ButterLib.Implementation.Tests/DependencyInjectionTests.cs index e24620f9..5903340b 100644 --- a/tests/Bannerlord.ButterLib.Implementation.Tests/DependencyInjectionTests.cs +++ b/tests/Bannerlord.ButterLib.Implementation.Tests/DependencyInjectionTests.cs @@ -61,7 +61,7 @@ private static bool MockedGetConfigName(ref string __result) [MethodImpl(MethodImplOptions.NoInlining)] private static bool MockedGetModuleNames(ref string[] __result) { - __result = Array.Empty(); + __result = []; return false; } diff --git a/tests/Bannerlord.ButterLib.Tests/SaveSystem/JsonSerializationTests.cs b/tests/Bannerlord.ButterLib.Tests/SaveSystem/JsonSerializationTests.cs index 37989e7a..36af8a74 100644 --- a/tests/Bannerlord.ButterLib.Tests/SaveSystem/JsonSerializationTests.cs +++ b/tests/Bannerlord.ButterLib.Tests/SaveSystem/JsonSerializationTests.cs @@ -89,7 +89,7 @@ IEnumerable Filter(IEnumerable types) } catch (Exception) { - return Enumerable.Empty(); + return []; } }) .ToList(); @@ -158,7 +158,7 @@ IEnumerable Filter(Type[] types) } catch (Exception) { - return Enumerable.Empty(); + return []; } }) .ToList();