From 94ea1f84de08a6c7da58ea341f9a965e8c5ae657 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Fri, 15 Mar 2024 01:28:58 -0700 Subject: [PATCH] .net 8 syntax/perf changes --- .editorconfig | 35 ++++++++++++++++++++++++---- Benchmark/Bench.cs | 5 ++-- Craftimizer/Configuration.cs | 4 ++-- Craftimizer/ImGuiUtils.cs | 24 +++++++------------ Craftimizer/ImRaii2.cs | 30 +++++++----------------- Craftimizer/Utils/Colors.cs | 12 +++++----- Craftimizer/Utils/CommunityMacros.cs | 22 ++++++++--------- Craftimizer/Utils/FoodStatus.cs | 14 +++++------ Craftimizer/Utils/FuzzyMatcher.cs | 2 +- Craftimizer/Utils/IconManager.cs | 8 +++---- Craftimizer/Utils/SimulatedMacro.cs | 6 ++--- Craftimizer/Utils/SqText.cs | 6 ++--- Craftimizer/Windows/MacroEditor.cs | 9 ++++--- Craftimizer/Windows/MacroList.cs | 10 ++++---- Craftimizer/Windows/RecipeNote.cs | 18 +++++--------- Craftimizer/Windows/Settings.cs | 2 +- Craftimizer/Windows/SynthHelper.cs | 6 ++--- Simulator/ActionCategory.cs | 8 +++---- Solver/ArenaNode.cs | 14 +++-------- Solver/SimulationNode.cs | 18 ++++---------- 20 files changed, 117 insertions(+), 136 deletions(-) diff --git a/.editorconfig b/.editorconfig index 2e37080..0bc922e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -129,7 +129,7 @@ resharper_suggest_var_or_type_built_in_types_highlighting = hint resharper_suggest_var_or_type_elsewhere_highlighting = hint resharper_suggest_var_or_type_simple_types_highlighting = hint resharper_unused_auto_property_accessor_global_highlighting = none -csharp_style_deconstructed_variable_declaration=true:silent +csharp_style_deconstructed_variable_declaration= true:silent csharp_using_directive_placement = outside_namespace:suggestion csharp_prefer_simple_using_statement = true:suggestion csharp_prefer_braces = true:silent @@ -151,7 +151,7 @@ dotnet_diagnostic.MA0048.severity = silent dotnet_diagnostic.MA0051.severity = silent dotnet_diagnostic.MA0006.severity = suggestion dotnet_diagnostic.MA0016.severity = suggestion -dotnet_diagnostic.MA0008.severity = suggestion +dotnet_diagnostic.MA0008.severity = none dotnet_diagnostic.CA1805.severity = warning dotnet_diagnostic.CA1825.severity = warning dotnet_diagnostic.CA1841.severity = warning @@ -166,13 +166,34 @@ csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion csharp_style_pattern_matching_over_as_with_null_check = true:suggestion csharp_style_prefer_not_pattern = true:suggestion csharp_style_prefer_extended_property_pattern = true:suggestion -csharp_style_prefer_primary_constructors = true:suggestion +csharp_style_prefer_primary_constructors = true:warning dotnet_diagnostic.CA1855.severity = warning dotnet_diagnostic.CA1866.severity = warning dotnet_diagnostic.CA1865.severity = warning dotnet_diagnostic.MA0112.severity = none dotnet_diagnostic.MA0038.severity = none dotnet_diagnostic.MA0041.severity = none +csharp_style_throw_expression = true:suggestion +csharp_style_prefer_null_check_over_type_check = true:suggestion +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_prefer_local_over_anonymous_function = true:suggestion +csharp_style_prefer_index_operator = true:suggestion +csharp_style_prefer_range_operator = true:suggestion +csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion +csharp_style_prefer_tuple_swap = true:suggestion +csharp_style_prefer_utf8_string_literals = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_unused_value_assignment_preference = discard_variable:suggestion +csharp_style_unused_value_expression_statement_preference = discard_variable:silent +csharp_prefer_static_local_function = true:suggestion +csharp_style_prefer_readonly_struct = true:suggestion +csharp_style_prefer_readonly_struct_member = true:suggestion +csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent +csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent +csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent +csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent +csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent +dotnet_diagnostic.MA0003.severity = none [*.{appxmanifest,asax,ascx,aspx,axaml,axml,build,c,c++,cc,cginc,compute,config,cp,cpp,cs,cshtml,csproj,css,cu,cuh,cxx,dbml,discomap,dtd,h,hh,hlsl,hlsli,hlslinc,hpp,htm,html,hxx,inc,inl,ino,ipp,js,json,jsproj,jsx,lsproj,master,mpp,mq4,mq5,mqh,njsproj,nuspec,paml,proj,props,proto,razor,resjson,resw,resx,skin,StyleCop,targets,tasks,tpp,ts,tsx,usf,ush,vb,vbproj,xaml,xamlx,xml,xoml,xsd}] indent_style = space @@ -184,10 +205,10 @@ dotnet_style_null_propagation = true:suggestion dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion dotnet_style_prefer_auto_properties = true:suggestion dotnet_style_operator_placement_when_wrapping = beginning_of_line -dotnet_style_collection_initializer = true:suggestion +dotnet_style_collection_initializer = true:warning dotnet_diagnostic.CA1852.severity = warning dotnet_style_namespace_match_folder = true:suggestion -dotnet_style_prefer_collection_expression = true:suggestion +dotnet_style_prefer_collection_expression = true:warning dotnet_style_prefer_simplified_boolean_expressions = true:suggestion dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion dotnet_style_prefer_conditional_expression_over_return = true:suggestion @@ -198,3 +219,7 @@ dotnet_style_prefer_compound_assignment = true:suggestion dotnet_style_prefer_simplified_interpolation = true:suggestion dotnet_analyzer_diagnostic.category-performance.severity = warning dotnet_diagnostic.CA1829.severity = error +dotnet_style_readonly_field = true:suggestion +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_allow_multiple_blank_lines_experimental = true:silent +dotnet_style_allow_statement_immediately_after_block_experimental = true:silent diff --git a/Benchmark/Bench.cs b/Benchmark/Bench.cs index e675d29..d6a2fe6 100644 --- a/Benchmark/Bench.cs +++ b/Benchmark/Bench.cs @@ -20,7 +20,8 @@ public class Bench $"{HashCode.Combine(Data.ToString()!):X8}"; } - private static SimulationInput[] Inputs { get; } = new SimulationInput[] { + private static SimulationInput[] Inputs { get; } = + [ // https://craftingway.app/rotation/loud-namazu-jVe9Y // Chondrite Saw new(new() @@ -76,7 +77,7 @@ public class Bench ProgressModifier = 80, ProgressDivider = 130 }) - }; + ]; public static IEnumerable> States => Inputs.Select(i => new HashWrapper(new(i))); diff --git a/Craftimizer/Configuration.cs b/Craftimizer/Configuration.cs index 9f6dcc7..ed8605a 100644 --- a/Craftimizer/Configuration.cs +++ b/Craftimizer/Configuration.cs @@ -15,7 +15,7 @@ public class Macro public string Name { get; set; } = string.Empty; [JsonProperty(PropertyName = "Actions")] - private List actions { get; set; } = new(); + private List actions { get; set; } = []; [JsonIgnore] public IReadOnlyList Actions { @@ -80,7 +80,7 @@ public class Configuration : IPluginConfiguration public static event Action? OnMacroListChanged; [JsonProperty(PropertyName = "Macros")] - private List macros { get; set; } = new(); + private List macros { get; set; } = []; [JsonIgnore] public IReadOnlyList Macros => macros; public int ReliabilitySimulationCount { get; set; } = 500; diff --git a/Craftimizer/ImGuiUtils.cs b/Craftimizer/ImGuiUtils.cs index 4102c7d..bea59c0 100644 --- a/Craftimizer/ImGuiUtils.cs +++ b/Craftimizer/ImGuiUtils.cs @@ -231,20 +231,12 @@ internal static class ImGuiUtils public sealed class ViolinData { - [StructLayout(LayoutKind.Sequential)] - public struct Point + public struct Point(float x, float y, float y2) { - public float X, Y, Y2; - - public Point(float x, float y, float y2) - { - X = x; - Y = y; - Y2 = y2; - } + public float X = x, Y = y, Y2 = y2; } - public ReadOnlySpan Data => (DataArray ?? Array.Empty()).AsSpan(); + public ReadOnlySpan Data => (DataArray ?? []).AsSpan(); private Point[]? DataArray { get; set; } public readonly float Min; public readonly float Max; @@ -261,7 +253,7 @@ internal static class ImGuiUtils .Select(n => Lerp(min, max, n / (float)resolution)) .Select(n => (n, (float)KernelDensity.EstimateGaussian(n, bandwidth, samplesList))) .Select(n => new Point(n.n, n.Item2, -n.Item2)); - DataArray = data.ToArray(); + DataArray = [.. data]; s.Stop(); Log.Debug($"Violin plot processing took {s.Elapsed.TotalMilliseconds:0.00}ms"); }); @@ -311,7 +303,7 @@ internal static class ImGuiUtils public SearchableComboData(IEnumerable items, T selectedItem, Func getString) { this.items = items.ToImmutableArray(); - filteredItems = new() { selectedItem }; + filteredItems = [selectedItem]; this.selectedItem = selectedItem; this.getString = getString; input = GetString(selectedItem); @@ -355,7 +347,7 @@ internal static class ImGuiUtils { if (string.IsNullOrWhiteSpace(input)) { - filteredItems = items.ToList(); + filteredItems = [.. items]; return; } var matcher = new FuzzyMatcher(input.ToLowerInvariant(), MatchMode.FuzzyParts); @@ -364,10 +356,10 @@ internal static class ImGuiUtils .OrderByDescending(t => t.Score) .Select(t => t.Item); token.ThrowIfCancellationRequested(); - filteredItems = query.ToList(); + filteredItems = [.. query]; } } - private static readonly Dictionary ComboData = new(); + private static readonly Dictionary ComboData = []; private static SearchableComboData GetComboData(uint comboKey, IEnumerable items, T selectedItem, Func getString) where T : class => (SearchableComboData)( diff --git a/Craftimizer/ImRaii2.cs b/Craftimizer/ImRaii2.cs index 89c32bf..4f2afbf 100644 --- a/Craftimizer/ImRaii2.cs +++ b/Craftimizer/ImRaii2.cs @@ -8,20 +8,13 @@ namespace Craftimizer.Plugin; public static class ImRaii2 { - private struct EndUnconditionally : ImRaii.IEndObject, IDisposable + private struct EndUnconditionally(Action endAction, bool success) : ImRaii.IEndObject, IDisposable { - private Action EndAction { get; } + private Action EndAction { get; } = endAction; - public bool Success { get; } + public bool Success { get; } = success; - public bool Disposed { get; private set; } - - public EndUnconditionally(Action endAction, bool success) - { - EndAction = endAction; - Success = success; - Disposed = false; - } + public bool Disposed { get; private set; } = false; public void Dispose() { @@ -33,20 +26,13 @@ public static class ImRaii2 } } - private struct EndConditionally : ImRaii.IEndObject, IDisposable + private struct EndConditionally(Action endAction, bool success) : ImRaii.IEndObject, IDisposable { - public bool Success { get; } + public bool Success { get; } = success; - public bool Disposed { get; private set; } + public bool Disposed { get; private set; } = false; - private Action EndAction { get; } - - public EndConditionally(Action endAction, bool success) - { - EndAction = endAction; - Success = success; - Disposed = false; - } + private Action EndAction { get; } = endAction; public void Dispose() { diff --git a/Craftimizer/Utils/Colors.cs b/Craftimizer/Utils/Colors.cs index 7a62786..02e2142 100644 --- a/Craftimizer/Utils/Colors.cs +++ b/Craftimizer/Utils/Colors.cs @@ -15,8 +15,8 @@ public static class Colors private static Vector4 SolverProgressBg => ImGui.ColorConvertU32ToFloat4(ImGui.GetColorU32(ImGuiCol.TableBorderLight)); private static Vector4 SolverProgressFgBland => ImGuiColors.DalamudWhite2; - private static readonly Vector4[] SolverProgressFg = new Vector4[] - { + private static readonly Vector4[] SolverProgressFg = + [ new(0.87f, 0.19f, 0.30f, 1f), new(0.96f, 0.62f, 0.12f, 1f), new(0.97f, 0.84f, 0.00f, 1f), @@ -24,14 +24,14 @@ public static class Colors new(0.21f, 0.30f, 0.98f, 1f), new(0.26f, 0.62f, 0.94f, 1f), new(0.70f, 0.49f, 0.88f, 1f), - }; + ]; - public static readonly Vector4[] CollectabilityThreshold = new Vector4[] - { + public static readonly Vector4[] CollectabilityThreshold = + [ new(0.47f, 0.78f, 0.93f, 1f), // Blue new(0.99f, 0.79f, 0f, 1f), // Yellow new(0.75f, 1f, 0.75f, 1f), // Green - }; + ]; public static (Vector4 Background, Vector4 Foreground) GetSolverProgressColors(int? stageValue) => stageValue is not { } stage ? diff --git a/Craftimizer/Utils/CommunityMacros.cs b/Craftimizer/Utils/CommunityMacros.cs index 96665e8..29a357a 100644 --- a/Craftimizer/Utils/CommunityMacros.cs +++ b/Craftimizer/Utils/CommunityMacros.cs @@ -73,7 +73,7 @@ public sealed class CommunityMacros [JsonRequired] public required ValueData Data { get; set; } - public T[] Value => Data.Values ?? Array.Empty(); + public T[] Value => Data.Values ?? []; public static implicit operator T[](ArrayValue v) => v.Value; } @@ -373,7 +373,7 @@ public sealed class CommunityMacros } } - private Dictionary> CachedRotations { get; } = new(); + private Dictionary> CachedRotations { get; } = []; public async Task> RetrieveRotations(int rlvl, CancellationToken token) { @@ -403,17 +403,17 @@ public sealed class CommunityMacros { StructuredQuery = new StructuredQuery { - From = new List - { + From = + [ new() { CollectionId = "rotations" } - }, + ], Where = new Filter { CompositeFilter = new CompositeFilter { Op = CompositeOperator.AND, - Filters = new List - { + Filters = + [ new() { FieldFilter = new FieldFilter @@ -432,11 +432,11 @@ public sealed class CommunityMacros Value = new IntegerValue { Value = rlvl } } } - } + ] }, }, - OrderBy = new List - { + OrderBy = + [ new() { Field = new FieldReference { FieldPath = "xivVersion" }, @@ -447,7 +447,7 @@ public sealed class CommunityMacros Field = new FieldReference { FieldPath = "__name__" }, Direction = Direction.DESCENDING } - } + ] }, }; diff --git a/Craftimizer/Utils/FoodStatus.cs b/Craftimizer/Utils/FoodStatus.cs index 6336c41..6d76985 100644 --- a/Craftimizer/Utils/FoodStatus.cs +++ b/Craftimizer/Utils/FoodStatus.cs @@ -1,18 +1,18 @@ using Craftimizer.Plugin; using Craftimizer.Plugin.Utils; using Lumina.Excel.GeneratedSheets; +using System.Collections.Frozen; using System.Collections.Generic; using System.Collections.Immutable; -using System.Collections.ObjectModel; using System.Linq; namespace Craftimizer.Utils; public static class FoodStatus { - private static readonly ReadOnlyDictionary ItemFoodToItemLUT; - private static readonly ReadOnlyDictionary FoodItems; - private static readonly ReadOnlyDictionary MedicineItems; + private static readonly FrozenDictionary ItemFoodToItemLUT; + private static readonly FrozenDictionary FoodItems; + private static readonly FrozenDictionary MedicineItems; private static readonly ImmutableArray FoodOrder; private static readonly ImmutableArray MedicineOrder; @@ -41,7 +41,7 @@ public static class FoodStatus lut.TryAdd(itemFood.RowId, item.RowId); } - ItemFoodToItemLUT = lut.AsReadOnly(); + ItemFoodToItemLUT = lut.ToFrozenDictionary(); var foods = new Dictionary(); var medicines = new Dictionary(); @@ -87,8 +87,8 @@ public static class FoodStatus } } - FoodItems = foods.AsReadOnly(); - MedicineItems = medicines.AsReadOnly(); + FoodItems = foods.ToFrozenDictionary(); + MedicineItems = medicines.ToFrozenDictionary(); FoodOrder = FoodItems.OrderByDescending(a => a.Value.Item.LevelItem.Row).Select(a => a.Key).ToImmutableArray(); MedicineOrder = MedicineItems.OrderByDescending(a => a.Value.Item.LevelItem.Row).Select(a => a.Key).ToImmutableArray(); diff --git a/Craftimizer/Utils/FuzzyMatcher.cs b/Craftimizer/Utils/FuzzyMatcher.cs index 0c1cb35..0613724 100644 --- a/Craftimizer/Utils/FuzzyMatcher.cs +++ b/Craftimizer/Utils/FuzzyMatcher.cs @@ -7,7 +7,7 @@ namespace Craftimizer.Utils; internal readonly struct FuzzyMatcher { private const bool IsBorderMatching = true; - private static readonly (int, int)[] EmptySegArray = Array.Empty<(int, int)>(); + private static readonly (int, int)[] EmptySegArray = []; private readonly string needleString = string.Empty; private readonly int needleFinalPosition = -1; diff --git a/Craftimizer/Utils/IconManager.cs b/Craftimizer/Utils/IconManager.cs index 9300e15..cf1d2a7 100644 --- a/Craftimizer/Utils/IconManager.cs +++ b/Craftimizer/Utils/IconManager.cs @@ -10,10 +10,10 @@ namespace Craftimizer.Utils; public sealed class IconManager : IDisposable { - private readonly Dictionary iconCache = new(); - private readonly Dictionary hqIconCache = new(); - private readonly Dictionary textureCache = new(); - private readonly Dictionary assemblyCache = new(); + private readonly Dictionary iconCache = []; + private readonly Dictionary hqIconCache = []; + private readonly Dictionary textureCache = []; + private readonly Dictionary assemblyCache = []; public IDalamudTextureWrap GetIcon(uint id) { diff --git a/Craftimizer/Utils/SimulatedMacro.cs b/Craftimizer/Utils/SimulatedMacro.cs index 73a96d7..7a32d19 100644 --- a/Craftimizer/Utils/SimulatedMacro.cs +++ b/Craftimizer/Utils/SimulatedMacro.cs @@ -25,7 +25,7 @@ internal sealed class SimulatedMacro public Param() { - DataList = new(); + DataList = []; } public void Add(int value) @@ -124,7 +124,7 @@ internal sealed class SimulatedMacro new(initialState, actionSet, Service.Configuration.ReliabilitySimulationCount, recipeData); }; - private List Macro { get; set; } = new(); + private List Macro { get; set; } = []; private SimulationState initialState; public SimulationState InitialState { @@ -139,7 +139,7 @@ internal sealed class SimulatedMacro } } private object QueueLock { get; } = new(); - private List QueuedSteps { get; set; } = new(); + private List QueuedSteps { get; set; } = []; public SimulationState State => Macro.Count > 0 ? Macro[^1].State : InitialState; diff --git a/Craftimizer/Utils/SqText.cs b/Craftimizer/Utils/SqText.cs index 86db04f..3df27de 100644 --- a/Craftimizer/Utils/SqText.cs +++ b/Craftimizer/Utils/SqText.cs @@ -1,7 +1,7 @@ using Dalamud.Game.Text; using System; +using System.Collections.Frozen; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Numerics; namespace Craftimizer.Utils; @@ -10,7 +10,7 @@ public static class SqText { public static SeIconChar LevelPrefix => SeIconChar.LevelEn; - public static readonly ReadOnlyDictionary LevelNumReplacements = new(new Dictionary + public static readonly FrozenDictionary LevelNumReplacements = new Dictionary { ['0'] = SeIconChar.Number0, ['1'] = SeIconChar.Number1, @@ -22,7 +22,7 @@ public static class SqText ['7'] = SeIconChar.Number7, ['8'] = SeIconChar.Number8, ['9'] = SeIconChar.Number9, - }); + }.ToFrozenDictionary(); public static string ToLevelString(T value) where T : IBinaryInteger { diff --git a/Craftimizer/Windows/MacroEditor.cs b/Craftimizer/Windows/MacroEditor.cs index 35ebc80..fdff311 100644 --- a/Craftimizer/Windows/MacroEditor.cs +++ b/Craftimizer/Windows/MacroEditor.cs @@ -119,8 +119,7 @@ public sealed class MacroEditor : Window, IDisposable MacroSetter = setter; DefaultActions = actions.ToArray(); - HQIngredientCounts = new(); - HQIngredientCounts.AddRange(Enumerable.Repeat(0, RecipeData.Ingredients.Count)); + HQIngredientCounts = [.. Enumerable.Repeat(0, RecipeData.Ingredients.Count)]; RecalculateState(); foreach (var action in DefaultActions) @@ -145,8 +144,8 @@ public sealed class MacroEditor : Window, IDisposable SizeConstraints = new() { MinimumSize = new(821, 750), MaximumSize = new(float.PositiveInfinity) }; - TitleBarButtons = new() - { + TitleBarButtons = + [ new() { Icon = FontAwesomeIcon.Cog, @@ -154,7 +153,7 @@ public sealed class MacroEditor : Window, IDisposable Click = _ => Service.Plugin.OpenSettingsWindow(), ShowTooltip = () => ImGuiUtils.Tooltip("Open Craftimizer Settings") } - }; + ]; Service.WindowSystem.AddWindow(this); } diff --git a/Craftimizer/Windows/MacroList.cs b/Craftimizer/Windows/MacroList.cs index 886ff0b..2d57431 100644 --- a/Craftimizer/Windows/MacroList.cs +++ b/Craftimizer/Windows/MacroList.cs @@ -23,7 +23,7 @@ public sealed class MacroList : Window, IDisposable public RecipeData? RecipeData { get; private set; } private IReadOnlyList Macros => Service.Configuration.Macros; - private Dictionary MacroStateCache { get; } = new(); + private Dictionary MacroStateCache { get; } = []; public MacroList() : base("Craftimizer Macro List", WindowFlags, false) { @@ -37,8 +37,8 @@ public sealed class MacroList : Window, IDisposable SizeConstraints = new() { MinimumSize = new(465, 520), MaximumSize = new(float.PositiveInfinity) }; - TitleBarButtons = new() - { + TitleBarButtons = + [ new() { Icon = FontAwesomeIcon.Cog, @@ -46,7 +46,7 @@ public sealed class MacroList : Window, IDisposable Click = _ => Service.Plugin.OpenSettingsWindow(), ShowTooltip = () => ImGuiUtils.Tooltip("Open Craftimizer Settings") } - }; + ]; Service.WindowSystem.AddWindow(this); } @@ -345,7 +345,7 @@ public sealed class MacroList : Window, IDisposable .Where(t => t.Score > 0) .OrderByDescending(t => t.Score) .Select(t => t.Item); - sortedMacros = query.ToList(); + sortedMacros = [.. query]; } private void OpenEditor(Macro? macro) diff --git a/Craftimizer/Windows/RecipeNote.cs b/Craftimizer/Windows/RecipeNote.cs index 6ad24b6..2bd4cf3 100644 --- a/Craftimizer/Windows/RecipeNote.cs +++ b/Craftimizer/Windows/RecipeNote.cs @@ -64,20 +64,14 @@ public sealed unsafe class RecipeNote : Window, IDisposable public CharacterStats? CharacterStats { get; private set; } public CraftableStatus CraftStatus { get; private set; } - public sealed class BackgroundTask : IDisposable where T : struct + public sealed class BackgroundTask(Func func) : IDisposable where T : struct { public T? Result { get; private set; } public Exception? Exception { get; private set; } public bool Completed { get; private set; } - private CancellationTokenSource TokenSource { get; } - private Func Func { get; } - - public BackgroundTask(Func func) - { - Func = func; - TokenSource = new(); - } + private CancellationTokenSource TokenSource { get; } = new(); + private Func Func { get; } = func; public void Start() { @@ -142,8 +136,8 @@ public sealed unsafe class RecipeNote : Window, IDisposable MaximumSize = new(10000, 10000) }; - TitleBarButtons = new() - { + TitleBarButtons = + [ new() { Icon = FontAwesomeIcon.Cog, @@ -151,7 +145,7 @@ public sealed unsafe class RecipeNote : Window, IDisposable Click = _ => Service.Plugin.OpenSettingsWindow(), ShowTooltip = () => ImGuiUtils.Tooltip("Open Craftimizer Settings") } - }; + ]; Service.WindowSystem.AddWindow(this); } diff --git a/Craftimizer/Windows/Settings.cs b/Craftimizer/Windows/Settings.cs index 8391188..046bbf2 100644 --- a/Craftimizer/Windows/Settings.cs +++ b/Craftimizer/Windows/Settings.cs @@ -805,7 +805,7 @@ public sealed class Settings : Window, IDisposable pool.Add(combo); } } - actionPool = pool.ToArray(); + actionPool = [.. pool]; } } diff --git a/Craftimizer/Windows/SynthHelper.cs b/Craftimizer/Windows/SynthHelper.cs index 3c20a18..754813a 100644 --- a/Craftimizer/Windows/SynthHelper.cs +++ b/Craftimizer/Windows/SynthHelper.cs @@ -92,8 +92,8 @@ public sealed unsafe class SynthHelper : Window, IDisposable MaximumSize = new(494, 10000) }; - TitleBarButtons = new() - { + TitleBarButtons = + [ new() { Icon = FontAwesomeIcon.Cog, @@ -101,7 +101,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable Click = _ => Service.Plugin.OpenSettingsWindow(), ShowTooltip = () => ImGuiUtils.Tooltip("Open Craftimizer Settings") } - }; + ]; Service.WindowSystem.AddWindow(this); } diff --git a/Simulator/ActionCategory.cs b/Simulator/ActionCategory.cs index 727772e..334ce09 100644 --- a/Simulator/ActionCategory.cs +++ b/Simulator/ActionCategory.cs @@ -1,5 +1,5 @@ using Craftimizer.Simulator.Actions; -using System.Collections.ObjectModel; +using System.Collections.Frozen; namespace Craftimizer.Simulator; @@ -16,14 +16,14 @@ public enum ActionCategory public static class ActionCategoryUtils { - private static readonly ReadOnlyDictionary SortedActions; + private static readonly FrozenDictionary SortedActions; static ActionCategoryUtils() { - SortedActions = new( + SortedActions = Enum.GetValues() .GroupBy(a => a.Category()) - .ToDictionary(g => g.Key, g => g.OrderBy(a => a.Level()).ToArray())); + .ToFrozenDictionary(g => g.Key, g => g.OrderBy(a => a.Level()).ToArray()); } public static IReadOnlyList GetActions(this ActionCategory me) diff --git a/Solver/ArenaNode.cs b/Solver/ArenaNode.cs index ebafacf..8a7b431 100644 --- a/Solver/ArenaNode.cs +++ b/Solver/ArenaNode.cs @@ -2,24 +2,16 @@ using System.Runtime.CompilerServices; namespace Craftimizer.Solver; -public sealed class ArenaNode where T : struct +public sealed class ArenaNode(in T state, ArenaNode? parent = null) where T : struct { - public T State; + public T State = state; public ArenaBuffer Children; public NodeScoresBuffer ChildScores; public (int arrayIdx, int subIdx) ChildIdx; - public readonly ArenaNode? Parent; + public readonly ArenaNode? Parent = parent; public NodeScoresBuffer? ParentScores => Parent?.ChildScores; - public ArenaNode(in T state, ArenaNode? parent = null) - { - State = state; - Children = new(); - ChildScores = new(); - Parent = parent; - } - public ArenaNode? ChildAt((int arrayIdx, int subIdx) at) => Children.Data?[at.arrayIdx]?[at.subIdx]; diff --git a/Solver/SimulationNode.cs b/Solver/SimulationNode.cs index a2b8f34..d980720 100644 --- a/Solver/SimulationNode.cs +++ b/Solver/SimulationNode.cs @@ -7,26 +7,18 @@ using System.Runtime.InteropServices; namespace Craftimizer.Solver; [StructLayout(LayoutKind.Auto)] -public struct SimulationNode +public struct SimulationNode(in SimulationState state, ActionType? action, CompletionState completionState, ActionSet actions) { - public readonly SimulationState State; - public readonly ActionType? Action; - public readonly CompletionState SimulationCompletionState; + public readonly SimulationState State = state; + public readonly ActionType? Action = action; + public readonly CompletionState SimulationCompletionState = completionState; - public ActionSet AvailableActions; + public ActionSet AvailableActions = actions; public readonly CompletionState CompletionState => GetCompletionState(SimulationCompletionState, AvailableActions); public readonly bool IsComplete => CompletionState != CompletionState.Incomplete; - public SimulationNode(in SimulationState state, ActionType? action, CompletionState completionState, ActionSet actions) - { - State = state; - Action = action; - SimulationCompletionState = completionState; - AvailableActions = actions; - } - public static CompletionState GetCompletionState(CompletionState simCompletionState, ActionSet actions) => actions.IsEmpty && simCompletionState == CompletionState.Incomplete ? CompletionState.NoMoreActions :