Decompiled source of Dotoo Direct To Scrap v0.2.1

BepInEx/plugins/dotoo_direct_to_scrap.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[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("dotoo_direct_to_scrap")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("dotoo_direct_to_scrap")]
[assembly: AssemblyTitle("dotoo_direct_to_scrap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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 dotoo_auto_scrap
{
	[BepInPlugin("dotoo.auto_scrap", "dotoo_auto_scrap", "0.2.1")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class AutoScrapPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> Verbose;

		internal static ConfigEntry<bool> ScrapStandard;

		internal static ConfigEntry<bool> ScrapRare;

		internal static ConfigEntry<bool> ScrapEpic;

		internal static ConfigEntry<bool> ScrapExotic;

		internal static ConfigEntry<bool> ScrapMissionRewards;

		internal static int AllowDepth;

		internal static int UnlockDepth;

		private static readonly FieldInfo ExoticCounter = AccessTools.Field(typeof(PlayerData), "upgradesCollectedSinceExotic");

		private void Awake()
		{
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch for auto-scrap.");
			Verbose = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Verbose", true, "Log a line every time an upgrade is auto-scrapped. Handy for testing; set to false once you trust it.");
			ScrapMissionRewards = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ScrapMissionRewards", true, "Also auto-scrap upgrades granted as mission completion rewards (uses the same rarity settings below).");
			ScrapStandard = ((BaseUnityPlugin)this).Config.Bind<bool>("Rarity", "Standard", true, "Auto-scrap Standard upgrades the moment they spawn.");
			ScrapRare = ((BaseUnityPlugin)this).Config.Bind<bool>("Rarity", "Rare", true, "Auto-scrap Rare upgrades the moment they spawn.");
			ScrapEpic = ((BaseUnityPlugin)this).Config.Bind<bool>("Rarity", "Epic", false, "Auto-scrap Epic upgrades the moment they spawn.");
			ScrapExotic = ((BaseUnityPlugin)this).Config.Bind<bool>("Rarity", "Exotic", false, "Auto-scrap Exotic upgrades the moment they spawn. Off by default: scrapped Exotics are gone for good.");
			new Harmony("dotoo.auto_scrap").PatchAll();
		}

		internal static bool RarityAllowed(Rarity r)
		{
			//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_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: 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_001b: Expected I4, but got Unknown
			return (int)r switch
			{
				0 => ScrapStandard.Value, 
				1 => ScrapRare.Value, 
				2 => ScrapEpic.Value, 
				3 => ScrapExotic.Value, 
				_ => false, 
			};
		}

		internal static void CountForExoticPity(UpgradeInstance inst)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Invalid comparison between Unknown and I4
			try
			{
				if (!(ExoticCounter == null) && inst != null && !inst.RemoveOnQuit && !inst.RemoveAfterMission && !inst.Upgrade.IsSkin())
				{
					PlayerData instance = PlayerData.Instance;
					int num = (int)ExoticCounter.GetValue(instance);
					ExoticCounter.SetValue(instance, ((int)inst.Upgrade.Rarity != 3) ? (num + 1) : 0);
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)ex);
			}
		}

		internal static MethodBase Find(Type t, string name, Type[] args = null)
		{
			MethodInfo methodInfo = ((args == null) ? AccessTools.Method(t, name, (Type[])null, (Type[])null) : AccessTools.Method(t, name, args, (Type[])null));
			if (methodInfo == null)
			{
				Log.LogError((object)("method not found: " + t.Name + "." + name));
			}
			return methodInfo;
		}
	}
	[HarmonyPatch]
	internal static class AllowedEntryPoints
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			List<MethodBase> list = new List<MethodBase>();
			list.Add(AutoScrapPlugin.Find(typeof(EnemyCore), "OnSpawnedUpgrades_ClientRpc"));
			list.Add(AutoScrapPlugin.Find(typeof(GameManager), "SpawnUpgrade_Rpc", new Type[3]
			{
				typeof(NetworkBehaviourReference),
				typeof(Vector3),
				typeof(float)
			}));
			list.Add(AutoScrapPlugin.Find(typeof(GameManager), "SpawnUpgrade_Rpc", new Type[4]
			{
				typeof(NetworkBehaviourReference),
				typeof(Vector3),
				typeof(Rarity),
				typeof(float)
			}));
			List<MethodBase> source = list;
			return source.Where((MethodBase m) => m != null);
		}

		private static void Prefix()
		{
			AutoScrapPlugin.AllowDepth++;
		}

		private static Exception Finalizer(Exception __exception)
		{
			AutoScrapPlugin.AllowDepth--;
			return __exception;
		}
	}
	[HarmonyPatch]
	internal static class ScrapOnSpawn
	{
		private static readonly FieldInfo UpgradeField = AccessTools.Field(typeof(UpgradeCollectable), "upgrade");

		private static IEnumerable<MethodBase> TargetMethods()
		{
			List<MethodBase> list = new List<MethodBase>();
			list.Add(AutoScrapPlugin.Find(typeof(PlayerData), "SpawnUpgradeCollectable", new Type[4]
			{
				typeof(Vector3),
				typeof(float),
				typeof(bool),
				typeof(bool)
			}));
			list.Add(AutoScrapPlugin.Find(typeof(PlayerData), "SpawnUpgradeCollectable", new Type[9]
			{
				typeof(Vector3),
				typeof(Rarity),
				typeof(IUpgradable),
				typeof(float),
				typeof(bool),
				typeof(DuplicateMode),
				typeof(bool),
				typeof(bool),
				typeof(bool)
			}));
			List<MethodBase> source = list;
			return source.Where((MethodBase m) => m != null);
		}

		private static void Postfix(UpgradeCollectable __result)
		{
			//IL_009b: 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 (AutoScrapPlugin.AllowDepth <= 0 || !AutoScrapPlugin.Enabled.Value || (Object)(object)__result == (Object)null)
			{
				return;
			}
			try
			{
				object? value = UpgradeField.GetValue(__result);
				UpgradeInstance val = (UpgradeInstance)((value is UpgradeInstance) ? value : null);
				if (val == null)
				{
					return;
				}
				Upgrade upgrade = val.Upgrade;
				if (!((Object)(object)upgrade == (Object)null) && !upgrade.IsSkin() && !val.RemoveAfterMission && !(((MissionData)(ref MissionManager.CurrentMission)).Mission is OuroMission) && AutoScrapPlugin.RarityAllowed(upgrade.Rarity))
				{
					PlayerData.Instance.rentedUpgrades.Remove(val);
					((ClientCollectable)__result).DespawnTrackedObject();
					upgrade.GiveDismantleResources(val);
					AutoScrapPlugin.CountForExoticPity(val);
					if (AutoScrapPlugin.Verbose.Value)
					{
						AutoScrapPlugin.Log.LogInfo((object)$"auto-scrapped: {upgrade} ({upgrade.Rarity})");
					}
				}
			}
			catch (Exception ex)
			{
				AutoScrapPlugin.Log.LogError((object)ex);
			}
		}
	}
	[HarmonyPatch]
	internal static class MissionRewardEntry
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			MethodBase m = AutoScrapPlugin.Find(typeof(LevelUnlock_UpgradeRarity), "Unlock");
			if (m != null)
			{
				yield return m;
			}
		}

		private static void Prefix()
		{
			AutoScrapPlugin.UnlockDepth++;
		}

		private static Exception Finalizer(Exception __exception)
		{
			AutoScrapPlugin.UnlockDepth--;
			return __exception;
		}
	}
	[HarmonyPatch]
	internal static class ScrapMissionReward
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			MethodBase m = AutoScrapPlugin.Find(typeof(PlayerData), "CollectInstance", new Type[2]
			{
				typeof(UpgradeInstance),
				typeof(UnlockFlags)
			});
			if (m != null)
			{
				yield return m;
			}
		}

		private static bool Prefix(UpgradeInstance instance, UnlockFlags flags)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Invalid comparison between Unknown and I4
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			if (AutoScrapPlugin.UnlockDepth <= 0 || instance == null)
			{
				return true;
			}
			if (!AutoScrapPlugin.Enabled.Value || !AutoScrapPlugin.ScrapMissionRewards.Value)
			{
				return true;
			}
			if ((int)flags > 0)
			{
				return true;
			}
			try
			{
				Upgrade upgrade = instance.Upgrade;
				if ((Object)(object)upgrade == (Object)null)
				{
					return true;
				}
				if (upgrade.IsSkin() || instance.RemoveAfterMission)
				{
					return true;
				}
				if (((MissionData)(ref MissionManager.CurrentMission)).Mission is OuroMission)
				{
					return true;
				}
				if (!AutoScrapPlugin.RarityAllowed(upgrade.Rarity))
				{
					return true;
				}
				PlayerData.Instance.rentedUpgrades.Remove(instance);
				upgrade.GiveDismantleResources(instance);
				AutoScrapPlugin.CountForExoticPity(instance);
				if (AutoScrapPlugin.Verbose.Value)
				{
					AutoScrapPlugin.Log.LogInfo((object)$"auto-scrapped (mission reward): {upgrade} ({upgrade.Rarity})");
				}
				return false;
			}
			catch (Exception ex)
			{
				AutoScrapPlugin.Log.LogError((object)ex);
				return true;
			}
		}
	}
}