|
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using Svelto.DataStructures;
- using Svelto.ECS;
- using Svelto.ECS.Internal;
- using TechbloxModdingAPI.Common.Utils;
- using TechbloxModdingAPI.Utility;
-
- namespace TechbloxModdingAPI
- {
- public abstract class EcsObjectBase
- {
- public EGID Id { get; }
-
- private static readonly Dictionary<Type, WeakDictionary<EGID, EcsObjectBase>> _instances = new();
-
- internal static WeakDictionary<EGID, EcsObjectBase> GetInstances(Type type)
- {
- return _instances.TryGetValue(type, out var dict) ? dict : null;
- }
-
- /// <summary>
- /// Returns a cached instance if there's an actively used instance of the object already.
- /// Objects still get garbage collected and then they will be removed from the cache.
- /// </summary>
- /// <param name="egid">The EGID of the entity</param>
- /// <param name="constructor">The constructor to construct the object</param>
- /// <typeparam name="T">The object type</typeparam>
- /// <returns></returns>
- internal static T GetInstance<T>(EGID egid, Func<EGID, T> constructor, Type type = null) where T : EcsObjectBase
- {
- var instances = GetInstances(type ?? typeof(T));
- if (instances == null || !instances.TryGetValue(egid, out var instance))
- return constructor(egid); // It will be added by the constructor
- return (T)instance;
- }
-
- protected EcsObjectBase(EGID id, Type entityDescriptorType)
- {
- if (!_instances.TryGetValue(GetType(), out var dict))
- {
- dict = new WeakDictionary<EGID, EcsObjectBase>();
- _instances.Add(GetType(), dict);
- }
- if (!dict.ContainsKey(id)) // Multiple instances may be created
- dict.Add(id, this);
- Id = id;
- }
-
- private void AnalyzeEntityDescriptor(Type entityDescriptorType)
- {
- // TODO: Cache
- // TODO: This should be in BlockClassGenerator
- // TODO: Add support for creating/deleting entities (getting an up to date server/client engines root)
- var templateType = typeof(EntityDescriptorTemplate<>).MakeGenericType(entityDescriptorType);
- var getTemplateClass = Expression.Constant(templateType);
- var getDescriptorExpr = Expression.PropertyOrField(getTemplateClass, "descriptor");
- var getTemplateDescriptorExpr =
- Expression.Lambda<Func<IEntityDescriptor>>(getDescriptorExpr);
- var getTemplateDescriptor = getTemplateDescriptorExpr.Compile();
- var builders = getTemplateDescriptor().componentsToBuild;
- }
-
- #region ECS initializer stuff
-
- protected internal EcsInitData InitData;
-
- /// <summary>
- /// Holds information needed to construct a component initializer.
- /// Necessary because the initializer is a ref struct which cannot be assigned to a field.
- /// </summary>
- protected internal struct EcsInitData
- {
- private FasterDictionary<RefWrapperType, ITypeSafeDictionary> group;
- private EntityReference reference;
-
- public static implicit operator EcsInitData(EntityInitializer initializer) => new()
- { group = GetInitGroup(initializer), reference = initializer.reference };
-
- public EntityInitializer Initializer(EGID id) => new(id, group, reference);
- public bool Valid => group != null;
- }
-
- private delegate FasterDictionary<RefWrapperType, ITypeSafeDictionary> GetInitGroupFunc(
- EntityInitializer initializer);
-
- /// <summary>
- /// Accesses the group field of the initializer
- /// </summary>
- private static readonly GetInitGroupFunc GetInitGroup = Reflections.CreateAccessor<GetInitGroupFunc>("_group");
-
- #endregion
- }
- }
|