Decompiled source of RandomZombieCosmetics v1.5.1

plugins/OvrSlepr_RandomZombieCosmetics.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Photon.Pun;
using RandomZombieCosmetics;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Rendering;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RandomZombieCosmetics")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RandomZombieCosmetics")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("0a55a913-d8b1-4a99-b400-2cc9dcc06f4c")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[HarmonyPatch(typeof(MushroomZombie), "FadeInRenderers")]
public static class MushroomZombieSkeletonRevealPatch
{
	[HarmonyPostfix]
	public static void Postfix(MushroomZombie __instance)
	{
		try
		{
			if ((Object)(object)__instance == (Object)null || !__instance.isNPCZombie || __instance.isMushroomMan || (Object)(object)((MonoBehaviourPun)__instance).photonView == (Object)null || ((MonoBehaviourPun)__instance).photonView.ViewID <= 0)
			{
				return;
			}
			int viewID = ((MonoBehaviourPun)__instance).photonView.ViewID;
			Random random = new Random(viewID ^ 0x5F3759DF);
			int num = random.Next(100);
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component == (Object)null || component.refs == null || (Object)(object)component.refs.customization == (Object)null || (Object)(object)component.refs.customization.refs == (Object)null)
			{
				return;
			}
			CustomizationRefs refs = component.refs.customization.refs;
			if (num < 7)
			{
				if ((Object)(object)refs.skeletonRenderer != (Object)null && (Object)(object)refs.skeletonThirdPerson != (Object)null && (Object)(object)refs.skeletonThirdPersonMat != (Object)null)
				{
					refs.SetSkeleton(true, false);
					if ((Object)(object)((Component)component).GetComponent<SkeletonZombieMarker>() == (Object)null)
					{
						((Component)component).gameObject.AddComponent<SkeletonZombieMarker>();
					}
				}
			}
			else if (num < 17)
			{
				MushroomZombieCosmeticsPatch.ApplyChubby(component, __instance, refs, viewID);
			}
			else if (num < 22)
			{
				MushroomZombieCosmeticsPatch.ApplyChicken(component, viewID);
				MushroomZombieColorizer.Apply(__instance, viewID);
				MushroomZombieCosmeticsPatch.HideChickenHumanParts(refs);
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[RandomZombieCosmetics] Error restoring zombie variant: " + ex));
		}
	}
}
[HarmonyPatch(typeof(CharacterInteractible), "IsCannibal")]
public static class ChickenZombieCannibalPatch
{
	[HarmonyPostfix]
	private static void Postfix(CharacterInteractible __instance, ref bool __result)
	{
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.character == (Object)null) && (Object)(object)((Component)__instance.character).GetComponent<ChickenZombieMarker>() != (Object)null)
		{
			__result = true;
		}
	}
}
[HarmonyPatch(typeof(CharacterInteractible), "IsInteractible")]
public static class ChickenZombieInteractPatch
{
	[HarmonyPostfix]
	private static void Postfix(CharacterInteractible __instance, ref bool __result)
	{
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.character == (Object)null) && (Object)(object)((Component)__instance.character).GetComponent<ChickenZombieMarker>() != (Object)null && __instance.character.refs != null && (Object)(object)__instance.character.refs.customization != (Object)null && __instance.character.refs.customization.isCannibalizable)
		{
			__result = true;
		}
	}
}
public class ChickenZombieMarker : MonoBehaviour
{
}
public class SkeletonZombieMarker : MonoBehaviour
{
}
public class ChubbyZombieMarker : MonoBehaviour
{
}
public static class MushroomZombieColorizer
{
	private static readonly Color32[] MushroomColors = (Color32[])(object)new Color32[6]
	{
		new Color32((byte)220, (byte)35, (byte)35, byte.MaxValue),
		new Color32((byte)45, (byte)170, (byte)65, byte.MaxValue),
		new Color32((byte)240, (byte)195, (byte)35, byte.MaxValue),
		new Color32((byte)235, (byte)115, (byte)25, byte.MaxValue),
		new Color32((byte)145, (byte)75, (byte)190, byte.MaxValue),
		new Color32((byte)45, (byte)165, (byte)190, byte.MaxValue)
	};

	private static readonly Color32[] MushroomSpotColors = (Color32[])(object)new Color32[6]
	{
		new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue),
		new Color32(byte.MaxValue, (byte)235, (byte)190, byte.MaxValue),
		new Color32(byte.MaxValue, (byte)220, (byte)70, byte.MaxValue),
		new Color32(byte.MaxValue, (byte)170, (byte)190, byte.MaxValue),
		new Color32((byte)190, (byte)130, (byte)80, byte.MaxValue),
		new Color32((byte)190, (byte)220, byte.MaxValue, byte.MaxValue)
	};

	public static void Apply(MushroomZombie zombie, int seed)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)zombie == (Object)null || zombie.mushroomVisuals == null)
		{
			return;
		}
		Random random = new Random(seed);
		MaterialPropertyBlock val = new MaterialPropertyBlock();
		int num = 0;
		Color val4 = default(Color);
		foreach (GameObject mushroomVisual in zombie.mushroomVisuals)
		{
			if ((Object)(object)mushroomVisual == (Object)null)
			{
				continue;
			}
			Renderer[] componentsInChildren = mushroomVisual.GetComponentsInChildren<Renderer>(true);
			if (componentsInChildren.Length == 0)
			{
				continue;
			}
			Color32 val2 = MushroomColors[random.Next(MushroomColors.Length)];
			Color32 val3 = MushroomSpotColors[random.Next(MushroomSpotColors.Length)];
			((Color)(ref val4))..ctor((float)(int)val3.r / 255f, (float)(int)val3.g / 255f, (float)(int)val3.b / 255f, 0.341f);
			Renderer[] array = componentsInChildren;
			foreach (Renderer val5 in array)
			{
				if (!((Object)(object)val5 == (Object)null))
				{
					Material[] sharedMaterials = val5.sharedMaterials;
					Material[] array2 = sharedMaterials;
					foreach (Material material in array2)
					{
						InspectShader(material);
					}
					val5.GetPropertyBlock(val);
					val.SetColor("_BaseColor", Color32.op_Implicit(val2));
					val.SetColor("_Color3", val4);
					val5.SetPropertyBlock(val);
				}
			}
			num++;
		}
	}

	private unsafe static int ColorMeshIslands(Mesh mesh, Random random)
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: 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_0086: Invalid comparison between Unknown and I4
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Invalid comparison between Unknown and I4
		//IL_0180: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)mesh == (Object)null || mesh.vertexCount == 0)
		{
			return 0;
		}
		int vertexCount = mesh.vertexCount;
		int[] array = new int[vertexCount];
		bool[] array2 = new bool[vertexCount];
		for (int i = 0; i < vertexCount; i++)
		{
			array[i] = i;
		}
		int num = 0;
		MeshDataArray val = Mesh.AcquireReadOnlyMeshData(mesh);
		try
		{
			MeshData val2 = ((MeshDataArray)(ref val))[0];
			for (int j = 0; j < mesh.subMeshCount; j++)
			{
				SubMeshDescriptor subMesh = ((MeshData)(ref val2)).GetSubMesh(j);
				if ((int)((SubMeshDescriptor)(ref subMesh)).topology > 0)
				{
					continue;
				}
				int indexStart = ((SubMeshDescriptor)(ref subMesh)).indexStart;
				int indexCount = ((SubMeshDescriptor)(ref subMesh)).indexCount;
				int baseVertex = ((SubMeshDescriptor)(ref subMesh)).baseVertex;
				if ((int)mesh.indexFormat == 0)
				{
					NativeArray<ushort> indexData = ((MeshData)(ref val2)).GetIndexData<ushort>();
					for (int k = 0; k + 2 < indexCount; k += 3)
					{
						int num2 = indexData[indexStart + k] + baseVertex;
						int num3 = indexData[indexStart + k + 1] + baseVertex;
						int num4 = indexData[indexStart + k + 2] + baseVertex;
						if (num2 >= 0 && num3 >= 0 && num4 >= 0 && num2 < vertexCount && num3 < vertexCount && num4 < vertexCount)
						{
							Union(array, num2, num3);
							Union(array, num3, num4);
							array2[num2] = true;
							array2[num3] = true;
							array2[num4] = true;
							num++;
						}
					}
					continue;
				}
				NativeArray<uint> indexData2 = ((MeshData)(ref val2)).GetIndexData<uint>();
				for (int l = 0; l + 2 < indexCount; l += 3)
				{
					long num5 = indexData2[indexStart + l] + baseVertex;
					long num6 = indexData2[indexStart + l + 1] + baseVertex;
					long num7 = indexData2[indexStart + l + 2] + baseVertex;
					if (num5 >= 0 && num6 >= 0 && num7 >= 0 && num5 < vertexCount && num6 < vertexCount && num7 < vertexCount)
					{
						int num8 = (int)num5;
						int num9 = (int)num6;
						int num10 = (int)num7;
						Union(array, num8, num9);
						Union(array, num9, num10);
						array2[num8] = true;
						array2[num9] = true;
						array2[num10] = true;
						num++;
					}
				}
			}
		}
		finally
		{
			((IDisposable)(*(MeshDataArray*)(&val))/*cast due to .constrained prefix*/).Dispose();
		}
		if (num == 0)
		{
			Debug.LogWarning((object)("[MushroomZombieColorizer] No readable triangle data: " + ((Object)mesh).name));
			return 0;
		}
		HashSet<int> hashSet = new HashSet<int>();
		for (int m = 0; m < vertexCount; m++)
		{
			if (array2[m])
			{
				hashSet.Add(Find(array, m));
			}
		}
		return hashSet.Count;
	}

	private static int Find(int[] parents, int x)
	{
		if (parents[x] != x)
		{
			parents[x] = Find(parents, parents[x]);
		}
		return parents[x];
	}

	private static void Union(int[] parents, int a, int b)
	{
		int num = Find(parents, a);
		int num2 = Find(parents, b);
		if (num != num2)
		{
			parents[num2] = num;
		}
	}

	private static void InspectShader(Material material)
	{
		//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_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Invalid comparison between Unknown and I4
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Invalid comparison between Unknown and I4
		//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)
		if ((Object)(object)material == (Object)null || (Object)(object)material.shader == (Object)null)
		{
			return;
		}
		Shader shader = material.shader;
		for (int i = 0; i < shader.GetPropertyCount(); i++)
		{
			string propertyName = shader.GetPropertyName(i);
			ShaderPropertyType propertyType = shader.GetPropertyType(i);
			string text = "";
			if ((int)propertyType == 0)
			{
				text = " | Color=" + ((object)material.GetColor(propertyName)/*cast due to .constrained prefix*/).ToString();
			}
			else if ((int)propertyType == 4)
			{
				Texture texture = material.GetTexture(propertyName);
				text = " | Texture=" + (((Object)(object)texture != (Object)null) ? ((Object)texture).name : "NULL");
			}
		}
	}
}
[HarmonyPatch(typeof(CharacterCustomization), "OnPlayerDataChange")]
public static class ZombieBadgesOnCustomizationPatch
{
	[HarmonyPostfix]
	public static void Postfix(CharacterCustomization __instance)
	{
		if (!((Object)(object)__instance == (Object)null))
		{
			MushroomZombie val = ((Component)__instance).GetComponent<MushroomZombie>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)__instance).GetComponentInParent<MushroomZombie>();
			}
			if ((Object)(object)val != (Object)null)
			{
				ZombieRandomBadges.Apply(val);
			}
		}
	}
}
[HarmonyPatch(typeof(MushroomZombie), "FadeInRenderers")]
public static class ZombieBadgesOnRevealPatch
{
	[HarmonyPostfix]
	public static void Postfix(MushroomZombie __instance)
	{
		if ((Object)(object)__instance != (Object)null)
		{
			ZombieRandomBadges.Apply(__instance);
		}
	}
}
public class ZombieRandomBadges : MonoBehaviour
{
	private Texture2D badgeTexture;

	private int cachedViewId = -1;

	public static void Apply(MushroomZombie zombie)
	{
		if ((Object)(object)zombie == (Object)null)
		{
			return;
		}
		Character component = ((Component)zombie).GetComponent<Character>();
		if ((Object)(object)component == (Object)null || component.refs == null || (Object)(object)component.refs.customization == (Object)null || (Object)(object)component.refs.customization.refs == (Object)null)
		{
			return;
		}
		Renderer sashRenderer = component.refs.customization.refs.sashRenderer;
		if ((Object)(object)sashRenderer == (Object)null || sashRenderer.materials.Length == 0)
		{
			return;
		}
		PhotonView component2 = ((Component)zombie).GetComponent<PhotonView>();
		if (!((Object)(object)component2 == (Object)null) && component2.ViewID != 0)
		{
			ZombieRandomBadges zombieRandomBadges = ((Component)zombie).GetComponent<ZombieRandomBadges>();
			if ((Object)(object)zombieRandomBadges == (Object)null)
			{
				zombieRandomBadges = ((Component)zombie).gameObject.AddComponent<ZombieRandomBadges>();
			}
			if ((Object)(object)zombieRandomBadges.badgeTexture == (Object)null || zombieRandomBadges.cachedViewId != component2.ViewID)
			{
				zombieRandomBadges.CreateTexture(component2.ViewID);
			}
			if ((Object)(object)zombieRandomBadges.badgeTexture != (Object)null)
			{
				sashRenderer.materials[0].SetTexture("BadgeUnlockTexture", (Texture)(object)zombieRandomBadges.badgeTexture);
			}
		}
	}

	private void CreateTexture(int viewId)
	{
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Expected O, but got Unknown
		//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)badgeTexture != (Object)null)
		{
			Object.Destroy((Object)(object)badgeTexture);
		}
		cachedViewId = viewId;
		if ((Object)(object)GUIManager.instance == (Object)null || (Object)(object)GUIManager.instance.mainBadgeManager == (Object)null || GUIManager.instance.mainBadgeManager.badgeData == null)
		{
			return;
		}
		BadgeData[] badgeData = GUIManager.instance.mainBadgeManager.badgeData;
		int num = badgeData.Length;
		if (num <= 0)
		{
			return;
		}
		List<int> list = new List<int>();
		for (int i = 0; i < num; i++)
		{
			int num2 = (((Object)(object)badgeData[i] != (Object)null) ? badgeData[i].visualID : i);
			if (num2 >= 0 && num2 < num && !list.Contains(num2))
			{
				list.Add(num2);
			}
		}
		if (list.Count != 0)
		{
			Random random = new Random(viewId);
			int num3 = Math.Min(64, list.Count);
			int num4 = random.Next(1, num3 + 1);
			for (int num5 = list.Count - 1; num5 > 0; num5--)
			{
				int index = random.Next(num5 + 1);
				int value = list[num5];
				list[num5] = list[index];
				list[index] = value;
			}
			badgeTexture = new Texture2D(num, 1, (TextureFormat)4, false);
			((Object)badgeTexture).name = "ZombieRandomBadges_" + viewId;
			((Texture)badgeTexture).filterMode = (FilterMode)0;
			((Texture)badgeTexture).wrapMode = (TextureWrapMode)1;
			for (int j = 0; j < num; j++)
			{
				badgeTexture.SetPixel(j, 0, Color.black);
			}
			for (int k = 0; k < num4; k++)
			{
				badgeTexture.SetPixel(list[k], 0, Color.white);
			}
			badgeTexture.Apply(false, false);
		}
	}

	private void OnDestroy()
	{
		if ((Object)(object)badgeTexture != (Object)null)
		{
			Object.Destroy((Object)(object)badgeTexture);
		}
	}
}
namespace RandomZombieCosmetics;

[HarmonyPatch(typeof(MushroomZombieBiteCollider), "OnTriggerEnter")]
public static class ZombieBiteCollider
{
	private const float HungerTotal = 0.45f;

	private const float HungerDuration = 60f;

	private static Coroutine mushroomStaminaCoroutine;

	private static readonly FieldInfo ParentZombieField = AccessTools.Field(typeof(MushroomZombieBiteCollider), "parentZombie");

	private static readonly FieldInfo LastBiteField = AccessTools.Field(typeof(MushroomZombieBiteCollider), "lastBitLocalCharacter");

	private static readonly MethodInfo CharacterFallMethod = AccessTools.Method(typeof(Character), "Fall", new Type[2]
	{
		typeof(float),
		typeof(float)
	}, (Type[])null);

	[HarmonyPrefix]
	private static bool Prefix(MushroomZombieBiteCollider __instance, Collider other)
	{
		if ((Object)(object)__instance == (Object)null || (Object)(object)other == (Object)null)
		{
			return true;
		}
		if (ParentZombieField == null || LastBiteField == null)
		{
			return true;
		}
		object? value = ParentZombieField.GetValue(__instance);
		MushroomZombie val = (MushroomZombie)((value is MushroomZombie) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return true;
		}
		Character component = ((Component)val).GetComponent<Character>();
		if ((Object)(object)component == (Object)null)
		{
			return true;
		}
		bool flag = (Object)(object)((Component)component).GetComponent<ChickenZombieMarker>() != (Object)null;
		bool flag2 = (Object)(object)((Component)component).GetComponent<SkeletonZombieMarker>() != (Object)null;
		bool flag3 = (Object)(object)((Component)component).GetComponent<ChubbyZombieMarker>() != (Object)null;
		if (!flag && !flag2 && !flag3)
		{
			return true;
		}
		float num = (float)LastBiteField.GetValue(__instance);
		if (Time.time - num < 5f)
		{
			return false;
		}
		Character val2 = default(Character);
		if (!CharacterRagdoll.TryGetCharacterFromCollider(other, ref val2) || !val2.IsLocal)
		{
			return false;
		}
		LastBiteField.SetValue(__instance, Time.time);
		if (val2.refs == null || (Object)(object)val2.refs.afflictions == (Object)null)
		{
			return false;
		}
		if (flag)
		{
			val2.refs.afflictions.AddStatus((STATUSTYPE)10, 0.05f, false, true, true, false, false);
			val2.refs.afflictions.AddStatus((STATUSTYPE)3, 0.2f, false, true, true, false, false);
			((MonoBehaviour)val2).StartCoroutine(ApplyHunger(val2));
		}
		else if (flag2)
		{
			val2.refs.afflictions.AddStatus((STATUSTYPE)5, 0.05f, false, true, true, false, false);
			val2.refs.afflictions.AddStatus((STATUSTYPE)10, 0.1f, false, true, true, false, false);
			((MonoBehaviour)val2).StartCoroutine(ApplySkeletonInjury(val2, val.biteDelayBeforeSpores, 35f));
		}
		else if (flag3)
		{
			val2.refs.afflictions.AddStatus((STATUSTYPE)1, 0.05f, false, true, true, false, false);
			val2.refs.afflictions.AddStatus((STATUSTYPE)3, 0.1f, false, true, true, false, false);
			val2.refs.afflictions.AddStatus((STATUSTYPE)0, 0.1f, false, true, true, false, false);
			((MonoBehaviour)val2).StartCoroutine(ApplyChubbySpores(val2));
			StartMushroomStamina();
		}
		CharacterFallMethod?.Invoke(val2, new object[2] { val.biteStunTime, 0f });
		val.OnBitCharacter(val2);
		return false;
	}

	private static IEnumerator ApplyChubbySpores(Character character)
	{
		float applied = 0f;
		float rate = 0.009166667f;
		while (applied < 0.55f && !((Object)(object)character == (Object)null) && character.refs != null && !((Object)(object)character.refs.afflictions == (Object)null))
		{
			float amount = Mathf.Min(rate * Time.deltaTime, 0.55f - applied);
			character.refs.afflictions.AddStatus((STATUSTYPE)10, amount, false, true, true, false, false);
			applied += amount;
			yield return null;
		}
	}

	private static void StartMushroomStamina()
	{
		if (!((Object)(object)GUIManager.instance == (Object)null))
		{
			GUIManager.instance.StartNumb();
			if (mushroomStaminaCoroutine != null)
			{
				((MonoBehaviour)GUIManager.instance).StopCoroutine(mushroomStaminaCoroutine);
				mushroomStaminaCoroutine = null;
			}
			mushroomStaminaCoroutine = ((MonoBehaviour)GUIManager.instance).StartCoroutine(StopMushroomStaminaAfterDelay());
		}
	}

	private static IEnumerator StopMushroomStaminaAfterDelay()
	{
		yield return (object)new WaitForSeconds(120f);
		if ((Object)(object)GUIManager.instance != (Object)null)
		{
			GUIManager.instance.StopNumb();
		}
		mushroomStaminaCoroutine = null;
	}

	private static IEnumerator ApplyHunger(Character character)
	{
		float applied = 0f;
		float rate = 0.0075f;
		while (applied < 0.45f && !((Object)(object)character == (Object)null) && character.refs != null && !((Object)(object)character.refs.afflictions == (Object)null))
		{
			float amount = Mathf.Min(rate * Time.deltaTime, 0.45f - applied);
			character.refs.afflictions.AddStatus((STATUSTYPE)1, amount, false, true, true, false, false);
			applied += amount;
			yield return null;
		}
	}

	private static IEnumerator ApplySkeletonInjury(Character character, float delay, float duration)
	{
		if (delay > 0f)
		{
			yield return (object)new WaitForSeconds(delay);
		}
		if (duration <= 0f)
		{
			duration = 35f;
		}
		float applied = 0f;
		float rate = 0.15f / duration;
		while (applied < 0.15f && !((Object)(object)character == (Object)null) && character.refs != null && !((Object)(object)character.refs.afflictions == (Object)null))
		{
			float amount = Mathf.Min(rate * Time.deltaTime, 0.15f - applied);
			character.refs.afflictions.AddStatus((STATUSTYPE)0, amount, false, true, true, false, false);
			applied += amount;
			yield return null;
		}
	}
}
public static class ChickenZombieEatingState
{
	public static readonly HashSet<CharacterAfflictions> Eating = new HashSet<CharacterAfflictions>();
}
[HarmonyPatch(typeof(CharacterInteractible), "GetEaten")]
public static class ChickenZombieEatingPatch
{
	[HarmonyPrefix]
	private static void Prefix(CharacterInteractible __instance, Character eater, ref CharacterAfflictions __state)
	{
		__state = null;
		if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.character == (Object)null) && !((Object)(object)eater == (Object)null) && eater.IsLocal && eater.refs != null && !((Object)(object)eater.refs.afflictions == (Object)null) && !((Object)(object)((Component)__instance.character).GetComponent<ChickenZombieMarker>() == (Object)null))
		{
			__state = eater.refs.afflictions;
			ChickenZombieEatingState.Eating.Add(__state);
		}
	}

	[HarmonyPostfix]
	private static void Postfix(CharacterAfflictions __state)
	{
		if (!((Object)(object)__state == (Object)null))
		{
			__state.AddStatus((STATUSTYPE)3, 0.3f, false, true, true, false, false);
			__state.AddStatus((STATUSTYPE)10, 0.2f, false, true, true, false, false);
		}
	}

	[HarmonyFinalizer]
	private static Exception Finalizer(CharacterAfflictions __state, Exception __exception)
	{
		if ((Object)(object)__state != (Object)null)
		{
			ChickenZombieEatingState.Eating.Remove(__state);
		}
		return __exception;
	}
}
[HarmonyPatch(typeof(CharacterAfflictions), "AddStatus")]
public static class ChickenZombieCursePatch
{
	[HarmonyPrefix]
	private static bool Prefix(CharacterAfflictions __instance, STATUSTYPE statusType)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Invalid comparison between Unknown and I4
		if ((int)statusType != 5)
		{
			return true;
		}
		return !ChickenZombieEatingState.Eating.Contains(__instance);
	}
}
[BepInPlugin("com.local.RandomZombieCosmetics", "Random Zombie Cosmetics", "1.5.0")]
public class RandomZombieCosmeticsPlugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("com.local.RandomZombieCosmetics");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Random Zombie Cosmetics loaded!");
	}
}
[HarmonyPatch(typeof(MushroomZombie), "RPC_SetOutfit")]
public static class MushroomZombieCosmeticsPatch
{
	public const int ChubbyChance = 10;

	public const int SkeletonChance = 7;

	public const int ChickenChance = 5;

	private static Coroutine mushroomStaminaCoroutine;

	[HarmonyPostfix]
	public static void Postfix(MushroomZombie __instance)
	{
		if ((Object)(object)__instance == (Object)null)
		{
			return;
		}
		try
		{
			if ((Object)(object)__instance == (Object)null || !__instance.isNPCZombie)
			{
				return;
			}
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component == (Object)null || component.refs == null)
			{
				return;
			}
			CharacterCustomization customization = component.refs.customization;
			if ((Object)(object)customization == (Object)null || (Object)(object)customization.refs == (Object)null)
			{
				return;
			}
			Customization instance = Singleton<Customization>.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			CustomizationRefs refs = customization.refs;
			if ((Object)(object)((MonoBehaviourPun)__instance).photonView == (Object)null || ((MonoBehaviourPun)__instance).photonView.ViewID <= 0 || __instance.isMushroomMan)
			{
				return;
			}
			int viewID = ((MonoBehaviourPun)__instance).photonView.ViewID;
			Random random = new Random(viewID ^ 0x5F3759DF);
			int num = random.Next(100);
			if (num < 7)
			{
				if ((Object)(object)refs.skeletonRenderer != (Object)null && (Object)(object)refs.skeletonThirdPerson != (Object)null && (Object)(object)refs.skeletonThirdPersonMat != (Object)null)
				{
					refs.SetSkeleton(true, false);
					if ((Object)(object)((Component)component).GetComponent<SkeletonZombieMarker>() == (Object)null)
					{
						((Component)component).gameObject.AddComponent<SkeletonZombieMarker>();
					}
					AccessTools.Method(typeof(MushroomZombie), "ClearMushroomVisuals", (Type[])null, (Type[])null)?.Invoke(__instance, null);
				}
			}
			else if (num < 17)
			{
				ApplyChubby(component, __instance, refs, viewID);
			}
			else if (num < 22)
			{
				ApplyChicken(component, viewID);
			}
			else
			{
				Random random2 = new Random(viewID);
				int fitIndex = ApplyRandomFit(refs, instance, random2, viewID);
				ApplyRandomHat(refs, instance, random2, viewID, fitIndex);
				ApplyRandomAccessory(refs, instance, random2, viewID);
				ApplyRandomSash(refs, instance, random2, viewID);
				ApplyRandomMedal(refs, instance, random2, viewID);
				ApplyRandomSkinColor(refs, instance, random2, viewID);
				ApplyRandomMouth(refs, instance, random2, viewID);
				MushroomZombieColorizer.Apply(__instance, viewID);
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[RandomZombieCosmetics] Error applying cosmetics: " + ex));
		}
	}

	public static void HideChickenHumanParts(CustomizationRefs refs)
	{
		if (!((Object)(object)refs == (Object)null))
		{
			SetRendererEnabled((Renderer)(object)refs.mainRenderer, enabled: false);
			SetRendererEnabled((Renderer)(object)refs.mainRendererShadow, enabled: false);
			SetRendererEnabled(refs.headShadow, enabled: false);
			SetRenderersEnabled(refs.PlayerRenderers, enabled: false);
			SetRenderersEnabled(refs.EyeRenderers, enabled: false);
			SetRendererEnabled(refs.mouthRenderer, enabled: false);
			SetRendererEnabled(refs.accessoryRenderer, enabled: false);
			if ((Object)(object)refs.thirdEye != (Object)null)
			{
				refs.thirdEye.SetActive(false);
			}
			SetRendererEnabled(refs.sashRenderer, enabled: false);
			SetRendererEnabled(refs.medalRenderer, enabled: false);
			SetRendererEnabled(refs.shorts, enabled: false);
			SetRendererEnabled(refs.skirt, enabled: false);
			SetRendererEnabled(refs.shortsShadow, enabled: false);
			SetRendererEnabled(refs.skirtShadow, enabled: false);
			SetRenderersEnabled(refs.playerHats, enabled: false);
			SetRendererEnabled(refs.chickenRenderer, enabled: true);
		}
	}

	private static void StartMushroomStamina()
	{
		if (!((Object)(object)GUIManager.instance == (Object)null))
		{
			GUIManager.instance.StartNumb();
			if (mushroomStaminaCoroutine != null)
			{
				((MonoBehaviour)GUIManager.instance).StopCoroutine(mushroomStaminaCoroutine);
				mushroomStaminaCoroutine = null;
			}
			mushroomStaminaCoroutine = ((MonoBehaviour)GUIManager.instance).StartCoroutine(StopMushroomStaminaAfterDelay());
		}
	}

	private static IEnumerator StopMushroomStaminaAfterDelay()
	{
		yield return (object)new WaitForSeconds(120f);
		if ((Object)(object)GUIManager.instance != (Object)null)
		{
			GUIManager.instance.StopNumb();
		}
		mushroomStaminaCoroutine = null;
	}

	private static void SetRenderersEnabled(Renderer[] renderers, bool enabled)
	{
		if (renderers != null)
		{
			foreach (Renderer renderer in renderers)
			{
				SetRendererEnabled(renderer, enabled);
			}
		}
	}

	private static void SetRendererEnabled(Renderer renderer, bool enabled)
	{
		if ((Object)(object)renderer != (Object)null)
		{
			renderer.enabled = enabled;
		}
	}

	public static void ApplyChicken(Character character, int viewId)
	{
		if (!((Object)(object)character == (Object)null) && character.refs != null && !((Object)(object)character.refs.customization == (Object)null))
		{
			character.refs.customization.BecomeChicken();
			if ((Object)(object)((Component)character).GetComponent<ChickenZombieMarker>() == (Object)null)
			{
				((Component)character).gameObject.AddComponent<ChickenZombieMarker>();
			}
			MushroomZombie component = ((Component)character).GetComponent<MushroomZombie>();
			if ((Object)(object)component != (Object)null)
			{
				AccessTools.Method(typeof(MushroomZombie), "ClearMushroomVisuals", (Type[])null, (Type[])null)?.Invoke(component, null);
			}
		}
	}

	public static void ApplyChubby(Character character, MushroomZombie zombie, CustomizationRefs refs, int viewId)
	{
		if ((Object)(object)character == (Object)null || (Object)(object)zombie == (Object)null || (Object)(object)refs == (Object)null)
		{
			return;
		}
		if ((Object)(object)refs.mushroomManMesh == (Object)null || (Object)(object)refs.mushroomManMaterial == (Object)null)
		{
			Debug.LogWarning((object)("[RandomZombieCosmetics] Mushroom Chubby model or material is missing! ViewID: " + viewId));
			return;
		}
		refs.SetMushroomMan(true);
		if ((Object)(object)((Component)character).GetComponent<ChubbyZombieMarker>() == (Object)null)
		{
			((Component)character).gameObject.AddComponent<ChubbyZombieMarker>();
		}
		object? obj = AccessTools.Field(typeof(MushroomZombie), "animatorMushroomMan")?.GetValue(zombie);
		RuntimeAnimatorController val = (RuntimeAnimatorController)((obj is RuntimeAnimatorController) ? obj : null);
		if ((Object)(object)val != (Object)null)
		{
			character.refs.animator.runtimeAnimatorController = val;
		}
		AccessTools.Method(typeof(MushroomZombie), "ClearMushroomVisuals", (Type[])null, (Type[])null)?.Invoke(zombie, null);
		if (!((Object)(object)refs.skeletonRenderer != (Object)null))
		{
			return;
		}
		Material[] materials = refs.skeletonRenderer.materials;
		foreach (Material val2 in materials)
		{
			if (!((Object)(object)val2 == (Object)null))
			{
				val2.SetFloat("_Opacity", 1f);
			}
		}
	}

	private static int ApplyRandomFit(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		if ((Object)(object)refs.mainRenderer == (Object)null || (Object)(object)refs.mainRendererShadow == (Object)null || cosmetics.fits == null)
		{
			return -1;
		}
		List<int> list = new List<int>();
		for (int i = 0; i < cosmetics.fits.Length; i++)
		{
			CustomizationOption val = cosmetics.fits[i];
			if ((Object)(object)val != (Object)null && (Object)(object)val.fitMesh != (Object)null && (Object)(object)val.fitMaterial != (Object)null && (Object)(object)val.fitMaterialShoes != (Object)null)
			{
				list.Add(i);
			}
		}
		if (list.Count == 0)
		{
			return -1;
		}
		int num = list[random.Next(list.Count)];
		CustomizationOption val2 = cosmetics.fits[num];
		refs.mainRenderer.sharedMesh = val2.fitMesh;
		refs.mainRendererShadow.sharedMesh = val2.fitMesh;
		List<Material> list2 = new List<Material>();
		Material[] materials = ((Renderer)refs.mainRenderer).materials;
		if (materials != null && materials.Length != 0)
		{
			list2.Add(materials[0]);
		}
		list2.Add(val2.fitMaterial);
		list2.Add(val2.fitMaterialShoes);
		((Renderer)refs.mainRenderer).SetSharedMaterials(list2);
		if (val2.noPants)
		{
			SetActive(refs.skirt, active: false);
			SetActive(refs.shorts, active: false);
			SetActive(refs.skirtShadow, active: false);
			SetActive(refs.shortsShadow, active: false);
		}
		else if (val2.isSkirt)
		{
			SetActive(refs.skirt, active: true);
			SetActive(refs.shorts, active: false);
			SetActive(refs.skirtShadow, active: true);
			SetActive(refs.shortsShadow, active: false);
			if ((Object)(object)refs.skirt != (Object)null)
			{
				refs.skirt.sharedMaterial = val2.fitPantsMaterial;
			}
		}
		else
		{
			SetActive(refs.skirt, active: false);
			SetActive(refs.shorts, active: true);
			SetActive(refs.skirtShadow, active: false);
			SetActive(refs.shortsShadow, active: true);
			if ((Object)(object)refs.shorts != (Object)null)
			{
				refs.shorts.sharedMaterial = val2.fitPantsMaterial;
			}
		}
		if (refs.playerHats != null)
		{
			for (int j = 0; j < refs.playerHats.Length && j < 2; j++)
			{
				if ((Object)(object)refs.playerHats[j] != (Object)null)
				{
					refs.playerHats[j].material = val2.fitHatMaterial;
				}
			}
		}
		return num;
	}

	private static void ApplyRandomHat(CustomizationRefs refs, Customization cosmetics, Random random, int viewId, int fitIndex)
	{
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		if (cosmetics.hats == null || refs.playerHats == null || refs.playerHats.Length == 0)
		{
			return;
		}
		if (fitIndex >= 0 && cosmetics.fits != null && fitIndex < cosmetics.fits.Length)
		{
			CustomizationOption val = cosmetics.fits[fitIndex];
			if ((Object)(object)val != (Object)null && val.overrideHat)
			{
				return;
			}
		}
		List<int> list = new List<int>();
		for (int i = 0; i < cosmetics.hats.Length; i++)
		{
			if ((Object)(object)cosmetics.hats[i] != (Object)null && i < refs.playerHats.Length && (Object)(object)refs.playerHats[i] != (Object)null)
			{
				list.Add(i);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		int num = list[random.Next(list.Count)];
		for (int j = 0; j < refs.playerHats.Length; j++)
		{
			if ((Object)(object)refs.playerHats[j] != (Object)null)
			{
				((Component)refs.playerHats[j]).gameObject.SetActive(j == num);
			}
		}
		if ((Object)(object)refs.hatShadowMeshFilter != (Object)null && (Object)(object)refs.playerHats[num] != (Object)null)
		{
			MeshFilter component = ((Component)refs.playerHats[num]).GetComponent<MeshFilter>();
			if ((Object)(object)component != (Object)null)
			{
				refs.hatShadowMeshFilter.sharedMesh = component.sharedMesh;
				((Component)refs.hatShadowMeshFilter).transform.SetPositionAndRotation(((Component)component).transform.position, ((Component)component).transform.rotation);
				((Component)refs.hatShadowMeshFilter).transform.localScale = ((Component)component).transform.localScale;
			}
		}
	}

	private static void ApplyRandomAccessory(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		if (cosmetics.accessories == null || (Object)(object)refs.accessoryRenderer == (Object)null)
		{
			return;
		}
		List<int> list = new List<int>();
		for (int i = 0; i < cosmetics.accessories.Length; i++)
		{
			CustomizationOption val = cosmetics.accessories[i];
			if (!((Object)(object)val == (Object)null) && (val.isThirdEye || (Object)(object)val.texture != (Object)null))
			{
				list.Add(i);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		int num = list[random.Next(list.Count)];
		CustomizationOption val2 = cosmetics.accessories[num];
		if (val2.isThirdEye)
		{
			refs.accessoryEnabled = false;
			if ((Object)(object)refs.thirdEye != (Object)null)
			{
				refs.thirdEye.SetActive(true);
			}
			return;
		}
		refs.accessoryRenderer.material.SetTexture(CharacterCustomization.MainTex, val2.texture);
		refs.accessoryRenderer.material.renderQueue = (val2.drawUnderEye ? 3007 : 3009);
		refs.accessoryEnabled = true;
		if ((Object)(object)refs.thirdEye != (Object)null)
		{
			refs.thirdEye.SetActive(false);
		}
	}

	private static void ApplyRandomSash(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		if ((Object)(object)refs.sashRenderer == (Object)null || refs.sashAscentMaterials == null || refs.sashAscentMaterials.Length == 0 || cosmetics.sashes == null)
		{
			return;
		}
		List<int> list = new List<int>();
		int num = Math.Min(cosmetics.sashes.Length, refs.sashAscentMaterials.Length);
		for (int i = 0; i < num; i++)
		{
			if ((Object)(object)cosmetics.sashes[i] != (Object)null && (Object)(object)refs.sashAscentMaterials[i] != (Object)null)
			{
				list.Add(i);
			}
		}
		if (list.Count != 0)
		{
			int num2 = list[random.Next(list.Count)];
			List<Material> list2 = new List<Material>();
			Material[] materials = refs.sashRenderer.materials;
			if (materials != null && materials.Length != 0)
			{
				list2.Add(materials[0]);
			}
			list2.Add(refs.sashAscentMaterials[num2]);
			refs.sashRenderer.SetMaterials(list2);
		}
	}

	private static void ApplyRandomMedal(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		if (!((Object)(object)refs.medalRenderer == (Object)null) && cosmetics.medals != null && cosmetics.medals.Length > 1)
		{
			int num = random.Next(2);
			((Component)refs.medalRenderer).gameObject.SetActive(num == 1);
		}
	}

	private static void ApplyRandomSkinColor(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		if (refs.PlayerRenderers == null || cosmetics.skins == null || cosmetics.skins.Length == 0)
		{
			return;
		}
		List<int> list = new List<int>();
		for (int i = 0; i < cosmetics.skins.Length; i++)
		{
			if ((Object)(object)cosmetics.skins[i] != (Object)null)
			{
				list.Add(i);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		int num = list[random.Next(list.Count)];
		Color color = cosmetics.skins[num].color;
		Renderer[] playerRenderers = refs.PlayerRenderers;
		foreach (Renderer val in playerRenderers)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Material[] materials = val.materials;
			foreach (Material val2 in materials)
			{
				if ((Object)(object)val2 != (Object)null)
				{
					val2.SetColor("_SkinColor", color);
				}
			}
		}
	}

	private static void ApplyRandomMouth(CustomizationRefs refs, Customization cosmetics, Random random, int viewId)
	{
		if ((Object)(object)refs.mouthRenderer == (Object)null || cosmetics.mouths == null || cosmetics.mouths.Length == 0)
		{
			return;
		}
		List<int> list = new List<int>();
		for (int i = 0; i < cosmetics.mouths.Length; i++)
		{
			if ((Object)(object)cosmetics.mouths[i] != (Object)null && (Object)(object)cosmetics.mouths[i].texture != (Object)null)
			{
				list.Add(i);
			}
		}
		if (list.Count != 0)
		{
			int num = list[random.Next(list.Count)];
			refs.mouthRenderer.material.SetTexture(CharacterCustomization.MainTex, cosmetics.mouths[num].texture);
		}
	}

	private static void SetActive(Renderer renderer, bool active)
	{
		if ((Object)(object)renderer != (Object)null)
		{
			((Component)renderer).gameObject.SetActive(active);
		}
	}
}