diff --git a/src/Flecs.NET/Core/Ecs.cs b/src/Flecs.NET/Core/Ecs/Aliases.cs similarity index 54% rename from src/Flecs.NET/Core/Ecs.cs rename to src/Flecs.NET/Core/Ecs/Aliases.cs index 284b0da5..335159bb 100644 --- a/src/Flecs.NET/Core/Ecs.cs +++ b/src/Flecs.NET/Core/Ecs/Aliases.cs @@ -1,480 +1,7 @@ -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using Flecs.NET.Bindings; -using Flecs.NET.Utilities; using static Flecs.NET.Bindings.Native; namespace Flecs.NET.Core { - /// - /// A static class for storing ECS related globals. - /// - public static partial class Ecs - { - /// - /// Default path separator. - /// - public const string DefaultSeparator = "."; - - /// - /// Default path root. - /// - public const string DefaultRootSeparator = "::"; - } - - // TODO: Add proper logging - // Debug - public static partial class Ecs - { -#if NETCOREAPP3_0_OR_GREATER - /// - /// Debug assert. - /// - /// - /// - /// - /// - /// - /// - [Conditional("DEBUG")] - public static void Assert( - bool condition, - string message = "", - [CallerLineNumber] int line = default, - [CallerMemberName] string member = "", - [CallerFilePath] string file = "", - [CallerArgumentExpression("condition")] string conditionStr = "") - { - if (condition) - return; - - throw new AssertionException($"\n[Flecs.NET Assertion Failed]: {member}, Line {line}, {file}\n[Condition]: {conditionStr}\n[Assertion Message]: {message}"); - } - - /// - /// Debug assert. - /// - /// - /// - /// - /// - [Conditional("DEBUG")] - public static void Error( - string message = "", - [CallerLineNumber] int line = default, - [CallerMemberName] string member = "", - [CallerFilePath] string file = "") - { - throw new ErrorException($"\n[Flecs.NET Error]: {member}, Line {line}, {file}\n[Error Message]: {message}"); - } -#else - /// - /// Debug assert. - /// - /// - /// - /// - /// - /// - [Conditional("DEBUG")] - public static void Assert( - bool condition, - string message = "", - [CallerLineNumber] int line = default, - [CallerMemberName] string member = "", - [CallerFilePath] string file = "") - { - if (condition) - return; - - throw new AssertionException($"\n[Flecs.NET Assertion Failed]: {member}, Line {line}, {file}\n[Assertion Message]: {message}"); - } - - /// - /// Debug assert. - /// - /// - /// - /// - /// - [Conditional("DEBUG")] - public static void Error( - string message = "", - [CallerLineNumber] int line = default, - [CallerMemberName] string member = "", - [CallerFilePath] string file = "") - { - throw new ErrorException($"\n[Flecs.NET Error]: {member}, Line {line}, {file}\n[Error Message]: {message}"); - } -#endif - - /// - /// Flecs.NET assertion exception. - /// - [Serializable] - public class AssertionException : Exception - { - /// - /// - /// - public AssertionException() - { - } - - /// - /// - /// - /// - public AssertionException(string message) : base(message) - { - } - - /// - /// - /// - /// - /// - public AssertionException(string message, Exception innerException) : base(message, innerException) - { - } - } - - /// - /// Flecs.NET error exception. - /// - [Serializable] - public class ErrorException : Exception - { - /// - /// - /// - public ErrorException() - { - } - - /// - /// - /// - /// - public ErrorException(string message) : base(message) - { - } - - /// - /// - /// - /// - /// - public ErrorException(string message, Exception innerException) : base(message, innerException) - { - } - } - - /// - /// Flecs native exception. - /// - [Serializable] - public class NativeException : Exception - { - /// - /// - /// - public NativeException() - { - } - - /// - /// - /// - /// - public NativeException(string message) : base(message) - { - } - - /// - /// - /// - /// - /// - public NativeException(string message, Exception innerException) : base(message, innerException) - { - } - } - } - - // Log namespace - public static unsafe partial class Ecs - { - /// - /// Static class for flecs logging functions. - /// - public static class Log - { - /// - /// Set log level. - /// - /// - public static void SetLevel(int level) - { - _ = ecs_log_set_level(level); - } - - /// - /// Get log level. - /// - /// - public static int GetLevel() - { - return ecs_log_get_level(); - } - - /// - /// Enable colors in logging. - /// - /// - public static void EnableColors(bool enabled = true) - { - ecs_log_enable_colors(Macros.Bool(enabled)); - } - - /// - /// Enable timestamps in logging. - /// - /// - public static void EnableTimestamp(bool enabled = true) - { - ecs_log_enable_timestamp(Macros.Bool(enabled)); - } - - /// - /// Enable time delta in logging. - /// - /// - public static void EnableTimeDelta(bool enabled = true) - { - ecs_log_enable_timedelta(Macros.Bool(enabled)); - } - - /// - /// Debug trace (Level 1) - /// - /// - /// - /// - public static void Dbg(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) - { - using NativeString nativeMessage = (NativeString)message; - using NativeString nativeFile = (NativeString)file; - ecs_log_(1, nativeFile, line, nativeMessage); - } - - /// - /// Trace (Level 0) - /// - /// - /// - /// - public static void Trace(string message = "", [CallerFilePath] string file = "", - [CallerLineNumber] int line = 0) - { - using NativeString nativeMessage = (NativeString)message; - using NativeString nativeFile = (NativeString)file; - ecs_log_(0, nativeFile, line, nativeMessage); - } - - /// - /// Trace (Level -2) - /// - /// - /// - /// - public static void Warn(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) - { - using NativeString nativeMessage = (NativeString)message; - using NativeString nativeFile = (NativeString)file; - ecs_log_(-2, nativeFile, line, nativeMessage); - } - - /// - /// Trace (Level -3) - /// - /// - /// - /// - public static void Err(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) - { - using NativeString nativeMessage = (NativeString)message; - using NativeString nativeFile = (NativeString)file; - ecs_log_(-3, nativeFile, line, nativeMessage); - } - - /// - /// Trace (Level 0) - /// - /// - /// - /// - public static void Push(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) - { - using NativeString nativeMessage = (NativeString)message; - using NativeString nativeFile = (NativeString)file; - ecs_log_(1, nativeFile, line, nativeMessage); - ecs_log_push_(0); - } - - /// - /// Increase log indentation. - /// - public static void Push() - { - ecs_log_push_(0); - } - - /// - /// Decrease log indentation. - /// - public static void Pop() - { - ecs_log_pop_(0); - } - } - } - - // Delegates - public static unsafe partial class Ecs - { - /// - /// App init action. - /// - public delegate int AppInitAction(ecs_world_t* world); - - /// - /// Context free. - /// - public delegate void ContextFree(void* ctx); - - /// - /// Copy type hook callback. - /// - public delegate void CopyCallback(void* src, void* dst, int count, ecs_type_info_t* typeInfo); - - /// - /// Copy type hook callback. - /// - /// - public delegate void CopyCallback(ref T src, ref T dst, TypeInfo typeInfo); - - /// - /// Ctor type hook callback. - /// - public delegate void CtorCallback(void* data, int count, ecs_type_info_t* typeInfo); - - /// - /// Ctor type hook callback. - /// - /// - public delegate void CtorCallback(ref T data, TypeInfo typeInfo); - - /// - /// Dtor type hook callback. - /// - public delegate void DtorCallback(void* data, int count, ecs_type_info_t* typeInfo); - - /// - /// Dtor type hook callback. - /// - /// - public delegate void DtorCallback(ref T data, TypeInfo typeInfo); - - /// - /// Each entity callback. - /// - public delegate void EachEntityCallback(Entity entity); - - /// - /// Each id callback. - /// - public delegate void EachIdCallback(Id id); - - /// - /// Each index callback. - /// - public delegate void EachIndexCallback(Iter it, int i); - - /// - /// Finish action. - /// - public delegate void FiniAction(ecs_world_t* world, void* ctx); - - /// - /// Free. - /// - public delegate void Free(IntPtr data); - - /// - /// GroupBy action. - /// - public delegate ulong GroupByAction(ecs_world_t* world, ecs_table_t* table, ulong groupId, void* ctx); - - /// - /// GroupBy action. - /// - public delegate ulong GroupByCallback(World world, Table table, Entity group); - - /// - /// Group create action. - /// - public delegate void* GroupCreateAction(ecs_world_t* world, ulong groupId, void* groupByCtx); - - /// - /// Group delete action. - /// - public delegate void GroupDeleteAction(ecs_world_t* world, ulong groupId, void* groupCtx, void* groupByCtx); - - /// - /// Iter action. - /// - public delegate void IterAction(ecs_iter_t* it); - - /// - /// Iter callback. - /// - public delegate void IterCallback(Iter it); - - /// - /// Iter next action. - /// - public delegate byte IterNextAction(ecs_iter_t* it); - - /// - /// Move type hook callback. - /// - public delegate void MoveCallback(void* src, void* dst, int count, ecs_type_info_t* typeInfo); - - /// - /// Move type hook callback. - /// - /// - public delegate void MoveCallback(ref T src, ref T dst, TypeInfo typeInfo); - - /// - /// OrderBy action. - /// - public delegate int OrderByAction(ulong e1, void* ptr1, ulong e2, void* ptr2); - - /// - /// A callback that takes a reference to a world. - /// - public delegate void WorldCallback(ref World world); - - /// - /// A callback that takes a reference to a term. - /// - public delegate void TermCallback(ref Term term); - } - - // Built-in global entities, tags, and flags. public static partial class Ecs { // Enums @@ -1078,34 +605,4 @@ public static partial class Ecs /// public static ref ulong Quantity => ref EcsQuantity; } - - // Static classes - public static partial class Ecs - { - /// - /// Utilities for documenting entities, components and systems. - /// - public static class Doc - { - /// - /// Reference to . - /// - public static ref ulong Brief => ref EcsDocBrief; - - /// - /// Reference to . - /// - public static ref ulong Detail => ref EcsDocDetail; - - /// - /// Reference to . - /// - public static ref ulong Link => ref EcsDocLink; - - /// - /// Reference to . - /// - public static ref ulong Color => ref EcsDocColor; - } - } } diff --git a/src/Flecs.NET/Core/Ecs/Constants.cs b/src/Flecs.NET/Core/Ecs/Constants.cs new file mode 100644 index 00000000..5ca3d85a --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Constants.cs @@ -0,0 +1,15 @@ +namespace Flecs.NET.Core +{ + public static partial class Ecs + { + /// + /// Default path separator. + /// + public const string DefaultSeparator = "."; + + /// + /// Default path root. + /// + public const string DefaultRootSeparator = "::"; + } +} diff --git a/src/Flecs.NET/Core/Ecs/Debug.cs b/src/Flecs.NET/Core/Ecs/Debug.cs new file mode 100644 index 00000000..16f5c067 --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Debug.cs @@ -0,0 +1,91 @@ +using System.Diagnostics; +using System.Runtime.CompilerServices; + +namespace Flecs.NET.Core +{ + public static partial class Ecs + { +#if NETCOREAPP3_0_OR_GREATER + /// + /// Debug assert. + /// + /// + /// + /// + /// + /// + /// + [Conditional("DEBUG")] + public static void Assert( + bool condition, + string message = "", + [CallerLineNumber] int line = default, + [CallerMemberName] string member = "", + [CallerFilePath] string file = "", + [CallerArgumentExpression("condition")] string conditionStr = "") + { + if (condition) + return; + + throw new AssertionException($"\n[Flecs.NET Assertion Failed]: {member}, Line {line}, {file}\n[Condition]: {conditionStr}\n[Assertion Message]: {message}"); + } + + /// + /// Debug assert. + /// + /// + /// + /// + /// + [Conditional("DEBUG")] + public static void Error( + string message = "", + [CallerLineNumber] int line = default, + [CallerMemberName] string member = "", + [CallerFilePath] string file = "") + { + throw new ErrorException($"\n[Flecs.NET Error]: {member}, Line {line}, {file}\n[Error Message]: {message}"); + } +#else + /// + /// Debug assert. + /// + /// + /// + /// + /// + /// + [Conditional("DEBUG")] + public static void Assert( + bool condition, + string message = "", + [CallerLineNumber] int line = default, + [CallerMemberName] string member = "", + [CallerFilePath] string file = "") + { + if (condition) + return; + + throw new AssertionException( + $"\n[Flecs.NET Assertion Failed]: {member}, Line {line}, {file}\n[Assertion Message]: {message}"); + } + + /// + /// Debug assert. + /// + /// + /// + /// + /// + [Conditional("DEBUG")] + public static void Error( + string message = "", + [CallerLineNumber] int line = default, + [CallerMemberName] string member = "", + [CallerFilePath] string file = "") + { + throw new ErrorException($"\n[Flecs.NET Error]: {member}, Line {line}, {file}\n[Error Message]: {message}"); + } +#endif + } +} diff --git a/src/Flecs.NET/Core/Ecs/Delegates.cs b/src/Flecs.NET/Core/Ecs/Delegates.cs new file mode 100644 index 00000000..e71b3e10 --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Delegates.cs @@ -0,0 +1,137 @@ +using System; +using static Flecs.NET.Bindings.Native; + +namespace Flecs.NET.Core +{ + public static unsafe partial class Ecs + { + /// + /// App init action. + /// + public delegate int AppInitAction(ecs_world_t* world); + + /// + /// Context free. + /// + public delegate void ContextFree(void* ctx); + + /// + /// Copy type hook callback. + /// + public delegate void CopyCallback(void* src, void* dst, int count, ecs_type_info_t* typeInfo); + + /// + /// Copy type hook callback. + /// + /// + public delegate void CopyCallback(ref T src, ref T dst, TypeInfo typeInfo); + + /// + /// Ctor type hook callback. + /// + public delegate void CtorCallback(void* data, int count, ecs_type_info_t* typeInfo); + + /// + /// Ctor type hook callback. + /// + /// + public delegate void CtorCallback(ref T data, TypeInfo typeInfo); + + /// + /// Dtor type hook callback. + /// + public delegate void DtorCallback(void* data, int count, ecs_type_info_t* typeInfo); + + /// + /// Dtor type hook callback. + /// + /// + public delegate void DtorCallback(ref T data, TypeInfo typeInfo); + + /// + /// Each entity callback. + /// + public delegate void EachEntityCallback(Entity entity); + + /// + /// Each id callback. + /// + public delegate void EachIdCallback(Id id); + + /// + /// Each index callback. + /// + public delegate void EachIndexCallback(Iter it, int i); + + /// + /// Finish action. + /// + public delegate void FiniAction(ecs_world_t* world, void* ctx); + + /// + /// Free. + /// + public delegate void Free(IntPtr data); + + /// + /// GroupBy action. + /// + public delegate ulong GroupByAction(ecs_world_t* world, ecs_table_t* table, ulong groupId, void* ctx); + + /// + /// GroupBy action. + /// + public delegate ulong GroupByCallback(World world, Table table, Entity group); + + /// + /// Group create action. + /// + public delegate void* GroupCreateAction(ecs_world_t* world, ulong groupId, void* groupByCtx); + + /// + /// Group delete action. + /// + public delegate void GroupDeleteAction(ecs_world_t* world, ulong groupId, void* groupCtx, void* groupByCtx); + + /// + /// Iter action. + /// + public delegate void IterAction(ecs_iter_t* it); + + /// + /// Iter callback. + /// + public delegate void IterCallback(Iter it); + + /// + /// Iter next action. + /// + public delegate byte IterNextAction(ecs_iter_t* it); + + /// + /// Move type hook callback. + /// + public delegate void MoveCallback(void* src, void* dst, int count, ecs_type_info_t* typeInfo); + + /// + /// Move type hook callback. + /// + /// + public delegate void MoveCallback(ref T src, ref T dst, TypeInfo typeInfo); + + /// + /// OrderBy action. + /// + public delegate int OrderByAction(ulong e1, void* ptr1, ulong e2, void* ptr2); + + /// + /// A callback that takes a reference to a world. + /// + public delegate void WorldCallback(ref World world); + + /// + /// A callback that takes a reference to a term. + /// + public delegate void TermCallback(ref Term term); + } +} diff --git a/src/Flecs.NET/Core/Ecs/Doc.cs b/src/Flecs.NET/Core/Ecs/Doc.cs new file mode 100644 index 00000000..e0239eda --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Doc.cs @@ -0,0 +1,33 @@ +using static Flecs.NET.Bindings.Native; + +namespace Flecs.NET.Core +{ + public static partial class Ecs + { + /// + /// Utilities for documenting entities, components and systems. + /// + public static class Doc + { + /// + /// Reference to . + /// + public static ref ulong Brief => ref EcsDocBrief; + + /// + /// Reference to . + /// + public static ref ulong Detail => ref EcsDocDetail; + + /// + /// Reference to . + /// + public static ref ulong Link => ref EcsDocLink; + + /// + /// Reference to . + /// + public static ref ulong Color => ref EcsDocColor; + } + } +} diff --git a/src/Flecs.NET/Core/Ecs/Ecs.cs b/src/Flecs.NET/Core/Ecs/Ecs.cs new file mode 100644 index 00000000..92126339 --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Ecs.cs @@ -0,0 +1,5 @@ +namespace Flecs.NET.Core +{ + /// + public static partial class Ecs { } +} diff --git a/src/Flecs.NET/Core/Ecs/Exceptions.cs b/src/Flecs.NET/Core/Ecs/Exceptions.cs new file mode 100644 index 00000000..1796149e --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Exceptions.cs @@ -0,0 +1,100 @@ +using System; + +namespace Flecs.NET.Core +{ + public static partial class Ecs + { + /// + /// Flecs.NET assertion exception. + /// + [Serializable] + public class AssertionException : Exception + { + /// + /// + /// + public AssertionException() + { + } + + /// + /// + /// + /// + public AssertionException(string message) : base(message) + { + } + + /// + /// + /// + /// + /// + public AssertionException(string message, Exception innerException) : base(message, innerException) + { + } + } + + /// + /// Flecs.NET error exception. + /// + [Serializable] + public class ErrorException : Exception + { + /// + /// + /// + public ErrorException() + { + } + + /// + /// + /// + /// + public ErrorException(string message) : base(message) + { + } + + /// + /// + /// + /// + /// + public ErrorException(string message, Exception innerException) : base(message, innerException) + { + } + } + + /// + /// Flecs native exception. + /// + [Serializable] + public class NativeException : Exception + { + /// + /// + /// + public NativeException() + { + } + + /// + /// + /// + /// + public NativeException(string message) : base(message) + { + } + + /// + /// + /// + /// + /// + public NativeException(string message, Exception innerException) : base(message, innerException) + { + } + } + } +} diff --git a/src/Flecs.NET/Core/Ecs/Log.cs b/src/Flecs.NET/Core/Ecs/Log.cs new file mode 100644 index 00000000..e3c727ec --- /dev/null +++ b/src/Flecs.NET/Core/Ecs/Log.cs @@ -0,0 +1,143 @@ +using System.Runtime.CompilerServices; +using Flecs.NET.Utilities; +using static Flecs.NET.Bindings.Native; + +namespace Flecs.NET.Core +{ + public static unsafe partial class Ecs + { + /// + /// Static class for flecs logging functions. + /// + public static class Log + { + /// + /// Set log level. + /// + /// + public static void SetLevel(int level) + { + _ = ecs_log_set_level(level); + } + + /// + /// Get log level. + /// + /// + public static int GetLevel() + { + return ecs_log_get_level(); + } + + /// + /// Enable colors in logging. + /// + /// + public static void EnableColors(bool enabled = true) + { + ecs_log_enable_colors(Macros.Bool(enabled)); + } + + /// + /// Enable timestamps in logging. + /// + /// + public static void EnableTimestamp(bool enabled = true) + { + ecs_log_enable_timestamp(Macros.Bool(enabled)); + } + + /// + /// Enable time delta in logging. + /// + /// + public static void EnableTimeDelta(bool enabled = true) + { + ecs_log_enable_timedelta(Macros.Bool(enabled)); + } + + /// + /// Debug trace (Level 1) + /// + /// + /// + /// + public static void Dbg(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) + { + using NativeString nativeMessage = (NativeString)message; + using NativeString nativeFile = (NativeString)file; + ecs_log_(1, nativeFile, line, nativeMessage); + } + + /// + /// Trace (Level 0) + /// + /// + /// + /// + public static void Trace(string message = "", [CallerFilePath] string file = "", + [CallerLineNumber] int line = 0) + { + using NativeString nativeMessage = (NativeString)message; + using NativeString nativeFile = (NativeString)file; + ecs_log_(0, nativeFile, line, nativeMessage); + } + + /// + /// Trace (Level -2) + /// + /// + /// + /// + public static void Warn(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) + { + using NativeString nativeMessage = (NativeString)message; + using NativeString nativeFile = (NativeString)file; + ecs_log_(-2, nativeFile, line, nativeMessage); + } + + /// + /// Trace (Level -3) + /// + /// + /// + /// + public static void Err(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) + { + using NativeString nativeMessage = (NativeString)message; + using NativeString nativeFile = (NativeString)file; + ecs_log_(-3, nativeFile, line, nativeMessage); + } + + /// + /// Trace (Level 0) + /// + /// + /// + /// + public static void Push(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) + { + using NativeString nativeMessage = (NativeString)message; + using NativeString nativeFile = (NativeString)file; + ecs_log_(1, nativeFile, line, nativeMessage); + ecs_log_push_(0); + } + + /// + /// Increase log indentation. + /// + public static void Push() + { + ecs_log_push_(0); + } + + /// + /// Decrease log indentation. + /// + public static void Pop() + { + ecs_log_pop_(0); + } + } + } +} diff --git a/src/Flecs.NET/Utilities/Macros.cs b/src/Flecs.NET/Utilities/Macros.cs index b2766cfd..fab336f9 100644 --- a/src/Flecs.NET/Utilities/Macros.cs +++ b/src/Flecs.NET/Utilities/Macros.cs @@ -54,7 +54,7 @@ public static bool Bool(byte value) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool AreSameReadOnlyRefs(in T a, in T b) { - return Unsafe.AreSame(ref Unsafe.AsRef(a), ref Unsafe.AsRef(b)); + return Unsafe.AreSame(ref Unsafe.AsRef(in a), ref Unsafe.AsRef(in b)); } ///