diff --git a/ValheimPlus/Configurations/Sections/StructuralIntegrityConfiguration.cs b/ValheimPlus/Configurations/Sections/StructuralIntegrityConfiguration.cs index 6c34ad48..d0297642 100644 --- a/ValheimPlus/Configurations/Sections/StructuralIntegrityConfiguration.cs +++ b/ValheimPlus/Configurations/Sections/StructuralIntegrityConfiguration.cs @@ -6,6 +6,7 @@ public class StructuralIntegrityConfiguration : ServerSyncConfig - /// Replaces the first load of dt inside Player::UpdateFood with a modified dt that is scaled - /// by the food duration scaling multiplier. This ensures the food lasts longer while maintaining - /// the same rate of regeneration. + /// Apply a modifier on all loads of ItemDrop::ItemData::SharedData.m_foodBurnTime in the Player::EatFood function. /// [HarmonyTranspiler] public static IEnumerable Transpiler(IEnumerable instructions) @@ -267,23 +265,23 @@ public static IEnumerable Transpiler(IEnumerable il = instructions.ToList(); - for (int i = 0; i < il.Count - 2; ++i) + int count = il.Count; + for (int i = 0; i < count; i++) { - if (il[i].LoadsField(field_Player_m_foodUpdateTimer) && - il[i + 1].opcode == OpCodes.Ldarg_1 /* dt */ && - il[i + 2].opcode == OpCodes.Add) + if (il[i].LoadsField(field_ItemDrop_ItemData_SharedData_m_foodBurnTime)) { - // We insert after Ldarg_1 (push dt) a call to our function, which computes the modified DT and returns it. - il.Insert(i + 2, new CodeInstruction(OpCodes.Call, method_ComputeModifiedDt)); + // We insert a call to our ComputeModifiedFoodBurnTime right after the foodBurnTime has been loaded to apply the food duration multiplier + il.Insert(i+1, new CodeInstruction(OpCodes.Call, method_ComputeModifiedFoodBurnTime)); + ++count; } } return il.AsEnumerable(); } - private static float ComputeModifiedDT(float dt) + private static float ComputeModifiedFoodBurnTime(float foodBurnTime) { - return dt / Helper.applyModifierValue(1.0f, Configuration.Current.Food.foodDurationMultiplier); + return Helper.applyModifierValue(foodBurnTime, Configuration.Current.Food.foodDurationMultiplier);; } } @@ -383,8 +381,7 @@ private static void Prefix(ref Player __instance, ref float v) v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.fishing); } } - - if (methodName.Contains(nameof(Player.UpdatePlacement)) || methodName.Contains(nameof(Player.Repair)) || methodName.Contains(nameof(Player.RemovePiece))) + else if (methodName.Contains(nameof(Player.UpdatePlacement)) || methodName.Contains(nameof(Player.Repair)) || methodName.Contains(nameof(Player.RemovePiece))) { string itemName = __instance.GetRightItem()?.m_shared.m_name; if (itemName == "$item_hammer") @@ -400,13 +397,9 @@ private static void Prefix(ref Player __instance, ref float v) v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.cultivator); } } - else if (methodName.Equals(nameof(Player.PlayerAttackInput))) + else if (methodName.Equals(nameof(Player.UpdateAttackBowDraw))) { - ItemDrop.ItemData item = __instance.GetCurrentWeapon(); - if (item?.m_shared.m_skillType == Skills.SkillType.Bows) - { - v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.bows); - } + v = Helper.applyModifierValue(v, Configuration.Current.StaminaUsage.bows); } else if (methodName.Equals(nameof(Player.BlockAttack))) { diff --git a/ValheimPlus/GameClasses/SEMan.cs b/ValheimPlus/GameClasses/SEMan.cs index 0855fabe..88c87556 100644 --- a/ValheimPlus/GameClasses/SEMan.cs +++ b/ValheimPlus/GameClasses/SEMan.cs @@ -1,6 +1,5 @@ using HarmonyLib; using System; -using UnityEngine; using ValheimPlus.Configurations; namespace ValheimPlus.GameClasses @@ -12,11 +11,11 @@ public static class SEMan_AddStatusEffect_Patch private static void Postfix(ref SEMan __instance, ref StatusEffect statusEffect, bool resetTime = false, int itemLevel = 0, float skillLevel = 0) { - if (!Configuration.Current.Player.IsEnabled ) + if (!Configuration.Current.Player.IsEnabled) return; // Don't execute if the affected person is not the player - if (__instance.m_character.IsPlayer()) + if (!__instance.m_character.IsPlayer()) return; // Every guardian power starts with GP_ @@ -26,11 +25,7 @@ private static void Postfix(ref SEMan __instance, ref StatusEffect statusEffect, { if (buff.m_name == __instance.GetStatusEffect(statusEffect.name).m_name) { - Player fromCharacter = (Player)__instance.m_character; - if (fromCharacter.m_guardianSE) - { - fromCharacter.m_guardianSE.m_ttl = Configuration.Current.Player.guardianBuffDuration; - } + __instance.GetStatusEffect(statusEffect.name).m_ttl = Configuration.Current.Player.guardianBuffDuration; } } } diff --git a/ValheimPlus/GameClasses/WearNTear.cs b/ValheimPlus/GameClasses/WearNTear.cs index 175780c1..c74965fc 100644 --- a/ValheimPlus/GameClasses/WearNTear.cs +++ b/ValheimPlus/GameClasses/WearNTear.cs @@ -4,9 +4,9 @@ namespace ValheimPlus.GameClasses { - /// - /// Disable weather damage - /// + /// + /// Disable weather damage + /// [HarmonyPatch(typeof(WearNTear), "HaveRoof")] public static class RemoveWearNTear { @@ -19,42 +19,42 @@ private static void Postfix(ref bool __result) } } - /// - /// Disable weather damage under water - /// - [HarmonyPatch(typeof(WearNTear), "IsUnderWater")] - public static class RemoveWearNTearFromUnderWater - { - private static void Postfix(ref bool __result) - { - if (Configuration.Current.Building.IsEnabled && Configuration.Current.Building.noWeatherDamage) - { - __result = false; - } - } - } + /// + /// Disable weather damage under water + /// + [HarmonyPatch(typeof(WearNTear), "IsUnderWater")] + public static class RemoveWearNTearFromUnderWater + { + private static void Postfix(ref bool __result) + { + if (Configuration.Current.Building.IsEnabled && Configuration.Current.Building.noWeatherDamage) + { + __result = false; + } + } + } - /// - /// Removes the integrity check for having a connected piece to the ground. - /// - [HarmonyPatch(typeof(WearNTear), "HaveSupport")] - public static class WearNTear_HaveSupport_Patch - { - private static void Postfix(ref bool __result) - { - if (Configuration.Current.Building.IsEnabled && Configuration.Current.StructuralIntegrity.disableStructuralIntegrity) - { - __result = true; - } - } - } + /// + /// Removes the integrity check for having a connected piece to the ground. + /// + [HarmonyPatch(typeof(WearNTear), "HaveSupport")] + public static class WearNTear_HaveSupport_Patch + { + private static void Postfix(ref bool __result) + { + if (Configuration.Current.Building.IsEnabled && Configuration.Current.StructuralIntegrity.disableStructuralIntegrity) + { + __result = true; + } + } + } - /// - /// Disable damage to player structures - /// - [HarmonyPatch(typeof(WearNTear), "ApplyDamage")] - public static class WearNTear_ApplyDamage_Patch - { + /// + /// Disable damage to player structures + /// + [HarmonyPatch(typeof(WearNTear), "ApplyDamage")] + public static class WearNTear_ApplyDamage_Patch + { private static bool Prefix(ref WearNTear __instance, ref float damage) { // Gets the name of the method calling the ApplyDamage method @@ -66,11 +66,11 @@ private static bool Prefix(ref WearNTear __instance, ref float damage) if (__instance.m_piece.m_name.StartsWith("$ship")) { - if (Configuration.Current.StructuralIntegrity.disableDamageToPlayerBoats || - (Configuration.Current.StructuralIntegrity.disableWaterDamageToPlayerBoats && - stackTrace.GetFrame(15).GetMethod().Name == "UpdateWaterForce")) return false; - - return true; + if (Configuration.Current.StructuralIntegrity.disableDamageToPlayerBoats || + (Configuration.Current.StructuralIntegrity.disableWaterDamageToPlayerBoats && + stackTrace.GetFrame(15).GetMethod().Name == "UpdateWaterForce")) return false; + + return true; } if (__instance.m_piece.m_name.StartsWith("$tool_cart")) { @@ -82,102 +82,115 @@ private static bool Prefix(ref WearNTear __instance, ref float damage) } return !Configuration.Current.StructuralIntegrity.disableDamageToPlayerStructures; } - } + } - /// - /// Disable structural integrity - /// - [HarmonyPatch(typeof(WearNTear), "GetMaterialProperties")] + /// + /// Disable structural integrity + /// + [HarmonyPatch(typeof(WearNTear), nameof(WearNTear.GetMaterialProperties))] public static class RemoveStructualIntegrity { private static bool Prefix(ref WearNTear __instance, out float maxSupport, out float minSupport, out float horizontalLoss, out float verticalLoss) { - if (Configuration.Current.StructuralIntegrity.IsEnabled && Configuration.Current.StructuralIntegrity.disableStructuralIntegrity) - { - maxSupport = 1500f; - minSupport = 20f; - verticalLoss = 0f; - horizontalLoss = 0f; - return false; - } - if (Configuration.Current.StructuralIntegrity.IsEnabled) - { - // This handling is choosen because we subtract from a value thats reduced by distance from ground contact. - Configuration.Current.StructuralIntegrity.wood = (Configuration.Current.StructuralIntegrity.wood >= 100 ? 100 : Configuration.Current.StructuralIntegrity.wood); - Configuration.Current.StructuralIntegrity.stone = (Configuration.Current.StructuralIntegrity.wood >= 100 ? 100 : Configuration.Current.StructuralIntegrity.stone); - Configuration.Current.StructuralIntegrity.iron = (Configuration.Current.StructuralIntegrity.wood >= 100 ? 100 : Configuration.Current.StructuralIntegrity.iron); - Configuration.Current.StructuralIntegrity.hardWood = (Configuration.Current.StructuralIntegrity.wood >= 100 ? 100 : Configuration.Current.StructuralIntegrity.hardWood); + if (Configuration.Current.StructuralIntegrity.IsEnabled && Configuration.Current.StructuralIntegrity.disableStructuralIntegrity) + { + maxSupport = 1500f; + minSupport = 20f; + verticalLoss = 0f; + horizontalLoss = 0f; + return false; + } + if (Configuration.Current.StructuralIntegrity.IsEnabled) + { + // This handling is choosen because we subtract from a value thats reduced by distance from ground contact. + Configuration.Current.StructuralIntegrity.wood = Helper.Clamp(Configuration.Current.StructuralIntegrity.wood, 0, 100); + Configuration.Current.StructuralIntegrity.stone = Helper.Clamp(Configuration.Current.StructuralIntegrity.stone, 0, 100); + Configuration.Current.StructuralIntegrity.iron = Helper.Clamp(Configuration.Current.StructuralIntegrity.iron, 0, 100); + Configuration.Current.StructuralIntegrity.hardWood = Helper.Clamp(Configuration.Current.StructuralIntegrity.hardWood, 0, 100); + Configuration.Current.StructuralIntegrity.marble = Helper.Clamp(Configuration.Current.StructuralIntegrity.marble, 0, 100); - switch (__instance.m_materialType) - { - case WearNTear.MaterialType.Wood: - maxSupport = 100f; - minSupport = 10f; - verticalLoss = 0.125f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.wood); - horizontalLoss = 0.2f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.wood); - return false; - case WearNTear.MaterialType.Stone: - maxSupport = 1000f; - minSupport = 100f; - verticalLoss = 0.125f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.stone); - horizontalLoss = 1f - ((1f / 100) * Configuration.Current.StructuralIntegrity.stone); - return false; - case WearNTear.MaterialType.Iron: - maxSupport = 1500f; - minSupport = 20f; - verticalLoss = 0.07692308f - ((0.07692308f / 100) * Configuration.Current.StructuralIntegrity.iron); - horizontalLoss = 0.07692308f - ((0.07692308f / 100) * Configuration.Current.StructuralIntegrity.iron); - return false; - case WearNTear.MaterialType.HardWood: - maxSupport = 140f; - minSupport = 10f; - verticalLoss = 0.1f - ((0.1f / 100) * Configuration.Current.StructuralIntegrity.hardWood); - horizontalLoss = 0.16666667f - ((0.16666667f / 100) * Configuration.Current.StructuralIntegrity.hardWood); - return false; - default: - maxSupport = 0f; - minSupport = 0f; - verticalLoss = 0f; - horizontalLoss = 0f; - return false; - } - } - else - { - switch (__instance.m_materialType) - { - case WearNTear.MaterialType.Wood: - maxSupport = 100f; - minSupport = 10f; - verticalLoss = 0.125f; - horizontalLoss = 0.2f; - return false; - case WearNTear.MaterialType.Stone: - maxSupport = 1000f; - minSupport = 100f; - verticalLoss = 0.125f; - horizontalLoss = 1f; - return false; - case WearNTear.MaterialType.Iron: - maxSupport = 1500f; - minSupport = 20f; - verticalLoss = 0.07692308f; - horizontalLoss = 0.07692308f; - return false; - case WearNTear.MaterialType.HardWood: - maxSupport = 140f; - minSupport = 10f; - verticalLoss = 0.1f; - horizontalLoss = 0.16666667f; - return false; - default: - maxSupport = 0f; - minSupport = 0f; - verticalLoss = 0f; - horizontalLoss = 0f; - return false; - } - } + switch (__instance.m_materialType) + { + case WearNTear.MaterialType.Wood: + maxSupport = 100f; + minSupport = 10f; + verticalLoss = 0.125f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.wood); + horizontalLoss = 0.2f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.wood); + return false; + case WearNTear.MaterialType.Stone: + maxSupport = 1000f; + minSupport = 100f; + verticalLoss = 0.125f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.stone); + horizontalLoss = 1f - ((1f / 100) * Configuration.Current.StructuralIntegrity.stone); + return false; + case WearNTear.MaterialType.Iron: + maxSupport = 1500f; + minSupport = 20f; + verticalLoss = 0.07692308f - ((0.07692308f / 100) * Configuration.Current.StructuralIntegrity.iron); + horizontalLoss = 0.07692308f - ((0.07692308f / 100) * Configuration.Current.StructuralIntegrity.iron); + return false; + case WearNTear.MaterialType.HardWood: + maxSupport = 140f; + minSupport = 10f; + verticalLoss = 0.1f - ((0.1f / 100) * Configuration.Current.StructuralIntegrity.hardWood); + horizontalLoss = 0.16666667f - ((0.16666667f / 100) * Configuration.Current.StructuralIntegrity.hardWood); + return false; + case WearNTear.MaterialType.Marble: + maxSupport = 1500f; + minSupport = 100f; + verticalLoss = 0.125f - ((0.125f / 100) * Configuration.Current.StructuralIntegrity.marble); + horizontalLoss = 0.5f - ((0.5f / 100) * Configuration.Current.StructuralIntegrity.marble); + return false; + default: + maxSupport = 0f; + minSupport = 0f; + verticalLoss = 0f; + horizontalLoss = 0f; + return false; + } + } + else + { + switch (__instance.m_materialType) + { + case WearNTear.MaterialType.Wood: + maxSupport = 100f; + minSupport = 10f; + verticalLoss = 0.125f; + horizontalLoss = 0.2f; + return false; + case WearNTear.MaterialType.Stone: + maxSupport = 1000f; + minSupport = 100f; + verticalLoss = 0.125f; + horizontalLoss = 1f; + return false; + case WearNTear.MaterialType.Iron: + maxSupport = 1500f; + minSupport = 20f; + verticalLoss = 0.07692308f; + horizontalLoss = 0.07692308f; + return false; + case WearNTear.MaterialType.HardWood: + maxSupport = 140f; + minSupport = 10f; + verticalLoss = 0.1f; + horizontalLoss = 0.16666667f; + return false; + case WearNTear.MaterialType.Marble: + maxSupport = 1500f; + minSupport = 100f; + verticalLoss = 0.125f; + horizontalLoss = 0.5f; + return false; + default: + maxSupport = 0f; + minSupport = 0f; + verticalLoss = 0f; + horizontalLoss = 0f; + return false; + } + } } } } diff --git a/ValheimPlus/GameClasses/ZNet.cs b/ValheimPlus/GameClasses/ZNet.cs index 7e341aef..9c786279 100644 --- a/ValheimPlus/GameClasses/ZNet.cs +++ b/ValheimPlus/GameClasses/ZNet.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using UnityEngine; using ValheimPlus.Configurations; using ValheimPlus.RPC; @@ -89,7 +90,7 @@ private static void Prefix(ref ZNet __instance) // Load the client config file on server ZNet instance exit (server disconnect) if (ConfigurationExtra.LoadSettings() != true) { - Debug.LogError("Error while loading configuration file."); + ZLog.LogError("Error while loading configuration file."); } ValheimPlusPlugin.harmony.PatchAll(); diff --git a/valheim_plus.cfg b/valheim_plus.cfg index 43eeb723..32a8340e 100644 --- a/valheim_plus.cfg +++ b/valheim_plus.cfg @@ -747,7 +747,7 @@ wood=0 stone=0 iron=0 hardWood=0 - +marble=0 [Ward]