A stable modding interface between Techblox and mods https://mod.exmods.org/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

53 行
2.3KB

  1. using Svelto.ECS;
  2. namespace TechbloxModdingAPI.Utility
  3. {
  4. public static class NativeApiExtensions
  5. {
  6. /// <summary>
  7. /// Attempts to query an entity and returns an optional that contains the result if succeeded.
  8. /// <b>This overload does not take initializer data into account.</b>
  9. /// </summary>
  10. /// <param name="entitiesDB">The entities DB</param>
  11. /// <param name="egid">The EGID to query</param>
  12. /// <typeparam name="T">The component type to query</typeparam>
  13. /// <returns>An optional that contains the result on success or is empty if not found</returns>
  14. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EGID egid)
  15. where T : unmanaged, IEntityComponent
  16. {
  17. return entitiesDB.TryQueryEntitiesAndIndex<T>(egid, out uint index, out var array)
  18. ? new OptionalRef<T>(array, index)
  19. : new OptionalRef<T>();
  20. }
  21. /// <summary>
  22. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  23. /// </summary>
  24. /// <param name="entitiesDB"></param>
  25. /// <param name="obj"></param>
  26. /// <typeparam name="T"></typeparam>
  27. /// <returns></returns>
  28. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
  29. where T : unmanaged, IEntityComponent
  30. {
  31. var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
  32. return opt ? opt : new OptionalRef<T>(obj, true);
  33. }
  34. /// <summary>
  35. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  36. /// </summary>
  37. /// <param name="entitiesDB"></param>
  38. /// <param name="obj"></param>
  39. /// <typeparam name="T"></typeparam>
  40. /// <returns></returns>
  41. public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj)
  42. where T : unmanaged, IEntityComponent
  43. {
  44. var opt = QueryEntityOptional<T>(entitiesDB, obj.Id);
  45. if (opt) return ref opt.Get();
  46. if (obj.InitData.Valid) return ref obj.InitData.Initializer(obj.Id).GetOrCreate<T>();
  47. return ref opt.Get(); //Default value
  48. }
  49. }
  50. }