using System;
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 UnityEngine;
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(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;
[BepInPlugin("com.local.RandomZombieCosmetics", "Random Zombie Cosmetics", "1.4.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
{
[HarmonyPostfix]
public static void Postfix(MushroomZombie __instance)
{
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))
{
Customization instance = Singleton<Customization>.Instance;
if (!((Object)(object)instance == (Object)null))
{
CustomizationRefs refs = customization.refs;
int viewId = (((Object)(object)((MonoBehaviourPun)__instance).photonView != (Object)null) ? ((MonoBehaviourPun)__instance).photonView.ViewID : 0);
ApplyRandomFit(refs, instance, viewId);
ApplyRandomHat(refs, instance, viewId);
ApplyRandomAccessory(refs, instance, viewId);
ApplyRandomSash(refs, instance, viewId);
ApplyRandomMedal(refs, instance, viewId);
ApplyRandomSkinColor(refs, instance, viewId);
ApplyRandomMouth(refs, instance, viewId);
}
}
}
catch (Exception ex)
{
Debug.LogError((object)("[RandomZombieCosmetics] Error applying cosmetics: " + ex));
}
}
private static void ApplyRandomFit(CustomizationRefs refs, Customization cosmetics, int viewId)
{
if ((Object)(object)refs.mainRenderer == (Object)null || (Object)(object)refs.mainRendererShadow == (Object)null || cosmetics.fits == null)
{
return;
}
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;
}
int num = list[SelectIndex(viewId, list.Count, 7919)];
CustomizationOption val2 = cosmetics.fits[num];
refs.mainRenderer.sharedMesh = val2.fitMesh;
refs.mainRendererShadow.sharedMesh = val2.fitMesh;
List<Material> list2 = new List<Material>();
list2.Add(((Renderer)refs.mainRenderer).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;
}
}
}
Debug.Log((object)("[RandomZombieCosmetics] Applied fit index " + num + ", ViewID: " + viewId));
}
private static void ApplyRandomHat(CustomizationRefs refs, Customization cosmetics, int viewId)
{
//IL_0248: Unknown result type (might be due to invalid IL or missing references)
//IL_0254: Unknown result type (might be due to invalid IL or missing references)
//IL_0271: 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 (cosmetics.fits != null)
{
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)
{
int num = list[SelectIndex(viewId, list.Count, 7919)];
if (cosmetics.fits[num].overrideHat)
{
Debug.Log((object)("[RandomZombieCosmetics] Keeping outfit override hat. ViewID: " + viewId));
return;
}
}
}
List<int> list2 = new List<int>();
for (int j = 0; j < cosmetics.hats.Length; j++)
{
if ((Object)(object)cosmetics.hats[j] != (Object)null && j < refs.playerHats.Length)
{
list2.Add(j);
}
}
if (list2.Count == 0)
{
return;
}
int num2 = list2[SelectIndex(viewId, list2.Count, 104729)];
for (int k = 0; k < refs.playerHats.Length; k++)
{
if ((Object)(object)refs.playerHats[k] != (Object)null)
{
((Component)refs.playerHats[k]).gameObject.SetActive(k == num2);
}
}
if ((Object)(object)refs.hatShadowMeshFilter != (Object)null && (Object)(object)refs.playerHats[num2] != (Object)null)
{
MeshFilter component = ((Component)refs.playerHats[num2]).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;
}
}
Debug.Log((object)("[RandomZombieCosmetics] Applied hat index " + num2 + ", ViewID: " + viewId));
}
private static void ApplyRandomAccessory(CustomizationRefs refs, Customization cosmetics, 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[SelectIndex(viewId, list.Count, 15485863)];
CustomizationOption val2 = cosmetics.accessories[num];
if (val2.isThirdEye)
{
refs.accessoryEnabled = false;
if ((Object)(object)refs.thirdEye != (Object)null)
{
refs.thirdEye.SetActive(true);
}
}
else
{
if ((Object)(object)val2.texture == (Object)null)
{
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);
}
}
Debug.Log((object)("[RandomZombieCosmetics] Applied accessory index " + num + ", ViewID: " + viewId));
}
private static void ApplyRandomSash(CustomizationRefs refs, Customization cosmetics, 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[SelectIndex(viewId, list.Count, 32452843)];
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);
Debug.Log((object)("[RandomZombieCosmetics] Applied sash index " + num2 + ", ViewID: " + viewId));
}
}
private static void ApplyRandomMedal(CustomizationRefs refs, Customization cosmetics, int viewId)
{
if (!((Object)(object)refs.medalRenderer == (Object)null) && cosmetics.medals != null && cosmetics.medals.Length > 1)
{
int num = SelectIndex(viewId, 2, 49979687);
((Component)refs.medalRenderer).gameObject.SetActive(num == 1);
Debug.Log((object)("[RandomZombieCosmetics] Applied medal state " + num + ", ViewID: " + viewId));
}
}
private static void ApplyRandomSkinColor(CustomizationRefs refs, Customization cosmetics, int viewId)
{
//IL_009e: 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_00f6: 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[SelectIndex(viewId, list.Count, 86028121)];
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);
}
}
}
Debug.Log((object)("[RandomZombieCosmetics] Applied skin color index " + num + ", ViewID: " + viewId));
}
private static void ApplyRandomMouth(CustomizationRefs refs, Customization cosmetics, 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[SelectIndex(viewId, list.Count, 982451653)];
refs.mouthRenderer.material.SetTexture(CharacterCustomization.MainTex, cosmetics.mouths[num].texture);
Debug.Log((object)("[RandomZombieCosmetics] Applied mouth index " + num + ", ViewID: " + viewId));
}
}
private static void SetActive(Renderer renderer, bool active)
{
if ((Object)(object)renderer != (Object)null)
{
((Component)renderer).gameObject.SetActive(active);
}
}
private static int SelectIndex(int viewId, int count, int salt)
{
if (count <= 0)
{
return 0;
}
uint num = (uint)((viewId * 397) ^ salt);
return (int)(num % (uint)count);
}
}