CalculateStrikeMagnitudeForSwing is called on save load

Users who are viewing this thread

Johnnydue

Recruit
I'm trying to make an damage/armor mod but got into some problems while trying out stuff.
I'm an experienced linux/python/c programmer but just leaned c# modding today.
If I try to patch ComputeRawDamageNew everything work as expected.
If I try to patch CalculateStrikeMagnitudeForSwing the game get stuck at loading save and harmony.log gets lots of print out of "PatchSwing".
If I try to patch CalculateStrikeMagnitudeForThrust the game will crash before main menu.
The functions are in TaleWorlds.Core / CombatStatCalculator

C#:
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;
using HarmonyLib;
namespace ExampleMod
{
    public static class MyPatcher
    {
        public static void DoPatching()
        {
            var harmony = new Harmony("com.jj.patch");
            harmony.PatchAll();
        }
    }

    [HarmonyPatch(typeof(CombatStatCalculator))]
    [HarmonyPatch("CalculateStrikeMagnitudeForSwing")]
    class PatchSwing
    {
        static bool Prefix(float swingSpeed, float impactPointAsPercent, float weaponWeight, float weaponLength, float weaponInertia, float weaponCoM, float extraLinearSpeed, ref float __result)
        {
            FileLog.Log("PatchSwing");
            FileLog.Log(swingSpeed.ToString());
            FileLog.Log(impactPointAsPercent.ToString());
            FileLog.Log(weaponWeight.ToString());
            FileLog.Log(weaponLength.ToString());
            FileLog.Log(weaponInertia.ToString());
            FileLog.Log(weaponCoM.ToString());
            FileLog.Log(extraLinearSpeed.ToString());
            return true;
        }
    }

    [HarmonyPatch(typeof(CombatStatCalculator))]
    [HarmonyPatch("CalculateStrikeMagnitudeForThrust")]
    class PatchThrust
    {
        static bool Prefix(float thrustWeaponSpeed, float weaponWeight, float extraLinearSpeed, bool isThrow)
        {
            FileLog.Log("PatchThrust");
            //FileLog.Log(thrustWeaponSpeed.ToString());
            //FileLog.Log(weaponWeight.ToString());
            //FileLog.Log(extraLinearSpeed.ToString());
            //FileLog.Log(isThrow.ToString());
            return true;
        }
    }

    [HarmonyPatch(typeof(CombatStatCalculator))]
    [HarmonyPatch("ComputeRawDamageNew")]
    class PatchArmor
    {
        static bool Prefix(DamageTypes damageType, float magnitude, float armorEffectiveness, float absorbedDamageRatio, ref float __result)
        {
            FileLog.Log("PatchArmor");
            FileLog.Log(damageType.ToString());
            FileLog.Log(magnitude.ToString());
            FileLog.Log(armorEffectiveness.ToString());
            FileLog.Log(absorbedDamageRatio.ToString());
            return true;
        }
    }

    class MySubModule : MBSubModuleBase
    {
        protected override void OnSubModuleLoad()
        {
            MyPatcher.DoPatching();
        }
    }
}
@Ster you seem experienced, could you give a hint?
 
Now that I think about it it maybe that all weapon stats is calculated at game load, still dosen't explain why CalculateStrikeMagnitudeForThrust will crash before main menu.
 
Upvote 0
Found the problem, the game crashes if bool isThrow is in argument list.Maybe it has Default Parameter Value that dnSpy does not show. I probably should buy a rubber duck.
 
Last edited:
Upvote 0
Modifying damage does not require you to patch via Harmony.

Instead, I would recommend that you create a AgentApplyDamageModel to override the old model.You can then read the AttackCollisionData and add your custom logic for damage. You can take a look at SandboxAgentApplyDamageModel class for more information.
 
Upvote 0
Back
Top Bottom