11using Elements . Core ;
22using FrooxEngine ;
33using HarmonyLib ;
4- using System ;
5- using System . Collections . Generic ;
64using System . Diagnostics . CodeAnalysis ;
7- using System . IO ;
8- using System . Linq ;
95using System . Runtime . CompilerServices ;
10- using System . Text ;
11- using System . Threading . Tasks ;
126
137namespace 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><<see cref="DynamicVariableSpace"/>>()</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><<see cref="DynamicVariableSpace"/>>()</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>.
0 commit comments