Decompiled source of MoreCustomHats v1.0.10

MoreCustomHats.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MoreCustomHats")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+9edca92bc3d3f38d316bc6dba3b93f7d26c7f963")]
[assembly: AssemblyProduct("MoreCustomHats")]
[assembly: AssemblyTitle("MoreCustomHats")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MoreCustomHats;

[BepInPlugin("monamiral.morecustomhats", "More custom hats", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private class Patcher
	{
		private static readonly int SkinColor = Shader.PropertyToID("_SkinColor");

		private static PlayerCustomizationDummy lastModifiedDummy;

		public static bool CreateHatOption(Customization customization, string name, Texture2D icon)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if (Array.Exists(customization.hats, (CustomizationOption hat) => ((Object)hat).name == name))
			{
				Debug.LogError((object)("[MonAmiral] Trying to add " + name + " a second time."));
				return false;
			}
			CustomizationOption val = ScriptableObject.CreateInstance<CustomizationOption>();
			val.color = Color.white;
			((Object)val).name = name;
			val.texture = (Texture)(object)icon;
			val.type = (Type)50;
			val.requiredAchievement = (ACHIEVEMENTTYPE)0;
			customization.hats = CollectionExtensions.AddToArray<CustomizationOption>(customization.hats, val);
			Debug.Log((object)$"[MonAmiral] PassportManager[{customization.hats.Length - 1}] = {name}");
			return true;
		}

		[HarmonyPatch(typeof(PassportManager), "Awake")]
		[HarmonyPostfix]
		public static void PassportManagerAwakePostfix(PassportManager __instance)
		{
			Customization component = ((Component)__instance).GetComponent<Customization>();
			Debug.Log((object)"[MonAmiral] Adding PassportManager CustomizationOptions.");
			Texture2D icon = assetBundle.LoadAsset<Texture2D>("Assets/Passport_Dummy.png");
			for (int i = 0; i < component.fits.Length; i++)
			{
				if (component.fits[i].overrideHat)
				{
					while (component.hats.Length - 1 < component.fits[i].overrideHatIndex)
					{
						CreateHatOption(component, $"Dummy#{i}", icon);
					}
				}
			}
			for (int j = 0; j < customHats.Count; j++)
			{
				HatEntry hatEntry = customHats[j];
				CreateHatOption(component, hatEntry.Name, hatEntry.Icon);
			}
			Debug.Log((object)"[MonAmiral] Done.");
		}

		[HarmonyPatch(typeof(CharacterCustomization), "Awake")]
		[HarmonyPostfix]
		public static void CharacterCustomizationAwakePostfix(CharacterCustomization __instance)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			Transform val = ((Component)__instance.refs).transform.Find("Armature/Hip/Mid/AimJoint/Torso/Head/Hat");
			List<Renderer> list = new List<Renderer>(__instance.refs.playerHats);
			Debug.Log((object)$"[MonAmiral] Instanciating CharacterCustomization hats as children of {__instance} / {val}.");
			for (int i = 0; i < customHats.Count; i++)
			{
				HatEntry hatEntry = customHats[i];
				GameObject obj = Object.Instantiate<GameObject>(hatEntry.Prefab, val.position, val.rotation, val);
				SetLayerRecursively(obj.transform, ((Component)val).gameObject.layer);
				Renderer componentInChildren = obj.GetComponentInChildren<Renderer>();
				((Component)componentInChildren).gameObject.SetActive(false);
				for (int j = 0; j < componentInChildren.materials.Length; j++)
				{
					componentInChildren.materials[j].shader = Shader.Find("W/Character");
				}
				Debug.Log((object)$"[MonAmiral] CharacterCustomization Hats[{list.Count}] = {hatEntry.Name}");
				__instance.refs.AllRenderers = CollectionExtensions.AddToArray<Renderer>(__instance.refs.AllRenderers, componentInChildren);
				list.Add(componentInChildren);
			}
			__instance.refs.playerHats = list.ToArray();
		}

		[HarmonyPatch(typeof(PlayerCustomizationDummy), "UpdateDummy")]
		[HarmonyPrefix]
		public static void PlayerCustomizationDummyUpdateDummyPrefix(PlayerCustomizationDummy __instance)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)(object)lastModifiedDummy)
			{
				return;
			}
			lastModifiedDummy = __instance;
			Transform val = ((Component)__instance.refs).transform.Find("Armature/Hip/Mid/AimJoint/Torso/Head/Hat");
			List<Renderer> list = new List<Renderer>(__instance.refs.playerHats);
			Debug.Log((object)$"[MonAmiral] Instanciating PlayerCustomizationDummy hats as children of {__instance} / {val}.");
			for (int i = 0; i < customHats.Count; i++)
			{
				HatEntry hatEntry = customHats[i];
				GameObject obj = Object.Instantiate<GameObject>(hatEntry.Prefab, val.position, val.rotation, val);
				SetLayerRecursively(obj.transform, ((Component)val).gameObject.layer);
				Renderer componentInChildren = obj.GetComponentInChildren<Renderer>();
				((Component)componentInChildren).gameObject.SetActive(false);
				for (int j = 0; j < componentInChildren.materials.Length; j++)
				{
					componentInChildren.materials[j].shader = Shader.Find("W/Character");
				}
				Debug.Log((object)$"[MonAmiral] PlayerCustomizationDummy Hats[{__instance.refs.playerHats.Length}] = {hatEntry.Name}");
				list.Add(componentInChildren);
			}
			__instance.refs.playerHats = list.ToArray();
		}

		[HarmonyPatch(typeof(CharacterCustomization), "OnPlayerDataChange")]
		[HarmonyPostfix]
		public static void CharacterCustomizationOnPlayerDataChangePostFix(CharacterCustomization __instance, ref PersistentPlayerData playerData)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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 ((Object)(object)__instance.refs.PlayerRenderers[0] == (Object)null)
			{
				return;
			}
			int num = playerData.customizationData.currentSkin;
			if (__instance.useDebugColor)
			{
				num = __instance.debugColorIndex;
			}
			Color color = Singleton<Customization>.Instance.skins[num].color;
			for (int i = 0; i < __instance.refs.playerHats.Length; i++)
			{
				for (int j = 0; j < __instance.refs.playerHats[i].materials.Length; j++)
				{
					__instance.refs.playerHats[i].materials[j].SetColor(SkinColor, color);
				}
			}
		}

		[HarmonyPatch(typeof(PlayerCustomizationDummy), "SetPlayerColor")]
		[HarmonyPostfix]
		public static void PlayerCustomizationDummySetPlayerColorPostFix(PlayerCustomizationDummy __instance, ref int index)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			if (index > Singleton<Customization>.Instance.skins.Length)
			{
				return;
			}
			Color color = Singleton<Customization>.Instance.skins[index].color;
			for (int i = 0; i < __instance.refs.playerHats.Length; i++)
			{
				for (int j = 0; j < __instance.refs.playerHats[i].materials.Length; j++)
				{
					__instance.refs.playerHats[i].materials[j].SetColor(SkinColor, color);
				}
			}
		}
	}

	public struct HatEntry
	{
		public string Name;

		public GameObject Prefab;

		public Texture2D Icon;

		public HatEntry(string name, GameObject prefab, Texture2D icon)
		{
			Name = name;
			Prefab = prefab;
			Icon = icon;
		}
	}

	[CompilerGenerated]
	private sealed class <LoadHatsFromDisk>d__4 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		private AssetBundleCreateRequest <createRequest>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <LoadHatsFromDisk>d__4(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<createRequest>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
			{
				<>1__state = -1;
				Debug.Log((object)"[MonAmiral] Loading hats from disk.");
				string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "morecustomhats");
				Debug.Log((object)("[MonAmiral] Path to AssetBundle: " + text));
				<createRequest>5__2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(text));
				<>2__current = <createRequest>5__2;
				<>1__state = 1;
				return true;
			}
			case 1:
				<>1__state = -1;
				assetBundle = <createRequest>5__2.assetBundle;
				Debug.Log((object)"[MonAmiral] AssetBundle loaded.");
				customHats = new List<HatEntry>();
				customHats.Add(LoadHat("Chibidoki/chibidoki"));
				customHats.Add(LoadHat("Timmy/timmyrobot"));
				customHats.Add(LoadHat("Cigarette/cigarette"));
				customHats.Add(LoadHat("Cigarette/cigaretteweed"));
				customHats.Add(LoadHat("BuzzBall/buzzBall"));
				customHats.Add(LoadHat("Gecko/gecko"));
				customHats.Add(LoadHat("Fruits/applegreen"));
				customHats.Add(LoadHat("Fruits/appleyellow"));
				customHats.Add(LoadHat("Fruits/applered"));
				customHats.Add(LoadHat("Fruits/pepperred"));
				customHats.Add(LoadHat("Fruits/banana"));
				customHats.Add(LoadHat("HockeyMask/hockeymask"));
				customHats.Add(LoadHat("arrow/arrow"));
				customHats.Add(LoadHat("DUM/dum"));
				customHats.Add(LoadHat("ExtraHead/extrahead"));
				customHats.Add(LoadHat("Bombzyz/reddhmis"));
				customHats.Add(LoadHat("Bombzyz/yellowdhmis"));
				customHats.Add(LoadHat("Bombzyz/duckdhmis"));
				customHats.Add(LoadHat("Tobs/tobs"));
				customHats.Add(LoadHat("Lyn/Lyn"));
				customHats.Add(LoadHat("CurryPaws/CurryPaws"));
				customHats.Add(LoadHat("MonAmiral/MonAmiral"));
				customHats.Add(LoadHat("Deme/Deme"));
				customHats.Add(LoadHat("CharborgPlush/CharborgPlush"));
				customHats.Add(LoadHat("Chirpling/Chirpling"));
				customHats.Add(LoadHat("PocketWaifu/FaceBunnySnout"));
				customHats.Add(LoadHat("PocketWaifu/FaceMamavale"));
				customHats.Add(LoadHat("PocketWaifu/FacePixelGlasses"));
				customHats.Add(LoadHat("PocketWaifu/HatBear"));
				customHats.Add(LoadHat("PocketWaifu/HatBeer"));
				customHats.Add(LoadHat("PocketWaifu/HatBeret"));
				customHats.Add(LoadHat("PocketWaifu/HatBow"));
				customHats.Add(LoadHat("PocketWaifu/HatBread"));
				customHats.Add(LoadHat("PocketWaifu/HatBucket"));
				customHats.Add(LoadHat("PocketWaifu/HatChickEgg"));
				customHats.Add(LoadHat("PocketWaifu/HatCloak"));
				customHats.Add(LoadHat("PocketWaifu/HatCowboyLarge"));
				customHats.Add(LoadHat("PocketWaifu/HatCowboySmall"));
				customHats.Add(LoadHat("PocketWaifu/HatCrown"));
				customHats.Add(LoadHat("PocketWaifu/HatFishFearMe"));
				customHats.Add(LoadHat("PocketWaifu/HatFlower"));
				customHats.Add(LoadHat("PocketWaifu/HatHeadphones"));
				customHats.Add(LoadHat("PocketWaifu/HatPetalPal"));
				customHats.Add(LoadHat("PocketWaifu/HatPetalPalBaby"));
				customHats.Add(LoadHat("PocketWaifu/HatPirate"));
				customHats.Add(LoadHat("PocketWaifu/HatPolice"));
				customHats.Add(LoadHat("PocketWaifu/HatPombeanie"));
				customHats.Add(LoadHat("PocketWaifu/HatSummerHat"));
				customHats.Add(LoadHat("PocketWaifuHair/HairBuns"));
				customHats.Add(LoadHat("PocketWaifuHair/HairDressBuns"));
				customHats.Add(LoadHat("PocketWaifuHair/HairLong"));
				customHats.Add(LoadHat("PocketWaifuHair/HairPonytail"));
				customHats.Add(LoadHat("PocketWaifuHair/HairRacer"));
				customHats.Add(LoadHat("PocketWaifuHair/HairShort"));
				customHats.Add(LoadHat("PocketWaifuHair/HairTwinTails"));
				Debug.Log((object)"[MonAmiral] Done!");
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	public static AssetBundle assetBundle;

	public static List<HatEntry> customHats;

	public void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("monamiral.morecustomhats").PatchAll(typeof(Patcher));
		((MonoBehaviour)this).StartCoroutine(LoadHatsFromDisk());
	}

	[IteratorStateMachine(typeof(<LoadHatsFromDisk>d__4))]
	private static IEnumerator LoadHatsFromDisk()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <LoadHatsFromDisk>d__4(0);
	}

	private static void SetLayerRecursively(Transform transform, int layer)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		((Component)transform).gameObject.layer = layer;
		foreach (Transform item in transform)
		{
			SetLayerRecursively(item, layer);
		}
	}

	private static HatEntry LoadHat(string hatName)
	{
		Debug.Log((object)("[MonAmiral] Loading hat '" + hatName + "'."));
		GameObject val = assetBundle.LoadAsset<GameObject>("Assets/" + hatName + ".prefab");
		Texture2D val2 = assetBundle.LoadAsset<Texture2D>("Assets/" + hatName + ".png");
		Debug.Log((object)$"[MonAmiral] Loaded prefab {val} and texture {val2}");
		return new HatEntry(hatName, val, val2);
	}
}