Last active
September 11, 2022 21:36
-
-
Save dragonslaya84/88e6bb998eda8fe4ee912f01d67feec9 to your computer and use it in GitHub Desktop.
Automcatic way to control svelto engine order without having to pass in a string using attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Svelto.Common; | |
using Svelto.DataStructures; | |
using Svelto.ECS.SveltoOnDOTS; | |
using UnityEngine; | |
namespace Aeradona.ECS.Systems | |
{ | |
/// <summary> | |
/// The order of the engines found in the enginesOrder array is the order of execution of engines | |
/// </summary> | |
public struct SequenceEngineOrder : ISequenceOrder | |
{ | |
private string[] Setup() | |
{ | |
var engineList = new FasterList<string>(); | |
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
for (int i = 0; i < assemblies.Length; i++) | |
{ | |
if (!assemblies[i].GetName().Name.Contains("Aeradona.ECS.Systems")) continue; | |
var types = assemblies[i].GetTypes(); | |
for (int j = 0; j < types.Length; j++) | |
{ | |
var attributes = Attribute.GetCustomAttributes(types[j]); | |
foreach (var attribute in attributes) | |
{ | |
if (attribute is not SequencedAttribute foundAttribute) continue; | |
if (foundAttribute.priority > engineList.count) | |
{ | |
engineList.IncrementCountBy((uint)(foundAttribute.priority - engineList.count)); | |
if (engineList.count <= foundAttribute.priority) | |
engineList[foundAttribute.priority - 1] = foundAttribute.name; | |
else | |
{ | |
engineList[foundAttribute.priority] = foundAttribute.name; | |
} | |
} | |
else | |
{ | |
if (!string.IsNullOrEmpty(engineList[foundAttribute.priority])) | |
{ | |
#if UNITY_EDITOR | |
Debug.Log( | |
"Enginesort: Overriding list. Please check sequence numbers for duplicate numbers."); | |
#endif | |
} | |
engineList[foundAttribute.priority] = foundAttribute.name; | |
} | |
} | |
} | |
} | |
return engineList.ToArray(); | |
} | |
public string[] enginesOrder => Setup(); | |
} | |
public class EngineSort: SortedJobifiedEnginesGroup<IJobifiedEngine, SequenceEngineOrder> | |
{ | |
public EngineSort(FasterList<IJobifiedEngine> engines) : base(engines) | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Svelto.ECS.Extensions | |
{ | |
public class SequencedAttribute : Attribute | |
{ | |
public SequencedAttribute(string name, uint priority) { this.name = name; this.priority = priority; } | |
public string name { get; } | |
public uint priority { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage