@@ -402,10 +402,118 @@ public void ApplyMenSharpMeta()
402402 current . Heap . SetHeapVariable ( address , value , declared ?? value . GetType ( ) ) ;
403403 }
404404
405+ ApplyProxyInitializers ( current , meta , name ) ;
406+
405407 // the caller (RefreshProgram) stores the patched heap so the runtime
406408 // loads the same state
407409 }
408410
411+ /// Fills the fields whose initializer Udon cannot run. Like UdonSharp:
412+ /// construct the proxy behaviour (its C# field initializers run — this is
413+ /// where `new VRCUrl("...")` happens), read the field by reflection, and
414+ /// write the value into the heap default. Runs in the editor, on the
415+ /// world author's own code; the built world ships only the baked value.
416+ private static void ApplyProxyInitializers ( IUdonProgram current , MenSharpMeta meta , string classPath )
417+ {
418+ if ( meta . proxyInitialized == null || meta . proxyInitialized . Length == 0 )
419+ {
420+ return ;
421+ }
422+ Type proxyType = FindProxyType ( classPath ) ;
423+ if ( proxyType == null )
424+ {
425+ Debug . LogWarning (
426+ "MenSharp: could not find the C# behaviour to read field initializers from; "
427+ + "some fields will keep their default value." ) ;
428+ return ;
429+ }
430+ // AddComponent on a throwaway GameObject runs the field initializers
431+ // the way Unity allows — `new`ing a MonoBehaviour would run them too,
432+ // but logs a native "not allowed" warning the managed log handler
433+ // cannot intercept. Hidden and not saved, destroyed straight after.
434+ var probe = new GameObject ( "__MenSharpProxyProbe" ) { hideFlags = HideFlags . HideAndDontSave } ;
435+ try
436+ {
437+ Component instance ;
438+ try
439+ {
440+ instance = probe . AddComponent ( proxyType ) ;
441+ }
442+ catch ( Exception error )
443+ {
444+ Debug . LogWarning (
445+ $ "MenSharp: could not construct { proxyType . Name } to read its field "
446+ + $ "initializers ({ error . Message } ); those fields keep their default value.") ;
447+ return ;
448+ }
449+ foreach ( MenSharpProxyInit init in meta . proxyInitialized )
450+ {
451+ if ( ! current . SymbolTable . HasAddressForSymbol ( init . symbol ) )
452+ {
453+ continue ;
454+ }
455+ FieldInfo field = FindField ( proxyType , init . field ) ;
456+ if ( field == null )
457+ {
458+ continue ;
459+ }
460+ object value = field . GetValue ( instance ) ;
461+ if ( value == null )
462+ {
463+ continue ;
464+ }
465+ uint address = current . SymbolTable . GetAddressFromSymbol ( init . symbol ) ;
466+ Type declared = current . Heap . GetHeapVariableType ( address ) ;
467+ current . Heap . SetHeapVariable ( address , value , declared ?? field . FieldType ) ;
468+ }
469+ }
470+ finally
471+ {
472+ UnityEngine . Object . DestroyImmediate ( probe ) ;
473+ }
474+ }
475+
476+ /// The behaviour type this program was compiled from, by its class path
477+ /// — the full name the compiler names the asset after (reflection spells
478+ /// nesting with '+', the asset with '.').
479+ private static Type FindProxyType ( string classPath )
480+ {
481+ foreach ( Type type in
482+ UnityEditor . TypeCache . GetTypesDerivedFrom < MenSharp . MenSharpBehaviour > ( ) )
483+ {
484+ if ( type . IsAbstract || type . IsGenericTypeDefinition )
485+ {
486+ continue ;
487+ }
488+ string fullName = ( type . FullName ?? type . Name ) . Replace ( '+' , '.' ) ;
489+ if ( fullName == classPath || type . Name == classPath )
490+ {
491+ return type ;
492+ }
493+ }
494+ return null ;
495+ }
496+
497+ /// A field by name, private ones on base classes included (GetField does
498+ /// not return a base class's private fields).
499+ private static FieldInfo FindField ( Type type , string name )
500+ {
501+ for ( Type current = type ;
502+ current != null && current != typeof ( MenSharp . MenSharpBehaviour ) ;
503+ current = current . BaseType )
504+ {
505+ FieldInfo field = current . GetField (
506+ name ,
507+ BindingFlags . Public | BindingFlags . NonPublic
508+ | BindingFlags . Instance | BindingFlags . DeclaredOnly ) ;
509+ if ( field != null )
510+ {
511+ return field ;
512+ }
513+ }
514+ return null ;
515+ }
516+
409517 private static object Decode ( MenSharpHeapEntry entry )
410518 {
411519 switch ( entry . kind )
@@ -518,6 +626,19 @@ public class MenSharpMeta
518626 /// arguments over the network — the variable each argument arrives in
519627 /// and its type. Handed to the SDK when the program is stored.
520628 public MenSharpNetworkCallable [ ] networkCallable ;
629+ /// Fields whose C# initializer Udon cannot run (`new VRCUrl("...")`): the
630+ /// importer constructs the proxy, whose initializer runs in real C#, and
631+ /// bakes the field's value into the heap. Same idea as UdonSharp.
632+ public MenSharpProxyInit [ ] proxyInitialized ;
633+ }
634+
635+ [ Serializable ]
636+ public class MenSharpProxyInit
637+ {
638+ /// The heap symbol to write.
639+ public string symbol ;
640+ /// The C# field on the proxy behaviour to read.
641+ public string field ;
521642}
522643
523644[ Serializable ]
0 commit comments