Decompiled source of gigaTransporter v0.0.5

GigaTransporter.dll

Decompiled 4 hours ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Mirror;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("GigaTransporter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GigaTransporter")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f5bbf51a-671e-47f2-8c61-d0df1f8d89c5")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.buttergeland.supermarkettogether.gigatransporter", "Buttergeland_gigatransporter", "0.0.5")]
public class Main : BaseUnityPlugin
{
	[HarmonyPatch(typeof(MiniTransportBehaviour), "UserCode_RpcRequestOwnership__GameObject")]
	public static class Patch_UserCode_RpcRequestOwnership__GameObject
	{
		[HarmonyPostfix]
		public static void PostfixUnlockCamera(MiniTransportBehaviour __instance)
		{
			if ((Object)(object)Camera.main == (Object)null)
			{
				Debug.LogWarning((object)"[GigaTransporter] Main camera not found.");
				return;
			}
			CustomCameraController component = ((Component)Camera.main).GetComponent<CustomCameraController>();
			if ((Object)(object)component != (Object)null)
			{
				component.inVehicle = false;
				component.vehicleOBJ = null;
				Camera val = default(Camera);
				if (((Component)component).TryGetComponent<Camera>(ref val))
				{
					((Behaviour)val).enabled = false;
					((Behaviour)val).enabled = true;
				}
				Debug.Log((object)"[GigaTransporter] Camera successfully unlocked after vehicle ownership request.");
			}
			else
			{
				Debug.LogWarning((object)"[GigaTransporter] CustomCameraController not found on the main camera.");
			}
		}
	}

	public TMP_FontAsset customFont;

	private readonly string[] VehicleNames = new string[1] { "24_MiniTransportVehicle(Clone)" };

	private ConfigEntry<bool> DebugLogging;

	private HashSet<int> processedVehicles = new HashSet<int>();

	private void Awake()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Expected O, but got Unknown
		DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugLogging", false, "Enable debug logging for this mod.");
		Harmony val = new Harmony("com.buttergeland.supermarkettogether.gigatransporter");
		val.PatchAll();
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Mod loaded and harmony patched.");
		}
	}

	private void Start()
	{
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Starting vehicle update coroutine.");
		}
		((MonoBehaviour)this).StartCoroutine(CheckAndUpdateVehicles());
	}

	private IEnumerator CheckAndUpdateVehicles()
	{
		while (true)
		{
			if (DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Checking for vehicles to update...");
			}
			yield return (object)new WaitForSeconds(10f);
			MiniTransportBehaviour[] allVehicles = Object.FindObjectsByType<MiniTransportBehaviour>((FindObjectsSortMode)0);
			int updatedCount = 0;
			MiniTransportBehaviour[] array = allVehicles;
			foreach (MiniTransportBehaviour vehicleBehaviour in array)
			{
				if (VehicleNames.Contains(((Object)((Component)vehicleBehaviour).gameObject).name))
				{
					if (DebugLogging.Value)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Found vehicle: " + ((Object)((Component)vehicleBehaviour).gameObject).name + ". Updating..."));
					}
					UpdateVehicle(vehicleBehaviour);
					updatedCount++;
				}
			}
			if (updatedCount > 0 && DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Updated {updatedCount} MiniTransportVehicle(s).");
			}
		}
	}

	private void UpdateVehicle(MiniTransportBehaviour vehicleBehaviour)
	{
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Updating vehicle with ID {((Object)vehicleBehaviour).GetInstanceID()}...");
		}
		GameObject gameObject = ((Component)vehicleBehaviour).gameObject;
		BoxCollider component = gameObject.GetComponent<BoxCollider>();
		if ((Object)(object)component != (Object)null)
		{
			Vector3 size = component.size;
			size.x = 0.5f;
			size.z = 2f;
			component.size = size;
			if (DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Updated BoxCollider size.");
			}
		}
		else if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"No BoxCollider found on MiniTransportVehicle object.");
		}
		vehicleBehaviour.brakeTorque = 3000f;
		vehicleBehaviour.motorTorque = 2500f;
		vehicleBehaviour.maxSpeed = 30f;
		vehicleBehaviour.steeringRange = 70f;
		vehicleBehaviour.steeringRangeAtMaxSpeed = 30f;
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Vehicle behavior values updated.");
		}
		int instanceID = ((Object)vehicleBehaviour).GetInstanceID();
		if (processedVehicles.Contains(instanceID))
		{
			return;
		}
		processedVehicles.Add(instanceID);
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Processing new vehicle with ID {instanceID}...");
		}
		Transform val = gameObject.transform.Find("BoxContainer");
		Transform val2 = gameObject.transform.Find("Highlights");
		if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
		{
			if (DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"BoxContainer or Highlights not found. Cannot add new slots.");
			}
			return;
		}
		List<Collider> list = DuplicateBoxSubcontainers(val, -1.3f);
		DuplicateHighlights(val2, -1.3f);
		int count = list.Count;
		if (count > 0 && ((NetworkBehaviour)vehicleBehaviour).isServer)
		{
			AddNewSlotsToProductInfoArray(vehicleBehaviour, count);
			int num = vehicleBehaviour.productInfoArray.Length / 2 - count;
			for (int i = 0; i < count; i++)
			{
				int slotIndex = num + i;
				UpdateSlotViaReflection(vehicleBehaviour, slotIndex, -1, -1);
			}
			if (DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Added {count} new slots and highlights to the vehicle.");
			}
		}
		else if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"No new slots found or not server. Cannot update productInfoArray.");
		}
	}

	private List<Collider> DuplicateBoxSubcontainers(Transform parent, float zOffset)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Duplicating BoxSubcontainers...");
		}
		List<Transform> list = new List<Transform>();
		foreach (Transform item in parent)
		{
			Transform val = item;
			if (((Object)val).name.Contains("BoxSubcontainer"))
			{
				list.Add(val);
			}
		}
		List<Collider> list2 = new List<Collider>();
		foreach (Transform item2 in list)
		{
			GameObject val2 = Object.Instantiate<GameObject>(((Component)item2).gameObject, parent);
			Vector3 localPosition = item2.localPosition;
			localPosition.z += zOffset;
			val2.transform.localPosition = localPosition;
			Collider[] componentsInChildren = val2.GetComponentsInChildren<Collider>();
			if (componentsInChildren != null && componentsInChildren.Length != 0)
			{
				list2.AddRange(componentsInChildren);
			}
		}
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Duplicated {list2.Count} new BoxSubcontainers.");
		}
		return list2;
	}

	private void DuplicateHighlights(Transform highlightsParent, float zOffset)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: 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)
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Duplicating Highlights...");
		}
		List<Transform> list = new List<Transform>();
		foreach (Transform item2 in highlightsParent)
		{
			Transform item = item2;
			list.Add(item);
		}
		int count = list.Count;
		foreach (Transform item3 in list)
		{
			GameObject val = Object.Instantiate<GameObject>(((Component)item3).gameObject, highlightsParent);
			Vector3 localPosition = item3.localPosition;
			localPosition.z += zOffset;
			val.transform.localPosition = localPosition;
			((Object)val).name = count++.ToString();
		}
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Highlights duplicated successfully.");
		}
	}

	private void AddNewSlotsToProductInfoArray(MiniTransportBehaviour vehicleBehaviour, int newSlotCount)
	{
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Adding {newSlotCount} new slots to productInfoArray...");
		}
		int num = vehicleBehaviour.productInfoArray.Length;
		int num2 = num + newSlotCount * 2;
		int[] array = new int[num2];
		for (int i = 0; i < num; i++)
		{
			array[i] = vehicleBehaviour.productInfoArray[i];
		}
		for (int j = num; j < num2; j++)
		{
			array[j] = -1;
		}
		vehicleBehaviour.productInfoArray = array;
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"New slots added to productInfoArray.");
		}
	}

	private void UpdateSlotViaReflection(MiniTransportBehaviour vehicleBehaviour, int slotIndex, int PID, int PNUMBER)
	{
		if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Updating slot {slotIndex} with PID {PID} and PNUMBER {PNUMBER} via reflection...");
		}
		int[] productInfoArray = vehicleBehaviour.productInfoArray;
		productInfoArray[slotIndex * 2] = PID;
		productInfoArray[slotIndex * 2 + 1] = PNUMBER;
		vehicleBehaviour.productInfoArray = productInfoArray;
		MethodInfo method = typeof(MiniTransportBehaviour).GetMethod("UserCode_RpcUpdateArrayValuesStorage__Int32__Int32__Int32", BindingFlags.Instance | BindingFlags.NonPublic);
		if (method != null)
		{
			method.Invoke(vehicleBehaviour, new object[3]
			{
				slotIndex * 2,
				PID,
				PNUMBER
			});
			if (DebugLogging.Value)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Slot updated via RPC successfully.");
			}
		}
		else if (DebugLogging.Value)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to find RpcUpdateArrayValuesStorage via reflection.");
		}
	}
}