Decompiled source of Gouda Job Badge Tracker v1.0.0

BadgeTracker.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.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;
		}
	}
}
[BepInPlugin("dairymods.badgetracker", "Badge Tracker", "1.0.0")]
public class BadgePlugin : BaseUnityPlugin
{
	private class Req
	{
		public ACHIEVEMENTTYPE type;

		public bool run;

		public object key;

		public int required;

		public float last = -1f;

		public float shownUntil;
	}

	private const int MaxTracked = 4;

	private static BadgePlugin I;

	private ConfigEntry<bool> popups;

	private ConfigEntry<float> popupSeconds;

	private ConfigEntry<string> trackedCfg;

	private readonly List<ACHIEVEMENTTYPE> tracked = new List<ACHIEVEMENTTYPE>();

	private List<Req> reqs;

	private readonly Dictionary<ACHIEVEMENTTYPE, string> names = new Dictionary<ACHIEVEMENTTYPE, string>();

	private float nextPoll;

	private readonly Dictionary<ACHIEVEMENTTYPE, string> descs = new Dictionary<ACHIEVEMENTTYPE, string>();

	private static readonly Color On = new Color(0.2f, 0.9f, 0.3f, 1f);

	private static readonly Color Off = new Color(0f, 0f, 0f, 0.55f);

	private AchievementManager Manager => Singleton<AchievementManager>.Instance;

	private void Awake()
	{
		//IL_00d1: 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_00bc: Unknown result type (might be due to invalid IL or missing references)
		I = this;
		popups = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Progress Popups", true, "Show a popup when a counted badge advances.");
		popupSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Popup Seconds", 4f, "");
		trackedCfg = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Tracked Badges", "", "Set by the tick boxes in the badge menu. Comma separated, max 4.");
		string[] array = trackedCfg.Value.Split(',');
		for (int i = 0; i < array.Length; i++)
		{
			if (Enum.TryParse<ACHIEVEMENTTYPE>(array[i].Trim(), out ACHIEVEMENTTYPE result) && tracked.Count < 4 && !tracked.Contains(result))
			{
				tracked.Add(result);
			}
		}
		new Harmony("dairymods.badgetracker").PatchAll(typeof(BadgePlugin));
	}

	private bool Load()
	{
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		if (!AchievementManager.GotStats || !Object.op_Implicit((Object)(object)Manager))
		{
			return false;
		}
		List<Req> list = new List<Req>();
		(string, string, bool)[] array = new(string, string, bool)[2]
		{
			("steamStatBasedAchievements", "statType", false),
			("runBasedAchievements", "valueType", true)
		};
		for (int i = 0; i < array.Length; i++)
		{
			var (text, text2, run) = array[i];
			if (!(AccessTools.Field(typeof(AchievementManager), text)?.GetValue(Manager) is IEnumerable enumerable))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("AchievementManager." + text + " not found: game update changed it. Counters will not show."));
				continue;
			}
			foreach (object item in enumerable)
			{
				Traverse val = Traverse.Create(item);
				list.Add(new Req
				{
					type = val.Field("achievementType").GetValue<ACHIEVEMENTTYPE>(),
					key = val.Field(text2).GetValue(),
					required = val.Field("requiredValue").GetValue<int>(),
					run = run
				});
			}
		}
		if (list.Count == 0)
		{
			return false;
		}
		reqs = list;
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Badge Tracker: {reqs.Count} counted badges");
		return true;
	}

	private float Value(Req r)
	{
		//IL_0049: 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_002b: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (r.run)
			{
				return Mathf.Max((float)Manager.GetRunBasedInt((RUNBASEDVALUETYPE)r.key), Manager.GetRunBasedFloat((RUNBASEDVALUETYPE)r.key));
			}
			int num = default(int);
			return Manager.GetSteamStatInt((STEAMSTATTYPE)r.key, ref num) ? num : 0;
		}
		catch
		{
			return 0f;
		}
	}

	private string Name(ACHIEVEMENTTYPE type)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		Describe(type);
		return names[type];
	}

	private string Desc(ACHIEVEMENTTYPE type)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		Describe(type);
		return descs[type];
	}

	private unsafe void Describe(ACHIEVEMENTTYPE type)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: 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_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		if (names.ContainsKey(type))
		{
			return;
		}
		string value = ((object)(*(ACHIEVEMENTTYPE*)(&type))/*cast due to .constrained prefix*/).ToString();
		string value2 = "";
		try
		{
			BadgeData val = ((IEnumerable<BadgeData>)Resources.FindObjectsOfTypeAll<BadgeData>()).FirstOrDefault((Func<BadgeData, bool>)((BadgeData b) => b.linkedAchievement == type));
			if (Object.op_Implicit((Object)(object)val))
			{
				value = LocalizedText.GetText(LocalizedText.GetNameIndex(val.displayName), true);
				value2 = ((val.secret && val.IsLocked) ? "???" : LocalizedText.GetText(LocalizedText.GetDescriptionIndex(val.displayName), true));
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)$"No badge text for {type}: {ex.Message}");
		}
		names[type] = value;
		descs[type] = value2;
	}

	private string Line(ACHIEVEMENTTYPE type)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: 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)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		Req req = reqs.FirstOrDefault((Req q) => q.type == type);
		if (req == null)
		{
			return Name(type);
		}
		return $"{Name(type)}  {Mathf.Min(Mathf.Floor(Value(req)), (float)req.required):0}/{req.required}";
	}

	private void Update()
	{
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		if ((reqs == null && !Load()) || Time.time < nextPoll)
		{
			return;
		}
		nextPoll = Time.time + 0.5f;
		if (tracked.RemoveAll((ACHIEVEMENTTYPE t) => Manager.IsAchievementUnlocked(t)) > 0)
		{
			trackedCfg.Value = string.Join(",", tracked);
		}
		foreach (Req req in reqs)
		{
			float num = Mathf.Floor(Value(req));
			if (req.last >= 0f && num > req.last && popups.Value && !Manager.IsAchievementUnlocked(req.type))
			{
				req.shownUntil = Time.time + popupSeconds.Value;
			}
			req.last = num;
		}
	}

	private void OnGUI()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: 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_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: 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_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: 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)
		if ((int)Event.current.type != 7 || reqs == null || !Object.op_Implicit((Object)(object)Manager))
		{
			return;
		}
		float num = (float)Screen.height * 0.3f;
		foreach (ACHIEVEMENTTYPE item in tracked)
		{
			Draw.Text(new Rect(24f, num, 700f, 28f), Line(item), Color.white, 18, (TextAnchor)0);
			Draw.Text(new Rect(24f, num + 22f, 700f, 24f), Desc(item), new Color(0.85f, 0.85f, 0.85f, 1f), 14, (TextAnchor)0);
			num += 50f;
		}
		num = (float)Screen.height * 0.12f;
		foreach (Req req in reqs)
		{
			float num2 = req.shownUntil - Time.time;
			if (!(num2 <= 0f))
			{
				Draw.Text(new Rect(0f, num, (float)Screen.width, 32f), Line(req.type), new Color(1f, 0.9f, 0.4f, Mathf.Clamp01(num2)), 22, (TextAnchor)1);
				num += 30f;
			}
		}
	}

	[HarmonyPatch(typeof(BadgeUI), "Init")]
	[HarmonyPostfix]
	private static void BadgeInit(BadgeUI __instance)
	{
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: 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_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: 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_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: 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)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Expected O, but got Unknown
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: 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)
		if (!Object.op_Implicit((Object)(object)__instance.data) || __instance.onEndScreen)
		{
			return;
		}
		BadgeUI ui = __instance;
		Transform val = ((Component)ui).transform.Find("TrackBox");
		if (!Object.op_Implicit((Object)(object)val))
		{
			GameObject val2 = new GameObject("TrackBox", new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Button)
			});
			val2.transform.SetParent(((Component)ui).transform, false);
			RectTransform val3 = (RectTransform)val2.transform;
			Vector2 val4 = (val3.pivot = Vector2.one);
			Vector2 anchorMin = (val3.anchorMax = val4);
			val3.anchorMin = anchorMin;
			val3.sizeDelta = new Vector2(20f, 20f);
			val3.anchoredPosition = Vector2.zero;
			Button component = val2.GetComponent<Button>();
			Navigation navigation = ((Selectable)component).navigation;
			((Navigation)(ref navigation)).mode = (Mode)0;
			((Selectable)component).navigation = navigation;
			((UnityEvent)component.onClick).AddListener((UnityAction)delegate
			{
				I.Toggle(ui);
			});
			val = val2.transform;
		}
		((Graphic)((Component)val).GetComponent<Image>()).color = (I.tracked.Contains(ui.data.linkedAchievement) ? On : Off);
		((Component)val).gameObject.SetActive(!I.Manager.IsAchievementUnlocked(ui.data.linkedAchievement));
	}

	private void Toggle(BadgeUI ui)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: 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_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)ui.data))
		{
			return;
		}
		ACHIEVEMENTTYPE linkedAchievement = ui.data.linkedAchievement;
		if (!tracked.Remove(linkedAchievement))
		{
			if (Manager.IsAchievementUnlocked(linkedAchievement))
			{
				return;
			}
			if (tracked.Count >= 4)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Already tracking {4} badges, untick one first");
				return;
			}
			tracked.Add(linkedAchievement);
		}
		trackedCfg.Value = string.Join(",", tracked);
		((Graphic)((Component)((Component)ui).transform.Find("TrackBox")).GetComponent<Image>()).color = (tracked.Contains(linkedAchievement) ? On : Off);
	}
}
internal static class Draw
{
	private static GUIStyle style;

	public static Camera Cam
	{
		get
		{
			MainCamera instance = MainCamera.instance;
			Camera val = (Object.op_Implicit((Object)(object)instance) ? ((Component)instance).GetComponent<Camera>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return Camera.main;
			}
			return val;
		}
	}

	public static bool ToGui(Camera cam, Vector3 world, out Vector2 gui)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: 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_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = cam.WorldToViewportPoint(world);
		gui = new Vector2(val.x * (float)Screen.width, (1f - val.y) * (float)Screen.height);
		return val.z > 0f;
	}

	public static void Line(Vector2 a, Vector2 b, Color c, float width)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//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_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: 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_0060: Unknown result type (might be due to invalid IL or missing references)
		Matrix4x4 matrix = GUI.matrix;
		Vector2 val = b - a;
		GUI.color = c;
		GUIUtility.RotateAroundPivot(Mathf.Atan2(val.y, val.x) * 57.29578f, a);
		GUI.DrawTexture(new Rect(a.x, a.y - width / 2f, ((Vector2)(ref val)).magnitude, width), (Texture)(object)Texture2D.whiteTexture);
		GUI.matrix = matrix;
		GUI.color = Color.white;
	}

	public static void Dot(Vector2 p, Color c, float size)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: 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_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = c;
		GUI.DrawTexture(new Rect(p.x - size / 2f, p.y - size / 2f, size, size), (Texture)(object)Texture2D.whiteTexture);
		GUI.color = Color.white;
	}

	public static void Text(Rect r, string s, Color c, int size = 18, TextAnchor anchor = (TextAnchor)0)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: 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_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		if (style == null)
		{
			style = new GUIStyle(GUI.skin.label)
			{
				fontStyle = (FontStyle)1,
				wordWrap = false
			};
		}
		style.fontSize = size;
		style.alignment = anchor;
		style.normal.textColor = new Color(0f, 0f, 0f, c.a * 0.8f);
		GUI.Label(new Rect(((Rect)(ref r)).x + 2f, ((Rect)(ref r)).y + 2f, ((Rect)(ref r)).width, ((Rect)(ref r)).height), s, style);
		style.normal.textColor = c;
		GUI.Label(r, s, style);
	}
}