Decompiled source of LobbySkinsFix v1.2.4

plugins/LobbySkinsFix/LobbySkinsFix.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HG;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour.HookGen;
using RoR2;
using RoR2.ContentManagement;
using RoR2.SurvivorMannequins;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.4.0")]
namespace LobbySkinsFix;

[BepInPlugin("com.KingEnderBrine.LobbySkinsFix", "Lobby Skins Fix", "1.2.4")]
public class LobbySkinsFixPlugin : BaseUnityPlugin
{
	public const string GUID = "com.KingEnderBrine.LobbySkinsFix";

	public const string Name = "Lobby Skins Fix";

	public const string Version = "1.2.4";

	private static readonly MethodInfo onNetworkUserLoadoutChanged = typeof(SurvivorMannequinSlotController).GetMethod("ApplyLoadoutToMannequinInstance", BindingFlags.Instance | BindingFlags.NonPublic);

	internal static LobbySkinsFixPlugin Instance { get; private set; }

	internal static ManualLogSource InstanceLogger => ((BaseUnityPlugin)Instance).Logger;

	private void Awake()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		Instance = this;
		HookEndpointManager.Modify((MethodBase)onNetworkUserLoadoutChanged, (Delegate)new Manipulator(ReverseSkin.RevertSkinIL));
	}

	private void Destroy()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		Instance = null;
		HookEndpointManager.Unmodify((MethodBase)onNetworkUserLoadoutChanged, (Delegate)new Manipulator(ReverseSkin.RevertSkinIL));
	}
}
public class ReverseSkin : MonoBehaviour
{
	[CompilerGenerated]
	private sealed class <Initialize>d__3 : IEnumerator<object>, IEnumerator, IDisposable
	{
		private int <>1__state;

		private object <>2__current;

		public SkinDef skinDef;

		public ReverseSkin <>4__this;

		public GameObject modelObject;

		private IEnumerator <bakeEnumerator>5__2;

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

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

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

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

		private bool MoveNext()
		{
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: 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_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: 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_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			int num = <>1__state;
			ReverseSkin reverseSkin = <>4__this;
			switch (num)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<bakeEnumerator>5__2 = skinDef.BakeAsync();
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if (<bakeEnumerator>5__2.MoveNext())
			{
				<>2__current = <bakeEnumerator>5__2.Current;
				<>1__state = 1;
				return true;
			}
			RuntimeSkin runtimeSkin = skinDef.runtimeSkin;
			reverseSkin.baseRendererInfos.AddRange(modelObject.GetComponent<CharacterModel>().baseRendererInfos);
			Enumerator<GameObjectActivationTemplate> enumerator = runtimeSkin.gameObjectActivationTemplates.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					GameObjectActivationTemplate current = enumerator.Current;
					reverseSkin.gameObjectActivationTemplates.Add(new GameObjectActivationTemplate
					{
						transformPath = current.transformPath,
						shouldActivate = !current.shouldActivate
					});
				}
			}
			finally
			{
				((IDisposable)enumerator).Dispose();
			}
			Enumerator<MeshReplacementTemplate> enumerator2 = runtimeSkin.meshReplacementTemplates.GetEnumerator();
			try
			{
				while (enumerator2.MoveNext())
				{
					MeshReplacementTemplate current2 = enumerator2.Current;
					Transform val = modelObject.transform.Find(current2.transformPath);
					if (!Object.op_Implicit((Object)(object)val))
					{
						continue;
					}
					Renderer component = ((Component)val).GetComponent<Renderer>();
					Mesh sharedMesh;
					if (!(component is MeshRenderer))
					{
						SkinnedMeshRenderer val2 = (SkinnedMeshRenderer)(object)((component is SkinnedMeshRenderer) ? component : null);
						if (val2 == null)
						{
							continue;
						}
						sharedMesh = val2.sharedMesh;
					}
					else
					{
						sharedMesh = ((Component)component).GetComponent<MeshFilter>().sharedMesh;
					}
					reverseSkin.meshReplacementTemplates.Add(new MeshReplacementTemplate
					{
						transformPath = current2.transformPath,
						meshReference = new AssetOrDirectReference<Mesh>
						{
							directRef = sharedMesh
						}
					});
				}
			}
			finally
			{
				((IDisposable)enumerator2).Dispose();
			}
			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();
		}
	}

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

		private object <>2__current;

		public int skinIndex;

		public ModelSkinController modelSkinController;

		public IEnumerator applySkinAsync;

		private IEnumerator <init>5__2;

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

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

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

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

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
			{
				<>1__state = -1;
				if ((uint)skinIndex >= modelSkinController.skins.Length)
				{
					skinIndex = 0;
				}
				if (skinIndex == modelSkinController.currentSkinIndex || modelSkinController.skins.Length == 0)
				{
					break;
				}
				ReverseSkin component = ((Component)modelSkinController).GetComponent<ReverseSkin>();
				if (Object.op_Implicit((Object)(object)component))
				{
					component.Apply(((Component)modelSkinController).gameObject);
					Object.Destroy((Object)(object)component);
				}
				ReverseSkin reverseSkin = ((Component)modelSkinController).gameObject.AddComponent<ReverseSkin>();
				<init>5__2 = reverseSkin.Initialize(((Component)modelSkinController).gameObject, modelSkinController.skins[skinIndex]);
				goto IL_00ef;
			}
			case 1:
				<>1__state = -1;
				goto IL_00ef;
			case 2:
				{
					<>1__state = -1;
					break;
				}
				IL_00ef:
				if (<init>5__2.MoveNext())
				{
					<>2__current = <init>5__2.Current;
					<>1__state = 1;
					return true;
				}
				<init>5__2 = null;
				break;
			}
			if (applySkinAsync.MoveNext())
			{
				<>2__current = applySkinAsync.Current;
				<>1__state = 2;
				return true;
			}
			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();
		}
	}

	private readonly List<RendererInfo> baseRendererInfos = new List<RendererInfo>();

	private readonly List<MeshReplacementTemplate> meshReplacementTemplates = new List<MeshReplacementTemplate>();

	private readonly List<GameObjectActivationTemplate> gameObjectActivationTemplates = new List<GameObjectActivationTemplate>();

	[IteratorStateMachine(typeof(<Initialize>d__3))]
	private IEnumerator Initialize(GameObject modelObject, SkinDef skinDef)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <Initialize>d__3(0)
		{
			<>4__this = this,
			modelObject = modelObject,
			skinDef = skinDef
		};
	}

	private void Apply(GameObject modelObject)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: 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_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		Transform transform = modelObject.transform;
		CharacterModel component = modelObject.GetComponent<CharacterModel>();
		component.baseRendererInfos = baseRendererInfos.ToArray();
		foreach (GameObjectActivationTemplate gameObjectActivationTemplate in gameObjectActivationTemplates)
		{
			Transform val = transform.Find(gameObjectActivationTemplate.transformPath);
			if (Object.op_Implicit((Object)(object)val))
			{
				((Component)val).gameObject.SetActive(gameObjectActivationTemplate.shouldActivate);
			}
		}
		component.gameObjectActivationTransforms.Clear();
		foreach (MeshReplacementTemplate meshReplacementTemplate in meshReplacementTemplates)
		{
			Transform val2 = transform.Find(meshReplacementTemplate.transformPath);
			if (!Object.op_Implicit((Object)(object)val2))
			{
				continue;
			}
			Renderer component2 = ((Component)val2).GetComponent<Renderer>();
			if (!(component2 is MeshRenderer))
			{
				SkinnedMeshRenderer val3 = (SkinnedMeshRenderer)(object)((component2 is SkinnedMeshRenderer) ? component2 : null);
				if (val3 != null)
				{
					val3.sharedMesh = meshReplacementTemplate.meshReference.directRef;
				}
			}
			else
			{
				((Component)component2).GetComponent<MeshFilter>().sharedMesh = meshReplacementTemplate.meshReference.directRef;
			}
		}
	}

	internal static void RevertSkinIL(ILContext il)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		ILCursor val = new ILCursor(il);
		int skinIndex = -1;
		int num = default(int);
		if (!val.TryGotoNext((MoveType)0, new Func<Instruction, bool>[3]
		{
			(Instruction x) => ILPatternMatchingExt.MatchLdloc(x, ref skinIndex),
			(Instruction x) => ILPatternMatchingExt.MatchLdcI4(x, ref num),
			(Instruction x) => ILPatternMatchingExt.MatchCallOrCallvirt<ModelSkinController>(x, "ApplySkinAsync")
		}))
		{
			LobbySkinsFixPlugin.InstanceLogger.LogError((object)"Failed to apply RevertSkinIL hook");
			return;
		}
		val.Emit(OpCodes.Dup);
		val.Index += 3;
		val.Emit(OpCodes.Ldloc, skinIndex);
		val.Emit(OpCodes.Call, (MethodBase)typeof(ReverseSkin).GetMethod("RevertSkin", BindingFlags.Static | BindingFlags.NonPublic));
	}

	[IteratorStateMachine(typeof(<RevertSkin>d__6))]
	private static IEnumerator RevertSkin(ModelSkinController modelSkinController, IEnumerator applySkinAsync, int skinIndex)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <RevertSkin>d__6(0)
		{
			modelSkinController = modelSkinController,
			applySkinAsync = applySkinAsync,
			skinIndex = skinIndex
		};
	}
}