Decompiled source of IDRSJsonLoader v1.0.0

plugins/IDRSJsonLoader/IDRSJsonLoader.dll

Decompiled 6 hours 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 BepInEx;
using BepInEx.Logging;
using HG.Coroutines;
using HG.Reflection;
using IDRSJsonLoader.Models;
using RoR2;
using UnityEngine;
using UnityEngine.AddressableAssets;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: AssemblyVersion("0.0.0.0")]
namespace IDRSJsonLoader
{
	public delegate void ParseCallback(ItemDisplayRuleSet idrs);
	[BepInPlugin("com.KingEnderBrine.IDRSJsonLoader", "IDRS Json Loader", "1.0.0")]
	public class IDRSJsonLoaderPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.KingEnderBrine.IDRSJsonLoader";

		public const string Name = "IDRS Json Loader";

		public const string Version = "1.0.0";

		private static readonly List<DelayedReplacementInfo> delayedReplacementInfos = new List<DelayedReplacementInfo>();

		internal static IDRSJsonLoaderPlugin Instance { get; private set; }

		internal static ManualLogSource InstanceLogger
		{
			get
			{
				IDRSJsonLoaderPlugin instance = Instance;
				if (instance == null)
				{
					return null;
				}
				return ((BaseUnityPlugin)instance).Logger;
			}
		}

		private void Awake()
		{
			Instance = this;
			string[] files = Directory.GetFiles(Paths.PluginPath, "*.idrsjson", SearchOption.AllDirectories);
			foreach (string text in files)
			{
				try
				{
					ParseAndUpdate(File.ReadAllText(text), null);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Couldn't load file " + text));
					((BaseUnityPlugin)this).Logger.LogError((object)ex);
				}
			}
		}

		public static void ParseAndUpdate(string json, ParseCallback beforeGenerateRuntimeValues)
		{
			DelayedReplacementInfo item = new DelayedReplacementInfo
			{
				exportIdrs = JsonUtility.FromJson<ExportItemDisplayRuleSet>(json),
				beforeGenerateRuntimeValues = beforeGenerateRuntimeValues
			};
			delayedReplacementInfos.Add(item);
		}

		public static void ParseAndUpdate(string json, ItemDisplayRuleSet idrs, ParseCallback beforeGenerateRuntimeValues)
		{
			DelayedReplacementInfo item = new DelayedReplacementInfo
			{
				exportIdrs = JsonUtility.FromJson<ExportItemDisplayRuleSet>(json),
				idrs = idrs,
				beforeGenerateRuntimeValues = beforeGenerateRuntimeValues
			};
			delayedReplacementInfos.Add(item);
		}

		[SystemInitializer(new Type[]
		{
			typeof(ItemDisplayRuleSet),
			typeof(BodyCatalog)
		})]
		internal static IEnumerator Init()
		{
			Dictionary<string, AssetBundle> assetBundles = AssetBundle.GetAllLoadedAssetBundles().ToDictionary((AssetBundle b) => ((Object)b).name);
			Dictionary<ItemDisplayRuleSet, Dictionary<Object, ExportKeyAssetRuleGroup>> dictionary = new Dictionary<ItemDisplayRuleSet, Dictionary<Object, ExportKeyAssetRuleGroup>>();
			foreach (DelayedReplacementInfo delayedReplacementInfo in delayedReplacementInfos)
			{
				ExportItemDisplayRuleSet exportIdrs = delayedReplacementInfo.exportIdrs;
				if (!Object.op_Implicit((Object)(object)delayedReplacementInfo.idrs) && !string.IsNullOrEmpty(exportIdrs.bodyName))
				{
					GameObject val = BodyCatalog.FindBodyPrefab(exportIdrs.bodyName);
					if (Object.op_Implicit((Object)(object)val))
					{
						ModelLocator component = val.GetComponent<ModelLocator>();
						object obj;
						if (component == null)
						{
							obj = null;
						}
						else
						{
							Transform modelTransform = component.modelTransform;
							obj = ((modelTransform != null) ? ((Component)modelTransform).GetComponent<CharacterModel>() : null);
						}
						CharacterModel val2 = (CharacterModel)obj;
						if (Object.op_Implicit((Object)(object)val2))
						{
							if (!Object.op_Implicit((Object)(object)val2.itemDisplayRuleSet))
							{
								val2.itemDisplayRuleSet = ScriptableObject.CreateInstance<ItemDisplayRuleSet>();
							}
							delayedReplacementInfo.idrs = val2.itemDisplayRuleSet;
						}
					}
				}
				if (Object.op_Implicit((Object)(object)delayedReplacementInfo.idrs))
				{
					if (!dictionary.TryGetValue(delayedReplacementInfo.idrs, out var value))
					{
						value = (dictionary[delayedReplacementInfo.idrs] = new Dictionary<Object, ExportKeyAssetRuleGroup>());
					}
					for (int i = 0; i < exportIdrs.itemGroups.Count; i++)
					{
						ExportKeyAssetRuleGroup exportKeyAssetRuleGroup = exportIdrs.itemGroups[i];
						ItemDef itemDef = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex(exportKeyAssetRuleGroup.name));
						value[(Object)(object)itemDef] = exportKeyAssetRuleGroup;
					}
					for (int j = 0; j < exportIdrs.equipmentGroups.Count; j++)
					{
						ExportKeyAssetRuleGroup exportKeyAssetRuleGroup2 = exportIdrs.equipmentGroups[j];
						EquipmentDef equipmentDef = EquipmentCatalog.GetEquipmentDef(EquipmentCatalog.FindEquipmentIndex(exportKeyAssetRuleGroup2.name));
						value[(Object)(object)equipmentDef] = exportKeyAssetRuleGroup2;
					}
				}
			}
			ParallelCoroutine assetsCoroutine = null;
			foreach (KeyValuePair<ItemDisplayRuleSet, Dictionary<Object, ExportKeyAssetRuleGroup>> item in dictionary)
			{
				item.Deconstruct(out var key, out var value2);
				ItemDisplayRuleSet val3 = key;
				Dictionary<Object, ExportKeyAssetRuleGroup> dictionary3 = value2;
				for (int k = 0; k < val3.keyAssetRuleGroups.Length; k++)
				{
					Object keyAsset = val3.keyAssetRuleGroups[k].keyAsset;
					if (!dictionary3.Remove(keyAsset, out var value3))
					{
						continue;
					}
					IEnumerator enumerator3 = MapGroup(val3, k, keyAsset, value3, assetBundles);
					if (enumerator3.MoveNext())
					{
						if (assetsCoroutine == null)
						{
							assetsCoroutine = new ParallelCoroutine();
						}
						assetsCoroutine.Add(enumerator3);
					}
				}
				if (dictionary3.Count == 0)
				{
					continue;
				}
				int num = val3.keyAssetRuleGroups.Length;
				Array.Resize(ref val3.keyAssetRuleGroups, val3.keyAssetRuleGroups.Length + dictionary3.Count);
				foreach (KeyValuePair<Object, ExportKeyAssetRuleGroup> item2 in dictionary3)
				{
					item2.Deconstruct(out var key2, out var value4);
					Object keyAsset2 = key2;
					ExportKeyAssetRuleGroup exportGroup = value4;
					IEnumerator enumerator5 = MapGroup(val3, num++, keyAsset2, exportGroup, assetBundles);
					if (enumerator5.MoveNext())
					{
						if (assetsCoroutine == null)
						{
							assetsCoroutine = new ParallelCoroutine();
						}
						assetsCoroutine.Add(enumerator5);
					}
				}
			}
			if (assetsCoroutine != null)
			{
				while (assetsCoroutine.MoveNext())
				{
					yield return null;
				}
			}
			ParallelCoroutine idrsCoroutine = new ParallelCoroutine();
			foreach (DelayedReplacementInfo delayedReplacementInfo2 in delayedReplacementInfos)
			{
				if (Object.op_Implicit((Object)(object)delayedReplacementInfo2.idrs))
				{
					delayedReplacementInfo2.beforeGenerateRuntimeValues?.Invoke(delayedReplacementInfo2.idrs);
					idrsCoroutine.Add(delayedReplacementInfo2.idrs.GenerateRuntimeValuesAsync());
				}
			}
			while (idrsCoroutine.MoveNext())
			{
				yield return null;
			}
			delayedReplacementInfos.Clear();
		}

		private static IEnumerator MapGroup(ItemDisplayRuleSet idrs, int index, Object keyAsset, ExportKeyAssetRuleGroup exportGroup, Dictionary<string, AssetBundle> assetBundles)
		{
			ParallelCoroutine coroutine = null;
			KeyAssetRuleGroup group = default(KeyAssetRuleGroup);
			group.displayRuleGroup.rules = (ItemDisplayRule[])(object)new ItemDisplayRule[exportGroup.rules.Count];
			group.keyAsset = keyAsset;
			for (int i = 0; i < exportGroup.rules.Count; i++)
			{
				IEnumerator enumerator = MapRule(group.displayRuleGroup.rules, i, exportGroup.rules[i], assetBundles);
				if (enumerator.MoveNext())
				{
					if (coroutine == null)
					{
						coroutine = new ParallelCoroutine();
					}
					coroutine.Add(enumerator);
				}
			}
			if (coroutine != null)
			{
				while (coroutine.MoveNext())
				{
					yield return null;
				}
			}
			idrs.keyAssetRuleGroups[index] = group;
		}

		private static IEnumerator MapRule(ItemDisplayRule[] rules, int index, ExportItemDisplayRule exportRule, Dictionary<string, AssetBundle> assetBundles)
		{
			ItemDisplayRule rule = new ItemDisplayRule
			{
				childName = exportRule.childName,
				followerPrefabAddress = new AssetReferenceGameObject(exportRule.guid ?? ""),
				limbMask = exportRule.limbMask,
				localAngles = exportRule.localAngles,
				localPos = exportRule.localPos,
				localScale = exportRule.localScale,
				ruleType = exportRule.ruleType
			};
			if (!string.IsNullOrEmpty(exportRule.assetBundle))
			{
				if (assetBundles.TryGetValue(exportRule.assetBundle, out var value))
				{
					AssetBundleRequest request = value.LoadAssetAsync<GameObject>(exportRule.assetPath);
					while (!((AsyncOperation)request).isDone)
					{
						yield return null;
					}
					Object asset = request.asset;
					rule.followerPrefab = (GameObject)(object)((asset is GameObject) ? asset : null);
				}
				else
				{
					InstanceLogger.LogError((object)("AssetBundle " + exportRule.assetBundle + " was not found."));
				}
			}
			rules[index] = rule;
		}
	}
}
namespace IDRSJsonLoader.Models
{
	internal class DelayedReplacementInfo
	{
		public ExportItemDisplayRuleSet exportIdrs;

		public ParseCallback beforeGenerateRuntimeValues;

		internal ItemDisplayRuleSet idrs;
	}
	[Serializable]
	public class ExportItemDisplayRule
	{
		public string guid;

		public string assetBundle;

		public string assetPath;

		public Vector3 localScale;

		public Vector3 localPos;

		public Vector3 localAngles;

		public string childName;

		public LimbFlags limbMask;

		public ItemDisplayRuleType ruleType;
	}
	[Serializable]
	public class ExportItemDisplayRuleSet
	{
		public string bodyName;

		public List<ExportKeyAssetRuleGroup> itemGroups = new List<ExportKeyAssetRuleGroup>();

		public List<ExportKeyAssetRuleGroup> equipmentGroups = new List<ExportKeyAssetRuleGroup>();
	}
	[Serializable]
	public class ExportKeyAssetRuleGroup
	{
		public string name;

		public List<ExportItemDisplayRule> rules = new List<ExportItemDisplayRule>();
	}
}