Decompiled source of TakeyValuables v1.0.0

com.github.zehsteam.TakeyValuables.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using com.github.zehsteam.TakeyValuables.Helpers;
using com.github.zehsteam.TakeyValuables.Objects;
using com.github.zehsteam.TakeyValuables.Patches;

[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("com.github.zehsteam.TakeyValuables")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+e0064ab0240f9bf9fc4e6960525c859dc36cfb94")]
[assembly: AssemblyProduct("TakeyValuables")]
[assembly: AssemblyTitle("com.github.zehsteam.TakeyValuables")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 com.github.zehsteam.TakeyValuables
{
	internal static class Content
	{
		public static GameObject ValuableSmolTakeyPrefab { get; private set; }

		public static GameObject ValuableSmolTakeyDiamondPrefab { get; private set; }

		public static void Load()
		{
			LoadAssetsFromAssetBundle();
		}

		private static void LoadAssetsFromAssetBundle()
		{
			AssetBundle val = LoadAssetBundle("takeyvaluables_assets");
			if (!((Object)(object)val == (Object)null))
			{
				ValuableSmolTakeyPrefab = LoadAssetFromAssetBundle<GameObject>("Valuable SmolTakey", val);
				ValuableSmolTakeyDiamondPrefab = LoadAssetFromAssetBundle<GameObject>("Valuable SmolTakey Diamond", val);
				Plugin.Logger.LogInfo((object)"Successfully loaded assets from AssetBundle!");
			}
		}

		private static AssetBundle LoadAssetBundle(string fileName)
		{
			try
			{
				string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)Plugin.Instance).Info.Location);
				string text = Path.Combine(directoryName, fileName);
				return AssetBundle.LoadFromFile(text);
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Failed to load AssetBundle \"{fileName}\". {arg}");
			}
			return null;
		}

		private static T LoadAssetFromAssetBundle<T>(string name, AssetBundle assetBundle) where T : Object
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				Plugin.Logger.LogError((object)("Failed to load asset of type \"" + typeof(T).Name + "\" from AssetBundle. Name is null or whitespace."));
				return default(T);
			}
			if ((Object)(object)assetBundle == (Object)null)
			{
				Plugin.Logger.LogError((object)("Failed to load asset of type \"" + typeof(T).Name + "\" with name \"" + name + "\" from AssetBundle. AssetBundle is null."));
				return default(T);
			}
			T val = assetBundle.LoadAsset<T>(name);
			if ((Object)(object)val == (Object)null)
			{
				Plugin.Logger.LogError((object)("Failed to load asset of type \"" + typeof(T).Name + "\" with name \"" + name + "\" from AssetBundle. No asset found with that type and name."));
				return default(T);
			}
			return val;
		}
	}
	[BepInPlugin("com.github.zehsteam.TakeyValuables", "TakeyValuables", "1.0.0")]
	internal class Plugin : BaseUnityPlugin
	{
		private readonly Harmony _harmony = new Harmony("com.github.zehsteam.TakeyValuables");

		internal static Plugin Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Logger = Logger.CreateLogSource("com.github.zehsteam.TakeyValuables");
			Logger.LogInfo((object)"TakeyValuables has awoken!");
			_harmony.PatchAll(typeof(RunManagerPatch));
			Content.Load();
			RegisterValuables();
		}

		private void RegisterValuables()
		{
			ValuableHelper.RegisterValuable(Content.ValuableSmolTakeyPrefab, (Type)2);
			ValuableHelper.RegisterValuable(Content.ValuableSmolTakeyDiamondPrefab, (Type)2);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.github.zehsteam.TakeyValuables";

		public const string PLUGIN_NAME = "TakeyValuables";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace com.github.zehsteam.TakeyValuables.Patches
{
	[HarmonyPatch(typeof(RunManager))]
	internal static class RunManagerPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void AwakePatch()
		{
			ValuableHelper.RegisterValuables();
		}
	}
}
namespace com.github.zehsteam.TakeyValuables.Objects
{
	public class ModdedPrefabPool : IPunPrefabPool
	{
		public readonly IPunPrefabPool _originalPool;

		public readonly Dictionary<string, GameObject> _moddedPrefabs = new Dictionary<string, GameObject>();

		public ModdedPrefabPool(IPunPrefabPool existingPool)
		{
			_originalPool = existingPool;
		}

		public void RegisterPrefab(string prefabName, GameObject prefab)
		{
			if (!_moddedPrefabs.ContainsKey(prefabName))
			{
				_moddedPrefabs[prefabName] = prefab;
			}
		}

		public GameObject Instantiate(string prefabId, Vector3 position, Quaternion rotation)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			string text = prefabId.Substring(prefabId.LastIndexOf('/') + 1);
			if (!_moddedPrefabs.TryGetValue(text, out var value))
			{
				if (_originalPool == null)
				{
					Plugin.Logger.LogError((object)"Failed to spawn network prefab from original PrefabPool. Original PrefabPool is null.");
					return null;
				}
				return _originalPool.Instantiate(prefabId, position, rotation);
			}
			Plugin.Logger.LogInfo((object)$"Spawned modded network prefab \"{text}\" at position {position}, rotation: {((Quaternion)(ref rotation)).eulerAngles}.");
			bool activeSelf = value.activeSelf;
			if (activeSelf)
			{
				value.SetActive(false);
			}
			GameObject result = Object.Instantiate<GameObject>(value, position, rotation);
			if (activeSelf)
			{
				value.SetActive(true);
			}
			return result;
		}

		public void Destroy(GameObject gameObject)
		{
			Object.Destroy((Object)(object)gameObject);
		}
	}
	public class ValuableData
	{
		public GameObject Prefab;

		public Type VolumeType;

		public ValuableData(GameObject prefab, Type volumeType)
		{
			//IL_0010: 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)
			Prefab = prefab;
			VolumeType = volumeType;
		}
	}
}
namespace com.github.zehsteam.TakeyValuables.Helpers
{
	internal static class PhotonNetworkHelper
	{
		private static ModdedPrefabPool _prefabPool;

		public static ModdedPrefabPool PrefabPool
		{
			get
			{
				if (_prefabPool == null)
				{
					_prefabPool = new ModdedPrefabPool(PhotonNetwork.PrefabPool);
					PhotonNetwork.PrefabPool = (IPunPrefabPool)(object)_prefabPool;
				}
				return _prefabPool;
			}
			set
			{
				_prefabPool = value;
				PhotonNetwork.PrefabPool = (IPunPrefabPool)(object)_prefabPool;
			}
		}
	}
	internal static class ValuableHelper
	{
		private static readonly List<ValuableData> _valuablesToRegister = new List<ValuableData>();

		private static bool _registeredValuables;

		public static void RegisterValuable(GameObject prefab, Type volumeType)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)prefab == (Object)null) && !_valuablesToRegister.Any((ValuableData x) => (Object)(object)x.Prefab == (Object)(object)prefab))
			{
				_valuablesToRegister.Add(new ValuableData(prefab, volumeType));
			}
		}

		public static void RegisterValuables()
		{
			if ((Object)(object)RunManager.instance == (Object)null || _registeredValuables)
			{
				return;
			}
			foreach (ValuableData item in _valuablesToRegister)
			{
				PhotonNetworkHelper.PrefabPool.RegisterPrefab(((Object)item.Prefab).name, item.Prefab);
				foreach (Level level in RunManager.instance.levels)
				{
					RegisterValuableForLevel(item, level);
				}
			}
			_registeredValuables = true;
		}

		private static void RegisterValuableForLevel(ValuableData valuable, Level level)
		{
			if (!level.HasEnemies)
			{
				return;
			}
			foreach (LevelValuables valuablePreset in level.ValuablePresets)
			{
				AddValuableToPreset(valuable, valuablePreset);
			}
		}

		private static void AddValuableToPreset(ValuableData valuable, LevelValuables levelValuables)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			if (valuable != null && !((Object)(object)levelValuables == (Object)null) && !PresetHasValuable(valuable, levelValuables))
			{
				List<GameObject> valuableListFromPreset = GetValuableListFromPreset(levelValuables, valuable.VolumeType);
				valuableListFromPreset.Add(valuable.Prefab);
				Plugin.Logger.LogInfo((object)("Added Valuable \"" + ((Object)valuable.Prefab).name + "\" to ValuablePreset \"" + ((Object)levelValuables).name + "\"."));
			}
		}

		private static bool PresetHasValuable(ValuableData valuable, LevelValuables levelValuables)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (valuable == null || (Object)(object)levelValuables == (Object)null)
			{
				return false;
			}
			List<GameObject> valuableListFromPreset = GetValuableListFromPreset(levelValuables, valuable.VolumeType);
			return valuableListFromPreset.Any((GameObject x) => (Object)(object)x == (Object)(object)valuable.Prefab);
		}

		private static List<GameObject> GetValuableListFromPreset(LevelValuables levelValuables, Type volumeType)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected I4, but got Unknown
			if ((Object)(object)levelValuables == (Object)null)
			{
				return new List<GameObject>();
			}
			if (1 == 0)
			{
			}
			List<GameObject> result = (int)volumeType switch
			{
				0 => levelValuables.tiny, 
				1 => levelValuables.small, 
				2 => levelValuables.medium, 
				3 => levelValuables.big, 
				4 => levelValuables.wide, 
				5 => levelValuables.tall, 
				6 => levelValuables.veryTall, 
				_ => new List<GameObject>(), 
			};
			if (1 == 0)
			{
			}
			return result;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}