Decompiled source of TumbleweedThrottle v1.0.0

TumbleweedThrottle.dll

Decompiled a day ago
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.0.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.0.0";

		public static ManualLogSource Logger;

		public static bool DebugMode = false;

		public const float BASE_SYNC_INTERVAL = 1f;

		public const float MIN_SYNC_INTERVAL = 0.22f;

		public const float MAX_SYNC_INTERVAL = 0.9f;

		public const float TUMBLEWEED_LIFETIME_SECONDS = 6f;

		public const float TUMBLEWEED_TARGET_SEARCH_INTERVAL = 8f;

		public const float POSITION_TOLERANCE = 1.5f;

		public const int LOW_CLIENT_THRESHOLD = 2;

		public const int HIGH_CLIENT_THRESHOLD = 12;

		public static float CurrentSyncInterval = 1f;

		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)");
			UpdateSyncInterval();
			Harmony.CreateAndPatchAll(typeof(TumbleweedThrottlePlugin).Assembly, "jill920.tumbleweedthrottle");
			Logger.LogInfo((object)"[Tumbleweed Throttle 1.0.0] Loaded");
			Logger.LogInfo((object)$"  Adaptive sync: {CurrentSyncInterval:F2}s (adjusts with player count)");
			Logger.LogInfo((object)$"  Tumbleweed lifetime: {6f}s");
		}

		private void Update()
		{
			Room currentRoom = PhotonNetwork.CurrentRoom;
			int num = ((currentRoom == null) ? 1 : currentRoom.PlayerCount);
			float num2 = CalculateSyncInterval(num);
			if (Mathf.Abs(num2 - CurrentSyncInterval) > 0.01f)
			{
				CurrentSyncInterval = num2;
				if (DebugMode)
				{
					Logger.LogInfo((object)$"Player count: {num}, Sync interval adjusted to {CurrentSyncInterval:F2}s");
				}
			}
		}

		private static float CalculateSyncInterval(int playerCount)
		{
			if (playerCount <= 2)
			{
				return 0.22f;
			}
			if (playerCount >= 12)
			{
				return 0.9f;
			}
			float num = (float)(playerCount - 2) / 10f;
			return Mathf.Lerp(0.22f, 0.9f, num);
		}

		public static void UpdateSyncInterval()
		{
			Room currentRoom = PhotonNetwork.CurrentRoom;
			int playerCount = ((currentRoom == null) ? 1 : currentRoom.PlayerCount);
			CurrentSyncInterval = CalculateSyncInterval(playerCount);
		}
	}
	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);
			}
			if (TumbleweedThrottlePlugin.DebugMode)
			{
				TumbleweedThrottlePlugin.Logger.LogInfo((object)$"Throttled sync added to tumbleweed {((Object)this).GetInstanceID()}");
			}
		}

		void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
		{
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: 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_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 currentSyncInterval = TumbleweedThrottlePlugin.CurrentSyncInterval;
			if (stream.IsWriting)
			{
				if (!(time - lastSyncTime < currentSyncInterval))
				{
					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 linearVelocity = (Vector3)stream.ReceiveNext();
				float num = Mathf.Clamp01(Time.deltaTime * (1f / currentSyncInterval));
				body.position = Vector3.Lerp(body.position, val, num);
				body.linearVelocity = linearVelocity;
			}
		}
	}
	[HarmonyPatch]
	internal static class PerformanceFixPatches
	{
		private static Dictionary<int, float> lastMobTargeting;

		private static Dictionary<int, float> lastMobRaycast;

		private static Dictionary<int, float> lastTargetSearch;

		private static Dictionary<int, Character> cachedTarget;

		private static HashSet<int> patchedTumbleweeds;

		private static FieldInfo tumbleweedPhotonViewField;

		private static FieldInfo tumbleweedRigField;

		private static FieldInfo tumbleweedRollForceField;

		private static MethodInfo tumbleweedGetTargetMethod;

		static PerformanceFixPatches()
		{
			lastMobTargeting = new Dictionary<int, float>();
			lastMobRaycast = new Dictionary<int, float>();
			lastTargetSearch = new Dictionary<int, float>();
			cachedTarget = new Dictionary<int, Character>();
			patchedTumbleweeds = new HashSet<int>();
			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));
			}
		}

		[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, 6f);
					component.photonRemove = true;
					LogDebug($"Added RemoveAfterSeconds to tumbleweed {instanceID}");
				}
				if ((Object)(object)((Component)__instance).GetComponent<ThrottledTumbleweedSync>() == (Object)null)
				{
					((Component)__instance).gameObject.AddComponent<ThrottledTumbleweedSync>();
					LogDebug($"Added ThrottledTumbleweedSync to tumbleweed {instanceID}");
				}
			}
		}

		[HarmonyPatch(typeof(TumbleWeed), "FixedUpdate")]
		[HarmonyPrefix]
		private static bool Prefix_TumbleWeed_FixedUpdate(TumbleWeed __instance)
		{
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: 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_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: 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 (!lastTargetSearch.ContainsKey(instanceID) || time - lastTargetSearch[instanceID] >= 8f)
			{
				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;
		}
	}
}