Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryImMouse committed Jul 14, 2024
1 parent 3b7947b commit 65168e7
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void Register(DependenciesContainer rootContainer)
rootContainer.Register<ITextureManager, TextureManager>();

// Caching
rootContainer.Register<ICacheManager, CacheManager>();
rootContainer.Register<IResourceCacher, ResourceCacher>();

// Camera
rootContainer.Register<ICameraManager, CameraManager>();
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Entities/Systems/Sprite/SpriteSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Hypercube.Client.Entities.Systems.Sprite;
public sealed class SpriteSystem : EntitySystem
{
[Dependency] private readonly IRenderDrawing _drawing = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;
[Dependency] private readonly IResourceCacher _resourceCacher = default!;

public override void Initialize()
{
Expand All @@ -38,7 +38,7 @@ public void Render(Entity<SpriteComponent> entity, Transform2 transform)
{
if (entity.Component.TextureHandle == null)
entity.Component.TextureHandle =
_cacheManager.GetResource<TextureResource>(entity.Component.TexturePath).Texture ?? throw new NullReferenceException();
_resourceCacher.GetResource<TextureResource>(entity.Component.TexturePath).Texture ?? throw new NullReferenceException();

_drawing.DrawTexture(entity.Component.TextureHandle, Vector2.Zero, entity.Component.Color, transform.Matrix * entity.Component.Transform.Matrix);
}
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/Drawing/RenderDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Hypercube.Client.Graphics.Drawing;
public sealed class RenderDrawing : IRenderDrawing
{
[Dependency] private readonly IRenderer _renderer = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;
[Dependency] private readonly IResourceCacher _resourceCacher = default!;

public void DrawTexture(ITexture texture, Vector2 position)
{
Expand All @@ -38,7 +38,7 @@ public void DrawTexture(ITexture texture, Box2 quad, Box2 uv)

public void DrawTexture(ITexture texture, Box2 quad, Box2 uv, Color color)
{
var handle = _cacheManager.GetResource<TextureResource>(texture.Path).Texture;
var handle = _resourceCacher.GetResource<TextureResource>(texture.Path).Texture;
_renderer.DrawTexture(handle, quad, uv, color);
}

Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public sealed partial class Renderer

private void OnLoad()
{
_baseShader = _cacheManager.GetResource<ShaderSourceResource>("/Shaders/base").Shader;
_baseTexture = _cacheManager.GetResource<TextureResource>("/Textures/icon.png").Texture;
_baseShader = _resourceCacher.GetResource<ShaderSourceResource>("/Shaders/base").Shader;
_baseTexture = _resourceCacher.GetResource<TextureResource>("/Textures/icon.png").Texture;
_baseTexture.Bind(HTexTarget.Texture2D);

_cameraManager.SetMainCamera(_cameraManager.CreateCamera2D(MainWindow.Size));
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber
[Dependency] private readonly ITiming _timing = default!;
[Dependency] private readonly ICameraManager _cameraManager = default!;
[Dependency] private readonly IResourceManager _resourceManager = default!;
[Dependency] private readonly ICacheManager _cacheManager = default!;
[Dependency] private readonly IResourceCacher _resourceCacher = default!;

private readonly ILogger _logger = LoggingManager.GetLogger("renderer");
private readonly ILogger _loggerOpenGL = LoggingManager.GetLogger("open_gl")!;
Expand Down Expand Up @@ -106,7 +106,7 @@ private void OnStartup(ref RuntimeStartupEvent args)

InitOpenGL();

_cacheManager.PreloadTextures();
_resourceCacher.PreloadTextures();

OnLoad();
}
Expand Down
2 changes: 1 addition & 1 deletion Hypercube.Client/Resources/Caching/AudioSourceResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Hypercube.Client.Resources.Caching;

public class AudioSourceResource : BaseResource, IDisposable
public sealed class AudioSourceResource : Resource, IDisposable
{
public ResourcePath Path;
public IAudioSource Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace Hypercube.Client.Resources.Caching;

public partial class CacheManager
public partial class ResourceCacher
{
private readonly Logger _loggerPreload = LoggingManager.GetLogger("cache.preload");

public void PreloadTextures()
{
var logger = LoggingManager.GetLogger("cache.preload");
var container = DependencyManager.GetContainer();
PreloadTextures(logger, container);
PreloadShaders(logger, container);
PreloadAudio(logger, container);
PreloadTextures(container);
PreloadShaders(container);
PreloadAudio(container);
}
private void PreloadTextures(Logger logger, DependenciesContainer container)

private void PreloadTextures(DependenciesContainer container)
{
logger.EngineInfo("Preloading textures...");
_loggerPreload.EngineInfo("Preloading textures...");
var st = Stopwatch.StartNew();

var texDict = GetTypeDict<TextureResource>();
Expand All @@ -37,9 +39,9 @@ private void PreloadTextures(Logger logger, DependenciesContainer container)
_logger.EngineInfo($"Preloaded {count} textures in {st.Elapsed}");
}

private void PreloadShaders(Logger logger, DependenciesContainer container)
private void PreloadShaders(DependenciesContainer container)
{
logger.EngineInfo("Preloading shaders...");
_loggerPreload.EngineInfo("Preloading shaders...");
var st = Stopwatch.StartNew();

var shDict = GetTypeDict<ShaderSourceResource>();
Expand All @@ -60,9 +62,9 @@ private void PreloadShaders(Logger logger, DependenciesContainer container)
_logger.EngineInfo($"Preloaded {count} shaders in {st.Elapsed}");
}

private void PreloadAudio(Logger logger, DependenciesContainer container)
private void PreloadAudio(DependenciesContainer container)
{
logger.EngineInfo("Preloading shaders...");
_loggerPreload.EngineInfo("Preloading shaders...");
var st = Stopwatch.StartNew();

var aDict = GetTypeDict<ShaderSourceResource>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@

namespace Hypercube.Client.Resources.Caching;

public partial class CacheManager : ICacheManager
public partial class ResourceCacher : IResourceCacher
{
[Dependency] private readonly IResourceManager _resourceManager = default!;

private Dictionary<Type, Dictionary<ResourcePath, BaseResource>> _cachedResources =
new Dictionary<Type, Dictionary<ResourcePath, BaseResource>>();
private Dictionary<Type, Dictionary<ResourcePath, Resource>> _cachedResources = new();

private DependenciesContainer _container = default!;
private DependenciesContainer _container = DependencyManager.GetContainer();

private readonly Logger _logger = LoggingManager.GetLogger("cache");

#region PublicAPI

public CacheManager()
{
_container = DependencyManager.GetContainer();
}

public T GetResource<T>(ResourcePath path, bool useFallback = true) where T : BaseResource, new()
public T GetResource<T>(ResourcePath path, bool useFallback = true) where T : Resource, new()
{
var typeDict = GetTypeDict<T>();

Expand All @@ -46,12 +38,12 @@ public CacheManager()
if (useFallback && cache.FallbackPath is not null)
return GetResource<T>(cache.FallbackPath.Value, false);

_logger.Error($"Exception while loading resource {ex.Message}, Stack Trace: {ex.StackTrace}");
_logger.Fatal($"Exception while loading resource {ex.Message}, Stack Trace: {ex.StackTrace}");
throw;
}
}

public bool TryGetResource<T>(ResourcePath path, [NotNullWhen(true)] out T? resource) where T : BaseResource, new()
public bool TryGetResource<T>(ResourcePath path, [NotNullWhen(true)] out T? resource) where T : Resource, new()
{
var cont = DependencyManager.GetContainer();
var cache = GetTypeDict<T>();
Expand All @@ -77,35 +69,24 @@ public CacheManager()
}
catch (Exception ex)
{
_logger.Error($"Exception while loading resource: {ex.Message}, Stack Trace: {ex.StackTrace}");
_logger.Fatal($"Exception while loading resource: {ex.Message}, Stack Trace: {ex.StackTrace}");
throw;
}
}

public void CacheResource<T>(ResourcePath path, T resource) where T : BaseResource, new()
public void CacheResource<T>(ResourcePath path, T resource) where T : Resource, new()
{
GetTypeDict<T>()[path] = resource;
}

public T GetFallback<T>() where T : BaseResource, new()
{
throw new NotImplementedException();
}

#endregion

#region Private

private Dictionary<ResourcePath, BaseResource> GetTypeDict<T>()
private Dictionary<ResourcePath, Resource> GetTypeDict<T>()
{
if (_cachedResources.TryGetValue(typeof(T), out var dict))
return dict;

dict = new Dictionary<ResourcePath, BaseResource>();
dict = new Dictionary<ResourcePath, Resource>();
_cachedResources[typeof(T)] = dict;

return dict;
}

#endregion
}
2 changes: 1 addition & 1 deletion Hypercube.Client/Resources/Caching/ShaderSourceResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Hypercube.Client.Resources.Caching;

public class ShaderSourceResource : BaseResource, IDisposable
public sealed class ShaderSourceResource : Resource, IDisposable
{
public IShader Shader;
public string Base;
Expand Down
2 changes: 1 addition & 1 deletion Hypercube.Client/Resources/Caching/TextureResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hypercube.Client.Resources.Caching;

public class TextureResource : BaseResource, IDisposable
public sealed class TextureResource : Resource, IDisposable
{
public ITextureHandle Texture;
public ResourcePath Path;
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Hypercube.Example;

public sealed class Example : IEventSubscriber, IPostInject
{
[Dependency] private readonly ICacheManager _cacheManager = default!;
[Dependency] private readonly IResourceCacher _resourceCacher = default!;
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly IEntitiesManager _entitiesManager = default!;
[Dependency] private readonly IEntitiesComponentManager _entitiesComponentManager = default!;
Expand Down Expand Up @@ -43,7 +43,7 @@ private void Startup(ref RuntimeStartupEvent args)
CreateEntity(coord);
}

var source = _cacheManager.GetResource<AudioSourceResource>("/game_boi_3.wav").Stream;
var source = _resourceCacher.GetResource<AudioSourceResource>("/game_boi_3.wav").Stream;
// it's too loud :D
source.Gain = 0.3f;
source.Start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace Hypercube.Shared.Resources.Caching;

public interface ICacheManager
public interface IResourceCacher
{
T GetResource<T>(ResourcePath path, bool useFallback = true) where T : BaseResource, new();
T GetResource<T>(ResourcePath path, bool useFallback = true) where T : Resource.Resource, new();

bool TryGetResource<T>(ResourcePath path, [NotNullWhen(true)] out T? resource)
where T : BaseResource, new();
where T : Resource.Resource, new();

void CacheResource<T>(ResourcePath path, T resource)
where T : BaseResource, new();
where T : Resource.Resource, new();

void PreloadTextures();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Hypercube.Shared.Resources.Caching.Resource;

public abstract class BaseResource
public abstract class Resource
{
public ResourcePath? FallbackPath { get; }

public abstract void Load(ResourcePath path, DependenciesContainer container);

public virtual void Reload(ResourcePath path, DependenciesContainer container)
{

}

public ResourcePath? FallbackPath { get; }
}

0 comments on commit 65168e7

Please sign in to comment.