Decompiled source of Quotas Reroll v0.1.1

BepInEx/plugins/GraveyardOrbit.Quotas/GraveyardOrbit.Quotas.dll

Decompiled 2 hours ago
using System;
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.Configuration;
using BepInEx.Logging;
using GraveyardOrbit.Audio;
using GraveyardOrbit.UI;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UIElements;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 GraveyardOrbit.Quotas
{
	[HarmonyPatch(typeof(QuotasScreen), "MakeCard")]
	internal static class QuotaCardRerollPatch
	{
		private static readonly Color RerollOrange = new Color(1f, 0.62f, 0.18f);

		private static readonly Color RerollDim = new Color(1f, 0.62f, 0.18f, 0.32f);

		private static readonly Color Ink = new Color(0.03f, 0.05f, 0.035f);

		private static void Postfix(QuotasScreen __instance, QuotaState q, JObject inst, VisualElement __result)
		{
			//IL_00db: 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_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: 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_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)__instance == (Object)null || q == null || inst == null || __result == null)
				{
					return;
				}
				QuotaDef val = q.Def(inst);
				if (!QuotaReroll.CanReroll(q, inst, val))
				{
					return;
				}
				VisualElement val2 = UQueryExtensions.Q<VisualElement>(__result, (string)null, "q-slabkey");
				Button btn = ((val2 != null) ? UQueryExtensions.Q<Button>(val2, (string)null, (string)null) : null);
				if (btn == null)
				{
					return;
				}
				int price = QuotaReroll.Price;
				bool flag = QuotaReroll.CanAfford();
				bool flag2 = QuotaReroll.HasReplacement(q, val);
				bool flag3 = flag && flag2;
				val2.style.visibility = StyleEnum<Visibility>.op_Implicit((Visibility)0);
				((TextElement)btn).text = ((price > 0) ? ("REROLL $" + price.ToString("N0", Loc.Culture)) : "REROLL FREE");
				((VisualElement)btn).SetEnabled(flag3);
				((VisualElement)btn).style.color = StyleColor.op_Implicit(flag3 ? RerollOrange : RerollDim);
				((VisualElement)btn).tooltip = ((!flag2) ? "No other work order is available." : ((!flag) ? $"Need ${price} to reroll this work order." : ((price > 0) ? $"Reroll this work order for ${price}." : "Reroll this work order for free.")));
				SlabFrame frame = UQueryExtensions.Q<SlabFrame>(val2, (string)null, (string)null);
				Color restStroke = RerollOrange;
				Color restFill = ((frame != null) ? frame.FillColor : Color.black);
				Color restTint = ((frame != null) ? frame.TintColor : Color.clear);
				if (frame != null)
				{
					frame.StrokeColor = (flag3 ? RerollOrange : RerollDim);
					frame.TintColor = new Color(RerollOrange.r, RerollOrange.g, RerollOrange.b, 0.1f);
				}
				if (!flag3)
				{
					return;
				}
				btn.clicked += delegate
				{
					__result.schedule.Execute((Action)delegate
					{
						QuotaReroll.TryReroll(__instance, q, inst);
					});
				};
				((CallbackEventHandler)btn).RegisterCallback<ClickEvent>((EventCallback<ClickEvent>)delegate(ClickEvent e)
				{
					((EventBase)e).StopPropagation();
				}, (TrickleDown)0);
				((CallbackEventHandler)val2).RegisterCallback<PointerEnterEvent>((EventCallback<PointerEnterEvent>)delegate
				{
					//IL_0019: Unknown result type (might be due to invalid IL or missing references)
					//IL_001e: 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_003b: 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_0055: 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)
					if (((VisualElement)btn).enabledSelf)
					{
						((VisualElement)btn).style.color = StyleColor.op_Implicit(Ink);
						if (frame != null)
						{
							frame.StrokeColor = Color.Lerp(RerollOrange, Color.white, 0.5f);
							frame.FillColor = RerollOrange;
							frame.TintColor = Color.clear;
						}
					}
				}, (TrickleDown)0);
				((CallbackEventHandler)val2).RegisterCallback<PointerLeaveEvent>((EventCallback<PointerLeaveEvent>)delegate
				{
					//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_0029: Unknown result type (might be due to invalid IL or missing references)
					//IL_003a: Unknown result type (might be due to invalid IL or missing references)
					//IL_004b: Unknown result type (might be due to invalid IL or missing references)
					((VisualElement)btn).style.color = StyleColor.op_Implicit(RerollOrange);
					if (frame != null)
					{
						frame.StrokeColor = restStroke;
						frame.FillColor = restFill;
						frame.TintColor = restTint;
					}
				}, (TrickleDown)0);
			}
			catch (Exception ex)
			{
				QuotasPlugin.Log.LogWarning((object)("[reroll] card patch failed: " + ex));
			}
		}
	}
	internal static class QuotaConfig
	{
		public static ConfigEntry<int> Price;

		public static void Init(ConfigFile config)
		{
			Price = config.Bind<int>("Reroll", "Price", 50, "Money charged to reroll one incomplete work order. Set 0 for free rerolls.");
		}
	}
	internal static class QuotaReroll
	{
		internal enum Category
		{
			None,
			General,
			Family,
			Purchase,
			Cross
		}

		internal static int Price => Mathf.Max(0, QuotaConfig.Price.Value);

		internal static Category CategoryOf(QuotaDef def)
		{
			if (def == null)
			{
				return Category.None;
			}
			if (def.IsFamily)
			{
				return Category.Family;
			}
			if (def.IsGeneral)
			{
				return Category.General;
			}
			if (def.IsPurchase)
			{
				return Category.Purchase;
			}
			if (def.IsCross)
			{
				return Category.Cross;
			}
			return Category.None;
		}

		internal static bool CanReroll(QuotaState state, JObject inst, QuotaDef def)
		{
			if (state != null && inst != null && def != null && !state.IsClaimed(inst))
			{
				return !state.IsComplete(inst);
			}
			return false;
		}

		internal static bool CanAfford()
		{
			GameState i = GameState.I;
			if (i != null)
			{
				if (Price > 0)
				{
					return i.Money >= Price;
				}
				return true;
			}
			return false;
		}

		internal static bool HasReplacement(QuotaState state, QuotaDef current)
		{
			return BuildPool(state, current).Count > 0;
		}

		internal static QuotaDef PickReplacement(QuotaState state, QuotaDef current)
		{
			List<QuotaDef> list = BuildPool(state, current);
			if (list.Count == 0)
			{
				return null;
			}
			return list[Random.Range(0, list.Count)];
		}

		private static List<QuotaDef> BuildPool(QuotaState state, QuotaDef current)
		{
			List<QuotaDef> list = new List<QuotaDef>();
			if (state == null || current == null)
			{
				return list;
			}
			Category category = CategoryOf(current);
			if (category == Category.None)
			{
				return list;
			}
			bool flag = GameData.DemoLocksDrones || state.SetIndex <= 2;
			foreach (QuotaDef item in QuotaCatalog.All)
			{
				if (item != null && !(item.id == current.id) && CategoryOf(item) == category && (!flag || !QuotaCatalog.NeedsDrones(item)) && (category != Category.Family || (!(item.family == current.family) && !GameData.IsDemoLockedBranch(item.family))))
				{
					list.Add(item);
				}
			}
			return list;
		}

		internal static bool TryReroll(QuotasScreen screen, QuotaState state, JObject inst)
		{
			try
			{
				QuotaDef val = ((state != null) ? state.Def(inst) : null);
				if (!CanReroll(state, inst, val))
				{
					return false;
				}
				int price = Price;
				GameState i = GameState.I;
				if (i == null || (price > 0 && i.Money < price))
				{
					return false;
				}
				QuotaDef val2 = PickReplacement(state, val);
				if (val2 == null)
				{
					return false;
				}
				if (price > 0)
				{
					i.Money -= price;
				}
				inst["id"] = JToken.op_Implicit(val2.id);
				inst["progress"] = JToken.op_Implicit(0);
				inst["claimed"] = JToken.op_Implicit(false);
				state.Save();
				RunLog.Event("quota_reroll", (long)(-price), RunLog.D(new object[6] { "from", val.id, "to", val2.id, "set", state.SetIndex }));
				QuotasPlugin.Log.LogInfo((object)$"[reroll] {val.id} -> {val2.id} for ${price} (money {i.Money})");
				try
				{
					Sfx.Play("buy", 0f);
				}
				catch
				{
				}
				try
				{
					if (screen != null)
					{
						screen.DevRefresh();
					}
				}
				catch
				{
				}
				return true;
			}
			catch (Exception ex)
			{
				QuotasPlugin.Log.LogWarning((object)("[reroll] failed: " + ex));
				return false;
			}
		}
	}
	[BepInPlugin("agentbox.graveyardorbit.quotas", "Graveyard Orbit Quotas", "0.1.1")]
	public sealed class QuotasPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "agentbox.graveyardorbit.quotas";

		public const string PluginVersion = "0.1.1";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			QuotaConfig.Init(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("agentbox.graveyardorbit.quotas");
			try
			{
				_harmony.PatchAll(typeof(QuotasPlugin).Assembly);
				Log.LogInfo((object)$"Quotas loaded: work-order reroll costs ${QuotaConfig.Price.Value}.");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Quotas patch failure (mod inactive): " + ex));
			}
		}

		private void OnDestroy()
		{
			try
			{
				Harmony harmony = _harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
			}
			catch
			{
			}
		}
	}
}