From 36c4005232895d8a4d7f6f78f4c98f72de60bf12 Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Thu, 11 Jul 2024 05:13:05 +0100 Subject: [PATCH] Change OnAwake to OnStart, add IsValidGenericTypeDriver and StringToTypeDriver --- .../Data Feeds/Feeds/ComponentsDataFeed.cs | 4 +-- .../Utility/IsValidGenericTypeDriver.cs | 28 +++++++++++++++ .../Components/Utility/StringToTypeDriver.cs | 36 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs create mode 100644 ProjectObsidian/Components/Utility/StringToTypeDriver.cs diff --git a/ProjectObsidian/Components/Radiant UI/Data Feeds/Feeds/ComponentsDataFeed.cs b/ProjectObsidian/Components/Radiant UI/Data Feeds/Feeds/ComponentsDataFeed.cs index 1b3faf2..d0b1576 100644 --- a/ProjectObsidian/Components/Radiant UI/Data Feeds/Feeds/ComponentsDataFeed.cs +++ b/ProjectObsidian/Components/Radiant UI/Data Feeds/Feeds/ComponentsDataFeed.cs @@ -120,9 +120,9 @@ private void Unsubscribe(Slot s) s.ComponentRemoved -= OnSlotComponentRemoved; } - protected override void OnAwake() + protected override void OnStart() { - base.OnAwake(); + base.OnStart(); _lastSlot = TargetSlot.Target; if (_lastSlot != null) { diff --git a/ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs b/ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs new file mode 100644 index 0000000..1f63e89 --- /dev/null +++ b/ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs @@ -0,0 +1,28 @@ +using Elements.Core; +using FrooxEngine; +using System; +using System.Linq; + +namespace Obsidian; + +[Category(new string[] { "Obsidian/Utility" })] +public class IsValidGenericTypeDriver : Component +{ + public readonly SyncType Type; + + public readonly FieldDrive Target; + + protected override void OnChanges() + { + base.OnChanges(); + if (!Target.IsLinkValid) return; + if (Type.Value == null || !Type.Value.IsGenericType) + { + Target.Target.Value = false; + } + else + { + Target.Target.Value = Type.Value.IsValidGenericType(validForInstantiation: true); + } + } +} \ No newline at end of file diff --git a/ProjectObsidian/Components/Utility/StringToTypeDriver.cs b/ProjectObsidian/Components/Utility/StringToTypeDriver.cs new file mode 100644 index 0000000..ce2c61a --- /dev/null +++ b/ProjectObsidian/Components/Utility/StringToTypeDriver.cs @@ -0,0 +1,36 @@ +using Elements.Core; +using FrooxEngine; +using System; + +namespace Obsidian; + +[Category(new string[] { "Obsidian/Utility" })] +public class StringToTypeDriver : Component +{ + public readonly Sync Text; + + public readonly FieldDrive Target; + + protected override void OnChanges() + { + base.OnChanges(); + if (!Target.IsLinkValid) return; + if (string.IsNullOrWhiteSpace(Text.Value)) + { + Target.Target.Value = null; + } + else + { + try + { + var parsedType = WorkerManager.ParseNiceType(Text.Value); + Target.Target.Value = parsedType; + } + catch (Exception ex) + { + UniLog.Warning("Exception when parsing type from string:\n" + ex.ToString()); + Target.Target.Value = null; + } + } + } +} \ No newline at end of file