Skip to content

Commit 383eca2

Browse files
committed
Add QualifiedName to DynamicVariableIdentity and add filter for ML's SharedConfig system to the dyn var extensions
1 parent a11936c commit 383eca2

5 files changed

Lines changed: 114 additions & 70 deletions

File tree

MonkeyLoader.Resonite.Core/DynamicVariableExtensions.cs

Lines changed: 86 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
using Elements.Core;
22
using FrooxEngine;
33
using HarmonyLib;
4-
using System;
5-
using System.Collections.Generic;
64
using System.Diagnostics.CodeAnalysis;
7-
using System.IO;
8-
using System.Linq;
95
using System.Runtime.CompilerServices;
10-
using System.Text;
11-
using System.Threading.Tasks;
126

137
namespace MonkeyLoader.Resonite
148
{
@@ -19,6 +13,12 @@ namespace MonkeyLoader.Resonite
1913
[TypeForwardedFrom("MonkeyLoader.Resonite.Integration")]
2014
public static class DynamicVariableExtensions
2115
{
16+
/// <summary>
17+
/// The name of the shared config slot in a <see cref="World"/>'s
18+
/// <see cref="World.AssetsSlot">Assets</see> <see cref="Slot"/>.
19+
/// </summary>
20+
public const string SharedConfigIdentifier = "MonkeyLoader.SharedConfig";
21+
2222
//public static DynamicReference<T> CreateReferenceVariable<T>(this SyncRef<T> syncRef, string name, bool overrideOnLink = false, bool persistent = true)
2323
// where T : class, IWorldElement
2424
//{
@@ -70,11 +70,11 @@ public static DynamicVariableSpace FindOrCreateSpace(this Slot slot, string? spa
7070
/// </summary>
7171
/// <remarks>
7272
/// This function is different from a regular
73-
/// <c><see cref="Slot"/>.<see cref="Slot.GetComponentInParents{T}(Predicate{T}, bool, bool)">GetComponentsInParents</see>&lt;<see cref="DynamicVariableSpace"/>&gt;()</c>
74-
/// call,<br/> as it considers that <see cref="DynamicVariableSpace"/>s deeper in the hierarchy
75-
/// hide ones higher up with the same <see cref="DynamicVariableSpace.SpaceName">name</see>.<br/>
73+
/// <c><see cref="Slot"/>.<see cref="Slot.GetComponentsInParents{T}(Predicate{T})">GetComponentsInParents</see>&lt;<see cref="DynamicVariableSpace"/>&gt;()</c> call,<br/>
74+
/// as it considers that <see cref="DynamicVariableSpace"/>s deeper in the hierarchy
75+
/// hide ones higher up with the same <see cref="DynamicVariableSpace.CurrentName">name</see>.<br/>
7676
/// If a <see cref="DynamicVariableSpace"/> deeper in the hierarchy doesn't pass the <paramref name="filter"/>,<br/>
77-
/// it will still hide any higher up ones with the same <see cref="DynamicVariableSpace.SpaceName">name</see>
77+
/// it will still hide any higher up ones with the same <see cref="DynamicVariableSpace.CurrentName">name</see>
7878
/// that might pass it, because they don't apply to this <see cref="Slot"/>.
7979
/// </remarks>
8080
/// <param name="slot">The <see cref="Slot"/> to find all applicable <see cref="DynamicVariableSpace"/>s for.</param>
@@ -92,17 +92,14 @@ public static IEnumerable<DynamicVariableSpace> GetAvailableSpaces(this Slot slo
9292

9393
foreach (var currentSpace in currentSpaces)
9494
{
95-
if (!filter(currentSpace))
96-
{
97-
presentNames.Add(currentSpace.SpaceName);
95+
// Use CurrentName because it includes the processing of the SpaceName
96+
if (!presentNames.Add(currentSpace.CurrentName))
9897
continue;
99-
}
10098

101-
if (presentNames.Contains(currentSpace.SpaceName))
99+
if (!filter(currentSpace))
102100
continue;
103101

104102
result.Add(currentSpace);
105-
presentNames.Add(currentSpace.SpaceName.Value);
106103
}
107104

108105
slot = slot.Parent;
@@ -352,40 +349,6 @@ public static IEnumerable<DynamicVariableIdentity> GetVariableIdentities(this Dy
352349
public static IEnumerable<DynamicVariableIdentity> GetVariableIdentities(this DynamicVariableSpace space, string name)
353350
=> [.. space.GetVariableIdentities().Where(id => id.Name == name)];
354351

355-
//public static DynamicField<T>? CreateVariable<T>(this IField<T> field, string name, bool overrideOnLink = false, bool persistent = true)
356-
//{
357-
// var variable = field.FindNearestParent<Slot>().AttachComponent<DynamicField<T>>();
358-
// variable.TargetField.Target = field;
359-
// variable.VariableName.Value = name;
360-
// variable.OverrideOnLink.Value = overrideOnLink;
361-
// variable.Persistent = persistent;
362-
363-
// return variable;
364-
//}
365-
366-
//public static DynamicReferenceVariableDriver<T> DriveReferenceFromVariable<T>(this SyncRef<T> syncRef, string name, T? defaultTarget = default, bool persistent = true)
367-
// where T : class, IWorldElement
368-
//{
369-
// var driver = syncRef.FindNearestParent<Slot>().AttachComponent<DynamicReferenceVariableDriver<T>>();
370-
// driver.Target.Target = syncRef;
371-
// driver.VariableName.Value = name;
372-
// driver.DefaultTarget.Target = defaultTarget!;
373-
// driver.Persistent = persistent;
374-
375-
// return driver;
376-
//}
377-
378-
//public static DynamicValueVariableDriver<T> DriveValueFromVariable<T>(this IField<T> field, string name, T? defaultValue = default, bool persistent = true)
379-
//{
380-
// var driver = field.FindNearestParent<Slot>().AttachComponent<DynamicValueVariableDriver<T>>();
381-
// driver.Target.Target = field;
382-
// driver.VariableName.Value = name;
383-
// driver.DefaultValue.Value = defaultValue!;
384-
// driver.Persistent = persistent;
385-
386-
// return driver;
387-
//}
388-
389352
/// <summary>
390353
/// Determines whether this <see cref="IDynamicVariable">dynamic variable</see>
391354
/// is linked against a <see cref="DynamicVariableSpace"/>.
@@ -411,6 +374,40 @@ public static bool IsLinkedToSpace(this IDynamicVariable dynamicVariable)
411374
public static bool IsLinkedToSpace(this IDynamicVariable dynamicVariable, DynamicVariableSpace space)
412375
=> dynamicVariable.TryGetLinkedSpace(out var linkedSpace) && linkedSpace == space;
413376

377+
/// <summary>
378+
/// Determines whether this dynamic variable is part of
379+
/// the <see cref="SharedConfigIdentifier">shared config</see>.
380+
/// </summary>
381+
/// <param name="dynamicVariable">The dynamic variable to check.</param>
382+
/// <returns>
383+
/// <see langword="false"/> if this dynamic variable is part of
384+
/// the <see cref="SharedConfigIdentifier">shared config</see>; otherwise, <see langword="true"/>.
385+
/// </returns>
386+
public static bool IsNotSharedConfigVariable(this IDynamicVariable dynamicVariable)
387+
=> !dynamicVariable.VariableName.StartsWith(SharedConfigIdentifier) || dynamicVariable.GetLinkedSpace().CurrentName is not "World";
388+
389+
/// <summary>
390+
/// Determines whether this dynamic variable identity is part of
391+
/// the <see cref="SharedConfigIdentifier">shared config</see>.
392+
/// </summary>
393+
/// <param name="identity">The dynamic variable identity to check.</param>
394+
/// <returns>
395+
/// <see langword="false"/> if this dynamic variable identity is part of
396+
/// the <see cref="SharedConfigIdentifier">shared config</see>; otherwise, <see langword="true"/>.
397+
/// </returns>
398+
public static bool IsNotSharedConfigVariable(this DynamicVariableIdentity identity)
399+
=> identity.Space.CurrentName is not "World" || !identity.Name.StartsWith(SharedConfigIdentifier);
400+
401+
//public static DynamicValueVariableDriver<T> DriveValueFromVariable<T>(this IField<T> field, string name, T? defaultValue = default, bool persistent = true)
402+
//{
403+
// var driver = field.FindNearestParent<Slot>().AttachComponent<DynamicValueVariableDriver<T>>();
404+
// driver.Target.Target = field;
405+
// driver.VariableName.Value = name;
406+
// driver.DefaultValue.Value = defaultValue!;
407+
// driver.Persistent = persistent;
408+
// return driver;
409+
//}
410+
414411
/// <summary>
415412
/// Tests whether this string is a <see cref="DynamicVariableHelper.IsValidName">valid dynamic variable name</see>.
416413
/// </summary>
@@ -429,6 +426,17 @@ public static bool IsValidName(this string? variableName)
429426
public static void ParseAsPath(this string? path, out string? spaceName, out string? variableName)
430427
=> DynamicVariableHelper.ParsePath(path!, out spaceName, out variableName);
431428

429+
//public static DynamicReferenceVariableDriver<T> DriveReferenceFromVariable<T>(this SyncRef<T> syncRef, string name, T? defaultTarget = default, bool persistent = true)
430+
// where T : class, IWorldElement
431+
//{
432+
// var driver = syncRef.FindNearestParent<Slot>().AttachComponent<DynamicReferenceVariableDriver<T>>();
433+
// driver.Target.Target = syncRef;
434+
// driver.VariableName.Value = name;
435+
// driver.DefaultTarget.Target = defaultTarget!;
436+
// driver.Persistent = persistent;
437+
// return driver;
438+
//}
439+
432440
/// <summary>
433441
/// <see cref="DynamicVariableHelper.ProcessName">Processes</see>
434442
/// this string to a <see cref="string.Trim()">trimmed</see>
@@ -439,6 +447,34 @@ public static void ParseAsPath(this string? path, out string? spaceName, out str
439447
public static string? ProcessName(this string? variableName)
440448
=> DynamicVariableHelper.ProcessName(variableName!);
441449

450+
/// <summary>
451+
/// Removes all dynamic variables from this sequence that are
452+
/// <see cref="IsNotSharedConfigVariable(IDynamicVariable)">part of the shared config</see>.
453+
/// </summary>
454+
/// <param name="dynamicVariables">The sequence to remove <see cref="SharedConfigIdentifier">shared config</see> variables from.</param>
455+
/// <returns>The sequence without any <see cref="SharedConfigIdentifier">shared config</see> variables.</returns>
456+
public static IEnumerable<IDynamicVariable> RemoveSharedConfigVariables(this IEnumerable<IDynamicVariable> dynamicVariables)
457+
=> dynamicVariables.Where(IsNotSharedConfigVariable);
458+
459+
/// <summary>
460+
/// Removes all dynamic variables identities from this sequence that are
461+
/// <see cref="IsNotSharedConfigVariable(DynamicVariableIdentity)">part of the shared config</see>.
462+
/// </summary>
463+
/// <param name="identities">The sequence to remove the identities of <see cref="SharedConfigIdentifier">shared config</see> variables from.</param>
464+
/// <returns>The sequence without any identities of <see cref="SharedConfigIdentifier">shared config</see> variables.</returns>
465+
public static IEnumerable<DynamicVariableIdentity> RemoveSharedConfigVariables(this IEnumerable<DynamicVariableIdentity> identities)
466+
=> identities.Where(IsNotSharedConfigVariable);
467+
468+
//public static DynamicField<T>? CreateVariable<T>(this IField<T> field, string name, bool overrideOnLink = false, bool persistent = true)
469+
//{
470+
// var variable = field.FindNearestParent<Slot>().AttachComponent<DynamicField<T>>();
471+
// variable.TargetField.Target = field;
472+
// variable.VariableName.Value = name;
473+
// variable.OverrideOnLink.Value = overrideOnLink;
474+
// variable.Persistent = persistent;
475+
// return variable;
476+
//}
477+
442478
/// <summary>
443479
/// Tries to get the <see cref="DynamicVariableHandler{T}"/> of this
444480
/// <see cref="IDynamicVariable{T}">dynamic variable</see>.

MonkeyLoader.Resonite.Core/DynamicVariableIdentity.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using FrooxEngine;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
52
using System.Runtime.CompilerServices;
6-
using System.Text;
73

84
namespace MonkeyLoader.Resonite
95
{
@@ -15,10 +11,31 @@ namespace MonkeyLoader.Resonite
1511
public readonly struct DynamicVariableIdentity : IEquatable<DynamicVariableIdentity>
1612
{
1713
/// <summary>
18-
/// Gets the <see cref="IDynamicVariable.VariableName">name</see> of this Dynamic Variable.
14+
/// Gets the <see cref="DynamicVariableSpace.VariableIdentity.name">name</see> of this Dynamic Variable.
1915
/// </summary>
2016
public readonly string Name { get; }
2117

18+
/// <summary>
19+
/// Gets the qualified name of the this Dynamic Variable
20+
/// that will always link to its <see cref="Space">Space</see>.
21+
/// </summary>
22+
/// <remarks>
23+
/// Because of <see cref="DynamicVariableHelper.ProcessName(string)">processing</see>,
24+
/// this may use a different space prefix than the <see cref="DynamicVariableSpace.SpaceName"/>.<br/>
25+
/// If the processed space name <see cref="string.IsNullOrWhiteSpace(string?)">is null or white space</see>
26+
/// then it can only have variables bind to it when <see cref="DynamicVariableSpace.OnlyDirectBinding">OnlyDirectBinding</see> is disabled.
27+
/// </remarks>
28+
/// <value>
29+
/// <c>$"{<see cref="Space">Space</see>.<see cref="DynamicVariableSpace.CurrentName">CurrentName</see>}/{<see cref="Name">Name</see>}"</c>
30+
/// if the <see cref="DynamicVariableSpace.CurrentName">current space name</see>
31+
/// <see cref="string.IsNullOrWhiteSpace(string?)">is null or white space</see>;
32+
/// otherwise, just the <see cref="Name">Name</see>.
33+
/// </value>
34+
public readonly string QualifiedName
35+
=> !string.IsNullOrWhiteSpace(Space.CurrentName)
36+
? $"{Space.CurrentName}/{Name}"
37+
: Name;
38+
2239
/// <summary>
2340
/// Gets the <see cref="DynamicVariableSpace"/> that this Dynamic Variable is a part of.
2441
/// </summary>
@@ -34,7 +51,7 @@ namespace MonkeyLoader.Resonite
3451
/// </summary>
3552
/// <param name="space">The <see cref="DynamicVariableSpace"/> that this Dynamic Variable is a part of.</param>
3653
/// <param name="type">The <see cref="System.Type"/> of this Dynamic Variable.</param>
37-
/// <param name="name">The <see cref="IDynamicVariable.VariableName">name</see> of this Dynamic Variable.</param>
54+
/// <param name="name">The <see cref="DynamicVariableSpace.VariableIdentity.name">name</see> of this Dynamic Variable.</param>
3855
public DynamicVariableIdentity(DynamicVariableSpace space, Type type, string name)
3956
{
4057
Space = space;
@@ -48,11 +65,8 @@ public DynamicVariableIdentity(DynamicVariableSpace space, Type type, string nam
4865
/// <param name="space">The <see cref="DynamicVariableSpace"/> that this Dynamic Variable is a part of.</param>
4966
/// <param name="variableIdentity">The Dynamic Variable's identity within the given <paramref name="space"/>.</param>
5067
public DynamicVariableIdentity(DynamicVariableSpace space, DynamicVariableSpace.VariableIdentity variableIdentity)
51-
{
52-
Space = space;
53-
Type = variableIdentity.type;
54-
Name = variableIdentity.name;
55-
}
68+
: this(space, variableIdentity.type, variableIdentity.name)
69+
{ }
5670

5771
/// <summary>
5872
/// Determines whether two Dynamic Variable identities refer to different ones.

MonkeyLoader.Resonite.Integration/Configuration/SharedConfig.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
using MonkeyLoader.Components;
33
using MonkeyLoader.Configuration;
44
using MonkeyLoader.Meta;
5-
using System;
6-
using System.Collections.Generic;
75
using System.Diagnostics.CodeAnalysis;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
116

127
namespace MonkeyLoader.Resonite.Configuration
138
{
@@ -25,7 +20,7 @@ public static class SharedConfig
2520
/// The name of the <see cref="GetSharedConfigSlot(World)">shared config slot</see>
2621
/// in a <see cref="World"/>'s <see cref="World.AssetsSlot">Assets</see> <see cref="Slot"/>.
2722
/// </summary>
28-
public const string Identifier = "MonkeyLoader.SharedConfig";
23+
public const string Identifier = DynamicVariableExtensions.SharedConfigIdentifier;
2924

3025
/// <summary>
3126
/// The prefix for the <see cref="IDefiningConfigKey{T}.SetValue(T, string?)">SetValue</see>

MonkeyLoader.Resonite.Integration/UI/ContextMenus/InteractionHandlerContextMenuInjector.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
using FrooxEngine.CommonAvatar;
44
using FrooxEngine.UIX;
55
using HarmonyLib;
6-
using System;
7-
using System.Linq;
6+
87
using static FrooxEngine.InteractionHandler;
98

109
namespace MonkeyLoader.Resonite.UI.ContextMenus

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"msbuild-sdks": {
3-
"Remora.Resonite.Sdk": "2.0.10"
3+
"Remora.Resonite.Sdk": "2.1.0"
44
}
55
}

0 commit comments

Comments
 (0)