using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using Zorro.Core;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace DisableNightCold
{
[BepInPlugin("com.github.thanks.disable-night-cold", "Disable Night Cold", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
private sealed class PendingCancellation
{
public float BufferedFraction;
public int OwedChunks;
}
public const string PluginGuid = "com.github.thanks.disable-night-cold";
public const string PluginName = "Disable Night Cold";
public const string PluginVersion = "1.0.0";
private static Plugin _instance;
private readonly Dictionary<int, PendingCancellation> _pendingByViewId = new Dictionary<int, PendingCancellation>();
private void Awake()
{
if ((Object)(object)_instance != (Object)null && (Object)(object)_instance != (Object)(object)this)
{
Object.Destroy((Object)(object)this);
return;
}
_instance = this;
Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
((BaseUnityPlugin)this).Logger.LogInfo((object)"Disable Night Cold loaded");
}
private void Update()
{
if (!PhotonNetwork.InRoom || !PhotonNetwork.IsMasterClient)
{
_pendingByViewId.Clear();
}
else
{
if ((Object)(object)DayNightManager.instance == (Object)null)
{
return;
}
List<Character> allCharacters = Character.AllCharacters;
if (allCharacters == null || allCharacters.Count == 0)
{
return;
}
float nightColdDelta = GetNightColdDelta();
for (int num = allCharacters.Count - 1; num >= 0; num--)
{
Character val = allCharacters[num];
if (!((Object)(object)val == (Object)null) && !((Object)(object)((MonoBehaviourPun)val).photonView == (Object)null) && val.refs != null && !((Object)(object)val.refs.afflictions == (Object)null) && TryGetState(val, out var state))
{
if (nightColdDelta > 0f)
{
state.BufferedFraction += nightColdDelta;
while (state.BufferedFraction >= 0.025f)
{
state.BufferedFraction -= 0.025f;
state.OwedChunks++;
}
}
TryCancelOwedCold(val, state);
}
}
TrimMissingCharacters(allCharacters);
}
}
private float GetNightColdDelta()
{
if (!Ascents.isNightCold)
{
return 0f;
}
MountainProgressHandler instance = Singleton<MountainProgressHandler>.Instance;
if ((Object)(object)instance == (Object)null || instance.maxProgressPointReached >= 3)
{
return 0f;
}
if (DayNightManager.instance.isDay >= 0.5f)
{
return 0f;
}
return Time.deltaTime * (1f - DayNightManager.instance.isDay) * Ascents.nightColdRate;
}
private bool TryGetState(Character character, out PendingCancellation state)
{
int viewID = ((MonoBehaviourPun)character).photonView.ViewID;
if (viewID <= 0)
{
state = null;
return false;
}
if (!_pendingByViewId.TryGetValue(viewID, out state))
{
state = new PendingCancellation();
_pendingByViewId.Add(viewID, state);
}
return true;
}
private void TryCancelOwedCold(Character character, PendingCancellation state)
{
if (state.OwedChunks <= 0)
{
return;
}
float currentStatus = character.refs.afflictions.GetCurrentStatus((STATUSTYPE)2);
if (currentStatus < 0.025f)
{
return;
}
int num = Mathf.FloorToInt(currentStatus / 0.025f);
if (num <= 0)
{
return;
}
int num2 = Mathf.Min(state.OwedChunks, num);
float num3 = (float)num2 * 0.025f;
if (!(num3 <= 0f))
{
state.OwedChunks -= num2;
if (((MonoBehaviourPun)character).photonView.IsMine)
{
character.refs.afflictions.SubtractStatus((STATUSTYPE)2, num3, false, false);
return;
}
float[] array = new float[CharacterAfflictions.NumStatusTypes];
array[2] = 0f - num3;
((MonoBehaviourPun)character).photonView.RPC("RPC_ApplyStatusesFromFloatArray", (RpcTarget)0, new object[1] { array });
}
}
private void TrimMissingCharacters(List<Character> characters)
{
HashSet<int> hashSet = new HashSet<int>();
for (int i = 0; i < characters.Count; i++)
{
Character val = characters[i];
if ((Object)(object)val != (Object)null && (Object)(object)((MonoBehaviourPun)val).photonView != (Object)null && ((MonoBehaviourPun)val).photonView.ViewID > 0)
{
hashSet.Add(((MonoBehaviourPun)val).photonView.ViewID);
}
}
List<int> list = null;
foreach (KeyValuePair<int, PendingCancellation> item in _pendingByViewId)
{
if (!hashSet.Contains(item.Key))
{
if (list == null)
{
list = new List<int>();
}
list.Add(item.Key);
}
}
if (list != null)
{
for (int j = 0; j < list.Count; j++)
{
_pendingByViewId.Remove(list[j]);
}
}
}
}
}