using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.SceneManagement;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+94404bf4f72256e50f3920c7fd016b939512314e")]
[assembly: AssemblyProduct("StaminaToEnergy")]
[assembly: AssemblyTitle("StaminaToEnergy")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace Empress.StaminaToEnergy
{
[BepInPlugin("Empress.StaminaToWeaponEnergy", "StaminaToWeaponEnergy", "1.2.0")]
public class StaminaToWeaponEnergy : BaseUnityPlugin, IOnEventCallback, IConnectionCallbacks, IInRoomCallbacks
{
private ConfigEntry<KeyCode> keyActivate = null;
private ConfigEntry<float> staminaPerSecond = null;
private ConfigEntry<float> batteryPercentPerStamina = null;
private ConfigEntry<float> minStaminaReserve = null;
private const byte EventChargeRequest = 177;
internal static StaminaToWeaponEnergy Instance { get; private set; }
internal static ManualLogSource Logger => Instance._logger;
private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;
internal Harmony? Harmony { get; set; }
private void Awake()
{
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00cd: Expected O, but got Unknown
Instance = this;
((Component)this).gameObject.transform.parent = null;
((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
keyActivate = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "ActivateKey", (KeyCode)120, "");
staminaPerSecond = ((BaseUnityPlugin)this).Config.Bind<float>("Tuning", "StaminaPerSecond", 5f, "");
batteryPercentPerStamina = ((BaseUnityPlugin)this).Config.Bind<float>("Tuning", "BatteryPercentPerStamina", 0.5f, "");
minStaminaReserve = ((BaseUnityPlugin)this).Config.Bind<float>("Tuning", "MinStaminaReserve", 5f, "");
GameObject val = new GameObject("__StaminaToWeaponEnergy_Runtime")
{
hideFlags = (HideFlags)61
};
Object.DontDestroyOnLoad((Object)(object)val);
val.AddComponent<StaminaToWeaponEnergyRuntime>();
Patch();
Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} ready");
}
private void OnDestroy()
{
Harmony? harmony = Harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
internal void Patch()
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Expected O, but got Unknown
//IL_0026: Expected O, but got Unknown
if (Harmony == null)
{
Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
Harmony val2 = val;
Harmony = val;
}
Harmony.PatchAll();
}
private void Update()
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0192: Unknown result type (might be due to invalid IL or missing references)
//IL_0197: Unknown result type (might be due to invalid IL or missing references)
//IL_0199: Unknown result type (might be due to invalid IL or missing references)
//IL_01a0: Expected O, but got Unknown
//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
if (!StaminaToWeaponEnergyRuntime.RuntimeActive || !Input.GetKey(keyActivate.Value))
{
return;
}
PlayerController instance = PlayerController.instance;
if ((Object)(object)instance == (Object)null || instance.EnergyCurrent <= minStaminaReserve.Value)
{
return;
}
ItemBattery activeBattery = GetActiveBattery();
if ((Object)(object)activeBattery == (Object)null || activeBattery.isUnchargable || activeBattery.batteryLife >= 99.9f)
{
return;
}
float deltaTime = Time.deltaTime;
float num = Mathf.Max(0f, instance.EnergyCurrent - minStaminaReserve.Value);
if (num <= 0f)
{
return;
}
float num2 = Mathf.Min(staminaPerSecond.Value * deltaTime, num);
if (num2 <= 0f)
{
return;
}
float num3 = batteryPercentPerStamina.Value * staminaPerSecond.Value;
if (PhotonNetwork.IsMasterClient)
{
GameObject orCreateCharger = StaminaToWeaponEnergyRuntime.GetOrCreateCharger(PhotonNetwork.LocalPlayer.ActorNumber);
activeBattery.ChargeBattery(orCreateCharger, num3);
}
else
{
PhotonView component = ((Component)activeBattery).GetComponent<PhotonView>();
if ((Object)(object)component != (Object)null)
{
object[] array = new object[2] { component.ViewID, num3 };
RaiseEventOptions val = new RaiseEventOptions
{
Receivers = (ReceiverGroup)2
};
PhotonNetwork.RaiseEvent((byte)177, (object)array, val, SendOptions.SendUnreliable);
}
}
instance.EnergyCurrent = Mathf.Max(0f, instance.EnergyCurrent - num2);
}
public void OnEvent(EventData photonEvent)
{
if (!StaminaToWeaponEnergyRuntime.RuntimeActive || !PhotonNetwork.IsMasterClient || photonEvent.Code != 177 || !(photonEvent.CustomData is object[] array) || array.Length < 2)
{
return;
}
int num = (int)array[0];
float num2 = (float)array[1];
PhotonView val = PhotonView.Find(num);
if (Object.op_Implicit((Object)(object)val))
{
ItemBattery component = ((Component)val).GetComponent<ItemBattery>();
if (Object.op_Implicit((Object)(object)component) && !component.isUnchargable && !(component.batteryLife >= 99.9f))
{
GameObject orCreateCharger = StaminaToWeaponEnergyRuntime.GetOrCreateCharger(photonEvent.Sender);
component.ChargeBattery(orCreateCharger, num2);
}
}
}
public void OnConnected()
{
}
public void OnConnectedToMaster()
{
}
public void OnDisconnected(DisconnectCause cause)
{
StaminaToWeaponEnergyRuntime.FullDeactivate();
}
public void OnRegionListReceived(RegionHandler regionHandler)
{
}
public void OnCustomAuthenticationResponse(Dictionary<string, object> data)
{
}
public void OnCustomAuthenticationFailed(string debugMessage)
{
}
public void OnJoinedRoom()
{
StaminaToWeaponEnergyRuntime.TryActivate();
}
public void OnLeftRoom()
{
StaminaToWeaponEnergyRuntime.FullDeactivate();
}
public void OnPlayerEnteredRoom(Player newPlayer)
{
}
public void OnPlayerLeftRoom(Player otherPlayer)
{
}
public void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
}
public void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
{
}
public void OnMasterClientSwitched(Player newMasterClient)
{
}
private ItemBattery? GetActiveBattery()
{
Inventory instance = Inventory.instance;
if ((Object)(object)instance == (Object)null)
{
return null;
}
PhysGrabber physGrabber = instance.physGrabber;
if ((Object)(object)physGrabber != (Object)null && (Object)(object)physGrabber.grabbedObjectTransform != (Object)null)
{
ItemBattery componentInParent = ((Component)physGrabber.grabbedObjectTransform).GetComponentInParent<ItemBattery>();
if ((Object)(object)componentInParent != (Object)null)
{
return componentInParent;
}
}
List<InventorySpot> allSpots = instance.GetAllSpots();
if (allSpots != null)
{
for (int i = 0; i < allSpots.Count; i++)
{
InventorySpot obj = allSpots[i];
ItemEquippable val = ((obj != null) ? obj.CurrentItem : null);
if (!((Object)(object)val == (Object)null) && val.IsEquipped())
{
ItemBattery component = ((Component)val).GetComponent<ItemBattery>();
if ((Object)(object)component != (Object)null)
{
return component;
}
}
}
}
return null;
}
}
internal sealed class StaminaToWeaponEnergyRuntime : MonoBehaviour
{
private static StaminaToWeaponEnergyRuntime? _inst;
private bool _wasActive;
private static readonly Dictionary<int, GameObject> Chargers = new Dictionary<int, GameObject>();
internal static bool RuntimeActive { get; private set; }
private void Awake()
{
if (Object.op_Implicit((Object)(object)_inst) && (Object)(object)_inst != (Object)(object)this)
{
Object.Destroy((Object)(object)((Component)this).gameObject);
return;
}
_inst = this;
Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
SceneManager.activeSceneChanged += OnSceneChanged;
}
private void OnDestroy()
{
SceneManager.activeSceneChanged -= OnSceneChanged;
FullDeactivate();
}
private static void OnSceneChanged(Scene a, Scene b)
{
FullDeactivate();
}
private void OnDisable()
{
FullDeactivate();
}
private void Update()
{
if (!RuntimeReady())
{
if (_wasActive)
{
FullDeactivate();
}
}
else if (!_wasActive)
{
TryActivate();
}
}
internal static bool RuntimeReady()
{
if (!PhotonNetwork.IsConnected || !PhotonNetwork.InRoom)
{
return false;
}
if (SemiFunc.MenuLevel())
{
return false;
}
if (!Object.op_Implicit((Object)(object)LevelGenerator.Instance) || !LevelGenerator.Instance.Generated)
{
return false;
}
if (!Object.op_Implicit((Object)(object)PlayerAvatar.instance))
{
return false;
}
return true;
}
internal static void TryActivate()
{
StaminaToWeaponEnergyRuntime inst = _inst;
if (Object.op_Implicit((Object)(object)inst) && !inst._wasActive && RuntimeReady())
{
PhotonNetwork.AddCallbackTarget((object)StaminaToWeaponEnergy.Instance);
PhotonNetwork.AddCallbackTarget((object)StaminaToWeaponEnergy.Instance);
PhotonNetwork.AddCallbackTarget((object)StaminaToWeaponEnergy.Instance);
RuntimeActive = true;
inst._wasActive = true;
StaminaToWeaponEnergy.Logger.LogDebug((object)"[StaminaToWeaponEnergy] Runtime activated");
}
}
internal static void FullDeactivate()
{
StaminaToWeaponEnergyRuntime inst = _inst;
if (!Object.op_Implicit((Object)(object)inst))
{
Chargers.Clear();
return;
}
if (!inst._wasActive && !RuntimeActive)
{
Chargers.Clear();
return;
}
RuntimeActive = false;
inst._wasActive = false;
PhotonNetwork.RemoveCallbackTarget((object)StaminaToWeaponEnergy.Instance);
PhotonNetwork.RemoveCallbackTarget((object)StaminaToWeaponEnergy.Instance);
PhotonNetwork.RemoveCallbackTarget((object)StaminaToWeaponEnergy.Instance);
Chargers.Clear();
StaminaToWeaponEnergy.Logger.LogDebug((object)"[StaminaToWeaponEnergy] Runtime deactivated");
}
internal static GameObject GetOrCreateCharger(int actorNumber)
{
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Expected O, but got Unknown
if (Chargers.TryGetValue(actorNumber, out GameObject value) && Object.op_Implicit((Object)(object)value))
{
return value;
}
GameObject val = new GameObject($"EmpressCharger_{actorNumber}");
((Object)val).hideFlags = (HideFlags)61;
Chargers[actorNumber] = val;
return val;
}
}
}