Decompiled source of GoldenPan v2.0.0

GoldenPan.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BoneLib.BoneMenu;
using GoldenPan;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSLZ.Marrow.Data;
using Il2CppSLZ.Marrow.Pool;
using Il2CppSLZ.Marrow.Warehouse;
using Il2CppSystem;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(GoldenPanMod), "Golden Pan", "1.1.0", "Wakey", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[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 GoldenPan
{
	internal static class Gilding
	{
		public static readonly Color Gold = new Color(1f, 0.78f, 0.16f);

		public static float Tiling = 2f;

		private static bool _loaded;

		private static Texture2D _albedo;

		private static Texture2D _normal;

		private static Texture2D _metal;

		private static Texture2D _ao;

		private static void EnsureTextures()
		{
			if (_loaded)
			{
				return;
			}
			_loaded = true;
			try
			{
				string dir = Path.Combine(Path.GetDirectoryName(Application.dataPath), "UserData", "GoldenPan");
				_albedo = LoadPng(dir, "gold_albedo.png");
				_normal = LoadPng(dir, "gold_normal.png");
				_metal = LoadPng(dir, "gold_metallic.png");
				_ao = LoadPng(dir, "gold_ao.png");
				MelonLogger.Msg("[GoldenPan] gold textures: albedo=" + ((Object)(object)_albedo != (Object)null) + " normal=" + ((Object)(object)_normal != (Object)null) + " metal=" + ((Object)(object)_metal != (Object)null) + " ao=" + ((Object)(object)_ao != (Object)null));
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] texture load: " + ex.Message);
			}
		}

		private static Texture2D LoadPng(string dir, string file)
		{
			//IL_0016: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_0035: 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_0042: Expected O, but got Unknown
			//IL_0043: Expected O, but got Unknown
			string path = Path.Combine(dir, file);
			if (!File.Exists(path))
			{
				return null;
			}
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, true)
			{
				hideFlags = (HideFlags)61
			};
			ImageConversion.LoadImage(val, new Il2CppStructArray<byte>(File.ReadAllBytes(path)));
			((Texture)val).wrapMode = (TextureWrapMode)0;
			Object.DontDestroyOnLoad((Object)val);
			return val;
		}

		public static void Goldify(GameObject go)
		{
			if ((Object)(object)go == (Object)null)
			{
				return;
			}
			EnsureTextures();
			foreach (Renderer componentsInChild in go.GetComponentsInChildren<Renderer>(true))
			{
				if ((Object)(object)componentsInChild == (Object)null)
				{
					continue;
				}
				try
				{
					Il2CppReferenceArray<Material> materials = componentsInChild.materials;
					int num = ((Il2CppArrayBase<Material>)(object)materials)?.Length ?? 0;
					for (int i = 0; i < num; i++)
					{
						Material val = ((Il2CppArrayBase<Material>)(object)materials)[i];
						if ((Object)(object)val != (Object)null)
						{
							Gild(val);
						}
					}
				}
				catch
				{
				}
			}
		}

		private static void Gild(Material m)
		{
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: 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_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_albedo != (Object)null)
			{
				SetTex(m, "_BaseMap", (Texture)(object)_albedo);
				SetTex(m, "_MainTex", (Texture)(object)_albedo);
				SetColor(m, "_BaseColor", Color.white);
				SetColor(m, "_Color", Color.white);
				try
				{
					m.color = Color.white;
				}
				catch
				{
				}
				try
				{
					m.SetTextureScale("_BaseMap", new Vector2(Tiling, Tiling));
				}
				catch
				{
				}
				if ((Object)(object)_normal != (Object)null)
				{
					SetTex(m, "_BumpMap", (Texture)(object)_normal);
					TryKeyword(m, "_NORMALMAP");
					SetFloat(m, "_BumpScale", 1f);
				}
				if ((Object)(object)_metal != (Object)null)
				{
					SetTex(m, "_MetallicGlossMap", (Texture)(object)_metal);
					TryKeyword(m, "_METALLICSPECGLOSSMAP");
					SetFloat(m, "_Metallic", 1f);
					SetFloat(m, "_Smoothness", 1f);
					SetFloat(m, "_GlossMapScale", 1f);
				}
				else
				{
					SetFloat(m, "_Metallic", 1f);
					SetFloat(m, "_Smoothness", 0.9f);
				}
				if ((Object)(object)_ao != (Object)null)
				{
					SetTex(m, "_OcclusionMap", (Texture)(object)_ao);
					TryKeyword(m, "_OCCLUSIONMAP");
					SetFloat(m, "_OcclusionStrength", 1f);
				}
				SetColor(m, "_EmissionColor", Gold * 0.12f);
				TryKeyword(m, "_EMISSION");
			}
			else
			{
				SetTex(m, "_BaseMap", (Texture)(object)Texture2D.whiteTexture);
				SetTex(m, "_MainTex", (Texture)(object)Texture2D.whiteTexture);
				SetColor(m, "_BaseColor", Gold);
				SetColor(m, "_Color", Gold);
				try
				{
					m.color = Gold;
				}
				catch
				{
				}
				SetFloat(m, "_Metallic", 1f);
				SetFloat(m, "_Smoothness", 0.9f);
				SetFloat(m, "_Glossiness", 0.9f);
			}
		}

		public static void Freeze(GameObject go)
		{
			if ((Object)(object)go == (Object)null)
			{
				return;
			}
			try
			{
				foreach (Animator componentsInChild in go.GetComponentsInChildren<Animator>(true))
				{
					if ((Object)(object)componentsInChild != (Object)null)
					{
						try
						{
							((Behaviour)componentsInChild).enabled = false;
						}
						catch
						{
						}
					}
				}
				foreach (Joint componentsInChild2 in go.GetComponentsInChildren<Joint>(true))
				{
					if ((Object)(object)componentsInChild2 != (Object)null)
					{
						try
						{
							Object.Destroy((Object)(object)componentsInChild2);
						}
						catch
						{
						}
					}
				}
				Rigidbody val = go.GetComponent<Rigidbody>();
				foreach (Rigidbody componentsInChild3 in go.GetComponentsInChildren<Rigidbody>(true))
				{
					if (!((Object)(object)componentsInChild3 == (Object)null) && (!((Object)(object)val != (Object)null) || !(((Il2CppObjectBase)componentsInChild3).Pointer == ((Il2CppObjectBase)val).Pointer)))
					{
						try
						{
							Object.Destroy((Object)(object)componentsInChild3);
						}
						catch
						{
						}
					}
				}
				if ((Object)(object)val == (Object)null)
				{
					val = go.AddComponent<Rigidbody>();
				}
				val.isKinematic = false;
				val.useGravity = true;
				val.mass = 60f;
				try
				{
					val.collisionDetectionMode = (CollisionDetectionMode)2;
				}
				catch
				{
				}
				try
				{
					val.interpolation = (RigidbodyInterpolation)1;
				}
				catch
				{
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] petrify: " + ex.Message);
			}
		}

		private static void TryKeyword(Material m, string k)
		{
			try
			{
				m.EnableKeyword(k);
			}
			catch
			{
			}
		}

		private static void SetColor(Material m, string p, Color c)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (m.HasProperty(p))
				{
					m.SetColor(p, c);
				}
			}
			catch
			{
			}
		}

		private static void SetFloat(Material m, string p, float f)
		{
			try
			{
				if (m.HasProperty(p))
				{
					m.SetFloat(p, f);
				}
			}
			catch
			{
			}
		}

		private static void SetTex(Material m, string p, Texture t)
		{
			try
			{
				if (m.HasProperty(p))
				{
					m.SetTexture(p, t);
				}
			}
			catch
			{
			}
		}
	}
	public class GoldenPanBehavior : MonoBehaviour
	{
		public static AudioClip GoldSound;

		public static float MinSpeed = 1.2f;

		public static float Volume = 1f;

		private Transform _panRoot;

		private float _lastHit;

		public GoldenPanBehavior(IntPtr ptr)
			: base(ptr)
		{
		}

		public GoldenPanBehavior()
			: base(ClassInjector.DerivedConstructorPointer<GoldenPanBehavior>())
		{
			ClassInjector.DerivedConstructorBody((Il2CppObjectBase)(object)this);
		}

		private void Start()
		{
			_panRoot = ((Component)this).transform.root;
		}

		private void OnCollisionEnter(Collision c)
		{
			//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_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (Time.time - _lastHit < 0.1f)
				{
					return;
				}
				Vector3 relativeVelocity = c.relativeVelocity;
				if (((Vector3)(ref relativeVelocity)).magnitude < MinSpeed)
				{
					return;
				}
				Rigidbody rigidbody = c.rigidbody;
				if ((Object)(object)rigidbody == (Object)null || rigidbody.isKinematic)
				{
					return;
				}
				Transform root = ((Component)rigidbody).transform.root;
				if ((Object)(object)root == (Object)null || ((Object)(object)_panRoot != (Object)null && ((Il2CppObjectBase)root).Pointer == ((Il2CppObjectBase)_panRoot).Pointer))
				{
					return;
				}
				string name = ((Object)root).name;
				if (name != null)
				{
					string text = name.ToLower();
					if (text.Contains("rigmanager") || text.Contains("player"))
					{
						return;
					}
				}
				Gilding.Goldify(((Component)root).gameObject);
				Gilding.Freeze(((Component)root).gameObject);
				_lastHit = Time.time;
				if ((Object)(object)GoldSound != (Object)null)
				{
					AudioClip goldSound = GoldSound;
					ContactPoint contact = c.GetContact(0);
					AudioSource.PlayClipAtPoint(goldSound, ((ContactPoint)(ref contact)).point, Volume);
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] impact: " + ex.Message);
			}
		}
	}
	public class GoldenPanMod : MelonMod
	{
		public const string FryingPanBarcode = "c1534c5a-d0e9-4d53-9218-e76446727969";

		public static bool AutoGild = true;

		private static string _wavPath;

		private static bool _ready;

		public override void OnInitializeMelon()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			ClassInjector.RegisterTypeInIl2Cpp<GoldenPanBehavior>();
			_wavPath = Path.Combine(Path.GetDirectoryName(Application.dataPath), "UserData", "GoldenPan", "Goldered.wav");
			_ready = true;
			Page obj = Page.Root.CreatePage("Golden Pan", Gilding.Gold, 0, true);
			obj.CreateFunction("Spawn Golden Pan", Color.yellow, (Action)SpawnPan);
			obj.CreateBool("Auto-gild all frying pans", Color.white, AutoGild, (Action<bool>)delegate(bool v)
			{
				AutoGild = v;
			});
			((MelonBase)this).LoggerInstance.Msg("Golden Pan v1.1 loaded. Spawn a frying pan (menu or spawn gun) and it's golden.");
		}

		private static void EnsureSound()
		{
			if ((Object)(object)GoldenPanBehavior.GoldSound != (Object)null)
			{
				return;
			}
			if (!File.Exists(_wavPath))
			{
				MelonLogger.Warning("[GoldenPan] Goldered.wav missing at " + _wavPath);
				return;
			}
			AudioClip val = WavLoader.Load(_wavPath, "Goldered");
			if ((Object)(object)val != (Object)null)
			{
				Object.DontDestroyOnLoad((Object)(object)val);
				GoldenPanBehavior.GoldSound = val;
			}
		}

		public static void MakeGolden(GameObject go)
		{
			if ((Object)(object)go == (Object)null)
			{
				return;
			}
			try
			{
				EnsureSound();
				Rigidbody componentInChildren = go.GetComponentInChildren<Rigidbody>(true);
				GameObject val = (((Object)(object)componentInChildren != (Object)null) ? ((Component)componentInChildren).gameObject : go);
				if (!((Object)(object)val.GetComponent<GoldenPanBehavior>() != (Object)null))
				{
					Gilding.Goldify(go);
					val.AddComponent<GoldenPanBehavior>();
					MelonLogger.Msg("[GoldenPan] gilded '" + ((Object)go).name + "'.");
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] MakeGolden: " + ex.Message);
			}
		}

		private void SpawnPan()
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: 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_0031: 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_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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: 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_0095: Expected O, but got Unknown
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			try
			{
				if (_ready)
				{
					EnsureSound();
					Camera val = FindCam();
					Vector3 val2 = default(Vector3);
					Quaternion val3;
					if ((Object)(object)val != (Object)null)
					{
						Transform transform = ((Component)val).transform;
						val2 = transform.position + transform.forward * 0.7f - Vector3.up * 0.15f;
						val3 = transform.rotation;
					}
					else
					{
						((Vector3)(ref val2))..ctor(0f, 1.5f, 0f);
						val3 = Quaternion.identity;
					}
					Spawnable val4 = new Spawnable
					{
						crateRef = new SpawnableCrateReference("c1534c5a-d0e9-4d53-9218-e76446727969")
					};
					AssetSpawner.Register(val4);
					Action<GameObject> val5 = DelegateSupport.ConvertDelegate<Action<GameObject>>((Delegate)new Action<GameObject>(MakeGolden));
					AssetSpawner.Spawn(val4, val2, val3, new Nullable<Vector3>(), (Transform)null, false, new Nullable<int>(), val5, (Action<GameObject>)null);
					MelonLogger.Msg("[GoldenPan] spawning golden pan...");
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Error("[GoldenPan] spawn failed: " + ex);
			}
		}

		private static Camera FindCam()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Invalid comparison between Unknown and I4
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Invalid comparison between Unknown and I4
			Camera main = Camera.main;
			if ((Object)(object)main != (Object)null)
			{
				return main;
			}
			Il2CppReferenceArray<Camera> allCameras = Camera.allCameras;
			foreach (Camera item in (Il2CppArrayBase<Camera>)(object)allCameras)
			{
				if ((Object)(object)item != (Object)null && (int)item.cameraType == 1 && item.stereoEnabled)
				{
					return item;
				}
			}
			foreach (Camera item2 in (Il2CppArrayBase<Camera>)(object)allCameras)
			{
				if ((Object)(object)item2 != (Object)null && (int)item2.cameraType == 1)
				{
					return item2;
				}
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(Poolee), "OnSpawn")]
	internal static class Poolee_OnSpawn_Patch
	{
		private static void Postfix(Poolee __instance)
		{
			try
			{
				if (!GoldenPanMod.AutoGild || (Object)(object)__instance == (Object)null)
				{
					return;
				}
				SpawnableCrate spawnableCrate = __instance.SpawnableCrate;
				if (!((Object)(object)spawnableCrate == (Object)null))
				{
					Barcode barcode = ((Scannable)spawnableCrate).Barcode;
					if (!(barcode == (Barcode)null) && !(barcode.ID != "c1534c5a-d0e9-4d53-9218-e76446727969"))
					{
						GoldenPanMod.MakeGolden(((Component)__instance).gameObject);
					}
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] OnSpawn hook: " + ex.Message);
			}
		}
	}
	internal static class WavLoader
	{
		public static AudioClip Load(string path, string name)
		{
			try
			{
				byte[] array = File.ReadAllBytes(path);
				if (array.Length < 44 || Encoding.ASCII.GetString(array, 0, 4) != "RIFF")
				{
					return null;
				}
				int num = BitConverter.ToInt16(array, 22);
				int num2 = BitConverter.ToInt32(array, 24);
				int num3 = BitConverter.ToInt16(array, 34);
				int i = 12;
				int num4 = -1;
				int num5 = 0;
				int num6;
				for (; i + 8 <= array.Length; i += 8 + num6 + (num6 & 1))
				{
					string text = Encoding.ASCII.GetString(array, i, 4);
					num6 = BitConverter.ToInt32(array, i + 4);
					if (text == "data")
					{
						num4 = i + 8;
						num5 = num6;
						break;
					}
				}
				if (num4 < 0 || num <= 0 || num2 <= 0)
				{
					return null;
				}
				if (num4 + num5 > array.Length)
				{
					num5 = array.Length - num4;
				}
				int num7 = num3 / 8;
				int num8 = num5 / num7;
				float[] array2 = new float[num8];
				for (int j = 0; j < num8; j++)
				{
					int num9 = num4 + j * num7;
					switch (num3)
					{
					case 16:
						array2[j] = (float)BitConverter.ToInt16(array, num9) / 32768f;
						break;
					case 32:
						array2[j] = (float)BitConverter.ToInt32(array, num9) / 2.1474836E+09f;
						break;
					case 8:
						array2[j] = (float)(array[num9] - 128) / 128f;
						break;
					}
				}
				AudioClip val = AudioClip.Create(name, num8 / num, num, num2, false);
				val.SetData(new Il2CppStructArray<float>(array2), 0);
				MelonLogger.Msg("[GoldenPan] loaded sound '" + name + "' (" + num + "ch " + num2 + "Hz " + num3 + "bit).");
				return val;
			}
			catch (Exception ex)
			{
				MelonLogger.Warning("[GoldenPan] WAV load failed: " + ex.Message);
				return null;
			}
		}
	}
}