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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
[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("TumbleweedThrottle")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+8ea5883b0352137320aef213490b484f7644d538")]
[assembly: AssemblyProduct("TumbleweedThrottle")]
[assembly: AssemblyTitle("TumbleweedThrottle")]
[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 TumbleweedThrottle
{
[BepInPlugin("jill920.tumbleweedthrottle", "Tumbleweed Throttle", "1.1.0")]
public class TumbleweedThrottlePlugin : BaseUnityPlugin
{
public const string MOD_GUID = "jill920.tumbleweedthrottle";
public const string MOD_NAME = "Tumbleweed Throttle";
public const string MOD_VERSION = "1.1.0";
public static ManualLogSource Logger;
public static bool DebugMode = false;
public const float MAX_TATA = 10000f;
public const float TATA_OVERHEAD = 500f;
public const float DEFAULT_TUMBLE_LIFETIME = 15f;
public const float MIN_TUMBLE_LIFETIME = 4f;
public const float MIN_SERIALIZATION_FREQUENCY = 0.1f;
public const float TUMBLEWEED_TARGET_SEARCH_INTERVAL = 1f;
public const float POSITION_TOLERANCE = 1.5f;
public static float CurrentSerializationFrequency = 0.1f;
public static float CurrentTumbleweedLifetime = 15f;
private static int _currentTumbleweedCount = 0;
private static float _lastTATACalculation = 0f;
private static int GetClientCount()
{
Room currentRoom = PhotonNetwork.CurrentRoom;
int num = ((currentRoom == null) ? 1 : currentRoom.PlayerCount);
return Mathf.Max(0, num - 1);
}
public static void UpdateTumbleweedCount(int count)
{
_currentTumbleweedCount = count;
RecalculateThrottleSettings();
}
private static void RecalculateThrottleSettings()
{
int clientCount = GetClientCount();
if (clientCount == 0)
{
if (CurrentSerializationFrequency != 0.1f)
{
CurrentSerializationFrequency = 0.1f;
}
if (CurrentTumbleweedLifetime != 15f)
{
CurrentTumbleweedLifetime = 15f;
}
return;
}
float num = (float)(clientCount * _currentTumbleweedCount) * (1f / CurrentSerializationFrequency) * CurrentTumbleweedLifetime;
if (DebugMode)
{
Logger.LogInfo((object)($"[TATA] Clients={clientCount}, Tumbles={_currentTumbleweedCount}, " + $"Freq={CurrentSerializationFrequency:F3}s, Lifetime={CurrentTumbleweedLifetime:F1}s, " + $"TATA={num:F0}/{10000f}"));
}
if (num > 10000f)
{
float num2 = 9500f;
if (CurrentTumbleweedLifetime > 4f)
{
float num3 = CurrentTumbleweedLifetime * (num2 / num);
num3 = Mathf.Max(4f, num3);
if (Mathf.Abs(num3 - CurrentTumbleweedLifetime) > 0.1f)
{
CurrentTumbleweedLifetime = num3;
if (DebugMode)
{
Logger.LogInfo((object)$"[THROTTLE] Reduced lifetime to {CurrentTumbleweedLifetime:F1}s");
}
}
}
num = (float)(clientCount * _currentTumbleweedCount) * (1f / CurrentSerializationFrequency) * CurrentTumbleweedLifetime;
if (!(num > num2))
{
return;
}
float num4 = CurrentSerializationFrequency * (num / num2);
num4 = Mathf.Max(0.1f, num4);
if (Mathf.Abs(num4 - CurrentSerializationFrequency) > 0.01f)
{
CurrentSerializationFrequency = num4;
if (DebugMode)
{
Logger.LogInfo((object)($"[THROTTLE] Increased sync interval to {CurrentSerializationFrequency:F3}s " + $"(~{1f / CurrentSerializationFrequency:F1} Hz)"));
}
}
return;
}
if (CurrentTumbleweedLifetime < 15f)
{
float num5 = Mathf.Min(15f, CurrentTumbleweedLifetime + 0.5f);
if (num5 != CurrentTumbleweedLifetime)
{
CurrentTumbleweedLifetime = num5;
if (DebugMode)
{
Logger.LogInfo((object)$"[RESTORE] Increased lifetime to {CurrentTumbleweedLifetime:F1}s");
}
}
}
if (!(CurrentSerializationFrequency > 0.1f))
{
return;
}
float num6 = Mathf.Max(0.1f, CurrentSerializationFrequency - 0.05f);
if (num6 != CurrentSerializationFrequency)
{
CurrentSerializationFrequency = num6;
if (DebugMode)
{
Logger.LogInfo((object)($"[RESTORE] Decreased sync interval to {CurrentSerializationFrequency:F3}s " + $"(~{1f / CurrentSerializationFrequency:F1} Hz)"));
}
}
}
private void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
string[] commandLineArgs = Environment.GetCommandLineArgs();
string[] array = commandLineArgs;
foreach (string text in array)
{
if (text.Equals("-PerfDebug", StringComparison.OrdinalIgnoreCase))
{
DebugMode = true;
break;
}
}
Logger.LogInfo((object)$"Photon SendRate: {PhotonNetwork.SendRate}");
Logger.LogInfo((object)$"Photon SerializationRate: {PhotonNetwork.SerializationRate}");
Logger.LogInfo((object)$"Time.fixedDeltaTime: {Time.fixedDeltaTime} ({1f / Time.fixedDeltaTime:F0} Hz)");
Harmony.CreateAndPatchAll(typeof(TumbleweedThrottlePlugin).Assembly, "jill920.tumbleweedthrottle");
Logger.LogInfo((object)"[Tumbleweed Throttle 1.1.0] Loaded");
Logger.LogInfo((object)$" TATA Limit: {10000f} units (host excluded)");
Logger.LogInfo((object)$" Lifetime range: {4f}s - {15f}s");
Logger.LogInfo((object)$" Sync range: {0.1f}s - unlimited");
}
private void Update()
{
if (Time.time - _lastTATACalculation >= 2f)
{
_lastTATACalculation = Time.time;
}
}
}
public class ThrottledTumbleweedSync : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
private Rigidbody body;
private Vector3 lastSyncedPosition;
private Vector3 lastSyncedVelocity;
private float lastSyncTime;
private void Start()
{
photonView = ((Component)this).GetComponent<PhotonView>();
body = ((Component)this).GetComponent<Rigidbody>();
PhotonRigidbodyView component = ((Component)this).GetComponent<PhotonRigidbodyView>();
if ((Object)(object)component != (Object)null)
{
((Behaviour)component).enabled = false;
}
ItemPhysicsSyncer component2 = ((Component)this).GetComponent<ItemPhysicsSyncer>();
if ((Object)(object)component2 != (Object)null)
{
((Behaviour)component2).enabled = false;
FieldInfo field = typeof(ItemPhysicsSyncer).GetField("shouldSync", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
field.SetValue(component2, false);
}
}
if ((Object)(object)photonView != (Object)null)
{
if (photonView.ObservedComponents != null)
{
photonView.ObservedComponents.Clear();
}
photonView.ObservedComponents.Add((Component)(object)this);
}
}
void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
//IL_0115: Unknown result type (might be due to invalid IL or missing references)
//IL_011a: Unknown result type (might be due to invalid IL or missing references)
//IL_0122: Unknown result type (might be due to invalid IL or missing references)
//IL_0127: Unknown result type (might be due to invalid IL or missing references)
//IL_012f: Unknown result type (might be due to invalid IL or missing references)
//IL_0134: Unknown result type (might be due to invalid IL or missing references)
//IL_0158: Unknown result type (might be due to invalid IL or missing references)
//IL_015d: Unknown result type (might be due to invalid IL or missing references)
//IL_0161: Unknown result type (might be due to invalid IL or missing references)
//IL_0178: Unknown result type (might be due to invalid IL or missing references)
//IL_017d: Unknown result type (might be due to invalid IL or missing references)
//IL_0184: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: Unknown result type (might be due to invalid IL or missing references)
//IL_0071: Unknown result type (might be due to invalid IL or missing references)
//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)body == (Object)null)
{
return;
}
float time = Time.time;
float currentSerializationFrequency = TumbleweedThrottlePlugin.CurrentSerializationFrequency;
if (stream.IsWriting)
{
if (!(time - lastSyncTime < currentSerializationFrequency))
{
bool flag = Vector3.Distance(body.position, lastSyncedPosition) > 1.5f;
bool flag2 = Vector3.Distance(body.linearVelocity, lastSyncedVelocity) > 0.5f;
if (flag || flag2)
{
lastSyncTime = time;
lastSyncedPosition = body.position;
lastSyncedVelocity = body.linearVelocity;
stream.SendNext((object)body.position);
stream.SendNext((object)body.linearVelocity);
}
}
}
else if (stream.Count >= 2)
{
Vector3 val = (Vector3)stream.ReceiveNext();
Vector3 val2 = (Vector3)stream.ReceiveNext();
float num = Vector3.Distance(body.position, val);
float num2 = Mathf.Clamp01(num / 2.5f);
body.position = Vector3.Lerp(body.position, val, num2);
body.linearVelocity = Vector3.Lerp(body.linearVelocity, val2, 0.5f);
}
}
}
[HarmonyPatch]
internal static class PerformanceFixPatches
{
private static Dictionary<int, float> lastTargetSearch;
private static Dictionary<int, Character> cachedTarget;
private static HashSet<int> patchedTumbleweeds;
private static float _lastCleanupTime;
private static FieldInfo tumbleweedPhotonViewField;
private static FieldInfo tumbleweedRigField;
private static FieldInfo tumbleweedRollForceField;
private static MethodInfo tumbleweedGetTargetMethod;
static PerformanceFixPatches()
{
lastTargetSearch = new Dictionary<int, float>();
cachedTarget = new Dictionary<int, Character>();
patchedTumbleweeds = new HashSet<int>();
_lastCleanupTime = 0f;
tumbleweedPhotonViewField = typeof(TumbleWeed).GetField("photonView", BindingFlags.Instance | BindingFlags.NonPublic);
tumbleweedRigField = typeof(TumbleWeed).GetField("rig", BindingFlags.Instance | BindingFlags.NonPublic);
tumbleweedRollForceField = typeof(TumbleWeed).GetField("rollForce", BindingFlags.Instance | BindingFlags.NonPublic);
tumbleweedGetTargetMethod = typeof(TumbleWeed).GetMethod("GetTarget", BindingFlags.Instance | BindingFlags.NonPublic);
}
private static void LogDebug(string message)
{
if (TumbleweedThrottlePlugin.DebugMode)
{
TumbleweedThrottlePlugin.Logger.LogInfo((object)("[DEBUG] " + message));
}
}
private static void CleanupStaleTumbleweeds()
{
TumbleWeed[] array = Object.FindObjectsOfType<TumbleWeed>();
HashSet<int> activeIds = new HashSet<int>();
TumbleWeed[] array2 = array;
foreach (TumbleWeed val in array2)
{
if ((Object)(object)val != (Object)null)
{
activeIds.Add(((Object)val).GetInstanceID());
}
}
int num = patchedTumbleweeds.RemoveWhere((int id) => !activeIds.Contains(id));
if (num > 0)
{
LogDebug($"Cleaned up {num} stale tumbleweed IDs. Active: {patchedTumbleweeds.Count}");
TumbleweedThrottlePlugin.UpdateTumbleweedCount(patchedTumbleweeds.Count);
}
}
[HarmonyPatch(typeof(TumbleWeed), "Start")]
[HarmonyPostfix]
private static void Postfix_TumbleWeed_Start(TumbleWeed __instance)
{
int instanceID = ((Object)__instance).GetInstanceID();
if (!patchedTumbleweeds.Contains(instanceID))
{
patchedTumbleweeds.Add(instanceID);
RemoveAfterSeconds component = ((Component)__instance).GetComponent<RemoveAfterSeconds>();
if ((Object)(object)component == (Object)null)
{
component = ((Component)__instance).gameObject.AddComponent<RemoveAfterSeconds>();
component.Config(true, TumbleweedThrottlePlugin.CurrentTumbleweedLifetime);
component.photonRemove = true;
LogDebug($"Added RemoveAfterSeconds to tumbleweed {instanceID} (lifetime={TumbleweedThrottlePlugin.CurrentTumbleweedLifetime:F1}s)");
}
if ((Object)(object)((Component)__instance).GetComponent<ThrottledTumbleweedSync>() == (Object)null)
{
((Component)__instance).gameObject.AddComponent<ThrottledTumbleweedSync>();
LogDebug($"Added ThrottledTumbleweedSync to tumbleweed {instanceID}");
}
TumbleweedThrottlePlugin.UpdateTumbleweedCount(patchedTumbleweeds.Count);
}
}
[HarmonyPatch(typeof(RemoveAfterSeconds), "Update")]
[HarmonyPostfix]
private static void Postfix_RemoveAfterSeconds_Update(RemoveAfterSeconds __instance)
{
FieldInfo field = typeof(RemoveAfterSeconds).GetField("seconds", BindingFlags.Instance | BindingFlags.NonPublic);
if (!(field != null))
{
return;
}
float num = (float)field.GetValue(__instance);
if (!(num < 0f))
{
return;
}
TumbleWeed component = ((Component)__instance).GetComponent<TumbleWeed>();
if ((Object)(object)component != (Object)null)
{
int instanceID = ((Object)component).GetInstanceID();
if (patchedTumbleweeds.Contains(instanceID))
{
patchedTumbleweeds.Remove(instanceID);
LogDebug($"Tumbleweed {instanceID} destroyed (via RemoveAfterSeconds). Active: {patchedTumbleweeds.Count}");
TumbleweedThrottlePlugin.UpdateTumbleweedCount(patchedTumbleweeds.Count);
}
}
}
[HarmonyPatch(typeof(TumbleWeed), "FixedUpdate")]
[HarmonyPrefix]
private static bool Prefix_TumbleWeed_FixedUpdate(TumbleWeed __instance)
{
//IL_0123: Unknown result type (might be due to invalid IL or missing references)
//IL_0128: Unknown result type (might be due to invalid IL or missing references)
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
//IL_0184: Unknown result type (might be due to invalid IL or missing references)
//IL_0188: Unknown result type (might be due to invalid IL or missing references)
//IL_0162: Unknown result type (might be due to invalid IL or missing references)
//IL_016d: Unknown result type (might be due to invalid IL or missing references)
//IL_0172: Unknown result type (might be due to invalid IL or missing references)
//IL_0177: Unknown result type (might be due to invalid IL or missing references)
//IL_017b: Unknown result type (might be due to invalid IL or missing references)
//IL_0180: Unknown result type (might be due to invalid IL or missing references)
object? obj = tumbleweedPhotonViewField?.GetValue(__instance);
PhotonView val = (PhotonView)((obj is PhotonView) ? obj : null);
if ((Object)(object)val == (Object)null || !val.IsMine)
{
return true;
}
int instanceID = ((Object)__instance).GetInstanceID();
float time = Time.time;
if (time - _lastCleanupTime > 30f)
{
_lastCleanupTime = time;
CleanupStaleTumbleweeds();
}
if (!lastTargetSearch.ContainsKey(instanceID) || time - lastTargetSearch[instanceID] >= 1f)
{
lastTargetSearch[instanceID] = time;
if (tumbleweedGetTargetMethod != null)
{
Dictionary<int, Character> dictionary = cachedTarget;
object? obj2 = tumbleweedGetTargetMethod.Invoke(__instance, null);
dictionary[instanceID] = (Character)((obj2 is Character) ? obj2 : null);
}
}
object? obj3 = tumbleweedRigField?.GetValue(__instance);
Rigidbody val2 = (Rigidbody)((obj3 is Rigidbody) ? obj3 : null);
float num = ((tumbleweedRollForceField != null) ? ((float)tumbleweedRollForceField.GetValue(__instance)) : 10f);
if ((Object)(object)val2 != (Object)null)
{
Vector3 val3 = -Vector3.right;
if (cachedTarget.ContainsKey(instanceID) && (Object)(object)cachedTarget[instanceID] != (Object)null)
{
Vector3 val4 = cachedTarget[instanceID].Center - ((Component)__instance).transform.position;
val3 = ((Vector3)(ref val4)).normalized;
}
val2.AddForce(val3 * num, (ForceMode)5);
}
return false;
}
}
}