Decompiled source of Unremove v0.5.0

Unremove.dll

Decompiled 3 days 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 Photon.Pun;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("UnityEngine.CoreModule")]
[assembly: AssemblyCompany("SausageCake")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.5.0.0")]
[assembly: AssemblyInformationalVersion("0.5.0")]
[assembly: AssemblyProduct("Unremove")]
[assembly: AssemblyTitle("Unremove")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.5.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 Unremove
{
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(DebugCommandHandler), "RegisterDevCommands")]
	internal static class DebugCommandHandlerPatch
	{
		private static void Postfix(DebugCommandHandler __instance)
		{
			if (Unremove.Instance.configTryFixDebugRefs.Value)
			{
				foreach (PrefabRef debugModule in __instance.debugModules)
				{
					PrefabRef val = debugModule;
					if (((PrefabRef<GameObject>)(object)val).resourcePath == null)
					{
						((PrefabRef<GameObject>)(object)val).resourcePath = "Level/Debug/Modules/" + ((PrefabRef<GameObject>)(object)debugModule).prefabName;
					}
				}
			}
			__instance.debugModules.AddRange(Unremove.Refs("Level/Shop/Modules/Old/Module - Shop - DE - Bathroom 1", "Level/Shop/Modules/Old/Module - Shop - DE - Big Bathroom", "Level/Shop/Modules/Old/Module - Shop - DE - Storage", "Level/Arctic/Modules/Module - Arctic - M - 1 - Container Yard", "Level/Manor/Modules/Module - Manor - M - 2 - Silly Cemetery", "Level/Manor/Modules/Module - Manor - M - 2 - Whale room 1", "Level/Wizard/Modules/Module - Wizard - M - 1 - Crypts", "Level/Desert/Modules/Module - Desert - DE - 3 - Minecart to hole", "Level/Desert/Modules/Module - Desert - N - 1 - Employee Break Room", "Level/Manor/Modules/Module - M - Template", "Level/Manor/Modules/Module - Manor - DE - 1 - Template", "Level/Manor/Modules/Module - Manor - M - 1 - Earth Basement ruben wip", "Level/Manor/Modules/Module - N - Template", "Level/Museum/Modules/Module - Museum - M - ruben laser pontus", "Level/Shop/Modules/Module - Shop - DE - Secret Template"));
		}
	}
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(EnemyDirector), "Start")]
	internal static class EnemyDirectorPatch
	{
		private static EnemySetup virus;

		private static void Postfix(EnemyDirector __instance)
		{
			if (!SemiFunc.IsMultiplayer() && Unremove.Instance.configAddEnemyVirus.Value)
			{
				if (!Object.op_Implicit((Object)(object)virus))
				{
					virus = ScriptableObject.CreateInstance<EnemySetup>();
					PrefabRef val = Unremove.Ref("Enemies/Enemy - Virus");
					((Object)virus).name = ((PrefabRef<GameObject>)(object)val).prefabName;
					virus.spawnObjects = new List<PrefabRef>(1) { val };
					RarityPreset val2 = ScriptableObject.CreateInstance<RarityPreset>();
					val2.chance = 100f;
					virus.rarityPreset = val2;
				}
				__instance.enemiesDifficulty2.Add(virus);
			}
		}
	}
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(ItemWalkieTalkie), "Start")]
	internal static class PairWalkieToItselfPatch
	{
		private static readonly PrefabRef walkieBox = Unremove.Ref("Items/Item WalkieTalkieBox", log: false);

		private static Material[] walkieMaterials = null;

		private static void Postfix(ItemWalkieTalkie __instance)
		{
			//IL_0086: 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)
			if (SemiFunc.IsMasterClientOrSingleplayer() && __instance.otherWalkie == null)
			{
				int materialIndex = GetMaterialIndex(__instance);
				if (!SemiFunc.IsMultiplayer())
				{
					__instance.otherWalkie = __instance;
					__instance.SetMaterial(walkieMaterials[materialIndex]);
					Unremove.Logger.LogInfo((object)$"Paired Walkie Talkie to itself, material index {materialIndex}");
					return;
				}
				int viewID = __instance.photonView.ViewID;
				GameObject val = PhotonNetwork.InstantiateRoomObject(((PrefabRef<GameObject>)(object)walkieBox).resourcePath, AssetManager.instance.physDisabledPosition, Quaternion.identity, (byte)0, (object[])null);
				PunExtensions.GetPhotonView(val).RPC("LinkWalkiesRPC", (RpcTarget)0, new object[3] { viewID, viewID, materialIndex });
				PhotonNetwork.Destroy(val);
				Unremove.Logger.LogInfo((object)$"Paired Walkie Talkie to itself (RPC), material index {materialIndex}");
			}
		}

		private static int GetMaterialIndex(ItemWalkieTalkie walkie)
		{
			ItemAttributes component = ((Component)walkie).GetComponent<ItemAttributes>();
			int num = 0;
			foreach (ItemAttributes spawnedItem in ItemManager.instance.spawnedItems)
			{
				if (Object.op_Implicit((Object)(object)spawnedItem) && !((Object)(object)spawnedItem.item != (Object)(object)component.item))
				{
					if ((Object)(object)spawnedItem == (Object)(object)component)
					{
						break;
					}
					ItemWalkieTalkie component2 = ((Component)spawnedItem).GetComponent<ItemWalkieTalkie>();
					if ((Object)(object)component2.otherWalkie == (Object)(object)component2)
					{
						num++;
					}
				}
			}
			if (walkieMaterials == null)
			{
				walkieMaterials = ((PrefabRef<GameObject>)(object)walkieBox).Prefab.GetComponent<ItemWalkieBox>().walkieMaterials;
			}
			return Mathf.Clamp(num, 0, walkieMaterials.Length - 1);
		}
	}
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(Resources), "LoadAll", new Type[]
	{
		typeof(string),
		typeof(Type)
	})]
	internal static class ResourcesLoadAllPatch
	{
		private static Object[] Postfix(Object[] original, string path, Type systemTypeInstance)
		{
			if (systemTypeInstance != typeof(Item) || path.ToLowerInvariant() != "items/items")
			{
				return original;
			}
			IEnumerable<Item> source = Resources.LoadAll<Item>("Items/Removed Items").Where(delegate(Item item)
			{
				if (item.disabled || Unremove.Instance.configForceAdd.Value)
				{
					item.disabled = false;
					return true;
				}
				return false;
			});
			if (Unremove.Instance.configAddThrowUpgrade.Value)
			{
				source = source.Append(ThrowUpgradeItemFactory.Item);
			}
			Item[] array = source.ToArray();
			Unremove.Logger.LogInfo((object)($"{array.Count()} disabled items: " + string.Join(", ", array.Select((Item x) => x.itemName))));
			Item[] array2 = array;
			int num = 0;
			Object[] array3 = (Object[])(object)new Object[original.Length + array2.Length];
			ReadOnlySpan<Object> readOnlySpan = new ReadOnlySpan<Object>(original);
			readOnlySpan.CopyTo(new Span<Object>(array3).Slice(num, readOnlySpan.Length));
			num += readOnlySpan.Length;
			Item[] array4 = array2;
			foreach (Item val in array4)
			{
				array3[num] = (Object)(object)val;
				num++;
			}
			return array3;
		}
	}
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(RunManager), "Awake")]
	internal static class RunManagerPatch
	{
		private static readonly string[] vanillaShops = new string[5] { "Level - Shop Forest", "Level - Shop Harbor", "Level - Shop Wasteland", "Level - Shop Suburb", "Level - Shop Dead Forest" };

		private static void Postfix(RunManager __instance)
		{
			Harmony? harmony = Unremove.Instance.Harmony;
			if (harmony != null)
			{
				harmony.Unpatch((MethodBase)AccessTools.Method(typeof(RunManager), "Awake", (Type[])null, (Type[])null), AccessTools.Method(typeof(RunManagerPatch), "Postfix", (Type[])null, (Type[])null));
			}
			PatchArcticStuff(__instance);
			PatchManorStuff(__instance);
			PatchMuseumStuff(__instance);
			PatchShopStuff(__instance);
		}

		private static void PatchArcticStuff(RunManager rm)
		{
			Level val = Find<Level>(rm.levels, "Level - Arctic");
			val?.ModulesMega1.Add(Unremove.Ref("Level/Arctic/Modules/Module - Arctic - M - 1 - Cargo Central"));
			val?.ModulesNormal3.Add(Unremove.Ref("Level/Arctic/Modules/Module - Arctic - N - 3 - Sector S"));
			val?.ModulesPassage2.Add(Unremove.Ref("Level/Arctic/Modules/Module - Arctic - P - 2 - Frozen Medbay"));
		}

		private static void PatchManorStuff(RunManager rm)
		{
			Level val = Find<Level>(rm.levels, "Level - Manor");
			if (Object.op_Implicit((Object)(object)val))
			{
				if (Unremove.Instance.configAddManorTVStartRoom.Value)
				{
					val.StartRooms.Add(Unremove.Ref("Level/Manor/Start Room/Start Room - Manor - TV Room"));
				}
				Find<LevelValuables>(val.ValuablePresets, "Valuables - Manor")?.medium.Add(Unremove.Ref("Valuables/03 Medium/Valuable Manor Marble Table"));
			}
		}

		private static void PatchMuseumStuff(RunManager rm)
		{
			Find<Level>(rm.levels, "Level - Museum")?.ModulesNormal2.AddRange(Unremove.Refs("Level/Museum/Modules/Module - Museum - N - 2 - Basement Gallery", "Level/Museum/Modules/Module - Museum - N - 2 - Smaller rooms and stairs wip"));
		}

		private static void PatchShopStuff(RunManager rm)
		{
			PrefabRef item = Unremove.Ref("Level/Shop/Modules/Module - Shop - DE - Barricaded Door");
			string[] array = vanillaShops;
			foreach (string name in array)
			{
				Find<Level>(rm.levelShop, name)?.ModulesDeadEnd1.Add(item);
			}
		}

		private static T? Find<T>(List<T> list, string name) where T : Object
		{
			T val = list.Find((T x) => ((Object)x).name == name);
			if (!Object.op_Implicit((Object)(object)val))
			{
				Unremove.Logger.LogError((object)(name + " not found"));
			}
			return val;
		}
	}
	internal static class ThrowUpgradeItemFactory
	{
		private static Item _item;

		internal static Item Item
		{
			get
			{
				if (!Object.op_Implicit((Object)(object)_item))
				{
					Instantiate();
				}
				return _item;
			}
		}

		private static void Instantiate()
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			_item = ScriptableObject.CreateInstance<Item>();
			_item.prefab = Unremove.Ref("Items/Item Upgrade Player Grab Throw");
			((Object)_item).name = ((PrefabRef<GameObject>)(object)_item.prefab).prefabName;
			_item.itemName = "Throw Upgrade";
			_item.itemType = (itemType)3;
			_item.itemVolume = (itemVolume)6;
			_item.maxAmount = 10;
			_item.maxAmountInShop = 3;
			Value val = (_item.value = ScriptableObject.CreateInstance<Value>());
			val.valueMin = 250f;
			val.valueMax = 500f;
		}
	}
	[HarmonyWrapSafe]
	[HarmonyPatch(typeof(ItemAttributes), "Awake")]
	internal static class ThrowUpgradePatch
	{
		private static void Prefix(ItemAttributes __instance)
		{
			if (Object.op_Implicit((Object)(object)((Component)__instance).GetComponent<ItemUpgradePlayerGrabThrow>()))
			{
				__instance.item = ThrowUpgradeItemFactory.Item;
				((Behaviour)__instance).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<PhysGrabObject>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<ItemUpgrade>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<PhotonTransformView>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<NotValuableObject>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<RoomVolumeCheck>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<PhysGrabObjectImpactDetector>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<LineBetweenTwoPoints>()).enabled = true;
				((Behaviour)((Component)__instance).GetComponent<MapCustom>()).enabled = true;
				Unremove.Logger.LogInfo((object)"Patched Throw Upgrade");
			}
		}
	}
	[BepInPlugin("Eugene8388608.Unremove", "Unremove", "0.5.0")]
	public class Unremove : BaseUnityPlugin
	{
		internal ConfigEntry<bool> configAddThrowUpgrade = null;

		internal ConfigEntry<bool> configForceAdd = null;

		internal ConfigEntry<bool> configAddManorTVStartRoom = null;

		internal ConfigEntry<bool> configTryFixDebugRefs = null;

		internal ConfigEntry<bool> configAddEnemyVirus = null;

		internal static Unremove Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			configAddThrowUpgrade = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ThrowUpgradeBox", false, "Whether or not to add the Throw Upgrade shop item.\nPlease note that players without this mod will see the Upgrade box,\nbut they won't be able to grab or use it. They also won't see it being\ncarried away by you, i.e. they won't see the new position of the box.\n!!! Game restart is required after changing this option !!!");
			configForceAdd = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AddTechnicallyDisabledItems", true, "As of R.E.P.O v0.4.4 just adds single Walkie Talkies in the shop.\nThis mod pairs each of them to itself after first use,\nwhich looks odd actually.\n!!! Game restart is required after changing this option !!!");
			configAddManorTVStartRoom = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AddManorTVStartRoom", false, "Whether or not to add the Start Room - Manor - TV Room.\nDisabled by default because it doesn't have an extraction point.\n!!! Game restart is required after changing this option !!!");
			configTryFixDebugRefs = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FixVanillaDebugModules", true, "Debug Command Handler incorrectly registers vanilla debug modules.\nAn easy fix but you can disable it in case of problems.\n!!! Game restart is required after changing this option !!!");
			configAddEnemyVirus = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AddEnemyVirus", false, "Weird immortal yet harmless copy of Mentalist who doesn't\nattack. It does spawn scary popups around you.\nRestart is not required if changed through REPOConfig mod.\nSingleplayer only because its main attraction (The Popups)\ncannot be instantiated over Photon.");
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		internal static PrefabRef Ref(string resourcePath, bool log = true)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_0036: Expected O, but got Unknown
			string text = resourcePath.Split('/')[^1];
			if (log)
			{
				Logger.LogInfo((object)text);
			}
			PrefabRef val = new PrefabRef();
			((PrefabRef<GameObject>)val).resourcePath = resourcePath;
			((PrefabRef<GameObject>)val).prefabName = text;
			return val;
		}

		internal static IEnumerable<PrefabRef> Refs(params string[] resourcePaths)
		{
			return resourcePaths.Select((string x) => Ref(x));
		}
	}
}