Decompiled source of COTL JSONLoader v0.1.1

plugins/COTL_JSONLoader.dll

Decompiled a year 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 System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using COTL_API.CustomSkins;
using COTL_JSONLoader.Data.Skins;
using COTL_JSONLoader.Helpers;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("COTL_JSONLoader")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("COTL_JSONLoader")]
[assembly: AssemblyTitle("COTL_JSONLoader")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 COTL_JSONLoader
{
	internal static class DebugLoad
	{
		private static string Folder()
		{
			Plugin? instance = Plugin.Instance;
			return Path.GetDirectoryName(((instance != null) ? ((BaseUnityPlugin)instance).Info.Location : null) ?? string.Empty);
		}

		private static string DebugPath(string filename)
		{
			return Path.Combine(Folder(), filename);
		}

		internal static void MakePlayerSkin()
		{
			PlayerSkinData playerSkinData = new PlayerSkinData();
			playerSkinData.Name = "Debug";
			playerSkinData.ImagePath = "debug_lamb_sheet.png";
			playerSkinData.Overrides = new OverrideData[6]
			{
				new OverrideData("HeadBack", "0, 0, 128, 128"),
				new OverrideData("HeadBackDown", "128, 0, 128, 128"),
				new OverrideData("HeadBackDown_RITUAL", "0, 128, 128, 128"),
				new OverrideData("HeadBackDown_SERMON", "128, 128, 128, 128"),
				new OverrideData("HeadFront", "256, 0, 128, 128"),
				new OverrideData("HeadFrontDown", "256, 128, 128, 128")
			};
			string contents = JsonConvert.SerializeObject((object)playerSkinData, (Formatting)1);
			File.WriteAllText(DebugPath("debug_lamb.json"), contents);
		}

		internal static void MakeFollowerSkin()
		{
			FollowerSkinData followerSkinData = new FollowerSkinData();
			followerSkinData.Name = "Debug Follower";
			followerSkinData.ImagePath = "debug_follower.png";
			followerSkinData.Overrides = new OverrideData[4]
			{
				new OverrideData("HEAD_SKIN_TOP_BACK", "0, 127, 111, 127"),
				new OverrideData("HEAD_SKIN_BTM_BACK", "0, 0, 111, 127"),
				new OverrideData("HEAD_SKIN_TOP", "111, 127, 149, 127"),
				new OverrideData("HEAD_SKIN_BTM", "111, 0, 149, 127")
			};
			followerSkinData.Colors = new List<HexColor[]>
			{
				new HexColor[9]
				{
					new HexColor("ARM_LEFT_SKIN", "#FF0000"),
					new HexColor("ARM_RIGHT_SKIN", "#FF0000"),
					new HexColor("LEG_LEFT_SKIN", "#FF0000"),
					new HexColor("LEG_RIGHT_SKIN", "#FF0000"),
					new HexColor("BODY_SKIN", "#FF0000"),
					new HexColor("BODY_SKIN_BOWED", "#FF0000"),
					new HexColor("BODY_SKIN_UP", "#FF0000"),
					new HexColor("HEAD_SKIN_BTM", "#FF0000"),
					new HexColor("HEAD_SKIN_TOP", "#FF7F00")
				},
				new HexColor[9]
				{
					new HexColor("ARM_LEFT_SKIN", "#00FF00"),
					new HexColor("ARM_RIGHT_SKIN", "#00FF00"),
					new HexColor("LEG_LEFT_SKIN", "#00FF00"),
					new HexColor("LEG_RIGHT_SKIN", "#00FF00"),
					new HexColor("BODY_SKIN", "#00FF00"),
					new HexColor("BODY_SKIN_BOWED", "#00FF00"),
					new HexColor("BODY_SKIN_UP", "#00FF00"),
					new HexColor("HEAD_SKIN_BTM", "#00FF00"),
					new HexColor("HEAD_SKIN_TOP", "#00FF7F")
				},
				new HexColor[9]
				{
					new HexColor("ARM_LEFT_SKIN", "#0000FF"),
					new HexColor("ARM_RIGHT_SKIN", "#0000FF"),
					new HexColor("LEG_LEFT_SKIN", "#0000FF"),
					new HexColor("LEG_RIGHT_SKIN", "#0000FF"),
					new HexColor("BODY_SKIN", "#0000FF"),
					new HexColor("BODY_SKIN_BOWED", "#0000FF"),
					new HexColor("BODY_SKIN_UP", "#0000FF"),
					new HexColor("HEAD_SKIN_BTM", "#0000FF"),
					new HexColor("HEAD_SKIN_TOP", "#7F00FF")
				}
			};
			string contents = JsonConvert.SerializeObject((object)followerSkinData, (Formatting)1);
			File.WriteAllText(DebugPath("debug_follower.json"), contents);
		}
	}
	[BepInPlugin("kel.cotl.jsonloader", "COTL_JSONLoader", "0.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "kel.cotl.jsonloader";

		public const string PluginName = "COTL_JSONLoader";

		public const string PluginVersion = "0.2.0";

		public static Plugin? Instance;

		internal static ConfigEntry<bool>? Debug;

		private void Awake()
		{
			Instance = this;
			LoadConfig();
			if (Debug != null && Debug.Value)
			{
				LoadDebug();
			}
			LoadFiles();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded COTL_JSONLoader successfully!");
		}

		internal static void LogInfo(string x)
		{
			Plugin? instance = Instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogInfo((object)x);
			}
		}

		internal static void LogError(string x)
		{
			Plugin? instance = Instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogError((object)x);
			}
		}

		internal static void LoadConfig()
		{
			Plugin? instance = Instance;
			Debug = ((instance != null) ? ((BaseUnityPlugin)instance).Config.Bind<bool>("General.Debug", "Debug", false, "Enable creating and loading basic debug files.") : null);
		}

		internal static void LoadFiles()
		{
			string[] array = Directory.GetFiles(Paths.PluginPath, "*.json", SearchOption.AllDirectories).Concat(Directory.GetFiles(Paths.PluginPath, "*.jsonc", SearchOption.AllDirectories)).ToArray();
			if (array.Length != 0)
			{
				LoadSkins.LoadPlayerSkins(array.Where((string x) => x.EndsWith("_lamb.json") || x.EndsWith("_lamb.jsonc")).ToArray());
				LoadSkins.LoadFollowerSkins(array.Where((string x) => x.EndsWith("_follower.json") || x.EndsWith("_lamb.jsonc")).ToArray());
			}
		}

		internal static void LoadDebug()
		{
			DebugLoad.MakePlayerSkin();
			DebugLoad.MakeFollowerSkin();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "kel.cotl.jsonloader";

		public const string PLUGIN_NAME = "COTL_JSONLoader";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace COTL_JSONLoader.Helpers
{
	internal static class AssetHelpers
	{
		private static string Find(string filename)
		{
			return Directory.GetFiles(Paths.PluginPath, filename, SearchOption.AllDirectories).FirstOrDefault();
		}

		internal static Texture2D Load(string imagePath)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			if (!Path.IsPathRooted(imagePath))
			{
				imagePath = Find(imagePath);
			}
			byte[] array = File.ReadAllBytes(imagePath);
			Texture2D val = new Texture2D(1, 1, (TextureFormat)4, false, false);
			ImageConversion.LoadImage(val, array);
			((Texture)val).filterMode = (FilterMode)0;
			return val;
		}

		internal static Color32 HexToColor(string hex)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: 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_0050: 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)
			Queue<char> queue = new Queue<char>(hex.Trim());
			if (queue.Count == 0)
			{
				return default(Color32);
			}
			if (queue.Peek() == '#')
			{
				queue.Dequeue();
			}
			if (queue.Count < 6)
			{
				Plugin.LogError("Invalid hexcode: " + hex);
				return default(Color32);
			}
			byte[] array = new byte[3];
			for (int i = 0; i < 3; i++)
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(queue.Dequeue());
				stringBuilder.Append(queue.Dequeue());
				byte b = Convert.ToByte(stringBuilder.ToString(), 16);
				array[i] = b;
			}
			return new Color32(array[0], array[1], array[2], byte.MaxValue);
		}

		internal static Rect ToRect(string rectString)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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)
			string[] array = (from x in rectString.Split(new char[1] { ',' })
				select x.Trim()).ToArray();
			if (array.Length < 4)
			{
				Plugin.LogError("Invalid rect: " + rectString);
				return default(Rect);
			}
			int[] array2 = array.Select((string x) => Convert.ToInt32(x)).ToArray();
			return new Rect((float)array2[0], (float)array2[1], (float)array2[2], (float)array2[3]);
		}

		public static Vector2 ToVector2(string vectorString)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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)
			string[] array = (from x in vectorString.Split(new char[1] { ',' })
				select x.Trim()).ToArray();
			if (array.Length < 2)
			{
				Plugin.LogError("Invalid vector: " + vectorString);
				return default(Vector2);
			}
			float[] array2 = array.Select(Convert.ToSingle).ToArray();
			return new Vector2(array2[0], array2[1]);
		}
	}
}
namespace COTL_JSONLoader.Data.Skins
{
	public class SkinData
	{
		[JsonProperty("name")]
		public string Name { get; set; }

		[JsonProperty("imagePath")]
		public string ImagePath { get; set; }

		[JsonProperty("overrides")]
		public OverrideData[] Overrides { get; set; }
	}
	[Serializable]
	public class PlayerSkinData : SkinData
	{
		public PlayerSkinDummy CreateSkin()
		{
			return new PlayerSkinDummy(base.Name, base.ImagePath, base.Overrides);
		}
	}
	[Serializable]
	public class FollowerSkinData : SkinData
	{
		[JsonProperty("colors")]
		public List<HexColor[]> Colors { get; set; }

		public FollowerSkinDummy CreateSkin()
		{
			return new FollowerSkinDummy(base.Name, base.ImagePath, base.Overrides, Colors);
		}
	}
	[Serializable]
	public class HexColor
	{
		[JsonProperty("name")]
		public string Name { get; set; }

		[JsonProperty("hex")]
		public string Hex { get; set; }

		public Color GetColor()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			return Color32.op_Implicit(AssetHelpers.HexToColor(Hex));
		}

		public SlotAndColor CreateColor()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			return new SlotAndColor(Name, GetColor());
		}

		public HexColor(string name, string hex)
		{
			Name = name;
			Hex = hex;
		}
	}
	[Serializable]
	public class OverrideData
	{
		[JsonProperty("name")]
		public string Name { get; set; }

		[JsonProperty("rect")]
		public string Rect { get; set; }

		[JsonProperty("scale")]
		public string Scale { get; set; } = "1, 1";


		[JsonProperty("offset")]
		public string Offset { get; set; } = "0, 0";


		public SkinOverride CreateOverride()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			return new SkinOverride(Name, AssetHelpers.ToRect(Rect), AssetHelpers.ToVector2(Scale), AssetHelpers.ToVector2(Offset));
		}

		public OverrideData(string name, string rect)
		{
			Name = name;
			Rect = rect;
		}
	}
	internal static class LoadSkins
	{
		internal static void LoadPlayerSkins(string[] files)
		{
			foreach (string path in files)
			{
				try
				{
					PlayerSkinData playerSkinData = JsonConvert.DeserializeObject<PlayerSkinData>(File.ReadAllText(path));
					if (playerSkinData != null)
					{
						CustomSkinManager.AddPlayerSkin((CustomPlayerSkin)(object)playerSkinData.CreateSkin());
						Plugin.LogInfo("Loaded player skin from file " + Path.GetFileName(path) + "!");
					}
				}
				catch (Exception)
				{
					Plugin.LogError("Error loading player skin from file " + Path.GetFileName(path) + "!");
					throw;
				}
			}
		}

		internal static void LoadFollowerSkins(string[] files)
		{
			foreach (string path in files)
			{
				try
				{
					FollowerSkinData followerSkinData = JsonConvert.DeserializeObject<FollowerSkinData>(File.ReadAllText(path));
					if (followerSkinData != null)
					{
						CustomSkinManager.AddFollowerSkin((CustomFollowerSkin)(object)followerSkinData.CreateSkin());
						Plugin.LogInfo("Loaded follower skin from file " + Path.GetFileName(path) + "!");
					}
				}
				catch (Exception)
				{
					Plugin.LogError("Error loading follower skin from file " + Path.GetFileName(path) + "!");
					throw;
				}
			}
		}
	}
	public class PlayerSkinDummy : CustomPlayerSkin
	{
		public string name;

		public string imagePath;

		public OverrideData[] overrideData;

		public override string Name => name;

		public override Texture2D Texture => AssetHelpers.Load(imagePath);

		public override List<SkinOverride> Overrides => overrideData.Select((OverrideData x) => x.CreateOverride()).ToList();

		public PlayerSkinDummy(string name, string imagePath, OverrideData[] overrideData)
		{
			this.name = name;
			this.imagePath = imagePath;
			this.overrideData = overrideData;
		}
	}
	public class FollowerSkinDummy : CustomFollowerSkin
	{
		public string name;

		public string imagePath;

		public OverrideData[] overrideData;

		public List<HexColor[]>? colors;

		public override string Name => name;

		public override Texture2D Texture => AssetHelpers.Load(imagePath);

		public override List<SkinOverride> Overrides => overrideData.Select((OverrideData x) => x.CreateOverride()).ToList();

		public override List<SlotsAndColours> Colors => GenerateColors() ?? ((CustomFollowerSkin)this).Colors;

		public FollowerSkinDummy(string name, string imagePath, OverrideData[] overrideData, List<HexColor[]>? colors = null)
		{
			this.name = name;
			this.imagePath = imagePath;
			this.overrideData = overrideData;
			this.colors = colors;
		}

		public List<SlotsAndColours>? GenerateColors()
		{
			return colors?.Select((Func<HexColor[], SlotsAndColours>)((HexColor[] x) => new SlotsAndColours
			{
				SlotAndColours = x.Select((HexColor y) => y.CreateColor()).ToList()
			}))?.ToList();
		}
	}
}