Decompiled source of CartDump v0.1.7

RepoCartDump.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using REPOLib.Modules;
using RepoCartDump.Patches;
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("RepoCartDump")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RepoCartDump")]
[assembly: AssemblyTitle("RepoCartDump")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
}
namespace RepoCartDump
{
	public class CartQuickDepositController : MonoBehaviour
	{
		private PhysGrabCart cart;

		private PhotonView photonView;

		private Rigidbody rb;

		private bool isParked;

		private TextMesh promptTextMesh;

		private bool wasEligibleLastFrame;

		private float promptHideAtTime;

		private State lastLoggedState;

		private bool hasLoggedInitialState;

		private static readonly FieldRef<PhysGrabCart, State> CurrentStateRef = AccessTools.FieldRefAccess<PhysGrabCart, State>("currentState");

		private void Awake()
		{
			cart = ((Component)this).GetComponent<PhysGrabCart>();
			photonView = ((Component)this).GetComponent<PhotonView>();
			rb = ((Component)this).GetComponent<Rigidbody>();
			CreatePrompt();
		}

		private void CreatePrompt()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("CartQuickDepositPrompt");
			val.transform.SetParent(((Component)this).transform, false);
			val.transform.localPosition = new Vector3(0f, 1.6f, 0f);
			promptTextMesh = val.AddComponent<TextMesh>();
			promptTextMesh.text = $"[{ConfigManager.InteractKey.Value}] Quick Deposit";
			promptTextMesh.characterSize = 0.1f;
			promptTextMesh.fontSize = 48;
			promptTextMesh.anchor = (TextAnchor)4;
			promptTextMesh.alignment = (TextAlignment)1;
			promptTextMesh.color = Color.white;
			val.SetActive(false);
		}

		private void Update()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			State val = CurrentStateRef.Invoke(cart);
			LogStateChangeIfNeeded(val);
			if (isParked)
			{
				SetPromptVisible(visible: false);
				return;
			}
			bool flag = (int)CurrentStateRef.Invoke(cart) == 2;
			bool flag2 = IsLocalPlayerInRange();
			bool flag3 = flag && flag2;
			if (flag3 && !wasEligibleLastFrame)
			{
				promptHideAtTime = Time.time + 3f;
				RepoCartDump.DebugLog($"[{((Object)((Component)this).gameObject).name}] Prompt eingeblendet (State={val}, Distanz ok). " + $"Blendet in {3f}s automatisch wieder aus.");
			}
			wasEligibleLastFrame = flag3;
			bool promptVisible = flag3 && Time.time < promptHideAtTime;
			SetPromptVisible(promptVisible);
			if (flag && flag2 && LocalPlayerPressedInteract())
			{
				RepoCartDump.DebugLog($"[{((Object)((Component)this).gameObject).name}] Interact-Taste gedrückt, State={val}, InRange=true -> RequestQuickDeposit()");
				RequestQuickDeposit();
			}
			if ((Object)(object)promptTextMesh != (Object)null && ((Component)promptTextMesh).gameObject.activeSelf)
			{
				PlayerAvatar instance = PlayerAvatar.instance;
				if ((Object)(object)instance != (Object)null && (Object)(object)instance.localCamera != (Object)null)
				{
					((Component)promptTextMesh).transform.LookAt(((Component)instance.localCamera).transform);
					((Component)promptTextMesh).transform.Rotate(0f, 180f, 0f);
				}
			}
		}

		private void LogStateChangeIfNeeded(State currentState)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			if (!hasLoggedInitialState || currentState != lastLoggedState)
			{
				RepoCartDump.DebugLog($"[{((Object)((Component)this).gameObject).name}] Cart State: {currentState}");
				lastLoggedState = currentState;
				hasLoggedInitialState = true;
			}
		}

		private void SetPromptVisible(bool visible)
		{
			if ((Object)(object)promptTextMesh != (Object)null && ((Component)promptTextMesh).gameObject.activeSelf != visible)
			{
				((Component)promptTextMesh).gameObject.SetActive(visible);
			}
		}

		private bool IsLocalPlayerInRange()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			PlayerAvatar instance = PlayerAvatar.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return false;
			}
			Vector3 val = ((Component)this).transform.position - ((Component)instance).transform.position;
			float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
			return sqrMagnitude <= 6.25f;
		}

		private bool LocalPlayerPressedInteract()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			return Input.GetKeyDown(ConfigManager.InteractKey.Value);
		}

		public void RequestQuickDeposit()
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			if (!isParked)
			{
				int actorNumber = PhotonNetwork.LocalPlayer.ActorNumber;
				object[] array = new object[2] { photonView.ViewID, actorNumber };
				RepoCartDump.DebugLog("[" + ((Object)((Component)this).gameObject).name + "] RaiseEvent 'CartQuickDeposit.Trigger' " + $"(cartViewID={photonView.ViewID}, actorNumber={actorNumber})");
				RepoCartDump.QuickDepositEvent.RaiseEvent((object)array, NetworkingEvents.RaiseAll, SendOptions.SendReliable);
			}
		}

		public void ExecuteQuickDeposit(int triggeringActorNumber)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (!isParked)
			{
				isParked = true;
				RepoCartDump.DebugLog($"[{((Object)((Component)this).gameObject).name}] ExecuteQuickDeposit gestartet (triggeringActorNumber={triggeringActorNumber}). " + "Cart wird geparkt, Items sollten jetzt fallen.");
				SetCartVisualsEnabled(enabled: false);
				if ((Object)(object)rb != (Object)null)
				{
					rb.isKinematic = true;
					rb.velocity = Vector3.zero;
					rb.angularVelocity = Vector3.zero;
				}
				((MonoBehaviour)this).StartCoroutine(RespawnAfterDelay(triggeringActorNumber));
			}
		}

		private void SetCartVisualsEnabled(bool enabled)
		{
			Collider[] componentsInChildren = ((Component)this).GetComponentsInChildren<Collider>(true);
			foreach (Collider val in componentsInChildren)
			{
				val.enabled = enabled;
			}
			Renderer[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val2 in componentsInChildren2)
			{
				GameObject gameObject = ((Component)val2).gameObject;
				TextMesh obj = promptTextMesh;
				if (!((Object)(object)gameObject == (Object)(object)((obj != null) ? ((Component)obj).gameObject : null)))
				{
					val2.enabled = enabled;
				}
			}
		}

		private IEnumerator RespawnAfterDelay(int triggeringActorNumber)
		{
			yield return (object)new WaitForSeconds(ConfigManager.ParkedDurationSeconds.Value);
			Vector3 spawnPos = ResolveRespawnPosition(triggeringActorNumber);
			((Component)this).transform.position = spawnPos;
			((Component)this).transform.rotation = Quaternion.identity;
			if ((Object)(object)rb != (Object)null)
			{
				rb.isKinematic = false;
			}
			SetCartVisualsEnabled(enabled: true);
			isParked = false;
		}

		private Vector3 ResolveRespawnPosition(int actorNumber)
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			PlayerAvatar[] array = Object.FindObjectsOfType<PlayerAvatar>();
			PlayerAvatar[] array2 = array;
			foreach (PlayerAvatar val in array2)
			{
				PhotonView component = ((Component)val).GetComponent<PhotonView>();
				if ((Object)(object)component != (Object)null && component.Owner != null && component.Owner.ActorNumber == actorNumber)
				{
					return ComputeSpawnPointAbovePlayer(val);
				}
			}
			RepoCartDump.DebugLog($"[{((Object)((Component)this).gameObject).name}] Spieler mit ActorNumber {actorNumber} nicht gefunden - " + "Fallback auf letzte Cart-Position.");
			return ((Component)this).transform.position;
		}

		private Vector3 ComputeSpawnPointAbovePlayer(PlayerAvatar player)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			return ((Component)player).transform.position + Vector3.up * 2f;
		}

		private Vector3 ComputeSpawnPointInFrontOf(PlayerAvatar player)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			Transform val = (((Object)(object)player.localCamera != (Object)null) ? ((Component)player.localCamera).transform : ((Component)player).transform);
			Vector3 forward = val.forward;
			forward.y = 0f;
			if (((Vector3)(ref forward)).sqrMagnitude < 0.01f)
			{
				forward = ((Component)player).transform.forward;
				forward.y = 0f;
			}
			if (((Vector3)(ref forward)).sqrMagnitude < 0.01f)
			{
				forward = Vector3.forward;
			}
			((Vector3)(ref forward)).Normalize();
			Vector3 val2 = ((Component)player).transform.position + forward * 2f;
			RaycastHit val3 = default(RaycastHit);
			if (Physics.Raycast(val2 + Vector3.up * 3f, Vector3.down, ref val3, 10f))
			{
				val2.y = ((RaycastHit)(ref val3)).point.y;
			}
			else
			{
				val2.y = ((Component)player).transform.position.y;
			}
			return val2;
		}
	}
	[BepInPlugin("RepoCartDump", "Repo Cart Dump", "0.1.5")]
	public class RepoCartDump : BaseUnityPlugin
	{
		private const string modGUID = "RepoCartDump";

		private const string modName = "Repo Cart Dump";

		private const string modVersion = "0.1.5";

		private readonly Harmony harmony = new Harmony("RepoCartDump");

		private static RepoCartDump instance;

		public static ManualLogSource mls;

		public const float RespawnOffsetFromPlayer = 2f;

		public const float InteractRange = 2.5f;

		public const float PromptVisibleDurationSeconds = 3f;

		public static NetworkedEvent QuickDepositEvent;

		internal static ConfigEntry<bool> DebugLogsEnabled;

		internal static void DebugLog(string message)
		{
			if (DebugLogsEnabled != null && DebugLogsEnabled.Value)
			{
				mls.LogInfo((object)("[Debug] " + message));
			}
		}

		private void Awake()
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			mls = Logger.CreateLogSource("RepoCartDump");
			ConfigManager.Initialize(((BaseUnityPlugin)this).Config);
			DebugLogsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableDebugLogs", true, "Ausführliches Debug-Logging für Cart Quick Deposit (State-Wechsel, Prompt, Events).");
			QuickDepositEvent = new NetworkedEvent("CartQuickDeposit.Trigger", (Action<EventData>)HandleQuickDepositEvent);
			harmony.PatchAll();
			mls.LogInfo((object)"RepoCartDump is now awake!");
		}

		private static void HandleQuickDepositEvent(EventData eventData)
		{
			object[] array = (object[])eventData.CustomData;
			int num = (int)array[0];
			int num2 = (int)array[1];
			DebugLog($"NetworkedEvent empfangen: cartViewID={num}, triggeringActorNumber={num2}");
			PhotonView val = PhotonView.Find(num);
			if ((Object)(object)val == (Object)null)
			{
				mls.LogWarning((object)$"CartQuickDeposit: PhotonView {num} nicht gefunden.");
				return;
			}
			CartQuickDepositController component = ((Component)val).GetComponent<CartQuickDepositController>();
			if ((Object)(object)component == (Object)null)
			{
				mls.LogWarning((object)"CartQuickDeposit: Kein Controller auf dem Cart gefunden.");
			}
			else
			{
				component.ExecuteQuickDeposit(num2);
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "RepoCartDump";

		public const string PLUGIN_NAME = "RepoCartDump";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace RepoCartDump.Patches
{
	[HarmonyPatch(typeof(ChatManager))]
	internal class ChatManagerPatch
	{
		public static ChatState chatState;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePatch(ref ChatState ___chatState)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			chatState = ___chatState;
		}
	}
	internal class ConfigManager
	{
		public static ConfigEntry<KeyCode> rotateKey;

		public static ConfigEntry<KeyCode> InteractKey;

		public static ConfigEntry<float> ParkedDurationSeconds;

		public static void Initialize(ConfigFile cfg)
		{
			rotateKey = cfg.Bind<KeyCode>("Controls", "DebugKey", (KeyCode)120, "The key used to debug.");
			InteractKey = cfg.Bind<KeyCode>("Controls", "InteractKey", (KeyCode)102, "Key to activate Quick Deposit");
			ParkedDurationSeconds = cfg.Bind<float>("General", "ParkedDurationSeconds", 1f, "Time in Seconds to respawn cart");
		}
	}
	[HarmonyPatch(typeof(EnemyParent))]
	internal class EnemyParentPatch
	{
		private static readonly Dictionary<string, string> enemyNames = new Dictionary<string, string>
		{
			{ "Gnome", "Lego Stei" },
			{ "Hidden", "Flavio" },
			{ "Loom", "Noah" },
			{ "Mentalist", "Megamind" },
			{ "Apex Predator", "Halt die Schnauze" }
		};

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void SetEnemyNames(EnemyParent __instance)
		{
			if (enemyNames.TryGetValue(__instance.enemyName, out string value))
			{
				__instance.enemyName = value;
			}
		}
	}
	[HarmonyPatch(typeof(PhysGrabCart))]
	internal class PhysGrabCartPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPatch(PhysGrabCart __instance)
		{
			if (!__instance.isSmallCart && (Object)(object)((Component)__instance).GetComponent<CartQuickDepositController>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<CartQuickDepositController>();
			}
		}
	}
}