Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions AssetHelper/Plugin/LoadingPage/ILoadingScreen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using UnityEngine;

namespace Silksong.AssetHelper.Plugin.LoadingPage;

/// <summary>
/// Interface defining the contract for a loading screen.
/// </summary>
internal interface ILoadingScreen
{
public void SetText(string text);

public void SetSubtext(string text);

public void SetProgress(float progress);

public void SetVisible(bool visible);
}

internal static class LoadingScreenExtensions
{
public static T Create<T>() where T : MonoBehaviour, ILoadingScreen
{
GameObject go = new("AssetHelper LoadingScreen");
T ret = go.AddComponent<T>();
go.SetActive(true);
return ret;
}

public static void Reset(this ILoadingScreen self)
{
self.SetText(string.Empty);
self.SetSubtext(string.Empty);
self.SetProgress(0);
self.SetVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using UnityEngine;
using UnityEngine.UI;

internal class LoadingBar : MonoBehaviour
namespace Silksong.AssetHelper.Plugin.LoadingPage;

internal class LoadingScreen : MonoBehaviour, ILoadingScreen
{
private RectTransform _fillImageRect;
private GameObject _canvasObject;
private CanvasGroup _canvasGroup;
private Text _statusText;

public static LoadingBar Create()
{
GameObject go = new("AssetHelper LoadingBar");
LoadingBar ret = go.AddComponent<LoadingBar>();
go.SetActive(true);
return ret;
}
private Text _subText;

void Awake()
{
Expand Down Expand Up @@ -75,22 +70,43 @@ void Awake()
_fillImageRect.sizeDelta = Vector2.zero;
_fillImageRect.anchoredPosition = Vector2.zero;

GameObject textObj = new("StatusText");
textObj.transform.SetParent(_canvasObject.transform);
_statusText = textObj.AddComponent<Text>();

_statusText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
_statusText.text = string.Empty;
_statusText.fontSize = 30;
_statusText.alignment = TextAnchor.MiddleCenter;
_statusText.color = Color.white;
{
GameObject textObj = new("StatusText");
textObj.transform.SetParent(_canvasObject.transform);
_statusText = textObj.AddComponent<Text>();

_statusText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
_statusText.text = string.Empty;
_statusText.fontSize = 30;
_statusText.alignment = TextAnchor.MiddleCenter;
_statusText.color = Color.white;

RectTransform textRect = _statusText.rectTransform;
textRect.anchorMin = new Vector2(0.5f, 0.5f);
textRect.anchorMax = new Vector2(0.5f, 0.5f);
textRect.pivot = new Vector2(0.5f, 0f);
textRect.sizeDelta = new Vector2(800, 50);
textRect.anchoredPosition = new Vector2(0, 60);
}

RectTransform textRect = _statusText.rectTransform;
textRect.anchorMin = new Vector2(0.5f, 0.5f);
textRect.anchorMax = new Vector2(0.5f, 0.5f);
textRect.pivot = new Vector2(0.5f, 0f);
textRect.sizeDelta = new Vector2(800, 50);
textRect.anchoredPosition = new Vector2(0, 45);
{
GameObject subtextObj = new("SubText");
subtextObj.transform.SetParent(_canvasObject.transform);
_subText = subtextObj.AddComponent<Text>();

_subText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
_subText.text = string.Empty;
_subText.fontSize = 20;
_subText.alignment = TextAnchor.MiddleCenter;
_subText.color = Color.white;

RectTransform subtextRect = _subText.rectTransform;
subtextRect.anchorMin = new Vector2(0.5f, 0.5f);
subtextRect.anchorMax = new Vector2(0.5f, 0.5f);
subtextRect.pivot = new Vector2(0.5f, 1f);
subtextRect.sizeDelta = new Vector2(800, 50);
subtextRect.anchoredPosition = new Vector2(0, -45);
}

// For testing
_statusText.text = "Loading...";
Expand All @@ -112,6 +128,11 @@ public void SetText(string text)
_statusText.text = text;
}

public void SetSubtext(string text)
{
_subText.text = text;
}

private void OnDestroy()
{
if (_canvasObject != null)
Expand Down
9 changes: 5 additions & 4 deletions AssetHelper/Plugin/StartupOverrideManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using MonoDetour.HookGen;
using Silksong.AssetHelper.Core;
using Silksong.AssetHelper.Plugin.LoadingPage;
using Silksong.AssetHelper.Plugin.Tasks;
using UnityEngine;

Expand Down Expand Up @@ -42,13 +43,13 @@ private static IEnumerator WrapStartManagerStart(StartManager self, IEnumerator
// This should already be the case, but we should check just in case it matters.
yield return new WaitUntil(() => AddressablesData.IsAddressablesLoaded);

LoadingBar bar = LoadingBar.Create();
LoadingScreen screen = LoadingScreenExtensions.Create<LoadingScreen>();

bool failed = false;
foreach (BaseStartupTask task in _tasks)
{
// We have to enumerate like this because you can't yield from within a try-catch block
IEnumerator enumerator = task.Run(bar);
IEnumerator enumerator = task.Run(screen);

while (true)
{
Expand Down Expand Up @@ -86,7 +87,7 @@ private static IEnumerator WrapStartManagerStart(StartManager self, IEnumerator
AssetHelperPlugin.InstanceLogger.LogInfo($"{nameof(AssetHelper)} prep complete!");
AssetRequestAPI.AfterBundleCreationComplete.Activate();
_startupRun = true;
bar.SetProgress(1);
screen.SetProgress(1);
}
else
{
Expand All @@ -96,7 +97,7 @@ private static IEnumerator WrapStartManagerStart(StartManager self, IEnumerator
}

// Even if there was an error, still let them into the game normally
UObject.Destroy(bar);
UObject.Destroy(screen);
yield return null;

yield return original;
Expand Down
7 changes: 4 additions & 3 deletions AssetHelper/Plugin/Tasks/BaseStartupTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using Silksong.AssetHelper.Plugin.LoadingPage;
using System.Collections;

namespace Silksong.AssetHelper.Plugin.Tasks;

Expand All @@ -11,6 +12,6 @@ internal abstract class BaseStartupTask
/// Run the startup task. The objects yielded by this enumerator will
/// be passed through to Unity.
/// </summary>
/// <param name="loadingBar">A loading bar.</param>
public abstract IEnumerator Run(LoadingBar loadingBar);
/// <param name="loadingScreen">A loading screen.</param>
public abstract IEnumerator Run(ILoadingScreen loadingScreen);
}
9 changes: 5 additions & 4 deletions AssetHelper/Plugin/Tasks/BundleDepsPrecompute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
using System.Text;
using Silksong.AssetHelper.Core;
using Silksong.AssetHelper.Internal;
using Silksong.AssetHelper.Plugin.LoadingPage;

namespace Silksong.AssetHelper.Plugin.Tasks;

internal class BundleDepsPrecompute : BaseStartupTask
{
public override IEnumerator Run(LoadingBar loadingBar)
public override IEnumerator Run(ILoadingScreen loadingScreen)
{
if (AssetRequestAPI.Request.NonSceneAssets.Count == 0)
{
// Only need to precompute bundle deps if there're non-scene assets requested
yield break;
}

loadingBar.SetText(LanguageKeys.COMPUTING_BUNDLE_DEPS.GetLocalized());
loadingScreen.SetText(LanguageKeys.COMPUTING_BUNDLE_DEPS.GetLocalized());
yield return null;
AssetHelperPlugin.InstanceLogger.LogInfo("Computing bundle deps");

Expand All @@ -29,7 +30,7 @@ public override IEnumerator Run(LoadingBar loadingBar)
.BundleKeys!.Keys.Where(x => !x.Contains("scenes_scenes_scenes"))
.ToList();

loadingBar.SetProgress(0);
loadingScreen.SetProgress(0);
int ct = 0;
int misses = 0;

Expand All @@ -39,7 +40,7 @@ public override IEnumerator Run(LoadingBar loadingBar)

ct++;

loadingBar.SetProgress((float)ct / (float)bundles.Count);
loadingScreen.SetProgress((float)ct / (float)bundles.Count);

if (!cacheHit)
{
Expand Down
7 changes: 4 additions & 3 deletions AssetHelper/Plugin/Tasks/NonSceneCatalog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Silksong.AssetHelper.Core;
using Silksong.AssetHelper.Internal;
using Silksong.AssetHelper.Plugin.LoadingPage;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -18,12 +19,12 @@ internal class NonSceneCatalog : BaseStartupTask
// Path to the non-scene catalog .bin file
private static string NonSceneCatalogPath => Path.Combine(AssetPaths.CatalogFolder, $"{CatalogKeys.NonSceneCatalogId}.bin");

public override IEnumerator Run(LoadingBar loadingBar)
public override IEnumerator Run(ILoadingScreen loadingScreen)
{
return CreateAndLoadCatalog(loadingBar);
return CreateAndLoadCatalog(loadingScreen);
}

private IEnumerator CreateAndLoadCatalog(LoadingBar bar)
private IEnumerator CreateAndLoadCatalog(ILoadingScreen bar)
{
IEnumerator nonSceneCatalogCreate = CreateNonSceneAssetCatalog();
bar.SetText(LanguageKeys.BUILDING_NON_SCENE.GetLocalized());
Expand Down
26 changes: 15 additions & 11 deletions AssetHelper/Plugin/Tasks/SceneRepacking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using AssetHelperLib.PreloadTable;
using CPPCache = System.Collections.Generic.Dictionary<string, AssetHelperLib.PreloadTable.ContainerPointerPreloadsBundleData>;
using System;
using Silksong.AssetHelper.Plugin.LoadingPage;

namespace Silksong.AssetHelper.Plugin.Tasks;

Expand All @@ -39,12 +40,12 @@ internal class SceneRepacking : BaseStartupTask

private bool _didRepack = false;

public override IEnumerator Run(LoadingBar loadingBar)
public override IEnumerator Run(ILoadingScreen loadingScreen)
{
return RepackAndCatalogScenes(loadingBar);
return RepackAndCatalogScenes(loadingScreen);
}

private IEnumerator RepackAndCatalogScenes(LoadingBar bar)
private IEnumerator RepackAndCatalogScenes(ILoadingScreen bar)
{
IEnumerator repack = PrepareAndRun(bar);

Expand All @@ -56,12 +57,12 @@ private IEnumerator RepackAndCatalogScenes(LoadingBar bar)
// Yield after each repack op is done
yield return null;
}
bar.SetProgress(1f);
bar.Reset();

bar.SetText(LanguageKeys.BULDING_SCENE.GetLocalized());
yield return null;

IEnumerator catalogCreate = CreateSceneAssetCatalog(_repackData);
IEnumerator catalogCreate = CreateSceneAssetCatalog(_repackData, bar);
while (catalogCreate.MoveNext())
{
yield return null;
Expand All @@ -83,7 +84,7 @@ private IEnumerator RepackAndCatalogScenes(LoadingBar bar)
yield return null;
}

private IEnumerator PrepareAndRun(LoadingBar bar)
private IEnumerator PrepareAndRun(ILoadingScreen bar)
{
Prepare();

Expand Down Expand Up @@ -214,10 +215,10 @@ private static bool ResolveCab(string cabName, out string? bundlePath)
/// <summary>
/// Run the repacking procedure so that by the end, anything in the request which could be repacked has been.
/// </summary>
private IEnumerator RunRepacking(LoadingBar bar)
private IEnumerator RunRepacking(ILoadingScreen bar)
{
CachedObject<CPPCache> SyncedCppCache = CachedObject<CPPCache>.CreateSynced(
"container_pointer_preloads_cache.json", () => new(), mutable: true, out IDisposable? handle);
"container_pointer_preloads_cache.json", () => new(), mutable: true, out IDisposable? cppSyncHandle);

ContainerPointerPreloads cpp = new(ResolveCab) { Cache = SyncedCppCache.Value };
PreloadTableResolver resolver = new([new DefaultPreloadTableResolver(), cpp]);
Expand All @@ -234,6 +235,7 @@ private IEnumerator RunRepacking(LoadingBar bar)
{
Stopwatch sw = Stopwatch.StartNew();
AssetHelperPlugin.InstanceLogger.LogInfo($"Repacking {request.Count} objects in scene {scene}");
bar.SetSubtext(scene);

RepackingParams rParams = new()
{
Expand Down Expand Up @@ -270,10 +272,11 @@ private IEnumerator RunRepacking(LoadingBar bar)
mainSw.Stop();
AssetHelperPlugin.InstanceLogger.LogInfo($"Finished scene repacking after {mainSw.ElapsedMilliseconds} ms");

handle?.Dispose();
bar.Reset();
cppSyncHandle?.Dispose();
}

private IEnumerator CreateSceneAssetCatalog(RepackDataCollection data)
private IEnumerator CreateSceneAssetCatalog(RepackDataCollection data, ILoadingScreen screen)
{
string catalogMetadataPath = Path.ChangeExtension(SceneCatalogPath, ".json");

Expand Down Expand Up @@ -329,7 +332,8 @@ out string? relativePath

sw.Stop();
AssetHelperPlugin.InstanceLogger.LogInfo($"Prepared catalog in {sw.ElapsedMilliseconds} ms");


screen.SetText(LanguageKeys.WRITING_SCENE.GetLocalized());
yield return null;

sw = Stopwatch.StartNew();
Expand Down
1 change: 1 addition & 0 deletions AssetHelper/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"LOADING_NON_SCENE": "Loading non-scene catalog",
"REPACKING_SCENE": "Repacking scenes",
"BULDING_SCENE": "Building scene catalog",
"WRITING_SCENE": "Writing scene catalog",
"LOADING_SCENE": "Loading scene catalog"
}