Decompiled source of HaulTracker v0.1.0

HaulTracker.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("HaulTracker")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HaulTracker")]
[assembly: AssemblyTitle("HaulTracker")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 HaulTracker
{
	[BepInPlugin("lch.haultracker", "HaulTracker", "0.2.0")]
	public class HaulTrackerPlugin : BaseUnityPlugin
	{
		private class P
		{
			public string name = "?";

			public long haul;
		}

		[HarmonyPatch(typeof(ValuableObject), "AddToDollarHaulListRPC")]
		private static class AddRpcPatch
		{
			public static void Postfix(ValuableObject __instance, PhotonMessageInfo __info)
			{
				//IL_0028: Unknown result type (might be due to invalid IL or missing references)
				//IL_003c: Unknown result type (might be due to invalid IL or missing references)
				if (_fCurValue != null && _fCurValue.GetValue(__instance) is float num)
				{
					Get((__info.Sender != null) ? __info.Sender.ActorNumber : PhotonNetwork.LocalPlayer.ActorNumber).haul += (long)num;
				}
			}
		}

		[HarmonyPatch(typeof(ValuableObject), "RemoveFromDollarHaulListRPC")]
		private static class RemoveRpcPatch
		{
			public static void Postfix(ValuableObject __instance, PhotonMessageInfo __info)
			{
				//IL_0028: Unknown result type (might be due to invalid IL or missing references)
				//IL_003c: Unknown result type (might be due to invalid IL or missing references)
				if (_fCurValue != null && _fCurValue.GetValue(__instance) is float num)
				{
					Get((__info.Sender != null) ? __info.Sender.ActorNumber : PhotonNetwork.LocalPlayer.ActorNumber).haul -= (long)num;
				}
			}
		}

		[HarmonyPatch(typeof(ValuableObject), "AddToDollarHaulList")]
		private static class AddLocalPatch
		{
			public static void Postfix(ValuableObject __instance)
			{
				if (_fCurValue != null && _fCurValue.GetValue(__instance) is float num)
				{
					Get(PhotonNetwork.LocalPlayer.ActorNumber).haul += (long)num;
				}
			}
		}

		[HarmonyPatch(typeof(ValuableObject), "RemoveFromDollarHaulList")]
		private static class RemoveLocalPatch
		{
			public static void Postfix(ValuableObject __instance)
			{
				if (_fCurValue != null && _fCurValue.GetValue(__instance) is float num)
				{
					Get(PhotonNetwork.LocalPlayer.ActorNumber).haul -= (long)num;
				}
			}
		}

		public const string GUID = "lch.haultracker";

		public const string MODNAME = "HaulTracker";

		public const string VERSION = "0.2.0";

		private static readonly Dictionary<int, P> _players = new Dictionary<int, P>();

		private static readonly Dictionary<int, float> _lastValue = new Dictionary<int, float>();

		private static GUIStyle _style;

		private float _timer;

		private long _mapRemaining;

		private long _valueLost;

		private long _totalHaul;

		private long _lastSubmitted;

		private bool _captured;

		private int _submitted;

		private int _goal;

		private static readonly FieldInfo _fCurValue = typeof(ValuableObject).GetField("dollarValueCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static readonly FieldInfo _fHaulCurrent = typeof(ExtractionPoint).GetField("haulCurrent", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo _fHaulUIValue = typeof(HaulUI).GetField("currentHaulValue", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo _fPlayerName = typeof(PlayerAvatar).GetField("playerName", BindingFlags.Instance | BindingFlags.NonPublic);

		public static ConfigEntry<string> Language { get; private set; }

		private bool IsEnglish
		{
			get
			{
				if (Language != null)
				{
					return Language.Value == "English";
				}
				return false;
			}
		}

		private static P Get(int actor)
		{
			if (!_players.TryGetValue(actor, out var value))
			{
				value = new P
				{
					name = "P" + actor
				};
				_players[actor] = value;
			}
			return value;
		}

		private void Awake()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			Language = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Language", "Chinese", "Language: Chinese or English");
			try
			{
				new Harmony("lch.haultracker").PatchAll();
			}
			catch
			{
			}
		}

		private void Update()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected I4, but got Unknown
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			GameDirector instance = GameDirector.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			int num = (int)instance.currentState;
			if (num == 0 || num == 1)
			{
				_players.Clear();
				_lastValue.Clear();
				_valueLost = 0L;
				_mapRemaining = 0L;
				_totalHaul = 0L;
				_lastSubmitted = 0L;
				_captured = false;
				_submitted = 0;
				_goal = 0;
				return;
			}
			if (instance.LevelCompleted && !_captured)
			{
				_totalHaul += _submitted;
				_captured = true;
			}
			if (!instance.LevelCompleted)
			{
				_captured = false;
			}
			if (num != 2)
			{
				return;
			}
			_timer -= Time.deltaTime;
			if (_timer > 0f)
			{
				return;
			}
			_timer = 1f;
			foreach (PlayerAvatar player in instance.PlayerList)
			{
				if (!((Object)(object)player == (Object)null) && !((Object)(object)player.photonView == (Object)null))
				{
					P p = Get(player.photonView.OwnerActorNr);
					if (_fPlayerName != null && _fPlayerName.GetValue(player) is string text && !string.IsNullOrEmpty(text))
					{
						p.name = text;
					}
				}
			}
			if (PhotonNetwork.InRoom && !string.IsNullOrEmpty(PhotonNetwork.NickName))
			{
				Get(PhotonNetwork.LocalPlayer.ActorNumber).name = PhotonNetwork.NickName;
			}
			_submitted = 0;
			_goal = 0;
			PlayerAvatar instance2 = PlayerAvatar.instance;
			ExtractionPoint val = null;
			float num2 = float.MaxValue;
			ExtractionPoint[] array = Object.FindObjectsOfType<ExtractionPoint>(true);
			foreach (ExtractionPoint val2 in array)
			{
				if ((Object)(object)instance2 == (Object)null || (Object)(object)instance2.playerTransform == (Object)null)
				{
					val = val2;
					break;
				}
				float num3 = Vector3.Distance(instance2.playerTransform.position, ((Component)val2).transform.position);
				if (num3 < num2)
				{
					num2 = num3;
					val = val2;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				_submitted = ((_fHaulCurrent != null) ? ((int)_fHaulCurrent.GetValue(val)) : 0);
				_goal = val.haulGoal;
			}
			if ((Object)(object)HaulUI.instance != (Object)null && _fHaulUIValue != null)
			{
				int num4 = (int)_fHaulUIValue.GetValue(HaulUI.instance);
				if (num4 > _submitted)
				{
					_submitted = num4;
				}
			}
			if (_submitted > _totalHaul)
			{
				_totalHaul = _submitted;
			}
			_lastSubmitted = _submitted;
			_mapRemaining = 0L;
			if (!(_fCurValue != null))
			{
				return;
			}
			ValuableObject[] array2 = Object.FindObjectsOfType<ValuableObject>();
			foreach (ValuableObject val3 in array2)
			{
				if ((Object)(object)val3 == (Object)null)
				{
					continue;
				}
				int instanceID = ((Object)val3).GetInstanceID();
				float num5 = (float)_fCurValue.GetValue(val3);
				_mapRemaining += (long)num5;
				if (!_lastValue.TryGetValue(instanceID, out var value))
				{
					_lastValue[instanceID] = num5;
					continue;
				}
				if (num5 < value - 1f)
				{
					_valueLost += (long)(value - num5);
				}
				_lastValue[instanceID] = num5;
			}
		}

		private void OnGUI()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected I4, but got Unknown
			//IL_0028: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			GameDirector instance = GameDirector.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			int num = (int)instance.currentState;
			if (_style == null)
			{
				GUIStyle val = new GUIStyle(GUI.skin.label)
				{
					fontSize = 16
				};
				val.normal.textColor = Color.white;
				_style = val;
			}
			ExtractionPoint val2 = Object.FindObjectOfType<ExtractionPoint>();
			bool flag = (Object)(object)ShopKeeper.instance != (Object)null;
			switch (num)
			{
			case 2:
				if (!(instance.PlayerList == null || instance.PlayerList.Count == 0 || (Object)(object)val2 == (Object)null || flag))
				{
					int num2 = ((!instance.LevelCompleted) ? _submitted : 0);
					int num3 = ((!instance.LevelCompleted) ? _goal : 0);
					long num4 = Mathf.Max(0, num3 - num2);
					List<string> values2 = (IsEnglish ? new List<string>
					{
						"=== Economy Panel ===",
						$"Submitted: ${num2} / Goal: ${num3}",
						$"Remaining: ${num4}",
						$"On Map: ${_mapRemaining}"
					} : new List<string>
					{
						"=== 经济面板 ===",
						$"已提交: ${num2} / 目标 ${num3}",
						$"还差: ${num4}",
						$"图上剩余: ${_mapRemaining}"
					});
					DrawRight(string.Join("\n", values2));
				}
				break;
			case 4:
			case 5:
				if (instance.PlayerList != null && instance.PlayerList.Count != 0)
				{
					List<string> values = (IsEnglish ? new List<string>
					{
						"=== Run Recap ===",
						$"Total Haul: ${_totalHaul}",
						$"Total Loss: ${_valueLost}"
					} : new List<string>
					{
						"=== 本局结算 ===",
						$"全局搬运: ${_totalHaul}",
						$"全局损失: ${_valueLost}"
					});
					DrawRight(string.Join("\n", values));
				}
				break;
			}
		}

		private void DrawRight(string text)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			Vector2 val = _style.CalcSize(new GUIContent(text));
			float num = (float)Screen.width - val.x - 60f;
			Rect val2 = default(Rect);
			((Rect)(ref val2))..ctor(num, 120f, val.x + 24f, val.y + 20f);
			GUI.color = new Color(0f, 0f, 0f, 0.65f);
			GUI.Box(val2, GUIContent.none);
			GUI.color = Color.white;
			GUI.Label(new Rect(((Rect)(ref val2)).x + 12f, ((Rect)(ref val2)).y + 10f, ((Rect)(ref val2)).width - 20f, ((Rect)(ref val2)).height - 16f), text, _style);
		}
	}
}