Decompiled source of BenAndBrandonSkins v1.0.3

BepInEx/plugins/BenAndBrandonSkins/BenBrandonSuits.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using ModelReplacement;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

[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("BenBrandonSuits")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BenBrandonSuits")]
[assembly: AssemblyTitle("BenBrandonSuits")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BenBrandonSuits
{
	[BepInPlugin("benandbrandon.customsuits", "Ben and Brandon Skins", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static AssetBundle Models;

		internal static ManualLogSource Log;

		private static readonly Dictionary<string, GameObject> Prepared = new Dictionary<string, GameObject>();

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "benbrandon_suits_20261004");
			Models = AssetBundle.LoadFromFile(text);
			if ((Object)(object)Models == (Object)null)
			{
				throw new InvalidOperationException("Could not load custom suit assetbundle: " + text);
			}
			string[] array = new string[2] { "Colossus", "Tactical_Operative" };
			foreach (string text2 in array)
			{
				if ((Object)(object)Models.LoadAsset<GameObject>(text2) == (Object)null)
				{
					throw new InvalidOperationException("Missing suit prefab: " + text2);
				}
			}
			ValidateTextureIsolation();
			ModelReplacementAPI.RegisterSuitModelReplacement("Colossus", typeof(ColossusReplacement));
			ModelReplacementAPI.RegisterSuitModelReplacement("Tactical Operative", typeof(TacticalOperativeReplacement));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Ben and Brandon Skins 1.0.3 loaded: Colossus and Tactical Operative; native game shader repair and unique texture names enabled.");
		}

		private static void ValidateTextureIsolation()
		{
			Dictionary<string, Texture> dictionary = new Dictionary<string, Texture>();
			string[] array = new string[2] { "Colossus", "Tactical_Operative" };
			foreach (string text in array)
			{
				Renderer[] componentsInChildren = Models.LoadAsset<GameObject>(text).GetComponentsInChildren<Renderer>(true);
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					Material[] sharedMaterials = componentsInChildren[j].sharedMaterials;
					foreach (Material val in sharedMaterials)
					{
						string[] array2 = new string[3] { "_BaseColorMap", "_NormalMap", "_MaskMap" };
						foreach (string text2 in array2)
						{
							Texture texture = val.GetTexture(text2);
							string value = "BenAndBrandonSkins_" + text + "_";
							if ((Object)(object)texture == (Object)null || !((Object)texture).name.StartsWith(value, StringComparison.Ordinal))
							{
								throw new InvalidOperationException("Unexpected texture identity for " + text + " " + text2 + ". Update both the plugin and its asset bundle together.");
							}
							if (dictionary.TryGetValue(((Object)texture).name, out var value2) && (Object)(object)value2 != (Object)(object)texture)
							{
								throw new InvalidOperationException("Duplicate custom texture name: " + ((Object)texture).name);
							}
							dictionary[((Object)texture).name] = texture;
						}
					}
				}
			}
			Log.LogInfo((object)("SUIT_TEXTURE_ISOLATION_OK " + dictionary.Count + " unique authored textures; protected from name-based deduplication."));
		}

		public static GameObject PrepareModel(string name, Material gameMaterial)
		{
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Expected O, but got Unknown
			if (Prepared.TryGetValue(name, out var value))
			{
				return value;
			}
			if ((Object)(object)gameMaterial == (Object)null || (Object)(object)gameMaterial.shader == (Object)null || !gameMaterial.shader.isSupported || gameMaterial.FindPass("Forward") < 0)
			{
				throw new InvalidOperationException("The game's suit shader is unavailable for " + name);
			}
			GameObject val = Models.LoadAsset<GameObject>(name);
			if ((Object)(object)val == (Object)null)
			{
				throw new InvalidOperationException("Missing suit prefab: " + name);
			}
			Dictionary<Material, Material> dictionary = new Dictionary<Material, Material>();
			Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val2 in componentsInChildren)
			{
				Material[] sharedMaterials = val2.sharedMaterials;
				for (int j = 0; j < sharedMaterials.Length; j++)
				{
					Material val3 = sharedMaterials[j];
					if ((Object)(object)val3 == (Object)null)
					{
						throw new InvalidOperationException("Missing material on " + name);
					}
					if (!dictionary.TryGetValue(val3, out var value2))
					{
						Texture texture = val3.GetTexture("_BaseColorMap");
						if ((Object)(object)texture == (Object)null)
						{
							throw new InvalidOperationException("Missing colour texture on " + name);
						}
						value2 = new Material(val3)
						{
							name = ((Object)val3).name + " (Native Game Shader)",
							shader = gameMaterial.shader
						};
						HDMaterial.ValidateMaterial(value2);
						if ((Object)(object)value2.shader != (Object)(object)gameMaterial.shader || !value2.shader.isSupported || value2.FindPass("Forward") < 0 || (Object)(object)value2.GetTexture("_BaseColorMap") != (Object)(object)texture)
						{
							throw new InvalidOperationException("Native material validation failed for " + name);
						}
						dictionary.Add(val3, value2);
						Log.LogInfo((object)("SUIT_MATERIAL_OK " + name + " shader=" + ((Object)value2.shader).name + " nativeShaderId=" + ((Object)value2.shader).GetInstanceID() + " passes=" + value2.passCount + " albedo=" + ((Object)texture).name + " " + texture.width + "x" + texture.height + " normal=" + ((Object)(object)value2.GetTexture("_NormalMap") != (Object)null) + " mask=" + ((Object)(object)value2.GetTexture("_MaskMap") != (Object)null)));
					}
					sharedMaterials[j] = value2;
				}
				val2.sharedMaterials = sharedMaterials;
			}
			Prepared.Add(name, val);
			return val;
		}
	}
	public class ColossusReplacement : BodyReplacementBase
	{
		protected override GameObject LoadAssetsAndReturnModel()
		{
			return Plugin.PrepareModel("Colossus", ((Renderer)((BodyReplacementBase)this).controller.thisPlayerModel).sharedMaterial);
		}
	}
	public class TacticalOperativeReplacement : BodyReplacementBase
	{
		protected override GameObject LoadAssetsAndReturnModel()
		{
			return Plugin.PrepareModel("Tactical_Operative", ((Renderer)((BodyReplacementBase)this).controller.thisPlayerModel).sharedMaterial);
		}
	}
}