Decompiled source of ScrapCounter v1.0.0

plugins/ScrapCounter.dll

Decompiled 2 months 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ScrapCounter.Patches;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[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("ScrapCounter")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Displays scrap count at the start of each day")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ScrapCounter")]
[assembly: AssemblyTitle("ScrapCounter")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ScrapCounter
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.psyko.scrapcounter";

		public const string PLUGIN_NAME = "ScrapCounter";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	[BepInPlugin("com.psyko.scrapcounter", "ScrapCounter", "1.0.0")]
	public class ScrapCounterPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		private readonly Harmony harmony = new Harmony("com.psyko.scrapcounter");

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			harmony.PatchAll(typeof(ScrapCounterPlugin));
			harmony.PatchAll(typeof(ScrapCounterPatches));
			Logger.LogInfo((object)"Plugin com.psyko.scrapcounter is loaded!");
		}
	}
}
namespace ScrapCounter.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class ScrapCounterPatches
	{
		private static GameObject canvasObj;

		private static GameObject counterUI;

		private static TextMeshProUGUI counterText;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void InitializeUI()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0063: 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_0088: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: 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_0128: Expected O, but got Unknown
			//IL_014b: 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_0165: 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_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Expected O, but got Unknown
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0268: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)counterUI != (Object)null)
			{
				return;
			}
			try
			{
				canvasObj = new GameObject("ScrapCounterCanvas");
				Canvas val = canvasObj.AddComponent<Canvas>();
				val.renderMode = (RenderMode)0;
				val.sortingOrder = 1000;
				CanvasScaler val2 = canvasObj.AddComponent<CanvasScaler>();
				val2.uiScaleMode = (ScaleMode)1;
				val2.referenceResolution = new Vector2(1920f, 1080f);
				Object.DontDestroyOnLoad((Object)(object)canvasObj);
				counterUI = new GameObject("ScrapCounterUI");
				counterUI.transform.SetParent(canvasObj.transform, false);
				RectTransform val3 = counterUI.AddComponent<RectTransform>();
				val3.anchorMin = new Vector2(0f, 1f);
				val3.anchorMax = new Vector2(0f, 1f);
				val3.pivot = new Vector2(0f, 1f);
				val3.anchoredPosition = new Vector2(0f, 0f);
				val3.sizeDelta = new Vector2(80f, 40f);
				GameObject val4 = new GameObject("Background");
				val4.transform.SetParent(counterUI.transform, false);
				RectTransform val5 = val4.AddComponent<RectTransform>();
				val5.anchorMin = Vector2.zero;
				val5.anchorMax = Vector2.one;
				val5.sizeDelta = Vector2.zero;
				Image val6 = val4.AddComponent<Image>();
				((Graphic)val6).color = Color.black;
				GameObject val7 = new GameObject("Text");
				val7.transform.SetParent(counterUI.transform, false);
				RectTransform val8 = val7.AddComponent<RectTransform>();
				val8.anchorMin = Vector2.zero;
				val8.anchorMax = Vector2.one;
				val8.sizeDelta = Vector2.zero;
				counterText = val7.AddComponent<TextMeshProUGUI>();
				((TMP_Text)counterText).font = ((TMP_Text)HUDManager.Instance.controlTipLines[0]).font;
				((TMP_Text)counterText).fontSize = 24f;
				((TMP_Text)counterText).fontStyle = (FontStyles)1;
				((Graphic)counterText).color = Color.white;
				((TMP_Text)counterText).alignment = (TextAlignmentOptions)514;
				((TMP_Text)counterText).text = "";
				((TMP_Text)counterText).outlineWidth = 0.2f;
				((TMP_Text)counterText).outlineColor = Color32.op_Implicit(Color.black);
				counterUI.SetActive(false);
				SceneManager.sceneLoaded += OnSceneLoaded;
				ScrapCounterPlugin.Logger.LogInfo((object)"Scrap counter UI initialized!");
			}
			catch (Exception arg)
			{
				ScrapCounterPlugin.Logger.LogError((object)$"Error initializing UI: {arg}");
			}
		}

		private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if (((Scene)(ref scene)).name == "MainMenu" && (Object)(object)counterUI != (Object)null)
			{
				Object.Destroy((Object)(object)counterUI);
				counterUI = null;
				ScrapCounterPlugin.Logger.LogInfo((object)"Main menu loaded - hiding scrap counter");
			}
		}

		[HarmonyPatch("ShipHasLeft")]
		[HarmonyPostfix]
		private static void OnShipLeave()
		{
			if ((Object)(object)counterUI != (Object)null)
			{
				counterUI.SetActive(false);
				ScrapCounterPlugin.Logger.LogInfo((object)"Ship left - hiding counter");
			}
		}

		[HarmonyPatch("openingDoorsSequence")]
		[HarmonyPostfix]
		private static void OnShipLand()
		{
			if (!((Object)(object)counterUI == (Object)null))
			{
				if (((Object)StartOfRound.Instance.currentLevel).name == "CompanyBuildingLevel")
				{
					counterUI.SetActive(false);
					ScrapCounterPlugin.Logger.LogInfo((object)"At company building - not showing counter");
				}
				else
				{
					((MonoBehaviour)HUDManager.Instance).StartCoroutine(DelayedScrapCount());
				}
			}
		}

		private static IEnumerator DelayedScrapCount()
		{
			yield return (object)new WaitForSeconds(2f);
			CountAndDisplayScrap();
		}

		private static void CountAndDisplayScrap()
		{
			try
			{
				VehicleController val = Object.FindObjectOfType<VehicleController>();
				Transform val2 = (((Object)(object)val != (Object)null) ? ((Component)val).transform : null);
				HashSet<Transform> hashSet = new HashSet<Transform>();
				if ((Object)(object)val2 != (Object)null)
				{
					BuildTransformHierarchy(val2, hashSet);
				}
				GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
				int num = 0;
				int num2 = 0;
				GrabbableObject[] array2 = array;
				foreach (GrabbableObject val3 in array2)
				{
					if (val3.itemProperties.isScrap && !val3.isInShipRoom && !val3.isHeld && !val3.isPocketed && !val3.isInElevator && (hashSet.Count <= 0 || !IsInVehicle(((Component)val3).transform, hashSet)))
					{
						if (!val3.isInFactory || ((Object)((Component)val3).gameObject).name.Contains("Hive") || ((Object)((Component)val3).gameObject).name.Contains("Locust"))
						{
							num2++;
						}
						else
						{
							num++;
						}
					}
				}
				((TMP_Text)counterText).text = $"{num} {num2}";
				counterUI.SetActive(true);
				ScrapCounterPlugin.Logger.LogInfo((object)$"Scrap count - Inside: {num}, Outside: {num2}");
			}
			catch (Exception arg)
			{
				ScrapCounterPlugin.Logger.LogError((object)$"Error counting scrap: {arg}");
			}
		}

		private static void BuildTransformHierarchy(Transform root, HashSet<Transform> hierarchy)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			hierarchy.Add(root);
			foreach (Transform item in root)
			{
				Transform root2 = item;
				BuildTransformHierarchy(root2, hierarchy);
			}
		}

		private static bool IsInVehicle(Transform itemTransform, HashSet<Transform> vehicleHierarchy)
		{
			Transform val = itemTransform;
			while ((Object)(object)val != (Object)null)
			{
				if (vehicleHierarchy.Contains(val))
				{
					return true;
				}
				val = val.parent;
			}
			return false;
		}
	}
}