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.2.0")]
namespace LobbySkinsFix;
[BepInPlugin("com.KingEnderBrine.LobbySkinsFix", "Lobby Skins Fix", "1.2.2")]
public class LobbySkinsFixPlugin : BaseUnityPlugin
{
public const string GUID = "com.KingEnderBrine.LobbySkinsFix";
public const string Name = "Lobby Skins Fix";
public const string Version = "1.2.2";
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
{
private readonly List<RendererInfo> baseRendererInfos = new List<RendererInfo>();
private readonly List<GameObjectActivationTemplate> gameObjectActivationTemplates = new List<GameObjectActivationTemplate>();
private readonly List<MeshReplacementTemplate> meshReplacementTemplates = new List<MeshReplacementTemplate>();
private void Initialize(GameObject modelObject, SkinDef skinDef)
{
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: 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_0041: 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_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_006d: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
//IL_0107: Unknown result type (might be due to invalid IL or missing references)
//IL_010f: Unknown result type (might be due to invalid IL or missing references)
//IL_012f: Unknown result type (might be due to invalid IL or missing references)
IEnumerator enumerator = skinDef.BakeAsync();
while (enumerator.MoveNext())
{
}
RuntimeSkin runtimeSkin = skinDef.runtimeSkin;
baseRendererInfos.AddRange(modelObject.GetComponent<CharacterModel>().baseRendererInfos);
Enumerator<GameObjectActivationTemplate> enumerator2 = runtimeSkin.gameObjectActivationTemplates.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
GameObjectActivationTemplate current = enumerator2.Current;
gameObjectActivationTemplates.Add(new GameObjectActivationTemplate
{
transformPath = current.transformPath,
shouldActivate = !current.shouldActivate
});
}
}
finally
{
((IDisposable)enumerator2).Dispose();
}
Enumerator<MeshReplacementTemplate> enumerator3 = runtimeSkin.meshReplacementTemplates.GetEnumerator();
try
{
while (enumerator3.MoveNext())
{
MeshReplacementTemplate current2 = enumerator3.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;
}
meshReplacementTemplates.Add(new MeshReplacementTemplate
{
transformPath = current2.transformPath,
meshReference = new AssetOrDirectReference<Mesh>
{
directRef = sharedMesh
}
});
}
}
finally
{
((IDisposable)enumerator3).Dispose();
}
}
private void Apply(GameObject modelObject)
{
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
//IL_0032: 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_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0083: Unknown result type (might be due to invalid IL or missing references)
//IL_0088: 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_00ca: Unknown result type (might be due to invalid IL or missing references)
//IL_00df: Unknown result type (might be due to invalid IL or missing references)
Transform transform = modelObject.transform;
modelObject.GetComponent<CharacterModel>().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);
}
}
foreach (MeshReplacementTemplate meshReplacementTemplate in meshReplacementTemplates)
{
Transform val2 = transform.Find(meshReplacementTemplate.transformPath);
if (!Object.op_Implicit((Object)(object)val2))
{
continue;
}
Renderer component = ((Component)val2).GetComponent<Renderer>();
if (!(component is MeshRenderer))
{
SkinnedMeshRenderer val3 = (SkinnedMeshRenderer)(object)((component is SkinnedMeshRenderer) ? component : null);
if (val3 != null)
{
val3.sharedMesh = meshReplacementTemplate.meshReference.directRef;
}
}
else
{
((Component)component).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_0093: 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)
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.Emit(OpCodes.Ldloc, skinIndex);
val.Emit(OpCodes.Call, (MethodBase)typeof(ReverseSkin).GetMethod("RevertSkin", BindingFlags.Static | BindingFlags.NonPublic));
}
private static void RevertSkin(ModelSkinController modelSkinController, int skinIndex)
{
if ((uint)skinIndex >= modelSkinController.skins.Length)
{
skinIndex = 0;
}
if (skinIndex != modelSkinController.currentSkinIndex && modelSkinController.skins.Length != 0)
{
ReverseSkin component = ((Component)modelSkinController).GetComponent<ReverseSkin>();
if (Object.op_Implicit((Object)(object)component))
{
component.Apply(((Component)modelSkinController).gameObject);
Object.Destroy((Object)(object)component);
}
((Component)modelSkinController).gameObject.AddComponent<ReverseSkin>().Initialize(((Component)modelSkinController).gameObject, modelSkinController.skins[skinIndex]);
}
}
}