Skip to content

Commit 49b3eb8

Browse files
committed
build tweaks
1 parent 1f6e759 commit 49b3eb8

File tree

24 files changed

+1772
-150
lines changed

24 files changed

+1772
-150
lines changed

.github/workflows/build-unity-webgl.yml

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
name: Build Unity WebGL
1+
name: Build Unity WebGL and Deploy
22

33
# This workflow is disabled by default (.disabled suffix)
44
# Rename to remove .disabled when ready to use
55

66
on:
7+
push:
8+
branches:
9+
- main
10+
paths:
11+
- 'Unity/CraftSpace/**'
712
workflow_dispatch:
813
inputs:
9-
copy_to_sveltekit:
10-
description: 'Copy build to SvelteKit static directory'
14+
copy_to_spacetime:
15+
description: 'Copy build to WebSites/spacetime'
16+
type: boolean
17+
default: true
18+
trigger_pages_deploy:
19+
description: 'Trigger GitHub Pages deployment'
1120
type: boolean
1221
default: true
1322

@@ -16,12 +25,38 @@ jobs:
1625
name: Build Unity WebGL
1726
runs-on: self-hosted
1827
steps:
19-
- name: Checkout code
20-
uses: actions/checkout@v3
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
with:
31+
lfs: true
32+
33+
- name: Setup Node.js
34+
uses: actions/setup-node@v4
35+
with:
36+
node-version: '20'
37+
38+
- name: Install dependencies
39+
run: cd SvelteKit/BackSpace && npm ci
40+
41+
- name: Setup Unity environment
42+
run: cd SvelteKit/BackSpace && npm run unity:setup
43+
44+
- name: Export schemas to Unity
45+
run: cd SvelteKit/BackSpace && npm run schemas:export:unity
46+
47+
- name: Regenerate Unity schemas
48+
run: cd SvelteKit/BackSpace && npm run schemas:regenerate:unity
49+
50+
- name: Build Unity WebGL app
51+
run: cd SvelteKit/BackSpace && npm run unity:build-webgl
2152

22-
- name: Build Unity WebGL
23-
run: echo "Would build Unity WebGL here"
53+
- name: Deploy to WebSites/spacetime
54+
if: ${{ github.event.inputs.copy_to_spacetime == 'true' || github.event_name == 'push' }}
55+
run: cd SvelteKit/BackSpace && npm run unity:deploy-webgl
2456

25-
- name: Copy to SvelteKit
26-
if: ${{ github.event.inputs.copy_to_sveltekit }}
27-
run: echo "Would copy build to SvelteKit static directory"
57+
- name: Trigger GitHub Pages deployment
58+
if: ${{ github.event.inputs.trigger_pages_deploy == 'true' || github.event_name == 'push' }}
59+
uses: peter-evans/repository-dispatch@v2
60+
with:
61+
token: ${{ secrets.GITHUB_TOKEN }}
62+
event-type: spacetime-deploy

SvelteKit/BackSpace/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"unity:list-versions": "tsx scripts/unity-automation.js list-versions",
4646
"unity:generate-schemas": "tsx scripts/unity-automation.js generate-schemas",
4747
"unity:build-webgl": "tsx scripts/unity-automation.js build-webgl-prod",
48-
"unity:deploy-webgl": "rm -rf ../../WebSites/spacetime && cp -r ../../Unity/CraftSpace/Builds/SpaceCraft ../../WebSites/spacetime && mkdir -p ../../WebSites/spacetime/StreamingAssets && cp -r ../../Unity/CraftSpace/Assets/StreamingAssets/Content ../../WebSites/spacetime/StreamingAssets/",
48+
"unity:deploy-webgl": "rm -rf ../../WebSites/spacetime && cp -r ../../Unity/CraftSpace/Builds/SpaceCraft ../../WebSites/spacetime",
4949
"unity:build-and-deploy": "npm run unity:build-webgl && npm run unity:deploy-webgl",
5050
"unity:deploy-all": "npm run schemas:regenerate:unity && npm run unity:build-and-deploy"
5151
},

Unity/CraftSpace/Assets/Scripts/Core/Brewster.cs

Lines changed: 133 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)