WizGunCosmeticsAPI.dll

Decompiled a week ago
using System;
using System.Collections;
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.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using GGUtil;
using HarmonyLib;
using Inventory.Unity;
using Skins;
using UI;
using Unity.Collections;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("WizGunCosmeticsAPI")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WizGunCosmeticsAPI")]
[assembly: AssemblyTitle("WizGunCosmeticsAPI")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace WizGunCosmeticsAPI;

public class Glamour
{
	public GameObject gameObject;

	public StaticItem item;

	public StaticItemEquipmentComponent equipment;

	public GlamourRecipe recipe;

	public string itemCode;

	internal ItemType itemType;

	internal string itemName;

	internal string itemDescription;

	internal Sprite icon;

	internal List<SkinData> skins = new List<SkinData>();

	internal List<ColorData> colorSlots = new List<ColorData>();

	public Glamour(ItemType itemType, string itemCode, string itemName, string itemDescription)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		this.itemType = itemType;
		this.itemCode = itemCode;
		this.itemName = itemName;
		this.itemDescription = itemDescription;
		ConfigureRecipe();
	}

	public void CreateUnityObject()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Invalid comparison between Unknown and I4
		gameObject = new GameObject($"item-{((object)(ItemType)(ref itemType)).ToString().ToLower()}-{itemCode}");
		equipment = gameObject.AddComponent<StaticItemEquipmentComponent>();
		ConfigureEquipment();
		item = gameObject.AddComponent<StaticItem>();
		ConfigureItem();
		if ((int)itemType != 32)
		{
			recipe.glamour = gameObject;
		}
	}

	private void ConfigureItem()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: 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)
		item.arcanaWorth = 50;
		item.canConvertToArcana = true;
		item.displayDescription = new DisplayString
		{
			backupString = itemDescription
		};
		item.displayName = new DisplayString
		{
			backupString = itemName
		};
		item.itemRarity = (ItemRarity)0;
		item.icon = icon;
	}

	private void ConfigureEquipment()
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		equipment.itemType = itemType;
		SkinGroup val = ScriptableObject.CreateInstance<SkinGroup>();
		((Object)val).name = $"sg-{((object)(ItemType)(ref itemType)).ToString().ToLower()}-{itemCode}";
		val.skins = skins.ToArray();
		val.colorSlots = colorSlots.ToArray();
		equipment.skinGroup = val;
	}

	private void ConfigureRecipe()
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Invalid comparison between Unknown and I4
		if ((int)itemType != 32)
		{
			recipe = ScriptableObject.CreateInstance<GlamourRecipe>();
			((Object)recipe).name = $"recipe-glamour-{((object)(ItemType)(ref itemType)).ToString().ToLower()}-{itemCode}";
			((RecipeBase)recipe).recipeGroup = 6;
			((RecipeBase)recipe).requiredArcana = 10;
			((RecipeBase)recipe).requirements = (RecipeAssetRequirement[])(object)new RecipeAssetRequirement[0];
		}
	}
}
public static class GlamourExtensions
{
	public static Dictionary<string, SkinData> emptySkinData = new Dictionary<string, SkinData>();

	private static Texture2D emptyTexture = new Texture2D(8, 8);

	public static Glamour AddIcon(this Glamour glamour, string iconPath)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		glamour.icon = Helpers.CreateSpriteFromPNG(iconPath, $"{glamour.itemType}-{glamour.itemCode}", 75f);
		return glamour;
	}

	public static Glamour AddSkin(this Glamour glamour, SkinID skinID, string imgPath = "")
	{
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: 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_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: 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_0117: Unknown result type (might be due to invalid IL or missing references)
		string value = new Regex("[A-Z][a-z]+").Match(((object)(SkinID)(ref skinID)).ToString()).Value;
		SkinData value2;
		if (string.IsNullOrEmpty(imgPath))
		{
			string text = string.Format("skin-{0}-modded-empty-{1}", value.ToLower(), Regex.Replace(((object)(SkinID)(ref skinID)).ToString().Substring(value.Length), "([a-z])([A-Z])", "$1-$2").ToLower());
			if (!emptySkinData.TryGetValue(text, out value2))
			{
				Color[] pixels = emptyTexture.GetPixels();
				for (int i = 0; i < pixels.Length; i++)
				{
					pixels[i] = Color.clear;
				}
				emptyTexture.SetPixels(0, 0, 8, 8, pixels);
				emptyTexture.Apply();
				value2 = ScriptableObject.CreateInstance<SkinData>();
				value2.skinID = skinID;
				((Object)value2).name = text;
				value2.skin = Sprite.Create(emptyTexture, new Rect(0f, 0f, 8f, 8f), new Vector2(0.5f, 0.5f), 200f);
				emptySkinData.Add(text, value2);
			}
		}
		else
		{
			value2 = ScriptableObject.CreateInstance<SkinData>();
			value2.skinID = skinID;
			((Object)value2).name = string.Format("skin-{0}-{1}-{2}", value.ToLower(), glamour.itemCode, Regex.Replace(((object)(SkinID)(ref skinID)).ToString().Substring(value.Length), "([a-z])([A-Z])", "$1-$2").ToLower());
			value2.skin = Helpers.CreateSpriteFromPNG(imgPath, ((Object)value2).name, 200f);
		}
		glamour.skins.Add(value2);
		return glamour;
	}

	public static Glamour AddColorSlot(this Glamour glamour, ColorSlot slot, Color color)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		glamour.colorSlots.Add(new ColorData
		{
			slot = slot,
			color = color
		});
		return glamour;
	}
}
public static class GlamourManager
{
	private static List<Glamour> glamours = new List<Glamour>();

	internal static List<GameObject> glamourGameObjects => glamours.Select((Glamour x) => x.gameObject).ToList();

	internal static List<GameObject> eyeGameObjects => (from x in glamours
		where (int)x.itemType == 32
		select x.gameObject).ToList();

	internal static List<SkinGroup> glamourSkinGroups => glamours.Select((Glamour x) => x.equipment.skinGroup).ToList();

	internal static List<RecipeBase> glamourRecipeBases => ((IEnumerable<RecipeBase>)(from x in glamours
		where (Object)(object)x.recipe != (Object)null
		select x.recipe)).ToList();

	internal static List<GlamourRecipe> glamourRecipes => (from x in glamours
		where (Object)(object)x.recipe != (Object)null
		select x.recipe).ToList();

	public static Glamour New(ItemType itemType, string itemCode, string itemName, string itemDescription)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		WizGunCosmeticsAPI.Log.LogInfo((object)$"Adding Glamour {((object)(ItemType)(ref itemType)).ToString()}:{itemCode}");
		Glamour glamour = new Glamour(itemType, itemCode, itemName, itemDescription);
		glamours.Add(glamour);
		return glamour;
	}

	public static void CreateUnityObjects()
	{
		glamours.ForEach(delegate(Glamour x)
		{
			x.CreateUnityObject();
		});
	}
}
public class GlamourPatches
{
	public static bool AssetsLoadRunning;

	public static IEnumerator postfix_AssetsLoad(IEnumerator __results)
	{
		__results.MoveNext();
		yield return __results.Current;
		object assetsObj = Traverse.Create(typeof(Assets<GameObject>)).Field("_assets").GetValue();
		if (assetsObj != null && !AssetsLoadRunning)
		{
			WizGunCosmeticsAPI.Log.LogInfo((object)"Patching Assets");
			AssetsLoadRunning = true;
			GlamourManager.CreateUnityObjects();
			GlamourPatches.AddAssets<GameObject>(GlamourManager.glamourGameObjects);
			for (int j = 0; j < 9; j++)
			{
				__results.MoveNext();
				yield return __results.Current;
			}
			GlamourPatches.AddAssets<SkinGroup>(GlamourManager.glamourSkinGroups);
			__results.MoveNext();
			yield return __results.Current;
			GlamourPatches.AddAssets<RecipeBase>(GlamourManager.glamourRecipeBases);
			for (int i = 0; i < 5; i++)
			{
				__results.MoveNext();
				yield return __results.Current;
			}
			List<AssetEntry<GlamourRecipeCollection>> assets = Traverse.Create(typeof(Assets<GlamourRecipeCollection>)).Field("_assets").GetValue() as List<AssetEntry<GlamourRecipeCollection>>;
			if (UnityExtensions.First<AssetEntry<GlamourRecipeCollection>>((IList<AssetEntry<GlamourRecipeCollection>>)assets).asset.glamourCollection.Length == 268)
			{
				UnityExtensions.First<AssetEntry<GlamourRecipeCollection>>((IList<AssetEntry<GlamourRecipeCollection>>)assets).asset.glamourCollection = UnityExtensions.First<AssetEntry<GlamourRecipeCollection>>((IList<AssetEntry<GlamourRecipeCollection>>)assets).asset.glamourCollection.Concat(GlamourManager.glamourRecipes).ToArray();
				Traverse.Create(typeof(Assets<GlamourRecipeCollection>)).Field("_assets").SetValue((object)assets);
				WizGunCosmeticsAPI.Log.LogInfo((object)"Adjusted GlamourRecipeCollection");
			}
		}
		while (__results.MoveNext())
		{
			yield return __results.Current;
		}
		AssetsLoadRunning = false;
	}

	public static void AddAssets<T>(List<T> newAssets) where T : Object
	{
		//IL_003d: 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_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		List<AssetEntry<T>> list = Traverse.Create(typeof(Assets<T>)).Field("_assets").GetValue() as List<AssetEntry<T>>;
		foreach (T newAsset in newAssets)
		{
			AssetEntry<T> item = default(AssetEntry<T>);
			item.asset = newAsset;
			item.path = FixedString128.op_Implicit($"CosmeticsMod/{typeof(T).Name}/{((Object)newAsset).name}");
			if (!list.Contains(item))
			{
				list.Add(item);
				WizGunCosmeticsAPI.Log.LogInfo((object)$"Added {((Object)newAsset).name} to Assets<{typeof(T).Name}>");
			}
		}
		Traverse.Create(typeof(Assets<T>)).Field("_assets").SetValue((object)list);
	}

	public static void prefix_AddEquipmentOptions(GlamourRecipeCollection ___equipOptions)
	{
		if ((Object)(object)___equipOptions != (Object)null && ___equipOptions.glamourCollection.Length == 268 && GlamourManager.glamourRecipes.Count > 0)
		{
			WizGunCosmeticsAPI.Log.LogInfo((object)"Patching Equipment Options");
			___equipOptions.glamourCollection = ___equipOptions.glamourCollection.Concat(GlamourManager.glamourRecipes).ToArray();
		}
	}

	public static void postfix_AddEyeOptions(ref PlayerSpawnGameData ____spawnGameData)
	{
		if (____spawnGameData.eyeShapeItems != null && ____spawnGameData.eyeShapeItems.Length == 29 && GlamourManager.eyeGameObjects.Count > 0)
		{
			WizGunCosmeticsAPI.Log.LogInfo((object)"Patching Eye Options");
			____spawnGameData.eyeShapeItems = ____spawnGameData.eyeShapeItems.Concat(GlamourManager.eyeGameObjects).ToArray();
		}
	}
}
public static class Helpers
{
	public static Sprite CreateSpriteFromPNG(string iconPath, string name, float ppu)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Expected O, but got Unknown
		//IL_0041: 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)
		Texture2D val = new Texture2D(2, 2);
		byte[] array = File.ReadAllBytes(iconPath);
		ImageConversion.LoadImage(val, array);
		((Object)val).name = name;
		((Texture)val).filterMode = (FilterMode)0;
		Sprite val2 = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), ppu);
		((Object)val2).name = name;
		return val2;
	}
}
[BepInPlugin("cyantist.wwag.cosmeticsapi", "Cosmetics API", "1.2.1")]
public class WizGunCosmeticsAPI : BaseUnityPlugin
{
	public const string pluginGuid = "cyantist.wwag.cosmeticsapi";

	public const string pluginName = "Cosmetics API";

	public const string pluginVersion = "1.2.1";

	internal static ManualLogSource Log;

	public void Awake()
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Expected O, but got Unknown
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Expected O, but got Unknown
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Expected O, but got Unknown
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Log.LogInfo((object)"Cosmetics API Loaded");
		Harmony val = new Harmony("cyantist.wwag.cosmeticsapi");
		val.Patch((MethodBase)AccessTools.Method(typeof(WorldUtil), "LoadAssetsAsync", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(GlamourPatches), "postfix_AssetsLoad", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(typeof(PlayerSpawnGameData), "CheckUpdateEquipmentList", (Type[])null, (Type[])null), new HarmonyMethod(AccessTools.Method(typeof(GlamourPatches), "prefix_AddEquipmentOptions", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(typeof(PlayerSpawnSystem), "GetSystemData", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(GlamourPatches), "postfix_AddEyeOptions", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(typeof(CustomizeWizardUI), "InitializeWindow", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(GlamourPatches), "postfix_AddEyeOptions", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}
}