Carl.dll

Decompiled 17 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Zorro.Core;

[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("Carl")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Carl")]
[assembly: AssemblyTitle("Carl")]
[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 Carl
{
	[BepInPlugin("Carl", "Carl", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Id = "Carl";

		internal static Texture2D FaceTexture;

		internal static Texture2D BlankTexture;

		private readonly Harmony harmony = new Harmony("Carl");

		internal static ManualLogSource Log { get; private set; }

		internal static Plugin Instance { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			LoadTextures();
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"Carl loaded.");
		}

		private static void LoadTextures()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			FaceTexture = LoadEmbeddedTexture("carl_face.png");
			BlankTexture = new Texture2D(128, 128, (TextureFormat)4, false);
			((Object)BlankTexture).name = "Carl_blank";
			((Object)BlankTexture).hideFlags = (HideFlags)61;
			Color32[] pixels = Enumerable.Repeat<Color32>(new Color32((byte)0, (byte)0, (byte)0, (byte)0), 16384).ToArray();
			BlankTexture.SetPixels32(pixels);
			BlankTexture.Apply(false, false);
		}

		private static Texture2D LoadEmbeddedTexture(string fileName)
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string text = executingAssembly.GetManifestResourceNames().FirstOrDefault((string n) => n.EndsWith(fileName, StringComparison.OrdinalIgnoreCase));
			if (text == null)
			{
				Log.LogError((object)("Embedded texture not found: " + fileName));
				return null;
			}
			try
			{
				using Stream stream = executingAssembly.GetManifestResourceStream(text);
				if (stream == null)
				{
					Log.LogError((object)("Could not open embedded resource stream: " + text));
					return null;
				}
				byte[] array = new byte[stream.Length];
				int num2;
				for (int num = 0; num < array.Length; num += num2)
				{
					num2 = stream.Read(array, num, array.Length - num);
					if (num2 <= 0)
					{
						break;
					}
				}
				Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
				if (!ImageConversion.LoadImage(val, array))
				{
					Log.LogError((object)("Failed to decode embedded texture: " + text));
					Object.Destroy((Object)(object)val);
					return null;
				}
				((Object)val).name = "Carl_" + Path.GetFileNameWithoutExtension(fileName);
				((Texture)val).wrapMode = (TextureWrapMode)1;
				((Texture)val).filterMode = (FilterMode)1;
				((Object)val).hideFlags = (HideFlags)61;
				Log.LogInfo((object)$"Loaded texture {((Object)val).name} ({((Texture)val).width}x{((Texture)val).height}).");
				return val;
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Failed to load embedded texture {text}: {arg}");
				return null;
			}
		}
	}
}
namespace Carl.Patches
{
	[HarmonyPatch]
	internal static class CustomizationPatch
	{
		private const string OptionPrefix = "Carl_option_";

		private static void AddCustomizationOption(Type type, string key, Texture texture = null)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//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_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Invalid comparison between Unknown and I4
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Invalid comparison between Unknown and I4
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Invalid comparison between Unknown and I4
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			Customization instance = Singleton<Customization>.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			CustomizationOption[] list = instance.GetList(type);
			string optionName = "Carl_option_" + key;
			if (list != null && list.Any((CustomizationOption o) => (Object)(object)o != (Object)null && ((Object)o).name == optionName))
			{
				return;
			}
			CustomizationOption val = ScriptableObject.CreateInstance<CustomizationOption>();
			((Object)val).name = optionName;
			((Object)val).hideFlags = (HideFlags)61;
			val.type = type;
			val.texture = texture;
			val.requiredAchievement = (ACHIEVEMENTTYPE)0;
			val.customRequirement = (CUSTOMREQUIREMENT)0;
			val.testLocked = false;
			val.drawUnderEye = false;
			val.color = Color.white;
			CustomizationOption[] array = ((IEnumerable<CustomizationOption>)(((object)list) ?? ((object)new CustomizationOption[0]))).Concat((IEnumerable<CustomizationOption>)(object)new CustomizationOption[1] { val }).ToArray();
			if ((int)type != 10)
			{
				if ((int)type != 20)
				{
					if ((int)type != 30)
					{
						return;
					}
					instance.mouths = array;
				}
				else
				{
					instance.eyes = array;
				}
			}
			else
			{
				instance.accessories = array;
			}
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogInfo((object)$"Added {type} customization option '{optionName}'.");
			}
		}

		[HarmonyPatch(typeof(GUIManager), "Start")]
		[HarmonyPostfix]
		private static void OnGUIManagerStart()
		{
			try
			{
				if (!((Object)(object)Plugin.FaceTexture == (Object)null))
				{
					AddCustomizationOption((Type)10, "face", (Texture)(object)Plugin.FaceTexture);
					AddCustomizationOption((Type)30, "mouth", (Texture)(object)Plugin.BlankTexture);
					AddCustomizationOption((Type)20, "eyes");
				}
			}
			catch (Exception arg)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)$"Carl customization patch error: {arg}");
				}
			}
		}
	}
}