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

Commit

Permalink
add some visual messages for patches being applied
Browse files Browse the repository at this point in the history
have patches specify if they were applied

filled out hash check for ItemComparisonColorPatch and fixed harmony patch owners check
  • Loading branch information
Tyler-IN committed Apr 6, 2020
1 parent de53fa8 commit 2944916
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/CommunityPatch/CommunityPatchSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace CommunityPatch {
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 @@ -194,12 +194,14 @@ public override void OnGameInitializationFinished(Game game) {
patch.Apply(game);
}
catch (Exception ex) {
Error(ex, $"Error while applying patch: {patch.GetType().FullName}");
Error(ex, $"Error while applying patch: {patch.GetType().Name}");
}
}
catch (Exception ex) {
Error(ex, $"Error while checking if patch is applicable: {patch.GetType().FullName}");
Error(ex, $"Error while checking if patch is applicable: {patch.GetType().Name}");
}

ShowMessage($"{(patch.Applied ? "Applied" : "Skipped")} Patch: {patch.GetType().Name}");
}

base.OnGameInitializationFinished(game);
Expand Down
2 changes: 2 additions & 0 deletions src/CommunityPatch/IPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IPatch {
bool IsApplicable(Game game);

void Apply(Game game);

bool Applied { get; }

}

Expand Down
3 changes: 3 additions & 0 deletions src/CommunityPatch/Patches/DisciplinarianPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace CommunityPatch.Patches {

internal class DisciplinarianPatch : IPatch {

public bool Applied { get; private set; }

private readonly PerkObject _perk
= PerkObject.FindFirst(x => x.Name.GetID() == "ER3ieXOb");

Expand Down Expand Up @@ -34,6 +36,7 @@ public void Apply(Game game) {
_perk.PrimaryRole, _perk.PrimaryBonus,
_perk.IncrementType
);
Applied = true;
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/CommunityPatch/Patches/HealthyScoutPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace CommunityPatch.Patches {

internal class HealthyScoutPatch : IPatch {

public bool Applied { get; private set; }

private readonly PerkObject _perk
= PerkObject.FindFirst(x => x.Name.GetID() == "dDKOoD3e");

Expand Down Expand Up @@ -36,6 +38,7 @@ public void Apply(Game game) {
_perk.SecondaryRole, _perk.SecondaryBonus,
_perk.IncrementType
);
Applied = true;
}

}
Expand Down
17 changes: 13 additions & 4 deletions src/CommunityPatch/Patches/ItemComparisonColorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,35 @@ namespace CommunityPatch.Patches {

public class ItemComparisonColorPatch : IPatch {

public bool Applied { get; private set; }

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())
if (patchInfo != null && 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[] { });
return hash.SequenceEqual(new byte[] {
0x4C, 0x29, 0xDC, 0x2D, 0x78, 0x89, 0xA7, 0xA8,
0xC6, 0xDA, 0x84, 0xDB, 0x07, 0x2E, 0x7D, 0xB4,
0x99, 0xED, 0xB2, 0xB9, 0xC4, 0xBB, 0xAD, 0xE4,
0xC9, 0xD1, 0xC8, 0x0F, 0xD7, 0x8C, 0x25, 0x15
});
}

public void Apply(Game game)
=> CommunityPatchSubModule.Harmony.Patch(TargetMethodInfo,
public void Apply(Game game) {
CommunityPatchSubModule.Harmony.Patch(TargetMethodInfo,
new HarmonyMethod(PatchMethodInfo));
Applied = true;
}

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))) {
Expand Down
3 changes: 3 additions & 0 deletions src/CommunityPatch/Patches/PeakFormPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace CommunityPatch.Patches {

internal class PeakFormPatch : IPatch {

public bool Applied { get; private set; }

private readonly PerkObject _perk
= PerkObject.FindFirst(x => x.Name.GetID() == "fBgGbxaw");

Expand Down Expand Up @@ -35,6 +37,7 @@ public void Apply(Game game) {
_perk.SecondaryRole, _perk.SecondaryBonus,
_perk.IncrementType
);
Applied = true;
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/CommunityPatch/Patches/VassalReleasePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace CommunityPatch.Patches {

public class VassalReleasePatch : IPatch {

public bool Applied { get; private set; }

public bool IsApplicable(Game game)
=> Campaign.Current.ConversationManager.

Expand Down

0 comments on commit 2944916

Please sign in to comment.