11using System ;
2+ using System . Collections . Generic ;
3+ using System . Reflection ;
4+ using System . Reflection . Emit ;
25using FrooxEngine ;
36using HarmonyLib ;
47using NeosModLoader ;
@@ -18,9 +21,10 @@ public class ModConfigDynVarBridge : NeosMod
1821
1922 public override void OnEngineInit ( )
2023 {
24+ _config = GetConfiguration ( ) ;
2125 Harmony harmony = new Harmony ( "net.KyuubiYoru.ModConfigDynVarBridge" ) ;
2226 harmony . PatchAll ( ) ;
23- _config = GetConfiguration ( ) ;
27+
2428 }
2529
2630 [ HarmonyPatch ( typeof ( Userspace ) ) ]
@@ -37,32 +41,58 @@ static void OnAttachPostFix(Userspace __instance)
3741 foreach ( NeosModBase mod in ModLoader . Mods ( ) )
3842 {
3943 ModConfiguration config = mod . GetConfiguration ( ) ;
40- if ( config != null )
44+ if ( config != null )
4145 {
46+ Slot modConfigSlot = _configSlot . AddSlot ( mod . Name ) ;
47+ DynamicVariableSpace space =
48+ ( DynamicVariableSpace ) modConfigSlot . AttachComponent ( typeof ( DynamicVariableSpace ) ) ;
49+ space . OnlyDirectBinding . Value = true ;
50+ space . SpaceName . Value = mod . Name ;
51+
4252 foreach ( ModConfigurationKey key in config . ConfigurationItemDefinitions )
4353 {
44-
54+ if ( ! key . InternalAccessOnly )
55+ {
56+ Debug ( "Try to add" + key . Name ) ;
57+ MethodInfo method = typeof ( UserspacePatch ) . GetMethod ( nameof ( SetupDynVar ) ) ;
58+ MethodInfo generic = method ? . MakeGenericMethod ( key . ValueType ( ) ) ;
59+ generic ? . Invoke ( __instance , new object [ ] { modConfigSlot , mod , key } ) ;
60+ }
4561 }
4662 }
4763 }
64+ }
65+ catch ( Exception e )
66+ {
67+ Debug ( "OnAttach" ) ;
68+ Debug ( e . Message ) ;
69+ Debug ( e . StackTrace ) ;
70+ }
71+ }
4872
49- //IkCullingPatch.UserSpaceWorld =
50- // (DynamicVariableSpace)IkCullingPatch.ConfigSlot.AttachComponent(typeof(DynamicVariableSpace));
51- //IkCullingPatch.UserSpaceWorld.OnlyDirectBinding.Value = true;
52- //IkCullingPatch.UserSpaceWorld.SpaceName.Value = "IkCullingConfig";
53-
54- //DynamicValueVariable<bool> value = (DynamicValueVariable<bool>)IkCullingPatch.ConfigSlot.AttachComponent(typeof(DynamicValueVariable<bool>));
55- //value.VariableName.Value = "IkCullingConfig/Enable";
56- //value.Value.Changed += changeable => Config.Set(Enabled, ((SyncField<bool>)changeable).Value);
73+ public static void SetupDynVar < T > ( Slot slot , NeosModBase mod , ModConfigurationKey key )
74+ {
75+ try
76+ {
77+ DynamicValueVariable < T > dynVar = ( DynamicValueVariable < T > ) slot . AttachComponent ( typeof ( DynamicValueVariable < T > ) ) ;
78+ if ( dynVar == null )
79+ {
80+ Debug ( "dynVar is null" ) ;
81+ }
5782
83+ mod . GetConfiguration ( ) . TryGetValue ( ( ModConfigurationKey < T > ) key , out T value ) ;
84+ dynVar . Value . Value = value ;
85+ dynVar . VariableName . Value = $ "{ mod . Name } /{ key . Name } ";
86+ dynVar . Value . Changed += changeable => mod . GetConfiguration ( ) . Set ( key , ( ( SyncField < T > ) changeable ) . Value ) ;
5887
88+ //key.OnChanged += value => dynVar.Value.Value = (T)value;
5989
6090 }
6191 catch ( Exception e )
6292 {
63- Debug ( "OnAttach" ) ;
6493 Debug ( e . Message ) ;
65- Debug ( e . StackTrace ) ;
94+ Debug ( e . ToString ( ) ) ;
95+
6696 }
6797 }
6898 }
0 commit comments