Decompiled source of Climber RealBoy v2.0.0

ClimberBadGuy.dll

Decompiled 4 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using DarkMachine.UI;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.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 ClimberBadGuy
{
	[BepInPlugin("climber.realboy.badguy.whiteknuckle.mod", "Climber (Real Boy) - Bad Guy Toggle", "1.0.0")]
	public class ClimberBadGuyPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "climber.realboy.badguy.whiteknuckle.mod";

		public const string PluginName = "Climber (Real Boy) - Bad Guy Toggle";

		public const string PluginVersion = "1.0.0";

		private const string TargetCosmeticId = "climber-real boy";

		private const string TargetSpriteName = "HND_B1A2";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> BadGuyEnabled;

		private static string pluginDir;

		private static SwapSprite targetSwap;

		private static Sprite originalSprite;

		private static Sprite badGuySprite;

		private static bool ready;

		private static UI_CosmeticInfoPanel activePanel;

		private static GameObject toggleRow;

		private static SubmitSlider toggleSlider;

		private void Awake()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			pluginDir = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			BadGuyEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Cosmetic", "BadGuyMode", false, "When enabled, the HND_B1A2 hand frame of the Climber (Real Boy) cosmetic is replaced with the bad guy photo.");
			InstallCosmeticContent();
			new Harmony("climber.realboy.badguy.whiteknuckle.mod").PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"Climber Bad Guy toggle loaded.");
		}

		private void InstallCosmeticContent()
		{
			try
			{
				string text = Path.Combine(pluginDir, "CosmeticContent", "Climber_RealBoy");
				if (!Directory.Exists(text))
				{
					Log.LogWarning((object)("Bundled cosmetic content not found at " + text));
					return;
				}
				string text2 = Path.Combine(Paths.GameRootPath, "White Knuckle_Data", "Mods", "Cosmetics", "Hands", "Climber_RealBoy");
				if (Directory.Exists(text2))
				{
					Directory.Delete(text2, recursive: true);
				}
				CopyDirectory(text, text2);
				Log.LogInfo((object)("Installed Climber (Real Boy) cosmetic to " + text2));
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Failed to install cosmetic content: " + ex));
			}
		}

		private static void CopyDirectory(string sourceDir, string destDir)
		{
			Directory.CreateDirectory(destDir);
			string[] files = Directory.GetFiles(sourceDir);
			foreach (string text in files)
			{
				File.Copy(text, Path.Combine(destDir, Path.GetFileName(text)), overwrite: true);
			}
			files = Directory.GetDirectories(sourceDir);
			foreach (string text2 in files)
			{
				CopyDirectory(text2, Path.Combine(destDir, Path.GetFileName(text2)));
			}
		}

		internal static void EnsureReady(Cosmetic_HandItem instance)
		{
			if (!ready && !(instance?.cosmeticData?.id != "climber-real boy") && instance.cosmeticData.swapSprites != null)
			{
				SwapSprite val = instance.cosmeticData.swapSprites.Find((SwapSprite s) => s.spriteName == "HND_B1A2");
				if (val?.replacementSprites != null && val.replacementSprites.Count != 0)
				{
					targetSwap = val;
					originalSprite = val.replacementSprites[0];
					badGuySprite = LoadBadGuySprite(originalSprite);
					ready = true;
					ApplyState();
				}
			}
		}

		private static void ApplyState()
		{
			targetSwap.replacementSprites[0] = (BadGuyEnabled.Value ? badGuySprite : originalSprite);
		}

		private static Sprite LoadBadGuySprite(Sprite reference)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0098: 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_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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)
			byte[] array = File.ReadAllBytes(Path.Combine(pluginDir, "Assets", "BadGuy.png"));
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			(Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule")?.GetMethod("LoadImage", new Type[3]
			{
				typeof(Texture2D),
				typeof(byte[]),
				typeof(bool)
			}))?.Invoke(null, new object[3] { val, array, false });
			((Texture)val).filterMode = (FilterMode)0;
			Vector2 pivot = reference.pivot;
			Rect rect = reference.rect;
			Vector2 val2 = pivot / ((Rect)(ref rect)).size;
			float pixelsPerUnit = reference.pixelsPerUnit;
			float num = ((Texture)val).width;
			rect = reference.rect;
			float num2 = pixelsPerUnit * (num / ((Rect)(ref rect)).width);
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), val2, num2, 0u, (SpriteMeshType)0);
		}

		internal static void OnInfoPanelUpdated(UI_CosmeticInfoPanel panel)
		{
			Cosmetic_Base selectedCosmetic = panel.selectedCosmetic;
			Cosmetic_HandItem val = (Cosmetic_HandItem)(object)((selectedCosmetic is Cosmetic_HandItem) ? selectedCosmetic : null);
			if (val == null || !(val.cosmeticData.id == "climber-real boy"))
			{
				if ((Object)(object)toggleRow != (Object)null)
				{
					toggleRow.SetActive(false);
				}
			}
			else
			{
				activePanel = panel;
				EnsureToggleUi(panel);
				panel.settingRoot.SetActive(true);
				toggleRow.SetActive(true);
				toggleSlider.SetValueWithoutNotify(BadGuyEnabled.Value ? 1f : 0f);
			}
		}

		private static void EnsureToggleUi(UI_CosmeticInfoPanel panel)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			if (!((Object)(object)toggleRow != (Object)null))
			{
				GameObject gameObject = ((Component)((Component)panel.accessorySlider).transform.parent).gameObject;
				toggleRow = Object.Instantiate<GameObject>(gameObject, gameObject.transform.parent);
				((Object)toggleRow).name = "BadGuyToggleRow";
				toggleSlider = toggleRow.GetComponentInChildren<SubmitSlider>(true);
				toggleSlider.onValueChanged = new SliderEvent();
				toggleSlider.minValue = 0f;
				toggleSlider.maxValue = 1f;
				toggleSlider.wholeNumbers = true;
				toggleSlider.stepAmount = 1f;
				((UnityEvent<float>)(object)toggleSlider.onValueChanged).AddListener((UnityAction<float>)OnToggleChanged);
				TMP_Text componentInChildren = toggleRow.GetComponentInChildren<TMP_Text>(true);
				if ((Object)(object)componentInChildren != (Object)null)
				{
					componentInChildren.text = "Bad guy";
				}
			}
		}

		private static void OnToggleChanged(float value)
		{
			BadGuyEnabled.Value = value >= 0.5f;
			ApplyState();
			activePanel.UpdateSprite();
		}
	}
	[HarmonyPatch(typeof(Cosmetic_HandItem), "SwapUISprite")]
	internal static class SwapUISpritePatch
	{
		private static void Prefix(Cosmetic_HandItem __instance, Image image)
		{
			ClimberBadGuyPlugin.EnsureReady(__instance);
		}
	}
	[HarmonyPatch(typeof(Cosmetic_HandItem), "SpriteSwap")]
	internal static class SpriteSwapPatch
	{
		private static void Prefix(Cosmetic_HandItem __instance)
		{
			ClimberBadGuyPlugin.EnsureReady(__instance);
		}
	}
	[HarmonyPatch(typeof(UI_CosmeticInfoPanel), "UpdateSprite")]
	internal static class UpdateSpritePatch
	{
		private static void Postfix(UI_CosmeticInfoPanel __instance)
		{
			ClimberBadGuyPlugin.OnInfoPanelUpdated(__instance);
		}
	}
}