namespace Nerfed.Runtime.Scheduling; public enum SystemAccessMode { Read, WriteExisting, StructuralWrite, } public readonly struct ComponentAccess { public ComponentAccess(Type componentType, SystemAccessMode mode) { ComponentType = componentType; Mode = mode; } public Type ComponentType { get; } public SystemAccessMode Mode { get; } } public readonly struct SystemAccessDeclaration { private readonly ComponentAccess[] accesses; public static readonly SystemAccessDeclaration Empty = new(Array.Empty()); public SystemAccessDeclaration(params ComponentAccess[] accesses) { this.accesses = accesses ?? Array.Empty(); } public ReadOnlySpan Accesses => accesses ?? Array.Empty(); public bool ConflictsWith(in SystemAccessDeclaration other) { ReadOnlySpan left = Accesses; ReadOnlySpan right = other.Accesses; for (int i = 0; i < left.Length; i++) { for (int j = 0; j < right.Length; j++) { if (left[i].Mode == SystemAccessMode.StructuralWrite || right[j].Mode == SystemAccessMode.StructuralWrite) { return true; } if (left[i].ComponentType != right[j].ComponentType) { continue; } if (left[i].Mode == SystemAccessMode.Read && right[j].Mode == SystemAccessMode.Read) { continue; } return true; } } return false; } public static ComponentAccess Read() where T : unmanaged => new(typeof(T), SystemAccessMode.Read); public static ComponentAccess WriteExisting() where T : unmanaged => new(typeof(T), SystemAccessMode.WriteExisting); public static ComponentAccess StructuralWrite() where T : unmanaged => new(typeof(T), SystemAccessMode.StructuralWrite); }