Small ResourceManager refactor, add ResourceSaver<T>.

This commit is contained in:
2024-01-21 20:24:53 +01:00
parent cfec5a59b1
commit 62e0e013f1
16 changed files with 215 additions and 54 deletions

View File

@@ -14,6 +14,7 @@ namespace DaggerFramework
LoadResources();
Ready();
Run();
Shutdown();
}
/// <summary>

View File

@@ -1,15 +1,13 @@
namespace DaggerFramework.Resources;
public class FontLoader : IResourceLoader
public class FontLoader : IResourceLoader<Font>
{
public IEnumerable<string> SupportedExtensions => new string[]
{
"ttf"
};
public Type ResourceType => typeof(Font);
public Resource Load(string path)
public Font Load(string path)
{
byte[] fileBuffer = File.ReadAllBytes(path);
var result = new Font(path, fileBuffer);

View File

@@ -1,9 +1,8 @@
namespace DaggerFramework.Resources
{
public interface IResourceLoader
public interface IResourceLoader<T> where T : Resource
{
public IEnumerable<string> SupportedExtensions { get; }
public Type ResourceType { get; }
public Resource Load(string path);
public T Load(string path);
}
}

View File

@@ -2,16 +2,14 @@ using StbVorbisSharp;
namespace DaggerFramework.Resources
{
public class SoundLoader : IResourceLoader
public class SoundLoader : IResourceLoader<Sound>
{
public IEnumerable<string> SupportedExtensions => new string[]
{
"ogg"
};
public Type ResourceType => typeof(Sound);
public Resource Load(string path)
public Sound Load(string path)
{
Vorbis vorbis;
Sound result;

View File

@@ -3,7 +3,7 @@ using StbImageSharp;
namespace DaggerFramework
{
public class Texture2dLoader : IResourceLoader
public class Texture2dLoader : IResourceLoader<Texture2d>
{
public IEnumerable<string> SupportedExtensions => new string[]
{
@@ -12,9 +12,7 @@ namespace DaggerFramework
".jpeg"
};
public Type ResourceType => typeof(Texture2d);
public Resource Load(string path)
public Texture2d Load(string path)
{
ImageResult image;
using (var stream = File.OpenRead(path))
@@ -28,10 +26,5 @@ namespace DaggerFramework
return result;
}
Resource IResourceLoader.Load(string path)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,10 +1,15 @@
using System.Text.Json.Serialization;
namespace DaggerFramework
{
public abstract class Resource : IDisposable
{
public string? Path { get => _path; set => _path = value; }
[JsonIgnore]
public byte[]? Buffer { get => _buffer; set => _buffer = value; }
[JsonIgnore]
public long BufferSize { get; set; }
public Resource(string path, byte[] buffer)

View File

@@ -24,7 +24,7 @@ namespace DaggerFramework.Resources
_logger.Info($"Loading {path} as {typeof(T)} with id \"{resourceId}\"...");
if (!TryGetLoader<T>(out IResourceLoader? loader))
if (!TryGetLoader(out IResourceLoader<T>? loader))
{
return false;
}
@@ -50,6 +50,21 @@ namespace DaggerFramework.Resources
return true;
}
public bool TrySave<T>(string path, in T resource) where T : Resource
{
if (!TryGetSaver(out IResourceSaver<T>? saver))
{
return false;
}
if (!saver.TrySave(path, in resource))
{
return false;
}
return true;
}
public bool TryGetResource<T>(string resourceId, [NotNullWhen(true)] out T? resource) where T : Resource
{
resource = null;
@@ -75,37 +90,82 @@ namespace DaggerFramework.Resources
public bool IsResourceLoaded(string resourceId) => _loadedResources.ContainsKey(resourceId);
public void AddResourceAssociation(Type resourceType, IResourceLoader loader)
public void AddResourceLoaderAssociation<T>(IResourceLoader<T> loader) where T : Resource
{
_resourceLoaderAssociations.Add(resourceType, loader);
_logger.Info($"Added resource loader association for {typeof(T)}.");
_resourceLoaderAssociations.Add(typeof(T), loader);
}
private bool TryGetLoader<T>([NotNullWhen(true)] out IResourceLoader? loader) where T : Resource
public void AddResourceSaverAssociation<T>(IResourceSaver<T> saver) where T : Resource
{
_logger.Info($"Added resource saver association for {typeof(T)}.");
_resourceSaverAssociations.Add(typeof(T), saver);
}
private bool TryGetLoader<T>([NotNullWhen(true)] out IResourceLoader<T>? loader) where T : Resource
{
loader = null;
if (!_resourceLoaderAssociations.ContainsKey(typeof(T)))
{
_logger.Error($"No loader for {typeof(T).ToString()} was found!");
_logger.Error($"No loader association found for {typeof(T)}.");
return false;
}
loader = _resourceLoaderAssociations[typeof(T)];
loader = _resourceLoaderAssociations[typeof(T)] as IResourceLoader<T>;
_logger.Info($"Using {loader.GetType().ToString()} for loading...");
if (loader is not null)
{
_logger.Info($"Using {loader.GetType()} for loading...");
}
else
{
_logger.Error($"No loader association found for {typeof(T)}.");
return false;
}
return true;
}
private bool TryGetSaver<T>([NotNullWhen(true)] out IResourceSaver<T>? saver) where T : Resource
{
saver = null;
if (!_resourceSaverAssociations.ContainsKey(typeof(T)))
{
_logger.Error($"No saver association found for {typeof(T)}.");
return false;
}
saver = _resourceSaverAssociations[typeof(T)] as IResourceSaver<T>;
if (saver is not null)
{
_logger.Info($"Using {saver.GetType()} for saving...");
}
else
{
_logger.Error($"No saver association found for {typeof(T)}.");
return false;
}
return true;
}
private Logger _logger = new(nameof(ResourceManager));
private readonly Dictionary<Type, IResourceLoader> _resourceLoaderAssociations = new()
private readonly Dictionary<Type, object> _resourceLoaderAssociations = new()
{
{typeof(Sound), new SoundLoader()},
{typeof(Texture2d), new Texture2dLoader()},
{typeof(Font), new FontLoader()}
};
private readonly Dictionary<Type, object> _resourceSaverAssociations = new()
{
};
private Dictionary<string, Resource> _loadedResources = new();
}
}

View File

@@ -0,0 +1,7 @@
namespace DaggerFramework.Resources
{
public interface IResourceSaver<T> where T : Resource
{
public bool TrySave(string path, in T resource);
}
}

View File

@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using DaggerFramework.Audio;
using DaggerFramework.Rendering;
@@ -26,4 +27,11 @@ namespace DaggerFramework.SceneGraph
Layer?.DestroyEntity(Id);
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Entity))]
internal partial class EntitySourceGenerationContext : JsonSerializerContext
{
}
}

View File

@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using DaggerFramework.Rendering;
using DaggerFramework.Utils;
@@ -5,17 +6,17 @@ namespace DaggerFramework.SceneGraph
{
public class EntityLayer : Layer
{
public List<Entity> Entities { get => _entities; }
[JsonInclude] public List<Entity> Entities { get; set; }
public Camera2d? CurrentCamera { get; set; }
public EntityLayer(List<Entity> entities)
{
_entities = entities;
Entities = entities;
}
public EntityLayer()
{
_entities = new List<Entity>();
Entities = new List<Entity>();
}
public void UpdateCurrentCamera()
@@ -53,14 +54,14 @@ namespace DaggerFramework.SceneGraph
public void DestroyEntity(int at)
{
_entities.RemoveAt(at);
Entities.RemoveAt(at);
}
protected override void OnStart()
{
for (int i = 0; i < _entities.Count; i++)
for (int i = 0; i < Entities.Count; i++)
{
var entity = _entities[i];
var entity = Entities[i];
entity.Layer = this;
entity.Start();
}
@@ -68,7 +69,7 @@ namespace DaggerFramework.SceneGraph
protected override void OnUpdate(double dt)
{
foreach (var entity in _entities)
foreach (var entity in Entities)
{
entity.Update(dt);
}
@@ -94,7 +95,7 @@ namespace DaggerFramework.SceneGraph
protected override void OnDraw(Renderer renderer)
{
// TODO: can be done more efficiently, needs rendering redesign.
foreach (var entity in _entities)
foreach (var entity in Entities)
{
if (entity is IDrawable drawable)
{
@@ -102,8 +103,6 @@ namespace DaggerFramework.SceneGraph
}
}
}
private List<Entity> _entities;
private List<Camera2d> _cameraEntities = new();
}
}

View File

@@ -1,13 +1,14 @@
using DaggerFramework.Resources;
using DaggerFramework.Rendering;
using System.Text.Json.Serialization;
namespace DaggerFramework.SceneGraph
{
public abstract class Layer : IDrawable
{
public Scene? Scene { get; set; }
public InputHandler? Input { get; set; }
public ResourceManager ResourceManager => Scene!.ResourceManager;
[JsonIgnore] public Scene? Scene { get; set; }
[JsonIgnore] public InputHandler? Input { get; set; }
[JsonIgnore] public ResourceManager ResourceManager => Scene!.ResourceManager;
public void BeginDraw(Renderer renderer) => OnBeginDraw(renderer);
public void Draw(Renderer renderer) => OnDraw(renderer);

View File

@@ -0,0 +1,20 @@
using System.Text.Json.Serialization;
namespace DaggerFramework.SceneGraph
{
public class SerializedScene : Resource
{
public Dictionary<string, Layer> Layers { get; set; }
public SerializedScene(string path, byte[] buffer) : base(path, buffer)
{
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SerializedScene))]
internal partial class SerializedSceneContext : JsonSerializerContext
{
}
}

View File

@@ -0,0 +1,23 @@
using DaggerFramework.Resources;
using DaggerFramework.Utils;
namespace DaggerFramework.SceneGraph
{
public class SerializedSceneSaver : IResourceSaver<SerializedScene>
{
public bool TrySave(string path, in SerializedScene resource)
{
if (resource.Buffer is null)
{
_logger.Error($"Tried to save a resource at \"{path}\" with a null buffer!");
return false;
}
File.WriteAllBytes(path, resource.Buffer);
return true;
}
private Logger _logger = new(nameof(SerializedSceneSaver));
}
}

View File

@@ -1,4 +1,4 @@
using System.Numerics;
using System.Text.Json;
using DaggerFramework.Audio;
using DaggerFramework.Rendering;
using DaggerFramework.Resources;
@@ -16,20 +16,43 @@ namespace DaggerFramework.SceneGraph
public double DeltaTime => _renderer.FrameTime;
public bool ShouldRun => Renderer.ShouldRun;
public Scene(Renderer renderer, InputHandler input, AudioBackend audioBackend, ResourceManager resourceManager)
public Scene(SceneSettings settings)
{
_renderer = renderer;
_input = input;
_audioBackend = audioBackend;
_resourceManager = resourceManager;
_layers = new Dictionary<string, Layer>();
_renderer = settings.Renderer;
_input = settings.InputHandler;
_audioBackend = settings.AudioBackend;
_resourceManager = settings.ResourceManager;
}
public Scene(Renderer renderer, ResourceManager resourceManager)
public static Scene FromSerialized(SerializedScene serializedScene, SceneSettings settings)
{
_renderer = renderer;
_resourceManager = resourceManager;
var scene = new Scene(settings);
scene.WithLayers(serializedScene.Layers);
return scene;
}
public bool TrySerialize(out SerializedScene serializedScene)
{
serializedScene = new SerializedScene(string.Empty, new byte[] { })
{
Layers = _layers
};
var sourceGenOptions = new JsonSerializerOptions
{
TypeInfoResolver = SerializedSceneContext.Default
};
serializedScene.Buffer = JsonSerializer.SerializeToUtf8Bytes(serializedScene, typeof(SerializedScene), sourceGenOptions);
serializedScene.BufferSize = serializedScene.Buffer.LongLength;
return true;
}
public void WithLayers(Dictionary<string, Layer> layers)
{
_layers = layers;
}
public void Init() => SetupRenderer();
@@ -97,4 +120,12 @@ namespace DaggerFramework.SceneGraph
private InputHandler? _input;
private ResourceManager _resourceManager;
}
public struct SceneSettings
{
public Renderer Renderer { get; set; }
public AudioBackend AudioBackend { get; set; }
public InputHandler InputHandler { get; set; }
public ResourceManager ResourceManager { get; set; }
}
}

View File

@@ -5,6 +5,8 @@ using DaggerFramework.Rendering;
using DaggerFramework.Audio;
using DaggerFramework.Resources;
using DaggerFramework.SceneGraph;
using System.Text.Json;
using System.Text.Json.Serialization;
public class TestGame : Game
@@ -17,6 +19,8 @@ public class TestGame : Game
_audioBackend = new FmodAudioBackend();
_inputHandler = new RaylibInputHandler();
_resourceManager.AddResourceSaverAssociation(new SerializedSceneSaver());
_renderer.CreateAndInitialize(new WindowSettings()
{
Title = "Test Game",
@@ -29,7 +33,13 @@ public class TestGame : Game
_audioBackend.Initialize();
_scene = new Scene(_renderer, _inputHandler, _audioBackend, _resourceManager);
_scene = new Scene(new SceneSettings()
{
Renderer = _renderer,
AudioBackend = _audioBackend,
InputHandler = _inputHandler,
ResourceManager = _resourceManager
});
_uiLayer = new UiLayer();
_worldLayer = new EntityLayer();
@@ -71,6 +81,13 @@ public class TestGame : Game
_scene.AddLayer("UI", _uiLayer!);
SerializedScene serializedScene;
if (_scene!.TrySerialize(out serializedScene))
{
_resourceManager.TrySave(Path.Combine(_resourceManager.ResourceRoot, "main.scene"), in serializedScene);
}
_scene.Start();
}

View File

@@ -5,6 +5,7 @@ using DaggerFramework.Utils;
public class TestPlayer : RectangleShape2d
{
public float SprintSpeed { get; set; } = 400f;
protected override void OnStart()
{
base.OnStart();
@@ -26,7 +27,7 @@ public class TestPlayer : RectangleShape2d
base.OnUpdate(dt);
var sprinting = Input.IsActionDown("sprint");
_speed = sprinting ? 400f : 200f;
_speed = sprinting ? SprintSpeed : 200f;
var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed;
Position += velocity * (float)dt;