Skip to content

Commit 6d5a1f8

Browse files
committed
Replaced AddToGC w/ GCBehavior Flags
1 parent f8b22ec commit 6d5a1f8

34 files changed

Lines changed: 261 additions & 163 deletions

LevelImposter/AssetLoader/AudioLoader.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace LevelImposter.AssetLoader;
77

8-
public class AudioLoader : AsyncQueue<LoadableAudio, AudioClip>
8+
public class AudioLoader : AsyncQueue<LoadableAudio, LoadedAudioClip>
99
{
1010
private AudioLoader()
1111
{
@@ -22,8 +22,6 @@ public static void LoadAsync(Guid? assetID, Action<AudioClip> onLoad)
2222
{
2323
if (assetID == null)
2424
return;
25-
26-
2725

2826
// Get Data Store from AssetDB
2927
var soundDataStore = GameConfiguration.CurrentMap?.mapAssetDB?.Get(assetID);
@@ -34,11 +32,12 @@ public static void LoadAsync(Guid? assetID, Action<AudioClip> onLoad)
3432
var loadableAudio = new LoadableAudio(assetID?.ToString() ?? "", soundDataStore);
3533

3634
// Enqueue Loadable
37-
Instance.AddToQueue(loadableAudio, onLoad);
35+
Instance.AddToQueue(loadableAudio, loadedAudioClip => onLoad(loadedAudioClip.AudioClip));
3836
}
3937

40-
protected override AudioClip Load(LoadableAudio loadable)
38+
protected override LoadedAudioClip Load(LoadableAudio loadable)
4139
{
42-
return WAVLoader.Load(loadable.DataStore, loadable.ID);
40+
var audioClip = WAVLoader.Load(loadable.DataStore, loadable.ID);
41+
return new LoadedAudioClip(audioClip);
4342
}
4443
}

LevelImposter/AssetLoader/FileContainers/GIFFile.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum FrameDisposalMethod
4141

4242
// Other Data
4343
private Color[]? _pixelBuffer; // Buffer of pixel colors
44-
private bool _addToGC = true;
44+
private GCBehavior? _gcBehavior;
4545

4646
// GIF File
4747
public bool IsLoaded { get; private set; }
@@ -88,13 +88,13 @@ public static bool IsGIF(byte[] data)
8888
/// Loads the GIF file from a given stream.
8989
/// </summary>
9090
/// <param name="dataStream">Stream of raw GIF data</param>
91-
/// <param name="addToGC">Whether to handle texture garbage collection automatically</param>
92-
public void Load(Stream dataStream, bool addToGC = true)
91+
/// <param name="gcBehavior">Garbage collection behavior for the loaded textures</param>
92+
public void Load(Stream dataStream, GCBehavior? gcBehavior = null)
9393
{
9494
using var reader = new BinaryReader(dataStream);
9595

9696
IsLoaded = false;
97-
_addToGC = addToGC;
97+
_gcBehavior = gcBehavior;
9898
ReadHeader(reader);
9999
ReadDescriptor(reader);
100100
ReadGlobalColorTable(reader);
@@ -573,8 +573,7 @@ public void RenderFrame(int frameIndex)
573573
texture.Apply(false, true); // Remove from CPU memory
574574

575575
// Add to GC
576-
if (_addToGC)
577-
GCHandler.Register(texture);
576+
GCHandler.Register(texture, _gcBehavior);
578577

579578
// Handle frame disposal
580579
switch (frame.DisposalMethod)

LevelImposter/AssetLoader/Loadables/LoadableAudio.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace LevelImposter.AssetLoader;
44

5-
public readonly struct LoadableAudio(string id, IDataStore dataStore) : ICachable
5+
public readonly struct LoadableAudio(string id, IDataStore dataStore) : IIdentifiable
66
{
77
public string ID => id;
88
public IDataStore DataStore => dataStore;
9+
public AudioOptions Options { get; } = new();
10+
11+
public class AudioOptions
12+
{
13+
/// Changes how and when the texture is disposed of.
14+
/// <c>null</c> will use <see cref="GCHandler"/>'s current default behavior.
15+
public GCBehavior? GCBehavior { get; set; } = null;
16+
}
917
}

LevelImposter/AssetLoader/Loadables/LoadableSprite.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using UnityEngine;
1+
using LevelImposter.Core;
2+
using UnityEngine;
23

34
namespace LevelImposter.AssetLoader;
45

5-
public readonly struct LoadableSprite(string id, LoadableTexture tex) : ICachable
6+
public readonly struct LoadableSprite(string id, LoadableTexture tex) : IIdentifiable
67
{
78
public string ID => id;
89
public LoadableTexture Texture => tex;
@@ -16,9 +17,9 @@ public class SpriteOptions
1617
/// If set, defines the portion of the texture to use for the sprite. Otherwise, the full texture is used.
1718
public Rect? Frame { get; set; }
1819

19-
/// If true (default), the sprite will be disposed automatically after the map is unloaded.
20-
/// If false, you must manage the sprite's lifecycle manually.
21-
public bool AddToGC { get; set; } = true;
20+
/// Changes how and when the texture is disposed of.
21+
/// <c>null</c> will use <see cref="GCHandler"/>'s current default behavior.
22+
public GCBehavior? GCBehavior { get; set; } = null;
2223
}
2324

2425
/// <summary>

LevelImposter/AssetLoader/Loadables/LoadableTexture.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using System;
2-
using Il2CppInterop.Runtime.InteropTypes.Arrays;
3-
using Il2CppSystem.IO;
4-
using LevelImposter.Core;
5-
using UnityEngine;
1+
using LevelImposter.Core;
62

73
namespace LevelImposter.AssetLoader;
84

9-
public readonly struct LoadableTexture(string id, IDataStore dataStore) : ICachable
5+
public readonly struct LoadableTexture(string id, IDataStore dataStore) : IIdentifiable
106
{
117
public string ID => id;
128
public IDataStore DataStore => dataStore;
@@ -16,10 +12,10 @@ public class TextureOptions
1612
{
1713
/// If true, the texture will use pixel art filtering (point filtering)
1814
public bool PixelArt { get; set; } = false;
19-
20-
/// If true (default), the texture will be disposed automatically after the map is unloaded.
21-
/// If false, you must manage the texture's lifecycle manually.
22-
public bool AddToGC { get; set; } = true;
15+
16+
/// Changes how and when the texture is disposed of.
17+
/// <c>null</c> will use <see cref="GCHandler"/>'s current default behavior.
18+
public GCBehavior? GCBehavior { get; set; } = null;
2319
}
2420

2521
/// <summary>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using LevelImposter.Core;
3+
using UnityEngine;
4+
5+
namespace LevelImposter.AssetLoader;
6+
7+
public class LoadedAudioClip(AudioClip audioClip) : ICachable
8+
{
9+
public AudioClip AudioClip => audioClip;
10+
public bool IsExpired => audioClip == null;
11+
12+
public static implicit operator AudioClip(LoadedAudioClip loadedAudioClip) => loadedAudioClip.AudioClip;
13+
}

LevelImposter/AssetLoader/Loadables/LoadedSprite.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace LevelImposter.AssetLoader;
66

7-
public class LoadedSprite(Sprite sprite, LoadedTexture texture)
7+
public class LoadedSprite(Sprite sprite, LoadedTexture texture) : ICachable
88
{
99
public Sprite Sprite => sprite;
1010
public LoadedTexture Texture => texture;
11-
11+
public bool IsExpired => sprite == null || texture.IsExpired;
12+
1213
public static implicit operator Sprite(LoadedSprite loadedSprite) => loadedSprite.Sprite;
1314
}

LevelImposter/AssetLoader/Loadables/LoadedTexture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace LevelImposter.AssetLoader;
66

7-
public class LoadedTexture(Texture2D texture)
7+
public class LoadedTexture(Texture2D texture) : ICachable
88
{
99
public Texture2D Texture => texture;
10+
public bool IsExpired => texture == null;
1011

1112
public static implicit operator Texture2D(LoadedTexture loadedTexture) => loadedTexture.Texture;
1213
}

LevelImposter/AssetLoader/Loaders/DDSLoader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ private static Texture2D ImageDataToTexture2D(
153153
texture.Apply(false, true);
154154

155155
// Register in GC
156-
if (options?.AddToGC ?? true)
157-
GCHandler.Register(texture);
156+
GCHandler.Register(texture, options?.GCBehavior);
158157

159158
return texture;
160159
}

LevelImposter/AssetLoader/Loaders/GIFLoader.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ public static class GIFLoader
1313
/// <returns>A fully-loaded GIFFile containing the image data</returns>
1414
public static LoadedGIFTexture Load(LoadableTexture loadable)
1515
{
16-
// Get whether to add to GC
17-
var addToGC = loadable.Options?.AddToGC ?? true;
18-
1916
// Create new file
2017
var gifFile = new GIFFile(loadable.ID);
21-
if (addToGC)
22-
GCHandler.Register(gifFile);
18+
GCHandler.Register(gifFile, loadable.Options.GCBehavior);
2319

2420
// Load data into managed memory
2521
var imgData = loadable.DataStore.LoadToManagedMemory();
2622

2723
// Load the GIF file from the stream
2824
using var imgStream = new MemoryStream(imgData);
29-
gifFile.Load(imgStream, addToGC);
25+
gifFile.Load(imgStream, loadable.Options.GCBehavior);
3026

3127
// Return the GIF file
3228
return new LoadedGIFTexture(gifFile);

0 commit comments

Comments
 (0)