Can't Patch Getter with Harmony Anymore BL 1.7.1

Users who are viewing this thread

nachomastero

Recruit
Since BL 1.7.1 update, my patch for Getter IsVisibleNotification is no longer working, patch is ignored like not existed. the BL code for this function seems not changed. Is somebody experiencing similar issues trying to patch getters?

[HarmonyPatch(typeof(PregnancyLogEntry), "get_IsVisibleNotification")]
internal class PregLogEntrySetIsVisibleNotificationPatch
{
private PregLogEntrySetIsVisibleNotificationPatch()
{
}
public static bool Prefix(PregnancyLogEntry __instance, ref bool __result)
{
InformationManager.DisplayMessage(new InformationMessage("PATCH WORKING"));
__result=true;
return false;
}
}

I am not getting the printed message anymore, I also try
[HarmonyPatch(typeof(PregnancyLogEntry), "IsVisibleNotification", MethodType.Getter)]
but still no success. Any help will be appreciated, thanks.
 
Solution
Try to call PregnancyLogEntry.IsVisibleNotification at OnApplicationTick().
I tried, it was not letting me do that, so I tried adding in a secondary class (Utility) to get the instance:

Code:
public static PregnancyLogEntry GetPregnancyLogEntryInstance()
        {
            return Campaign.Current.CampaignBehaviorManager.GetBehavior<PregnancyLogEntry>();
        }

Then I called it on the OnApplicationTick().

Code:
protected override void OnApplicationTick(float dt)
        {
         
            PregnancyLogEntry MyPregnancyLog = Utillty.GetPregnancyLogEntryInstance();
        }

And yes, I was able to access to MyPregnancyLog.IsVisibleNotification but since it is a bool thing, it can not...
Hey, instead of a prefix bool returning false, can you try a postfix void?
Tried that, but the patch persist with no effect. What could it be.. It worked fine since ever until now, I checked the native code and no changes was made to that function, not even to the entire PregnancyLogEntry class, I tried with the new and the old Harmony update, no effect
 
Upvote 0
It is executed when a child is conceived, I call MakePregnantAction.Apply(Hero mother). My mod Pregnancy control, basically adds all the heroes you talk to a list, and a function will be executed on daily tick hero
Code:
CheckAddNewPregnancies(Hero mother)
, it will make all the heroes on the list pregnant with MakePregnantAction.Apply(Hero mother). after this point, Native code will handle the pregnancy, at some point, it will call the
Code:
PregnancyLogEntry.IsVisibleNotification
, BUT the problem with this function is, it will try to read the mother's clan (DNspy prove).
Code:
public bool IsVisibleNotification
{
    get
    {
        return this.Mother.Clan.Equals(Hero.MainHero.Clan);
    }
}
if you make pregnant a notable or a wanderer, this will crash the game with a null exception cause they has no clan, that's why I patched the function with an harmony prefix.
You can see the complete call stack on this crash report (Caused by a notable, because the patch is not working :p)


Sorry for my poor tech speech, I am actually a novice at programing. also sorry for my poor english, thanks for answer and try to help, I appreciate that.
 
Upvote 0
Try to call PregnancyLogEntry.IsVisibleNotification at OnApplicationTick().
I tried, it was not letting me do that, so I tried adding in a secondary class (Utility) to get the instance:

Code:
public static PregnancyLogEntry GetPregnancyLogEntryInstance()
        {
            return Campaign.Current.CampaignBehaviorManager.GetBehavior<PregnancyLogEntry>();
        }

Then I called it on the OnApplicationTick().

Code:
protected override void OnApplicationTick(float dt)
        {
         
            PregnancyLogEntry MyPregnancyLog = Utillty.GetPregnancyLogEntryInstance();
        }

And yes, I was able to access to MyPregnancyLog.IsVisibleNotification but since it is a bool thing, it can not figure out what to do next, I tried MyPregnancyLog.IsVisibleNotification = false; but I was unable to modify it since it is read only.

Anyway, I managed to solve my problem using a different way, I realized that trying to patch and use the native PregnancyLogEntry class was kinda worthless compared to the fact that I could implement my own class for this log notification, so I did.
I simply patched the CommentPregnancyBehavior.OnChildConceived() instead, which is the function calling the PregnancyLogEntry Class, and it works as intended:

1.- Create an exact copy of the PregnancyLogEntry Class with all his functions:
Code:
public class PregnancyControlLogEntry : LogEntry, IEncyclopediaLog, IChatNotification
{
    //Excluded
}

2.- Modify whatever is needed.

3.- Patch the calling function:
Code:
using System;
using TaleWorlds.CampaignSystem.LogEntries;
using HarmonyLib;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.SandBox.CampaignBehaviors.CommentBehaviors;

namespace FixedPregnancyControl
{
    [HarmonyPatch(typeof(CommentPregnancyBehavior), "OnChildConceived")]

    public class PregnancyControlCommentPatch
    {
        private PregnancyControlCommentPatch()
        {

        }
        public static bool Prefix(CommentPregnancyBehavior __instance, ref Hero mother)
        {
            LogEntry.AddLogEntry(new PregnancyControlLogEntry(mother));

            return false;
        }
    }
}

Thanks so much for helping.
 
Upvote 0
Solution
I suspect that the problem are related to the class names to be too long.
I just noticed that other of my patches was also not working, and I fixed it by shorting the names, in both of the falied patches I was using long names:
Code:
internal class PregLogEntrySetIsVisibleNotificationPatch
{

}

Code:
public class PregnancyCheckOffspringsToDeliverPatch
{
}

Curious that only the long-name classes has the issue, I correct the second one by remplacing to another class with a shorter name and it is now working.
 
Upvote 0
Back
Top Bottom