|
- using System;
- using Svelto.DataStructures;
- using Svelto.ECS;
- using Svelto.ECS.Hybrid;
- using Svelto.ECS.Internal;
-
- namespace TechbloxModdingAPI.Utility
- {
- public ref struct RefCollection<T> where T : struct, IBaseEntityComponent
- {
- private readonly bool managed;
- private int count;
- private NB<T> nativeArray;
- private MB<T> managedArray;
- private NativeEntityIDs nativeIDs;
- private ManagedEntityIDs managedIDs;
-
- private RefCollection(EntityCollection<T> coll, T inst = default)
- {
- if (inst is IEntityComponent)
- {
- DeconstructCollection<T, T>(coll);
- }
- if (typeof(T).IsAssignableFrom(typeof(IEntityViewComponent)))
- {
- (managedArray, managedIDs, count) = coll2;
- }
- }
-
- private void DeconstructCollection<TM, TN>(EntityCollection<T> coll) where TM : struct, IEntityViewComponent
- where TN : unmanaged, IEntityComponent
- {
- switch (coll)
- {
- case EntityCollection<TM> cm:
- {
- MB<TM> ma;
- (ma, managedIDs, count) = cm;
- if (ma is MB<T> mb)
- managedArray = mb;
- else
- throw new InvalidCastException("Expected managed buffer in managed entity collection! Wut");
- break;
- }
- case EntityCollection<TN> cn:
- {
- NB<TN> na;
- (na, nativeIDs, count) = cn;
- if (na is NB<T> nb)
- nativeArray = nb;
- else
- throw new InvalidCastException("Expected native buffer in native entity collection! Wut");
- break;
- }
- }
- }
-
- public static implicit operator RefCollection<T>(EntityCollection<T> coll)
- {
- return new RefCollection<T>(coll);
- }
-
- public Enumerator GetEnumerator() => new(this);
-
- public ref struct Enumerator
- {
- private RefCollection<T> collection;
- private uint index;
- public Enumerator(RefCollection<T> collection)
- {
- index = default;
- this.collection = collection;
- }
-
- public OptionalRef<T> Current => collection.coll.[index];
-
- public bool MoveNext()
- {
- return true;
- }
- }
-
- private static void Test<TN>(EntityCollection<TN> coll) where TN : unmanaged, IEntityComponent
- {
-
- }
- }
- }
|