diff --git a/src/CommunityPatch/CommunityPatch.csproj b/src/CommunityPatch/CommunityPatch.csproj index 79ce517..00203fd 100644 --- a/src/CommunityPatch/CommunityPatch.csproj +++ b/src/CommunityPatch/CommunityPatch.csproj @@ -26,6 +26,10 @@ %(Identity) False + + %(Identity) + True + @@ -35,6 +39,10 @@ + + True + NU1701 + diff --git a/src/CommunityPatch/CommunityPatchSubModule.Initialization.cs b/src/CommunityPatch/CommunityPatchSubModule.Initialization.cs index d9c6776..24e75d3 100644 --- a/src/CommunityPatch/CommunityPatchSubModule.Initialization.cs +++ b/src/CommunityPatch/CommunityPatchSubModule.Initialization.cs @@ -85,6 +85,9 @@ static CommunityPatchSubModule() { private static void LoadDelayedSubModules() { foreach (var mod in ModuleInfo.GetModules()) { + if (!mod.IsSelected) + continue; + var id = mod.Id; var subModsXmlPath = ModuleInfo.GetPath(id); var modDir = Path.GetDirectoryName(subModsXmlPath); diff --git a/src/CommunityPatch/CommunityPatchSubModule.cs b/src/CommunityPatch/CommunityPatchSubModule.cs index 8b0985e..37af605 100644 --- a/src/CommunityPatch/CommunityPatchSubModule.cs +++ b/src/CommunityPatch/CommunityPatchSubModule.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Threading; +using HarmonyLib; using JetBrains.Annotations; using TaleWorlds.CampaignSystem; using TaleWorlds.Core; @@ -17,6 +18,8 @@ namespace CommunityPatch { [PublicAPI] public partial class CommunityPatchSubModule : MBSubModuleBase { + internal static readonly Harmony Harmony = new Harmony("CommunityPatch"); + internal static readonly LinkedList RecordedFirstChanceExceptions = new LinkedList(); @@ -113,7 +116,7 @@ private void ShowModOptions() ), new InquiryElement( nameof(RecordFirstChanceExceptions), - DisableIntroVideo ? "Record First Chance Exceptions" : "Ignore First Chance Exceptions", + RecordFirstChanceExceptions ? "Ignore First Chance Exceptions" : "Record First Chance Exceptions", null ), new InquiryElement( diff --git a/src/CommunityPatch/OptionsFile.cs b/src/CommunityPatch/OptionsFile.cs index 6421f31..327835f 100644 --- a/src/CommunityPatch/OptionsFile.cs +++ b/src/CommunityPatch/OptionsFile.cs @@ -32,7 +32,9 @@ public OptionsFile(string fileName) { [PublicAPI] public void Save() { using var sw = new StreamWriter(_path, false, Encoding.UTF8, 65536) {NewLine = "\n"}; - _toml.WriteTo(sw); + foreach (var kv in _toml.KeyValues) + sw.WriteLine(kv.ToString().Trim('\n')); + sw.WriteLine(); } [PublicAPI] diff --git a/src/CommunityPatch/Patches/ItemComparisonColorPatch.cs b/src/CommunityPatch/Patches/ItemComparisonColorPatch.cs new file mode 100644 index 0000000..7942feb --- /dev/null +++ b/src/CommunityPatch/Patches/ItemComparisonColorPatch.cs @@ -0,0 +1,69 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Security.Cryptography; +using HarmonyLib; +using TaleWorlds.CampaignSystem; +using TaleWorlds.CampaignSystem.ViewModelCollection; +using TaleWorlds.Core; +using TaleWorlds.Library; + +namespace CommunityPatch.Patches { + + public class ItemComparisonColorPatch : IPatch { + + private static readonly MethodInfo TargetMethodInfo = typeof(ItemMenuVM).GetMethod("GetColorFromComparison", BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly); + + private static readonly MethodInfo PatchMethodInfo = typeof(ItemComparisonColorPatch).GetMethod(nameof(GetColorFromComparisonPatched), BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.DeclaredOnly); + + public bool IsApplicable(Game game) { + var patchInfo = Harmony.GetPatchInfo(TargetMethodInfo); + if (patchInfo.Owners.Any()) + return false; + + var bytes = TargetMethodInfo.GetMethodBody()?.GetILAsByteArray(); + if (bytes == null) return false; + + using var hasher = SHA256.Create(); + var hash = hasher.ComputeHash(bytes); + return hash.SequenceEqual(new byte[] { }); + } + + public void Apply(Game game) + => CommunityPatchSubModule.Harmony.Patch(TargetMethodInfo, + new HarmonyMethod(PatchMethodInfo)); + + private static bool GetColorFromComparisonPatched(int result, bool isCompared, ref Color __result) { + if (MobileParty.MainParty != null && !(MobileParty.MainParty.HasPerk(DefaultPerks.Trade.WholeSeller) || MobileParty.MainParty.HasPerk(DefaultPerks.Trade.Appraiser))) { + __result = Colors.Black; + return false; + } + + if (result != -1) { + if (result != 1) { + __result = Colors.Black; + return false; + } + + if (!isCompared) { + __result = UIColors.PositiveIndicator; + return false; + } + + __result = UIColors.NegativeIndicator; + return false; + } + + if (!isCompared) { + __result = UIColors.NegativeIndicator; + return false; + } + + __result = UIColors.PositiveIndicator; + return false; + } + + } + +} \ No newline at end of file