From 04ef6120b6b4cf0bc7e514563386e833e9f13c13 Mon Sep 17 00:00:00 2001 From: hippolippo Date: Fri, 14 Oct 2022 17:01:35 -0400 Subject: [PATCH 1/8] Adding Node Properties Methods Created --- Plasma-Custom-Nodes/Program.cs | 54 ++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index 852cfe8..b1c9c0b 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -1,6 +1,7 @@ using HarmonyLib; using System.Reflection; using UnityEngine; +using Behavior; namespace PlasmaModding { public static class CustomNodeManager @@ -8,7 +9,7 @@ public static class CustomNodeManager static IEnumerable agentGestalts = Enumerable.Empty(); static bool loadedNodeResources = false; static bool awoken = false; - static Harmony harmony; + static Harmony? harmony; public static void Awake() { @@ -52,6 +53,56 @@ public static void CreateNode(AgentGestalt gestalt, string unique_node_name) gestalt.ports = new Dictionary(); RegisterGestalt(gestalt); } + private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string name, string description) + { + AgentGestalt.Port port = new AgentGestalt.Port(); + int port_dict_id = (gestalt.ports.Count() > 0) ? gestalt.ports.Keys.Max() + 1 : 1; + int position = (gestalt.ports.Count() > 0) ? gestalt.ports[gestalt.ports.Keys.Max()].position + 1 : 1; + port.position = position; + gestalt.ports.Add(port_dict_id, port); + port.name = name; + port.description = description; + return port; + } + + public static AgentGestalt.Port CreateCommandPort(AgentGestalt gestalt, string name, string description, int operation) + { + AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); + port.operation = operation; + return port; + } + + public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null) + { + if (defaultData == null) + defaultData = new Data(); + AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); + AgentGestalt.Property property = new AgentGestalt.Property(); + int property_dict_id = (gestalt.ports.Count() > 0) ? gestalt.properties.Keys.Max() + 1 : 1; + property.position = port.position; + gestalt.properties.Add(property_dict_id, property); + property.defaultData = defaultData; + property.configurable = configurable; + port.dataType = datatype; + port.mappedProperty = property_dict_id; + return port; + } + + public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype=Data.Types.None, bool configurable=true, Data? defaultData = null) + { + if (defaultData == null) + defaultData = new Data(); + AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); + AgentGestalt.Property property = new AgentGestalt.Property(); + int property_dict_id = (gestalt.ports.Count() > 0) ? gestalt.properties.Keys.Max() + 1 : 1; + property.position = port.position; + gestalt.properties.Add(property_dict_id, property); + property.defaultData = defaultData; + property.configurable = configurable; + port.dataType = datatype; + port.injectedProperty = property_dict_id; + return port; + } [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] {typeof(string), typeof(Type)})] class LoadResourcesPatch @@ -70,6 +121,5 @@ public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine } } } - } } \ No newline at end of file From 6f6daddf3356036586f87902de0db7e2f8ee7efc Mon Sep 17 00:00:00 2001 From: InvertedOwl <70726294+InvertedOwl@users.noreply.github.com> Date: Fri, 14 Oct 2022 20:41:14 -0600 Subject: [PATCH 2/8] Fixed some bugs --- Plasma-Custom-Nodes/Program.cs | 55 ++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index b1c9c0b..aab6e13 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -3,7 +3,8 @@ using UnityEngine; using Behavior; -namespace PlasmaModding { +namespace PlasmaModding +{ public static class CustomNodeManager { static IEnumerable agentGestalts = Enumerable.Empty(); @@ -19,7 +20,7 @@ public static void Awake() harmony = new Harmony("CustomNodeManager"); harmony.PatchAll(Assembly.GetExecutingAssembly()); - if(Holder.agentGestalts != null) + if (Holder.agentGestalts != null) { loadedNodeResources = true; } @@ -30,7 +31,7 @@ private static void RegisterGestalt(AgentGestalt gestalt) Awake(); if (loadedNodeResources) throw new LateGestaltRegistrationException(); - agentGestalts = agentGestalts.Concat(new[] {gestalt}); + agentGestalts = agentGestalts.Concat(new[] { gestalt }); } public class InsufficientGestaltDataException : Exception @@ -42,22 +43,28 @@ public InsufficientGestaltDataException(string message) : base(message) public static void CreateNode(AgentGestalt gestalt, string unique_node_name) { - gestalt.id = (AgentGestaltEnum)unique_node_name.GetHashCode()+1000; - if(gestalt.agent == null) + gestalt.id = (AgentGestaltEnum)unique_node_name.GetHashCode() + 1000; + if (gestalt.agent == null) throw new InsufficientGestaltDataException("No agent attached to gestalt"); if (gestalt.displayName == null) throw new InsufficientGestaltDataException("Node should have a display name"); if (gestalt.properties == null) gestalt.properties = new Dictionary(); - if ( gestalt.ports == null) + if (gestalt.ports == null) gestalt.ports = new Dictionary(); RegisterGestalt(gestalt); } private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string name, string description) { AgentGestalt.Port port = new AgentGestalt.Port(); - int port_dict_id = (gestalt.ports.Count() > 0) ? gestalt.ports.Keys.Max() + 1 : 1; - int position = (gestalt.ports.Count() > 0) ? gestalt.ports[gestalt.ports.Keys.Max()].position + 1 : 1; + int port_dict_id = 1; + try + { + port_dict_id = gestalt.ports.Keys.Last() + 1; + } + catch (Exception e) { } + + int position = 1; port.position = position; gestalt.ports.Add(port_dict_id, port); port.name = name; @@ -69,6 +76,7 @@ public static AgentGestalt.Port CreateCommandPort(AgentGestalt gestalt, string n { AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); port.operation = operation; + port.type = AgentGestalt.Port.Types.Command; return port; } @@ -78,33 +86,54 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string defaultData = new Data(); AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); AgentGestalt.Property property = new AgentGestalt.Property(); - int property_dict_id = (gestalt.ports.Count() > 0) ? gestalt.properties.Keys.Max() + 1 : 1; + int property_dict_id = 1; + try + { + property_dict_id = gestalt.properties.Keys.Last() + 1; + } + catch (Exception e) { } + + property.position = port.position; gestalt.properties.Add(property_dict_id, property); property.defaultData = defaultData; property.configurable = configurable; + property.name = name; + property.description = description; port.dataType = datatype; port.mappedProperty = property_dict_id; + port.type = AgentGestalt.Port.Types.Property; + port.expectsData = true; + + return port; } - - public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype=Data.Types.None, bool configurable=true, Data? defaultData = null) + + public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null) { if (defaultData == null) defaultData = new Data(); AgentGestalt.Port port = CreateGenericPort(gestalt, name, description); AgentGestalt.Property property = new AgentGestalt.Property(); - int property_dict_id = (gestalt.ports.Count() > 0) ? gestalt.properties.Keys.Max() + 1 : 1; + int property_dict_id = 1; + try + { + property_dict_id = gestalt.properties.Keys.Last() + 1; + } + catch (Exception e) { } property.position = port.position; gestalt.properties.Add(property_dict_id, property); property.defaultData = defaultData; property.configurable = configurable; + property.name = name; + property.description = description; port.dataType = datatype; port.injectedProperty = property_dict_id; + port.type = AgentGestalt.Port.Types.Output; return port; } - [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] {typeof(string), typeof(Type)})] + [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string), typeof(Type) })] class LoadResourcesPatch { public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine.Object[] __result) From 55d3b5e89cb311588f053b4beefabe3f3435b25a Mon Sep 17 00:00:00 2001 From: InvertedOwl <70726294+InvertedOwl@users.noreply.github.com> Date: Fri, 14 Oct 2022 21:17:01 -0600 Subject: [PATCH 3/8] Fixed GetHighestKey but IDs start at three now --- Plasma-Custom-Nodes/Program.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index aab6e13..6df2c97 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -2,6 +2,7 @@ using System.Reflection; using UnityEngine; using Behavior; +using System.Linq; namespace PlasmaModding { @@ -60,11 +61,18 @@ private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string int port_dict_id = 1; try { - port_dict_id = gestalt.ports.Keys.Last() + 1; + port_dict_id = GetHighestKey(gestalt.ports) + 1; } catch (Exception e) { } int position = 1; + try + { + position = gestalt.ports[port_dict_id - 1].position + 1; + } + catch (Exception e) { } + + port.position = position; gestalt.ports.Add(port_dict_id, port); port.name = name; @@ -89,7 +97,7 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string int property_dict_id = 1; try { - property_dict_id = gestalt.properties.Keys.Last() + 1; + property_dict_id = GetHighestKey(gestalt.ports) + 1; } catch (Exception e) { } @@ -109,6 +117,11 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string return port; } + private static int GetHighestKey(Dictionary l) + { + return l.Keys.OrderBy(b => b).Last(); + } + public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null) { if (defaultData == null) @@ -118,7 +131,7 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na int property_dict_id = 1; try { - property_dict_id = gestalt.properties.Keys.Last() + 1; + property_dict_id = GetHighestKey(gestalt.ports) + 1; } catch (Exception e) { } property.position = port.position; From 47a43d66d60d5a713f762b14bcafae7cd7023318 Mon Sep 17 00:00:00 2001 From: hippolippo Date: Sat, 15 Oct 2022 21:33:00 -0400 Subject: [PATCH 4/8] Fixed issue with harmony signature using string instead of string[] --- Plasma-Custom-Nodes/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index 6df2c97..a80b4fc 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -146,13 +146,13 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na return port; } - [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string), typeof(Type) })] + [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string[]), typeof(Type) })] class LoadResourcesPatch { public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine.Object[] __result) { if (path == "Gestalts/Logic Agents" && systemTypeInstance == typeof(AgentGestalt) && !loadedNodeResources) - { + { int size = __result.Length; int newSize = size + agentGestalts.Count(); UnityEngine.Object[] temp = new UnityEngine.Object[newSize]; From 12aafe9359111c37cc33790d3e62b782254c0217 Mon Sep 17 00:00:00 2001 From: Hippolippo Date: Sat, 15 Oct 2022 21:41:15 -0400 Subject: [PATCH 5/8] Undid a mistake commit --- Plasma-Custom-Nodes/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index a80b4fc..3502446 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -146,7 +146,7 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na return port; } - [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string[]), typeof(Type) })] + [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string), typeof(Type) })] class LoadResourcesPatch { public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine.Object[] __result) @@ -164,4 +164,4 @@ public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine } } } -} \ No newline at end of file +} From f46e71f3659b8793b361d1b80409627f8a70ff29 Mon Sep 17 00:00:00 2001 From: hippolippo Date: Sun, 16 Oct 2022 00:15:01 -0400 Subject: [PATCH 6/8] Created Class Inheriting Agent to use for Custom Agents --- Plasma-Custom-Nodes/CustomAgent.cs | 31 ++++++++++++++++++++++++++++++ Plasma-Custom-Nodes/Program.cs | 14 +++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 Plasma-Custom-Nodes/CustomAgent.cs diff --git a/Plasma-Custom-Nodes/CustomAgent.cs b/Plasma-Custom-Nodes/CustomAgent.cs new file mode 100644 index 0000000..93f9333 --- /dev/null +++ b/Plasma-Custom-Nodes/CustomAgent.cs @@ -0,0 +1,31 @@ +using Behavior; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlasmaModding +{ + public class CustomAgent: Agent + { + protected static Dictionary properties = new Dictionary(); + + protected AgentProperty GetProperty(string name) + { + return _runtimeProperties[properties[name]]; + } + + protected static Dictionary outputs = new Dictionary(); + + protected static void WriteOutput(string name, Data data, SketchNode node) + { + node.ports[outputs[name]].Commit(data); + } + + protected void WriteOutput(string name, Data data) + { + currentSketchNode.ports[outputs[name]].Commit(data); + } + } +} diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index 3502446..75ae710 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -63,14 +63,14 @@ private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string { port_dict_id = GetHighestKey(gestalt.ports) + 1; } - catch (Exception e) { } + catch (Exception) { } int position = 1; try { position = gestalt.ports[port_dict_id - 1].position + 1; } - catch (Exception e) { } + catch (Exception) { } port.position = position; @@ -88,7 +88,7 @@ public static AgentGestalt.Port CreateCommandPort(AgentGestalt gestalt, string n return port; } - public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null) + public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null, string? reference_name = null) { if (defaultData == null) defaultData = new Data(); @@ -104,6 +104,8 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string property.position = port.position; gestalt.properties.Add(property_dict_id, property); + if (gestalt.agent.GetType() == typeof(CustomAgent)) + ((Dictionary) gestalt.agent.GetField("properties", BindingFlags.NonPublic | BindingFlags.Static).GetValue(gestalt.agent)).Add(reference_name ?? name, property_dict_id); property.defaultData = defaultData; property.configurable = configurable; property.name = name; @@ -122,7 +124,7 @@ private static int GetHighestKey(Dictionary l) return l.Keys.OrderBy(b => b).Last(); } - public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null) + public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string name, string description, Data.Types datatype = Data.Types.None, bool configurable = true, Data? defaultData = null, string? reference_name = null) { if (defaultData == null) defaultData = new Data(); @@ -133,9 +135,11 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na { property_dict_id = GetHighestKey(gestalt.ports) + 1; } - catch (Exception e) { } + catch (Exception) { } property.position = port.position; gestalt.properties.Add(property_dict_id, property); + if (gestalt.agent.GetType() == typeof(CustomAgent)) + ((Dictionary)gestalt.agent.GetField("outputs", BindingFlags.NonPublic | BindingFlags.Static).GetValue(gestalt.agent)).Add(reference_name ?? name, property_dict_id); property.defaultData = defaultData; property.configurable = configurable; property.name = name; From 78daa9470c213467b3a31a4f52c40c01ad442bd8 Mon Sep 17 00:00:00 2001 From: hippolippo Date: Sun, 16 Oct 2022 02:56:19 -0400 Subject: [PATCH 7/8] Adding Gestalt Creator and fixed the Custom Agent class --- Plasma-Custom-Nodes/CustomAgent.cs | 27 +++-- .../Plasma-Custom-Nodes.csproj | 3 + Plasma-Custom-Nodes/Program.cs | 105 +++++++++++++++++- 3 files changed, 122 insertions(+), 13 deletions(-) diff --git a/Plasma-Custom-Nodes/CustomAgent.cs b/Plasma-Custom-Nodes/CustomAgent.cs index 93f9333..a1ffb79 100644 --- a/Plasma-Custom-Nodes/CustomAgent.cs +++ b/Plasma-Custom-Nodes/CustomAgent.cs @@ -4,28 +4,41 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection; +using UnityEngine; namespace PlasmaModding { public class CustomAgent: Agent { - protected static Dictionary properties = new Dictionary(); + + internal static Dictionary> properties = new Dictionary>(); protected AgentProperty GetProperty(string name) { - return _runtimeProperties[properties[name]]; + return _runtimeProperties[properties[this.GetType()][name]]; } - protected static Dictionary outputs = new Dictionary(); - + internal static Dictionary> outputs = new Dictionary>(); + /* protected static void WriteOutput(string name, Data data, SketchNode node) { - node.ports[outputs[name]].Commit(data); - } + node.ports[outputs[MethodBase.GetCurrentMethod().DeclaringType][name]].Commit(data); + }*/ protected void WriteOutput(string name, Data data) { - currentSketchNode.ports[outputs[name]].Commit(data); + Debug.Log("1"); + var a = currentSketchNode.ports; + Debug.Log("2"); + var b = outputs[this.GetType()]; + Debug.Log("3"); + var c = b[name]; + Debug.Log("4"); + var d = a[c]; + Debug.Log("5"); + d.Commit(data); + //currentSketchNode.ports[outputs[this.GetType()][name]].Commit(data); } } } diff --git a/Plasma-Custom-Nodes/Plasma-Custom-Nodes.csproj b/Plasma-Custom-Nodes/Plasma-Custom-Nodes.csproj index 23668bb..de0795e 100644 --- a/Plasma-Custom-Nodes/Plasma-Custom-Nodes.csproj +++ b/Plasma-Custom-Nodes/Plasma-Custom-Nodes.csproj @@ -19,6 +19,9 @@ D:\SteamLibrary\steamapps\common\Plasma Demo\Plasma_Data\Managed\PlasmaLibrary.dll + + D:\SteamLibrary\steamapps\common\Plasma Demo\Plasma_Data\Managed\Sirenix.Serialization.dll + D:\SteamLibrary\steamapps\common\Plasma Demo\Plasma_Data\Managed\UnityEngine.dll diff --git a/Plasma-Custom-Nodes/Program.cs b/Plasma-Custom-Nodes/Program.cs index 75ae710..444e77f 100644 --- a/Plasma-Custom-Nodes/Program.cs +++ b/Plasma-Custom-Nodes/Program.cs @@ -11,7 +11,10 @@ public static class CustomNodeManager static IEnumerable agentGestalts = Enumerable.Empty(); static bool loadedNodeResources = false; static bool awoken = false; + static Dictionary customCategories = new Dictionary(); + private static int highestCategoryId = 3; static Harmony? harmony; + private static int recent_port_dict_id; public static void Awake() { @@ -55,6 +58,21 @@ public static void CreateNode(AgentGestalt gestalt, string unique_node_name) gestalt.ports = new Dictionary(); RegisterGestalt(gestalt); } + public static AgentGestalt CreateGestalt(Type agent, string displayName, string? description = null, AgentCategoryEnum category = AgentCategoryEnum.Misc) + { + AgentGestalt gestalt = (AgentGestalt)ScriptableObject.CreateInstance(typeof(AgentGestalt)); + gestalt.componentCategory = AgentGestalt.ComponentCategories.Behavior; + gestalt.properties = new Dictionary(); + gestalt.ports = new Dictionary(); + gestalt.type = AgentGestalt.Types.Logic; + + gestalt.agent = agent; + gestalt.displayName = displayName; + gestalt.description = description; + gestalt.nodeCategory = category; + + return gestalt; + } private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string name, string description) { AgentGestalt.Port port = new AgentGestalt.Port(); @@ -77,6 +95,7 @@ private static AgentGestalt.Port CreateGenericPort(AgentGestalt gestalt, string gestalt.ports.Add(port_dict_id, port); port.name = name; port.description = description; + recent_port_dict_id = port_dict_id; return port; } @@ -104,8 +123,15 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string property.position = port.position; gestalt.properties.Add(property_dict_id, property); - if (gestalt.agent.GetType() == typeof(CustomAgent)) - ((Dictionary) gestalt.agent.GetField("properties", BindingFlags.NonPublic | BindingFlags.Static).GetValue(gestalt.agent)).Add(reference_name ?? name, property_dict_id); + if (gestalt.agent.IsSubclassOf(typeof(CustomAgent))) + { + if (!CustomAgent.properties.ContainsKey(gestalt.agent)) + { + CustomAgent.properties.Add(gestalt.agent, new Dictionary()); + + } + CustomAgent.properties[gestalt.agent].Add(reference_name ?? name, property_dict_id); + } property.defaultData = defaultData; property.configurable = configurable; property.name = name; @@ -115,7 +141,6 @@ public static AgentGestalt.Port CreatePropertyPort(AgentGestalt gestalt, string port.type = AgentGestalt.Port.Types.Property; port.expectsData = true; - return port; } @@ -138,8 +163,15 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na catch (Exception) { } property.position = port.position; gestalt.properties.Add(property_dict_id, property); - if (gestalt.agent.GetType() == typeof(CustomAgent)) - ((Dictionary)gestalt.agent.GetField("outputs", BindingFlags.NonPublic | BindingFlags.Static).GetValue(gestalt.agent)).Add(reference_name ?? name, property_dict_id); + if (gestalt.agent.IsSubclassOf(typeof(CustomAgent))) + { + if (!CustomAgent.outputs.ContainsKey(gestalt.agent)) + { + CustomAgent.outputs.Add(gestalt.agent, new Dictionary()); + + } + CustomAgent.outputs[gestalt.agent].Add(reference_name ?? name, recent_port_dict_id); + } property.defaultData = defaultData; property.configurable = configurable; property.name = name; @@ -150,8 +182,17 @@ public static AgentGestalt.Port CreateOutputPort(AgentGestalt gestalt, string na return port; } + public static AgentCategoryEnum CustomCategory(string name) + { + name = name.ToUpperInvariant(); + if (customCategories.ContainsKey(name)) + return customCategories[name]; + customCategories.Add(name, (AgentCategoryEnum)(++highestCategoryId)); + return (AgentCategoryEnum) highestCategoryId; + } + [HarmonyPatch(typeof(Resources), "LoadAll", new Type[] { typeof(string), typeof(Type) })] - class LoadResourcesPatch + private class LoadResourcesPatch { public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine.Object[] __result) { @@ -167,5 +208,57 @@ public static void Postfix(string path, Type systemTypeInstance, ref UnityEngine } } } + + [HarmonyPatch(typeof(Visor.ProcessorUICategoryItem), nameof(Visor.ProcessorUICategoryItem.Setup))] + private class AddCategoryToDictPatch + { + static int applied = 0; + public static void Prefix() + { + if (Holder.instance.agentCategories != null && applied < customCategories.Count()) + foreach (string categoryName in customCategories.Keys) + { + if (!Holder.instance.agentCategories.ContainsKey(customCategories[categoryName])){ + applied++; + Holder.instance.agentCategories.Add(customCategories[categoryName], categoryName); + } + } + } + } + + [HarmonyPatch(typeof(System.Enum), "GetNames")] + public class EnumNamePatch + { + public static void Postfix(System.Type enumType, ref string[] __result) + { + if (enumType == typeof(AgentCategoryEnum)) + { + string[] tabs = customCategories.Keys.ToArray(); + string[] names = new string[__result.Length + tabs.Length]; + __result.CopyTo(names, 0); + tabs.CopyTo(names, __result.Length); + __result = names; + } + } + } + + [HarmonyPatch(typeof(System.Enum), "TryParseEnum")] + public class EnumParsePatch + { + public static void Postfix(System.Type enumType, string value, ref object parseResult, ref bool __result) + { + if (!__result && enumType == typeof(AgentCategoryEnum) && customCategories.ContainsKey(value)) + { + __result = true; + System.Type EnumResult = typeof(System.Enum).GetNestedType("EnumResult", BindingFlags.NonPublic); + MethodInfo Init = EnumResult.GetMethod("Init", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo parsedEnum = EnumResult.GetField("parsedEnum", BindingFlags.NonPublic | BindingFlags.Instance); + var presult = System.Convert.ChangeType(System.Activator.CreateInstance(EnumResult), EnumResult); + Init.Invoke(presult, new object[] { false }); + parsedEnum.SetValue(presult, customCategories[value]); + parseResult = presult; + } + } + } } } From 33d5e83e65f6809f91ce77d6c65e707ddcb308d8 Mon Sep 17 00:00:00 2001 From: hippolippo Date: Sun, 16 Oct 2022 02:57:50 -0400 Subject: [PATCH 8/8] Removed debug print messages --- Plasma-Custom-Nodes/CustomAgent.cs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/Plasma-Custom-Nodes/CustomAgent.cs b/Plasma-Custom-Nodes/CustomAgent.cs index a1ffb79..85cd951 100644 --- a/Plasma-Custom-Nodes/CustomAgent.cs +++ b/Plasma-Custom-Nodes/CustomAgent.cs @@ -20,25 +20,10 @@ protected AgentProperty GetProperty(string name) } internal static Dictionary> outputs = new Dictionary>(); - /* - protected static void WriteOutput(string name, Data data, SketchNode node) - { - node.ports[outputs[MethodBase.GetCurrentMethod().DeclaringType][name]].Commit(data); - }*/ protected void WriteOutput(string name, Data data) { - Debug.Log("1"); - var a = currentSketchNode.ports; - Debug.Log("2"); - var b = outputs[this.GetType()]; - Debug.Log("3"); - var c = b[name]; - Debug.Log("4"); - var d = a[c]; - Debug.Log("5"); - d.Commit(data); - //currentSketchNode.ports[outputs[this.GetType()][name]].Commit(data); + currentSketchNode.ports[outputs[this.GetType()][name]].Commit(data); } } }