Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
add patch for item comparison coloring
Browse files Browse the repository at this point in the history
fix record fce mod option

fix delayed module loading unselected mods

more directly enforce line endings for saving options
  • Loading branch information
Tyler-IN committed Apr 6, 2020
1 parent 2c6b3eb commit 7ec429d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/CommunityPatch/CommunityPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<HintPath>%(Identity)</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="$(PkgLib_Harmony)\lib\net48\0Harmony.dll">
<HintPath>%(Identity)</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="..\..\SubModule.xml">
Expand All @@ -35,6 +39,10 @@
<ItemGroup>
<PackageReference Include="HardwareProviders.CPU.Standard" Version="2.0.1" />
<PackageReference Include="HardwareProviders.Standard" Version="2.0.1" />
<PackageReference Include="Lib.Harmony" Version="2.0.0.9" PrivateAssets="All">
<GeneratePathProperty>True</GeneratePathProperty>
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
<PackageReference Include="Tomlyn" Version="0.1.2" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/CommunityPatch/CommunityPatchSubModule.Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/CommunityPatch/CommunityPatchSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@ namespace CommunityPatch {
[PublicAPI]
public partial class CommunityPatchSubModule : MBSubModuleBase {

internal static readonly Harmony Harmony = new Harmony("CommunityPatch");

internal static readonly LinkedList<Exception> RecordedFirstChanceExceptions
= new LinkedList<Exception>();

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/CommunityPatch/OptionsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
69 changes: 69 additions & 0 deletions src/CommunityPatch/Patches/ItemComparisonColorPatch.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}

}

0 comments on commit 7ec429d

Please sign in to comment.