Skip to content

Commit

Permalink
Use GetUnique with the extensions methods instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed May 12, 2024
1 parent 86280d7 commit 5977819
Show file tree
Hide file tree
Showing 32 changed files with 128 additions and 85 deletions.
1 change: 1 addition & 0 deletions src/Murder.Editor/EditorScene_Shortcuts.cs
Expand Up @@ -276,6 +276,7 @@ private void ReloadAtlasWithChangesToggled(bool value)
private static void ReloadMetadata()
{
AssetsFilter.RefreshCache();
ReflectionHelper.ClearCache();
}

private static void ReloadShaders()
Expand Down
9 changes: 5 additions & 4 deletions src/Murder.Editor/Systems/Debug/DebugActivatorSystem.cs
Expand Up @@ -18,7 +18,7 @@ public class DebugActivatorSystem : IUpdateSystem, IStartupSystem
private bool _showConsole = false;
private bool _showEditorSystems = false;

private ImmutableArray<Type> _debugSystems = ImmutableArray<Type>.Empty;
private IEnumerable<Type> _debugSystems = ImmutableArray<Type>.Empty;

public void Start(Context context)
{
Expand All @@ -27,11 +27,12 @@ public void Start(Context context)
_showEditorSystems = editorComponent.EditorHook.ShowDebug;
}

_debugSystems = ReflectionHelper.GetAllTypesWithAttributeDefined<OnlyShowOnDebugViewAttribute>()
.ToImmutableArray();
_debugSystems = ReflectionHelper.GetAllTypesWithAttributeDefined<OnlyShowOnDebugViewAttribute>();

UpdateConsoleSystem(context);
UpdateEditorSystems(context);

// [Perf] Only deactivate editor systems. All remaining editor systems should be DEACTIVATED by default. Or else.
context.World.DeactivateSystem<EditorSystem>();
}

public void Update(Context context)
Expand Down
5 changes: 3 additions & 2 deletions src/Murder.Editor/Systems/Debug/DebugMapRenderSystem.cs
@@ -1,4 +1,5 @@
using Bang.Contexts;
using Bang;
using Bang.Contexts;
using Bang.Systems;
using Murder.Components;
using Murder.Core;
Expand Down Expand Up @@ -26,7 +27,7 @@ public void Draw(RenderContext render, Context context)
return;
}

Map? map = context.World.TryGetUnique<MapComponent>()?.Map;
Map? map = context.World.TryGetUniqueMap()?.Map;
if (map is null)
{
return;
Expand Down
7 changes: 4 additions & 3 deletions src/Murder.Editor/Systems/Debug/DebugRouteSystem.cs
@@ -1,4 +1,5 @@
using Bang.Components;
using Bang;
using Bang.Components;
using Bang.Contexts;
using Bang.Systems;
using Murder.Components;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void Draw(RenderContext render, Context context)

Color numberColor = new(.2f, .9f, .1f, .2f);

Map map = context.World.GetUnique<MapComponent>().Map;
Map map = context.World.GetUniqueMap().Map;
(int minX, int maxX, int minY, int maxY) = render.Camera.GetSafeGridBounds(map);

for (int y = minY; y < maxY; y++)
Expand Down Expand Up @@ -76,7 +77,7 @@ public void Draw(RenderContext render, Context context)
{
Color nodeColor = new(.8f, .5f, .1f, .1f);

if (context.World.TryGetUnique<HAAStarPathfindComponent>()?.Data is HAAStar pathfind)
if (context.World.TryGetUniqueHAAStarPathfind()?.Data is HAAStar pathfind)
{
DrawQuadtreeGrid(render, HAAStar.CLUSTER_SIZE, nodeColor, map.Width, map.Height,
minX, maxX, minY, maxY);
Expand Down
6 changes: 3 additions & 3 deletions src/Murder.Editor/Systems/Debug/QuadTreeDebugSystem.cs
@@ -1,6 +1,6 @@
using Bang.Contexts;
using Bang;
using Bang.Contexts;
using Bang.Systems;
using Murder.Components;
using Murder.Core.Graphics;
using Murder.Core.Physics;
using Murder.Editor.Attributes;
Expand All @@ -15,7 +15,7 @@ public class QuadTreeDebugSystem : IMurderRenderSystem
{
public void Draw(RenderContext render, Context context)
{
Quadtree? qt = context.World.TryGetUnique<QuadtreeComponent>()?.Quadtree;
Quadtree? qt = context.World.TryGetUniqueQuadtree()?.Quadtree;
if (qt is null)
{
return;
Expand Down
18 changes: 15 additions & 3 deletions src/Murder.Editor/Systems/Debug/SystemsDiagnosticsSystem.cs
Expand Up @@ -89,7 +89,7 @@ public void DrawGui(RenderContext render, Context context)
Dictionary<int, SmoothCounter>? stats = default;
switch (_targetView)
{
case TargetView.None:
case TargetView.Total:
break;

case TargetView.Update:
Expand All @@ -115,6 +115,10 @@ public void DrawGui(RenderContext render, Context context)
case TargetView.GuiRender:
stats = world.GuiCounters;
break;

case TargetView.Startup:
stats = world.StartCounters;
break;
}

if (stats is not null)
Expand Down Expand Up @@ -212,13 +216,14 @@ private enum TargetView
PreRender = 3,
Render = 4,
GuiRender = 5,
None = 6
Startup = 6,
Total = 7
}

/// <summary>
/// This is the overall time reported per each system.
/// </summary>
private readonly double[] _timePerSystems = new double[6];
private readonly double[] _timePerSystems = new double[(int)TargetView.Total];

private TargetView _targetView = TargetView.Update;

Expand Down Expand Up @@ -253,6 +258,12 @@ private void DrawTabs()
{
_targetView = TargetView.GuiRender;
}

if (ImGui.Selectable($"Startup ({PrintTime(TargetView.Startup)})###startup", _targetView == TargetView.Startup))
{
_targetView = TargetView.Startup;
}

ImGui.Separator();
}

Expand Down Expand Up @@ -326,6 +337,7 @@ private void CalculateAllOverallTime(MonoWorld world)
_timePerSystems[(int)TargetView.PreRender] = world.PreRenderCounters.Sum(k => k.Value.MaximumTime);
_timePerSystems[(int)TargetView.Render] = world.RenderCounters.Sum(k => k.Value.MaximumTime);
_timePerSystems[(int)TargetView.GuiRender] = world.GuiCounters.Sum(k => k.Value.MaximumTime);
_timePerSystems[(int)TargetView.Startup] = world.StartCounters.Sum(k => k.Value.MaximumTime);
}

private Dictionary<int, (string name, double size)> CalculateStatistics(MonoWorld world, double overallTime, IDictionary<int, SmoothCounter> stats)
Expand Down
2 changes: 1 addition & 1 deletion src/Murder.Editor/Utilities/EntityInspector.cs
Expand Up @@ -20,7 +20,7 @@ public static bool DrawInspector(World world, Entity entity)

if (ImGui.Begin($"{entity.EntityId}##Entity_Inspector", ref isOpen))
{
var cameraMan = world.GetUniqueEntity<CameraFollowComponent>();
var cameraMan = world.GetUniqueEntityCameraFollow();
if (cameraMan.HasIdTarget())
{
if (ImGui.SmallButton("release camera"))
Expand Down
51 changes: 41 additions & 10 deletions src/Murder.Editor/Utilities/ReflectionHelper.cs
Expand Up @@ -2,7 +2,7 @@
using Bang.StateMachines;
using Murder.Attributes;
using Murder.Editor.Reflection;
using Murder.Utilities.Attributes;
using Murder.Utilities;
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;
Expand All @@ -11,6 +11,9 @@ namespace Murder.Editor.Utilities
{
public static class ReflectionHelper
{
private static readonly CacheDictionary<Type, IEnumerable<Type>> _cachedTypesWithAttributes = new(12);
private static List<Type>? _allTypesInAllAssemblies = null;

/// <summary>
/// Find the first abstract class from the type <see cref="T"/>.
/// </summary>
Expand Down Expand Up @@ -58,13 +61,19 @@ public static IEnumerable<Type> GetAllImplementationsOf(Type type)
return types;
}

public static IEnumerable<Type> GetAllTypesWithAttributeDefined<T>() =>
GetAllTypesWithAttributeDefined(typeof(T));
public static IEnumerable<Type> GetAllTypesWithAttributeDefined<T>() => GetAllTypesWithAttributeDefined(typeof(T));

public static IEnumerable<Type> GetAllTypesWithAttributeDefined(Type t)
{
return SafeGetAllTypesInAllAssemblies()
.Where(p => Attribute.IsDefined(p, t));
if (_cachedTypesWithAttributes.TryGetValue(t, out var result))
{
return result;
}

result = SafeGetAllTypesInAllAssemblies().Where(p => Attribute.IsDefined(p, t));
_cachedTypesWithAttributes[t] = result;

return result;
}

public static IEnumerable<Type> GetAllTypesWithAttributeDefinedOfType<T>(Type ofType)
Expand Down Expand Up @@ -165,21 +174,43 @@ public static string GetGenericName(Type t)
?.GetCustomAttribute<TooltipAttribute>()?.Text;
}

private static readonly HashSet<string> _ignoredAssemblies = new() { "Bang.Generator" };
private static readonly HashSet<string> _ignoredAssemblies = [ "Bang.Generator" ];

public static IEnumerable<Type> SafeGetAllTypesInAllAssemblies()
public static List<Type> SafeGetAllTypesInAllAssemblies()
{
if (_allTypesInAllAssemblies is not null)
{
return _allTypesInAllAssemblies;
}

List<Type> allTypes = [];
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var assemblyName = assembly.GetName().Name;
if (assemblyName is not null && _ignoredAssemblies.Contains(assemblyName))
string? assemblyName = assembly.GetName().Name;
if (assemblyName is null || assemblyName.StartsWith("System"))
{
continue;
}

if (_ignoredAssemblies.Contains(assemblyName))
{
continue;
}

foreach (Type type in assembly.GetTypes())
{
yield return type;
allTypes.Add(type);
}
}

_allTypesInAllAssemblies = allTypes;
return _allTypesInAllAssemblies;
}

public static void ClearCache()
{
_allTypesInAllAssemblies = null;
_cachedTypesWithAttributes.Clear();
}
}
}
2 changes: 1 addition & 1 deletion src/Murder/Assets/Save/SaveDataAsset.cs
Expand Up @@ -290,7 +290,7 @@ protected HashSet<Guid> GetOrCreateEntitiesToBeDestroyedAt(World world)

protected Guid? EntityToGuid(World world, int id)
{
if (world.TryGetUnique<InstanceToEntityLookupComponent>() is not InstanceToEntityLookupComponent lookup)
if (world.TryGetUniqueInstanceToEntityLookup() is not InstanceToEntityLookupComponent lookup)
{
GameLogger.Warning("How does this world do not have InstanceToEntityLookupComponent setup?");
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Murder/Assets/WorldAsset.cs
Expand Up @@ -209,7 +209,7 @@ private static void CreateAllEntities(World world, ImmutableArray<EntityInstance
/// <param name="instancesToEntities">A map of each serialized guid to an entity id in the world.</param>
protected static void PostProcessEntities(World world, Dictionary<Guid, int> instancesToEntities)
{
if (world.TryGetUniqueEntity<InstanceToEntityLookupComponent>() is not null)
if (world.TryGetUniqueEntityInstanceToEntityLookup() is not null)
{
// Most likely, we are reloading a saved world. Do not post process this.
return;
Expand Down
1 change: 1 addition & 0 deletions src/Murder/Components/Dialogs/ChoiceComponent.cs
Expand Up @@ -5,6 +5,7 @@

namespace Murder.Components
{
[Unique]
[RuntimeOnly]
[DoNotPersistOnSave]
public readonly struct ChoiceComponent : IComponent
Expand Down
1 change: 1 addition & 0 deletions src/Murder/Components/Dialogs/LineComponent.cs
Expand Up @@ -5,6 +5,7 @@

namespace Murder.Components
{
[Unique]
[RuntimeOnly]
[DoNotPersistOnSave]
public readonly struct LineComponent : IComponent
Expand Down
1 change: 1 addition & 0 deletions src/Murder/Components/Sound/MusicComponent.cs
Expand Up @@ -8,6 +8,7 @@ namespace Murder.Components
/// Music component which will be immediately played and destroyed.
/// </summary>
[Sound]
[Unique]
public readonly struct MusicComponent : IComponent
{
public readonly SoundEventId Id = new();
Expand Down
6 changes: 3 additions & 3 deletions src/Murder/Core/Ai/PathfindServices.cs
Expand Up @@ -27,7 +27,7 @@ public static class PathfindServices
return Astar.FindPath(map, initial, target);

case PathfindAlgorithmKind.HAAstar:
if (world.TryGetUnique<HAAStarPathfindComponent>()?.Data is not HAAStar haastar)
if (world.TryGetUniqueHAAStarPathfind()?.Data is not HAAStar haastar)
{
GameLogger.Error("Unable to find component for HAAStar. Pathfind will abort!");
return ImmutableDictionary<Point, Point>.Empty;
Expand Down Expand Up @@ -55,12 +55,12 @@ public static class PathfindServices
/// <param name="world"></param>
public static void UpdatePathfind(World world)
{
if (world.TryGetUnique<MapComponent>()?.Map is not Map map)
if (world.TryGetUniqueMap()?.Map is not Map map)
{
return;
}

if (world.TryGetUnique<HAAStarPathfindComponent>()?.Data is HAAStar haastar)
if (world.TryGetUniqueHAAStarPathfind()?.Data is HAAStar haastar)
{
haastar.Refresh(map);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Murder/Core/Physics/Quadtree.cs
Expand Up @@ -135,10 +135,10 @@ public void GetCollisionEntitiesAt(Rectangle boundingBox, List<NodeInfo<Entity>>

public static Quadtree GetOrCreateUnique(World world)
{
if (world.TryGetUnique<QuadtreeComponent>() is not QuadtreeComponent qt)
if (world.TryGetUniqueQuadtree() is not QuadtreeComponent qt)
{
Rectangle quadTreeSize;
if (world.TryGetUnique<MapComponent>() is MapComponent map)
if (world.TryGetUniqueMap() is MapComponent map)
{
quadTreeSize = new Rectangle(0, 0, map.Width * Grid.CellSize, map.Height * Grid.CellSize);
}
Expand Down
5 changes: 0 additions & 5 deletions src/Murder/Interactions/Sounds/PlayMusicInteraction.cs
Expand Up @@ -36,11 +36,6 @@ public void Interact(World world, Entity interactor, Entity? interacted)
}
}

if (world.TryGetUniqueEntity<MusicComponent>() is not Entity e)
{
e = world.AddEntity();
}

_ = SoundServices.Play(Music, SoundProperties.Persist | SoundProperties.SkipIfAlreadyPlaying);
}
}
Expand Down

0 comments on commit 5977819

Please sign in to comment.