From ed2ee89b13432a1c040f7dafb68721cb00199664 Mon Sep 17 00:00:00 2001 From: IhateTrains Date: Tue, 26 Dec 2023 18:26:50 +0100 Subject: [PATCH] Fix some warnings from Meziantou analyzer (#1668) #minor --- .../Imperator/Characters/CharacterTests.cs | 2 +- .../CommonUtils/DiffHistoryField.cs | 2 +- .../Genes/AccessoryGeneTemplate.cs | 2 +- ImperatorToCK3/CommonUtils/IHistoryField.cs | 2 +- ImperatorToCK3/CommonUtils/IListExtensions.cs | 19 +++++++++++++++++++ .../CommonUtils/LiteralHistoryField.cs | 2 +- .../CommonUtils/SimpleHistoryField.cs | 2 +- ImperatorToCK3/Helpers/RakalyCaller.cs | 5 ++--- .../Imperator/Characters/Character.cs | 6 +++--- ImperatorToCK3/ImperatorToCK3.csproj | 4 ++-- 10 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 ImperatorToCK3/CommonUtils/IListExtensions.cs diff --git a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs index 642eca674..84f83de5b 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs @@ -57,7 +57,7 @@ public void FieldsCanBeSet() { var character = ImperatorToCK3.Imperator.Characters.Character.Parse(reader, "42", genesDB); var spouse1Reader = new BufferedReader(string.Empty); var spouse2Reader = new BufferedReader(string.Empty); - character.Spouses = new() { + character.Spouses = new Dictionary { { 3, ImperatorToCK3.Imperator.Characters.Character.Parse(spouse1Reader, "3", genesDB) }, { 4, ImperatorToCK3.Imperator.Characters.Character.Parse(spouse2Reader, "4", genesDB) } }; diff --git a/ImperatorToCK3/CommonUtils/DiffHistoryField.cs b/ImperatorToCK3/CommonUtils/DiffHistoryField.cs index b2a7f7a00..5ee4d99dd 100644 --- a/ImperatorToCK3/CommonUtils/DiffHistoryField.cs +++ b/ImperatorToCK3/CommonUtils/DiffHistoryField.cs @@ -6,7 +6,7 @@ namespace ImperatorToCK3.CommonUtils; internal class DiffHistoryField : IHistoryField { public string Id { get; } - public List> InitialEntries { get; } = new(); + public IList> InitialEntries { get; } = new List>(); public SortedDictionary>> DateToEntriesDict { get; } = new(); diff --git a/ImperatorToCK3/CommonUtils/Genes/AccessoryGeneTemplate.cs b/ImperatorToCK3/CommonUtils/Genes/AccessoryGeneTemplate.cs index f73d719cf..bdee91f0f 100644 --- a/ImperatorToCK3/CommonUtils/Genes/AccessoryGeneTemplate.cs +++ b/ImperatorToCK3/CommonUtils/Genes/AccessoryGeneTemplate.cs @@ -7,7 +7,7 @@ namespace ImperatorToCK3.CommonUtils.Genes; public class AccessoryGeneTemplate : IIdentifiable { public string Id { get; } public uint Index { get; private set; } = 0; - public Dictionary AgeSexWeightBlocks { get; } = new(); + public IDictionary AgeSexWeightBlocks { get; } = new Dictionary(); public AccessoryGeneTemplate(string id, BufferedReader reader) { Id = id; diff --git a/ImperatorToCK3/CommonUtils/IHistoryField.cs b/ImperatorToCK3/CommonUtils/IHistoryField.cs index 9d4f67f6f..b3ee69aa6 100644 --- a/ImperatorToCK3/CommonUtils/IHistoryField.cs +++ b/ImperatorToCK3/CommonUtils/IHistoryField.cs @@ -7,7 +7,7 @@ namespace ImperatorToCK3.CommonUtils; public interface IHistoryField : IIdentifiable { - public List> InitialEntries { get; } + public IList> InitialEntries { get; } public SortedDictionary>> DateToEntriesDict { get; } public object? GetValue(Date date); diff --git a/ImperatorToCK3/CommonUtils/IListExtensions.cs b/ImperatorToCK3/CommonUtils/IListExtensions.cs new file mode 100644 index 000000000..94fe84446 --- /dev/null +++ b/ImperatorToCK3/CommonUtils/IListExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; + +namespace ImperatorToCK3.CommonUtils; + +public static class IListExtensions { + public static int RemoveAll(this IList list, Predicate match) { + int count = 0; + + for (int i = list.Count - 1; i >= 0; i--) { + if (match(list[i])) { + ++count; + list.RemoveAt(i); + } + } + + return count; + } +} \ No newline at end of file diff --git a/ImperatorToCK3/CommonUtils/LiteralHistoryField.cs b/ImperatorToCK3/CommonUtils/LiteralHistoryField.cs index 80923882a..3c1ef8bb2 100644 --- a/ImperatorToCK3/CommonUtils/LiteralHistoryField.cs +++ b/ImperatorToCK3/CommonUtils/LiteralHistoryField.cs @@ -7,7 +7,7 @@ namespace ImperatorToCK3.CommonUtils; public class LiteralHistoryField : IHistoryField { public string Id { get; } - public List> InitialEntries { get; } = new(); // every entry is a pair + public IList> InitialEntries { get; } = new List>(); // every entry is a pair public SortedDictionary>> DateToEntriesDict { get; } = new(); diff --git a/ImperatorToCK3/CommonUtils/SimpleHistoryField.cs b/ImperatorToCK3/CommonUtils/SimpleHistoryField.cs index b966153bd..011850b24 100644 --- a/ImperatorToCK3/CommonUtils/SimpleHistoryField.cs +++ b/ImperatorToCK3/CommonUtils/SimpleHistoryField.cs @@ -7,7 +7,7 @@ namespace ImperatorToCK3.CommonUtils; public class SimpleHistoryField : IHistoryField { public string Id { get; } - public List> InitialEntries { get; } = new(); // every entry is a pair + public IList> InitialEntries { get; } = new List>(); // every entry is a pair public SortedDictionary>> DateToEntriesDict { get; } = new(); diff --git a/ImperatorToCK3/Helpers/RakalyCaller.cs b/ImperatorToCK3/Helpers/RakalyCaller.cs index 8696cb303..1dd66b0b6 100644 --- a/ImperatorToCK3/Helpers/RakalyCaller.cs +++ b/ImperatorToCK3/Helpers/RakalyCaller.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; namespace ImperatorToCK3.Helpers; @@ -13,10 +12,10 @@ public static class RakalyCaller { static RakalyCaller() { string currentDir = Directory.GetCurrentDirectory(); RakalyExecutablePath = $"Resources/rakaly/rakaly-{RakalyVersion}-x86_64-pc-windows-msvc/rakaly.exe"; - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + if (OperatingSystem.IsMacOS()) { RakalyExecutablePath = $"Resources/rakaly/rakaly-{RakalyVersion}-x86_64-apple-darwin/rakaly"; Exec($"chmod +x {currentDir}/{RakalyExecutablePath}"); - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { + } else if (OperatingSystem.IsLinux()) { RakalyExecutablePath = $"Resources/rakaly/rakaly-{RakalyVersion}-x86_64-unknown-linux-musl/rakaly"; Exec($"chmod +x {currentDir}/{RakalyExecutablePath}"); } diff --git a/ImperatorToCK3/Imperator/Characters/Character.cs b/ImperatorToCK3/Imperator/Characters/Character.cs index 225b0b7c2..7514b7765 100644 --- a/ImperatorToCK3/Imperator/Characters/Character.cs +++ b/ImperatorToCK3/Imperator/Characters/Character.cs @@ -65,11 +65,11 @@ public bool LinkFamily(FamilyCollection families, SortedSet? missingDefin public bool IsDead => DeathDate is not null; public string? DeathReason { get; set; } private HashSet parsedSpouseIds = new(); - public Dictionary Spouses { get; set; } = new(); + public IDictionary Spouses { get; set; } = new Dictionary(); public OrderedSet FriendIds { get; } = new(); public OrderedSet RivalIds { get; } = new(); private HashSet parsedChildrenIds = new(); - public Dictionary Children { get; set; } = new(); + public IDictionary Children { get; set; } = new Dictionary(); private ulong? parsedMotherId; public Character? Mother { get; set; } private ulong? parsedFatherId; @@ -85,7 +85,7 @@ public Family? Family { family = value; } } - public List Traits { get; set; } = new(); + public IList Traits { get; set; } = new List(); public CharacterAttributes Attributes { get; private set; } = new(); public IReadOnlySet Variables { get; private set; } = ImmutableHashSet.Empty; public bool IsBald => Variables.Contains("bald"); diff --git a/ImperatorToCK3/ImperatorToCK3.csproj b/ImperatorToCK3/ImperatorToCK3.csproj index 91ed2f80f..2345331dd 100644 --- a/ImperatorToCK3/ImperatorToCK3.csproj +++ b/ImperatorToCK3/ImperatorToCK3.csproj @@ -15,13 +15,13 @@ ..\Release\ImperatorToCK3\ - 1701;1702;MA0002;MA0006;MA0074 + 1701;1702;MA0002;MA0006;MA0011;MA0074 ..\Debug\ImperatorToCK3\ true - 1701;1702;MA0002;MA0006;MA0074 + 1701;1702;MA0002;MA0006;MA0011;MA0074