using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BowModForSophie")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BowModForSophie")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7747639c-bcd3-46ea-a853-193acf9189e1")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ValheimBowStaminaMod;
[BepInPlugin("com.gartley.valheimbowstaminamod", "Valheim Configurable Bow Stamina", "1.0.1")]
public class BowStaminaMod : BaseUnityPlugin
{
public static ConfigEntry<float> BowStaminaMultiplier;
public static ManualLogSource LogInstance;
public static float SyncedStaminaMultiplier = 1f;
private Harmony _harmony;
private void Awake()
{
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_0064: Expected O, but got Unknown
LogInstance = ((BaseUnityPlugin)this).Logger;
BowStaminaMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "StaminaMultiplier", 1f, "Multiplier for bow stamina usage. 1.0 is default, 0.5 uses half stamina, 2.0 uses double.");
LogInstance.LogInfo((object)$"Valheim Configurable Bow Stamina Mod loaded. Current Stamina Multiplier: {BowStaminaMultiplier.Value}");
_harmony = new Harmony("com.gartley.valheimbowstaminamod");
_harmony.PatchAll();
if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer())
{
SyncedStaminaMultiplier = BowStaminaMultiplier.Value;
}
BowStaminaMultiplier.SettingChanged += delegate
{
if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer())
{
foreach (ZNetPeer peer in ZNet.instance.GetPeers())
{
ZRoutedRpc.instance.InvokeRoutedRPC(peer.m_uid, "BowStaminaMod_SyncConfig", new object[2] { "StaminaMultiplier", BowStaminaMultiplier.Value });
}
SyncedStaminaMultiplier = BowStaminaMultiplier.Value;
}
};
if ((Object)(object)ZNet.instance != (Object)null)
{
RegisterRPC();
}
}
private void RegisterRPC()
{
ZRoutedRpc.instance.Register<string, float>("BowStaminaMod_SyncConfig", (Action<long, string, float>)RPC_ReceiveConfig);
if (!ZNet.instance.IsServer())
{
return;
}
ZNet.instance.GetPeers().ForEach(delegate(ZNetPeer peer)
{
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Expected O, but got Unknown
peer.m_rpc.Register("BowStaminaMod_PlayerConnect", (Method)delegate
{
OnPlayerConnect(peer.m_uid);
});
});
}
private void OnPlayerConnect(long peerID)
{
ZRoutedRpc.instance.InvokeRoutedRPC(peerID, "BowStaminaMod_SyncConfig", new object[2] { "StaminaMultiplier", BowStaminaMultiplier.Value });
LogInstance.LogInfo((object)$"Sent stamina multiplier {BowStaminaMultiplier.Value} to peer {peerID}");
}
private void RPC_ReceiveConfig(long sender, string key, float value)
{
if (key == "StaminaMultiplier")
{
SyncedStaminaMultiplier = value;
LogInstance.LogInfo((object)$"Received stamina multiplier from server: {value}");
}
}
private void OnDestroy()
{
if (_harmony != null)
{
_harmony.UnpatchSelf();
}
LogInstance.LogInfo((object)"Valheim Configurable Bow Stamina Mod unloaded.");
}
}
[HarmonyPatch(typeof(Player), "UseStamina")]
public static class Player_UseStamina_Patch
{
private static bool Prefix(Player __instance, ref float __0)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Invalid comparison between Unknown and I4
if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
{
ItemData currentWeapon = ((Humanoid)__instance).GetCurrentWeapon();
if (currentWeapon != null && (int)currentWeapon.m_shared.m_itemType == 4)
{
float num = __0;
__0 *= BowStaminaMod.SyncedStaminaMultiplier;
if (BowStaminaMod.SyncedStaminaMultiplier != 1f)
{
BowStaminaMod.LogInstance.LogDebug((object)$"Bow stamina usage modified. Original: {num:F2}, Modified: {__0:F2}");
}
}
}
return true;
}
}