Decompiled source of PlasmaCustomAgents v1.0.1

PlasmaCustomAgents.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Behavior;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Visor;

[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.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PlasmaCustomAgents")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("PlasmaCustomAgents")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("PlasmaCustomAgents")]
[assembly: AssemblyTitle("PlasmaCustomAgents")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace PlasmaCustomAgents
{
	public class CustomAgent : Agent
	{
		internal static Dictionary<Type, Dictionary<string, int>> properties = new Dictionary<Type, Dictionary<string, int>>();

		internal static Dictionary<Type, Dictionary<string, int>> outputs = new Dictionary<Type, Dictionary<string, int>>();

		protected AgentProperty GetProperty(string name)
		{
			return base._runtimeProperties[properties[((object)this).GetType()][name]];
		}

		protected void WriteOutput(string name, Data data)
		{
			((Agent)this).currentSketchNode.ports[outputs[((object)this).GetType()][name]].Commit(data);
		}
	}
	public class LateGestaltRegistrationException : Exception
	{
	}
	[BepInPlugin("PlasmaCustomAgents", "PlasmaCustomAgents", "1.0.1")]
	public class CustomAgentManager : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Resources), "LoadAll", new Type[]
		{
			typeof(string),
			typeof(Type)
		})]
		private class LoadResourcesPatch
		{
			public static void Postfix(string path, Type systemTypeInstance, ref Object[] __result)
			{
				if (path == "Gestalts/Logic Agents" && systemTypeInstance == typeof(AgentGestalt) && !loadedResources)
				{
					int num = __result.Length;
					int num2 = num + agentGestalts.Count();
					Object[] array = (Object[])(object)new Object[num2];
					__result.CopyTo(array, 0);
					agentGestalts.ToArray().CopyTo(array, num);
					__result = array;
					loadedResources = true;
				}
			}
		}

		[HarmonyPatch(typeof(ProcessorUICategoryItem), "Setup")]
		private class AddCategoryToDictPatch
		{
			private static int applied;

			public static void Prefix()
			{
				//IL_0054: Unknown result type (might be due to invalid IL or missing references)
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				if (Holder.instance.agentCategories == null || applied >= customNodeCategories.Count())
				{
					return;
				}
				foreach (string key in customNodeCategories.Keys)
				{
					if (!Holder.instance.agentCategories.ContainsKey(customNodeCategories[key]))
					{
						applied++;
						Holder.instance.agentCategories.Add(customNodeCategories[key], key);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Enum), "GetNames")]
		public class EnumNamePatch
		{
			public static void Postfix(Type enumType, ref string[] __result)
			{
				if (enumType == typeof(AgentCategoryEnum))
				{
					string[] array = customNodeCategories.Keys.ToArray();
					string[] array2 = new string[__result.Length + array.Length];
					__result.CopyTo(array2, 0);
					array.CopyTo(array2, __result.Length);
					__result = array2;
				}
			}
		}

		[HarmonyPatch(typeof(Enum), "TryParseEnum")]
		public class EnumParsePatch
		{
			public static void Postfix(Type enumType, string value, ref object parseResult, ref bool __result)
			{
				//IL_008f: Unknown result type (might be due to invalid IL or missing references)
				if (!__result && enumType == typeof(AgentCategoryEnum) && customNodeCategories.ContainsKey(value))
				{
					__result = true;
					Type nestedType = typeof(Enum).GetNestedType("EnumResult", BindingFlags.NonPublic);
					MethodInfo method = nestedType.GetMethod("Init", BindingFlags.Instance | BindingFlags.NonPublic);
					FieldInfo field = nestedType.GetField("parsedEnum", BindingFlags.Instance | BindingFlags.NonPublic);
					object obj = Convert.ChangeType(Activator.CreateInstance(nestedType), nestedType);
					method.Invoke(obj, new object[1] { false });
					field.SetValue(obj, customNodeCategories[value]);
					parseResult = obj;
				}
			}
		}

		internal static IEnumerable<AgentGestalt> agentGestalts = Enumerable.Empty<AgentGestalt>();

		internal static bool loadedResources = false;

		internal static Dictionary<string, AgentCategoryEnum> customNodeCategories = new Dictionary<string, AgentCategoryEnum>();

		private static int highestNodeCategoryId = 3;

		public static ManualLogSource mls;

		private void Awake()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			mls = ((BaseUnityPlugin)this).Logger;
			mls.LogInfo((object)"Starting initialization of PlasmaCustomAgents");
			Harmony val = new Harmony("CustomAgentManager");
			val.PatchAll(Assembly.GetExecutingAssembly());
			if (Holder.agentGestalts != null)
			{
				loadedResources = true;
				mls.LogInfo((object)"PlasmaCustomAgents initialized too late and will not work properly");
				((Behaviour)this).enabled = false;
			}
			else
			{
				mls.LogInfo((object)"PlasmaCustomAgents initialized successfully");
			}
		}

		public static void RegisterGestalt(AgentGestalt gestalt, string unique_name)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (loadedResources)
			{
				throw new LateGestaltRegistrationException();
			}
			gestalt.id = (AgentGestaltEnum)(unique_name.GetHashCode() + 1000);
			agentGestalts = agentGestalts.Concat((IEnumerable<AgentGestalt>)(object)new AgentGestalt[1] { gestalt });
		}

		public static AgentGestalt CreateComponentGestalt(GameObject prefab, string displayName, string description = null, ComponentCategories category = 4)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0061: 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_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			AgentGestalt val = (AgentGestalt)ScriptableObject.CreateInstance(typeof(AgentGestalt));
			val.componentCategory = category;
			val.properties = new Dictionary<int, Property>();
			val.ports = new Dictionary<int, Port>();
			val.type = (Types)0;
			val.componentPrefab = prefab;
			val.agent = null;
			val.displayName = displayName;
			val.description = description ?? "";
			val.nodeCategory = (AgentCategoryEnum)0;
			val.componentScaleXLimits = new FloatRange(1f, 1f);
			val.componentScaleYLimits = new FloatRange(1f, 1f);
			val.componentScaleZLimits = new FloatRange(1f, 1f);
			return val;
		}

		public static AgentGestalt CreateNodeGestalt(Type agent, string displayName, string description = null, AgentCategoryEnum category = 0)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0018: 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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			AgentGestalt val = (AgentGestalt)ScriptableObject.CreateInstance(typeof(AgentGestalt));
			val.componentCategory = (ComponentCategories)0;
			val.properties = new Dictionary<int, Property>();
			val.ports = new Dictionary<int, Port>();
			val.type = (Types)1;
			val.agent = agent;
			val.displayName = displayName;
			val.description = description ?? "";
			val.nodeCategory = category;
			return val;
		}

		private static Port CreateGenericPort(AgentGestalt gestalt, string name, string description, out int recent_port_dict_id)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			Port val = new Port();
			int num = 1;
			try
			{
				num = GetHighestKey(gestalt.ports) + 1;
			}
			catch (Exception)
			{
			}
			int position = 1;
			try
			{
				position = gestalt.ports[num - 1].position + 1;
			}
			catch (Exception)
			{
			}
			val.position = position;
			gestalt.ports.Add(num, val);
			val.name = name;
			val.description = description;
			recent_port_dict_id = num;
			return val;
		}

		public static Port CreateCommandPort(AgentGestalt gestalt, string name, string description, int operation)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			int recent_port_dict_id;
			Port val = CreateGenericPort(gestalt, name, description, out recent_port_dict_id);
			val.operation = operation;
			val.type = (Types)0;
			return val;
		}

		public static Port CreatePropertyPort(AgentGestalt gestalt, string name, string description, Types datatype = 101, bool configurable = true, Data defaultData = null, string reference_name = null)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: 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_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			if (defaultData == null)
			{
				defaultData = new Data();
				defaultData.type = datatype;
			}
			int recent_port_dict_id;
			Port val = CreateGenericPort(gestalt, name, description, out recent_port_dict_id);
			Property val2 = new Property();
			int num = 1;
			try
			{
				num = GetHighestKey(gestalt.ports) + 1;
			}
			catch (Exception)
			{
			}
			val2.position = val.position;
			gestalt.properties.Add(num, val2);
			if (gestalt.agent.IsSubclassOf(typeof(CustomAgent)))
			{
				if (!CustomAgent.properties.ContainsKey(gestalt.agent))
				{
					CustomAgent.properties.Add(gestalt.agent, new Dictionary<string, int>());
				}
				CustomAgent.properties[gestalt.agent].Add(reference_name ?? name, num);
			}
			val2.defaultData = defaultData;
			val2.configurable = configurable;
			val2.name = name;
			val2.description = description;
			val.dataType = datatype;
			val.mappedProperty = num;
			val.type = (Types)1;
			val.expectsData = true;
			return val;
		}

		private static int GetHighestKey(Dictionary<int, Port> l)
		{
			return l.Keys.OrderBy((int b) => b).DefaultIfEmpty(1).LastOrDefault();
		}

		public static Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Types datatype = 101, bool configurable = true, Data defaultData = null, string reference_name = null)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			//IL_0016: 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_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			if (defaultData == null)
			{
				defaultData = new Data();
				defaultData.type = datatype;
			}
			int recent_port_dict_id;
			Port val = CreateGenericPort(gestalt, name, description, out recent_port_dict_id);
			Property val2 = new Property();
			int num = 1;
			try
			{
				num = GetHighestKey(gestalt.ports) + 1;
			}
			catch (Exception)
			{
			}
			val2.position = val.position;
			gestalt.properties.Add(num, val2);
			if (gestalt.agent.IsSubclassOf(typeof(CustomAgent)))
			{
				if (!CustomAgent.outputs.ContainsKey(gestalt.agent))
				{
					CustomAgent.outputs.Add(gestalt.agent, new Dictionary<string, int>());
				}
				CustomAgent.outputs[gestalt.agent].Add(reference_name ?? name, recent_port_dict_id);
			}
			val2.defaultData = defaultData;
			val2.configurable = configurable;
			val2.name = name;
			val2.description = description;
			val.dataType = datatype;
			val.injectedProperty = num;
			val.type = (Types)2;
			return val;
		}

		public static AgentCategoryEnum CustomNodeCategory(string name)
		{
			//IL_0045: 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_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			name = name.ToUpperInvariant();
			if (customNodeCategories.ContainsKey(name))
			{
				return customNodeCategories[name];
			}
			customNodeCategories.Add(name, (AgentCategoryEnum)(++highestNodeCategoryId));
			return customNodeCategories[name];
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "PlasmaCustomAgents";

		public const string PLUGIN_NAME = "PlasmaCustomAgents";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}