@@ -125,12 +125,12 @@ public void InitializeRegistry()
125125 Debug . Log ( "[Brewster/Registry] About to load collection IDs from index..." ) ;
126126 try
127127 {
128- LoadCollectionIdsFromIndex ( ) ;
129- Debug . Log ( "[Brewster/Registry] Returned from LoadCollectionIdsFromIndex successfully." ) ;
128+ LoadCollectionIndex ( ) ;
129+ Debug . Log ( "[Brewster/Registry] Returned from LoadCollectionIndex successfully." ) ;
130130 }
131131 catch ( Exception loadEx )
132132 {
133- Debug . LogError ( $ "[Brewster/Registry] ERROR: Exception during LoadCollectionIdsFromIndex : { loadEx . Message } ") ;
133+ Debug . LogError ( $ "[Brewster/Registry] ERROR: Exception during LoadCollectionIndex : { loadEx . Message } ") ;
134134 Debug . LogError ( $ "[Brewster/Registry] Exception type: { loadEx . GetType ( ) . FullName } ") ;
135135 Debug . LogError ( $ "[Brewster/Registry] Stack trace: { loadEx . StackTrace } ") ;
136136 throw ;
@@ -238,7 +238,7 @@ private void LoadAllCollections()
238238 /// <summary>
239239 /// Loads the collection IDs from the index file into the internal list.
240240 /// </summary>
241- private void LoadCollectionIdsFromIndex ( )
241+ private void LoadCollectionIndex ( )
242242 {
243243 // (This logic is mostly the same as the old LoadCollectionIds method)
244244 try
@@ -329,7 +329,11 @@ private IEnumerator LoadCollectionIndexWithWebRequest(string indexFilePath)
329329 IsInitialized = true ;
330330 if ( loadCollectionsAutomatically )
331331 {
332- EagerLoadCollections ( ) ;
332+ // Load all collections if auto-loading is enabled
333+ foreach ( var collectionId in _collectionIds )
334+ {
335+ GetCollection ( collectionId ) ;
336+ }
333337 }
334338 }
335339 }
@@ -444,6 +448,15 @@ public Collection GetCollection(string collectionId)
444448
445449 // 2. Load from file if not cached
446450 if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Cache miss for Collection: { collectionId } . Attempting to load from file.") ;
451+
452+ // For WebGL, we need to use a coroutine with UnityWebRequest
453+ if ( Application . platform == RuntimePlatform . WebGLPlayer )
454+ {
455+ StartCoroutine ( LoadCollectionWithWebRequest ( collectionId ) ) ;
456+ return null ; // Will be loaded asynchronously
457+ }
458+
459+ // Standard file loading for non-WebGL platforms
447460 try
448461 {
449462 string collectionPath = Path . Combine ( Application . streamingAssetsPath , baseResourcePath , "collections" , collectionId , "collection.json" ) ;
@@ -458,6 +471,44 @@ public Collection GetCollection(string collectionId)
458471 string jsonContent = File . ReadAllText ( collectionPath ) ;
459472 if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Collection JSON loaded ({ jsonContent . Length } chars), Preview: { Truncate ( jsonContent , 100 ) } ") ;
460473
474+ return ProcessCollectionJson ( collectionId , jsonContent , collectionPath ) ;
475+ }
476+ catch ( Exception e )
477+ {
478+ Debug . LogError ( $ "[Brewster/Registry] Error loading collection '{ collectionId } ' from file: { e . Message } ") ;
479+ return null ;
480+ }
481+ }
482+
483+ // WebGL-specific loading of collections using UnityWebRequest
484+ private IEnumerator LoadCollectionWithWebRequest ( string collectionId )
485+ {
486+ string collectionPath = Path . Combine ( Application . streamingAssetsPath , baseResourcePath , "collections" , collectionId , "collection.json" ) ;
487+ if ( verbose ) Debug . Log ( "[Brewster/Registry] Loading Collection via WebRequest from: '" + collectionPath + "'" ) ;
488+
489+ using ( UnityEngine . Networking . UnityWebRequest www = UnityEngine . Networking . UnityWebRequest . Get ( collectionPath ) )
490+ {
491+ yield return www . SendWebRequest ( ) ;
492+
493+ if ( www . result != UnityEngine . Networking . UnityWebRequest . Result . Success )
494+ {
495+ Debug . LogWarning ( $ "[Brewster/Registry] Collection file not found: { collectionPath } ") ;
496+ Debug . LogWarning ( "[Brewster/Registry] WebRequest error: " + www . error ) ;
497+ yield break ;
498+ }
499+
500+ string jsonContent = www . downloadHandler . text ;
501+ if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Collection JSON loaded via WebRequest ({ jsonContent . Length } chars), Preview: { Truncate ( jsonContent , 100 ) } ") ;
502+
503+ ProcessCollectionJson ( collectionId , jsonContent , collectionPath ) ;
504+ }
505+ }
506+
507+ // Common processing logic for collection JSON
508+ private Collection ProcessCollectionJson ( string collectionId , string jsonContent , string collectionPath )
509+ {
510+ try
511+ {
461512 Collection collection = Collection . FromJson ( jsonContent ) ;
462513
463514 if ( collection != null )
@@ -509,7 +560,7 @@ public Collection GetCollection(string collectionId)
509560 }
510561 catch ( Exception e )
511562 {
512- Debug . LogError ( $ "[Brewster/Registry] Error loading collection '{ collectionId } ' from file : { e . Message } ") ;
563+ Debug . LogError ( $ "[Brewster/Registry] Error processing collection JSON for '{ collectionId } ': { e . Message } ") ;
513564 return null ;
514565 }
515566 }
@@ -589,12 +640,27 @@ public Item GetItem(string collectionId, string itemId)
589640
590641 // 2. Load from file if not cached
591642 if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Cache miss for Item: { itemCacheKey } . Attempting to load from file (Context: Collection '{ collectionId } ').") ;
643+
644+ // SINGULAR DEFINITIVE PATH CONSTRUCTION - This is the ONLY place that should construct this path
645+ string itemFilePath = Path . Combine ( Application . streamingAssetsPath , baseResourcePath ,
646+ "collections" , collectionId , "items" , itemId , "item.json" ) ;
647+
648+ // For WebGL, we need to use a coroutine with UnityWebRequest
649+ if ( Application . platform == RuntimePlatform . WebGLPlayer )
650+ {
651+ StartCoroutine ( LoadItemWithWebRequest ( collectionId , itemId , itemCacheKey , itemFilePath ) ) ;
652+
653+ // Return a temporary placeholder while loading
654+ Item tempPlaceholder = ScriptableObject . CreateInstance < Item > ( ) ;
655+ tempPlaceholder . Id = itemId ;
656+ tempPlaceholder . Title = "Loading..." ;
657+ tempPlaceholder . ParentCollectionId = collectionId ;
658+ return tempPlaceholder ;
659+ }
660+
661+ // Standard file loading for non-WebGL platforms
592662 try
593663 {
594- // SINGULAR DEFINITIVE PATH CONSTRUCTION - This is the ONLY place that should construct this path
595- string itemFilePath = Path . Combine ( Application . streamingAssetsPath , baseResourcePath ,
596- "collections" , collectionId , "items" , itemId , "item.json" ) ;
597-
598664 if ( verbose ) Debug . Log ( "[Brewster/Registry] Loading Item from: '" + itemFilePath + "'" ) ;
599665
600666 // STRICTLY CHECK EXISTENCE - Fatal error if missing
@@ -618,6 +684,62 @@ public Item GetItem(string collectionId, string itemId)
618684 string jsonContent = File . ReadAllText ( itemFilePath ) ;
619685 if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Item JSON loaded ({ jsonContent . Length } chars), Preview: { Truncate ( jsonContent , 100 ) } ") ;
620686
687+ return ProcessItemJson ( itemId , collectionId , itemCacheKey , jsonContent , itemFilePath ) ;
688+ }
689+ catch ( Exception e )
690+ {
691+ Debug . LogError ( $ "[Brewster/Registry] FATAL ERROR: Exception loading item '{ itemId } ' (Collection '{ collectionId } '): { e . Message } ") ;
692+
693+ // Create placeholder item with MISSING title
694+ Item placeholderItem = ScriptableObject . CreateInstance < Item > ( ) ;
695+ placeholderItem . Id = itemId ;
696+ placeholderItem . Title = "MISSING" ; // Use MISSING as title for exceptions too
697+ placeholderItem . ParentCollectionId = collectionId ;
698+
699+ // Add to cache so we don't keep trying to load it
700+ _loadedItems [ itemCacheKey ] = placeholderItem ;
701+
702+ return placeholderItem ;
703+ }
704+ }
705+
706+ // WebGL-specific loading of items using UnityWebRequest
707+ private IEnumerator LoadItemWithWebRequest ( string collectionId , string itemId , string itemCacheKey , string itemFilePath )
708+ {
709+ if ( verbose ) Debug . Log ( "[Brewster/Registry] Loading Item via WebRequest from: '" + itemFilePath + "'" ) ;
710+
711+ using ( UnityEngine . Networking . UnityWebRequest www = UnityEngine . Networking . UnityWebRequest . Get ( itemFilePath ) )
712+ {
713+ yield return www . SendWebRequest ( ) ;
714+
715+ if ( www . result != UnityEngine . Networking . UnityWebRequest . Result . Success )
716+ {
717+ Debug . LogError ( $ "[Brewster/Registry] FATAL ERROR: Item file not found: { itemFilePath } ") ;
718+ Debug . LogError ( $ "[Brewster/Registry] WebRequest error: { www . error } ") ;
719+
720+ // Create placeholder item with MISSING title
721+ Item placeholderItem = ScriptableObject . CreateInstance < Item > ( ) ;
722+ placeholderItem . Id = itemId ;
723+ placeholderItem . Title = "MISSING" ;
724+ placeholderItem . ParentCollectionId = collectionId ;
725+
726+ // Add to cache so we don't keep trying to load it
727+ _loadedItems [ itemCacheKey ] = placeholderItem ;
728+ yield break ;
729+ }
730+
731+ string jsonContent = www . downloadHandler . text ;
732+ if ( verbose ) Debug . Log ( $ "[Brewster/Registry] Item JSON loaded via WebRequest ({ jsonContent . Length } chars), Preview: { Truncate ( jsonContent , 100 ) } ") ;
733+
734+ ProcessItemJson ( itemId , collectionId , itemCacheKey , jsonContent , itemFilePath ) ;
735+ }
736+ }
737+
738+ // Common processing logic for item JSON
739+ private Item ProcessItemJson ( string itemId , string collectionId , string itemCacheKey , string jsonContent , string itemFilePath )
740+ {
741+ try
742+ {
621743 Item item = Item . FromJson ( jsonContent ) ;
622744
623745 if ( item != null )
@@ -661,7 +783,7 @@ public Item GetItem(string collectionId, string itemId)
661783 }
662784 catch ( Exception e )
663785 {
664- Debug . LogError ( $ "[Brewster/Registry] FATAL ERROR: Exception loading item '{ itemId } ' (Collection '{ collectionId } '): { e . Message } ") ;
786+ Debug . LogError ( $ "[Brewster/Registry] FATAL ERROR: Exception processing item JSON for '{ itemId } ' (Collection '{ collectionId } '): { e . Message } ") ;
665787
666788 // Create placeholder item with MISSING title
667789 Item placeholderItem = ScriptableObject . CreateInstance < Item > ( ) ;
0 commit comments