Skip to content

Commit

Permalink
Load base components too if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Mar 23, 2024
1 parent 6ca2221 commit c3327b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
21 changes: 8 additions & 13 deletions fluXis.Game/FluXisGame.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using fluXis.Game.Audio;
Expand Down Expand Up @@ -62,6 +61,8 @@ public partial class FluXisGame : FluXisGameBase, IKeyBindingHandler<FluXisGloba
public static readonly string[] IMAGE_EXTENSIONS = { ".jpg", ".jpeg", ".png" };
public static readonly string[] VIDEO_EXTENSIONS = { ".mp4", ".mov", ".avi", ".flv", ".mpg", ".wmv", ".m4v" };

protected override bool LoadComponentsLazy => true;

private BufferedContainer buffer;
private GlobalClock globalClock;
private GlobalBackground globalBackground;
Expand All @@ -74,7 +75,6 @@ public partial class FluXisGame : FluXisGameBase, IKeyBindingHandler<FluXisGloba
private ExitAnimation exitAnimation;

private LoadInfo loadInfo { get; } = new();
private Dictionary<Drawable, Action<Drawable>> loadQueue { get; } = new();

private bool isExiting;

Expand Down Expand Up @@ -161,8 +161,8 @@ private T loadComponent<T>(T component, Action<T> action, bool cache = false, bo
return component;
}

loadInfo.Increment();
loadQueue[component] = _ => action(component);
LoadQueue[component] = _ => action(component);
loadInfo.TotalTasks = LoadQueue.Count;
Scheduler.AddOnce(loadNext);

return component;
Expand All @@ -176,11 +176,11 @@ private void loadNext()
return;
}

if (loadQueue.Count == 0)
if (LoadQueue.Count == 0)
return;

var next = loadQueue.First();
loadQueue.Remove(next.Key);
var next = LoadQueue.First();
LoadQueue.Remove(next.Key);

var (component, action) = next;

Expand Down Expand Up @@ -438,16 +438,11 @@ private void loadLocales()
public class LoadInfo
{
public long TasksDone { get; private set; }
public long TotalTasks { get; private set; }
public long TotalTasks { get; set; }

public event Action<string> TaskStarted;
public event Action AllFinished;

public void Increment()
{
TotalTasks++;
}

public void StartNext(string task) => TaskStarted?.Invoke(task);

public void FinishCurrent()
Expand Down
35 changes: 24 additions & 11 deletions fluXis.Game/FluXisGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public partial class FluXisGameBase : osu.Framework.Game

public Vector2 ContentSize => content.DrawSize;

protected virtual bool LoadComponentsLazy => false;
protected Dictionary<Drawable, Action<Drawable>> LoadQueue { get; } = new();

private Vector2 targetDrawSize => new(1920, 1080);
private DrawSizePreservingFillContainer drawSizePreserver;
private Bindable<float> uiScale;
Expand Down Expand Up @@ -201,27 +204,37 @@ private void load(Storage storage, FrameworkConfigManager frameworkConfig)
private void cacheComponent<T>(T component, bool load = false, bool add = false)
where T : class
{
var loadable = component is Drawable;
var drawable = component as Drawable;

if (loadable && drawable == null)
if (load && drawable == null)
throw new InvalidOperationException($"Component of type {typeof(T)} is not a drawable.");

GameDependencies.CacheAs(component);

if (load && loadable)
if (!load)
return;

if (LoadComponentsLazy)
{
Schedule(() =>
LoadQueue[drawable] = d =>
{
Logger.Log($"Loading {component.GetType().Name}...", LoggingTarget.Runtime, LogLevel.Debug);

if (!drawable.IsLoaded)
LoadComponent(drawable);

if (add)
base.Content.Add(drawable);
});
base.Content.Add(d);
};

return;
}

Schedule(() =>
{
Logger.Log($"Loading {component.GetType().Name}...", LoggingTarget.Runtime, LogLevel.Debug);

if (!drawable.IsLoaded)
LoadComponent(drawable);

if (add)
base.Content.Add(drawable);
});
}

protected override void LoadComplete()
Expand Down

0 comments on commit c3327b6

Please sign in to comment.