Merge branch 'fixes' into 'master'

Fix unregisterSystem function

See merge request Mergul/bubel-ecs!26
This commit is contained in:
Dawid Masiukiewicz 2023-03-09 11:02:53 +00:00
commit beb1837c43
3 changed files with 78 additions and 16 deletions

View file

@ -341,7 +341,7 @@ nothrow:
int result; int result;
foreach (ref Key k; this) foreach (ref Key k; this)
{ {
result = (cast(int delegate(ref Key k) nothrow)dg)(k); result = (cast(int delegate(ref Key k) nothrow @nogc)dg)(k);
if (result) if (result)
break; break;
} }
@ -353,7 +353,7 @@ nothrow:
int result; int result;
foreach (ref Value v; this) foreach (ref Value v; this)
{ {
result = (cast(int delegate(ref Value v) nothrow)dg)(v); result = (cast(int delegate(ref Value v) nothrow @nogc)dg)(v);
if (result) if (result)
break; break;
} }
@ -365,7 +365,7 @@ nothrow:
int result; int result;
foreach (ref Key k, ref Value v; this) foreach (ref Key k, ref Value v; this)
{ {
result = (cast(int delegate(ref Key k, ref Value v) nothrow)dg)(k, v); result = (cast(int delegate(ref Key k, ref Value v) nothrow @nogc)dg)(k, v);
if (result) if (result)
break; break;
} }

View file

@ -76,7 +76,7 @@ export struct EntityManager
UpdatePass* pass = Mallocator.make!UpdatePass; UpdatePass* pass = Mallocator.make!UpdatePass;
pass.name = Mallocator.makeArray(cast(char[]) "update"); pass.name = Mallocator.makeArray(cast(char[]) "update");
passes.add(pass); passes.add(pass);
passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1)); passes_map.add(cast(const(char)[]) pass.name, cast(ushort)(passes.length - 1));
} }
} }
} }
@ -84,7 +84,7 @@ export struct EntityManager
/************************************************************************************************************************ /************************************************************************************************************************
Deinitialize and destroy ECS. This function release whole memory. Deinitialize and destroy ECS. This function release whole memory.
*/ */
export static void destroy() export static void destroy() nothrow @nogc
{ {
if (gEntityManager is null) if (gEntityManager is null)
return; return;
@ -334,16 +334,23 @@ export struct EntityManager
*/ */
void unregisterSystem(Sys)() void unregisterSystem(Sys)()
{ {
assert(register_state, "unregisterSystem must be called between beginRegister() and endRegister().");
ushort system_id = becsID!Sys; ushort system_id = becsID!Sys;
System* system = getSystem(system_id); System* system = getSystem(system_id);
assert(system, "System was not registered"); unregisterSystem(system);
}
/************************************************************************************************************************
Unregister given system form EntityManager.
*/
export void unregisterSystem(System* system) nothrow @nogc
{
assert(register_state, "unregisterSystem must be called between beginRegister() and endRegister().");
assert(system !is null, "System was not registered");
assert(system.isAlive, "System already unregistered"); assert(system.isAlive, "System already unregistered");
system.destroy(); //disable, destroy and dispose user created system but keep name and other data
*system = System.init; system.destroySystemData();
} }
/************************************************************************************************************************ /************************************************************************************************************************
@ -1248,7 +1255,8 @@ export struct EntityManager
} }
/************************************************************************************************************************ /************************************************************************************************************************
Return system ECS api by id Return system ECS api by id.
System pointer might become invalid after registration process. It should be get once again after new systems are registered.
*/ */
export System* getSystem(ushort id) nothrow @nogc export System* getSystem(ushort id) nothrow @nogc
{ {

View file

@ -97,38 +97,92 @@ struct System
return m_system_pointer != null; return m_system_pointer != null;
} }
/************************************************************************************************************************
Return pointer to user side system object
*/
export void* ptr() nothrow @nogc
{
return m_system_pointer;
}
package: package:
void destroy() ///destory system. Dispose all data
void destroy() nothrow @nogc
{
import bubel.ecs.std : Mallocator;
destroySystemData();
if (m_name)
{
Mallocator.dispose(m_name);
m_name = null;
}
}
///destroy all system data but keeps name which is used for case of system re-registration
void destroySystemData() nothrow @nogc
{ {
import bubel.ecs.std : Mallocator; import bubel.ecs.std : Mallocator;
disable(); disable();
if (m_destroy) if (m_destroy)
(cast(void function(void*)) m_destroy)(m_system_pointer); {
(cast(void function(void*) nothrow @nogc) m_destroy)(m_system_pointer);
m_destroy = null;
}
if (m_name)
Mallocator.dispose(m_name);
if (m_components) if (m_components)
{
Mallocator.dispose(m_components); Mallocator.dispose(m_components);
m_components = null;
}
if (m_excluded_components) if (m_excluded_components)
{
Mallocator.dispose(m_excluded_components); Mallocator.dispose(m_excluded_components);
m_excluded_components = null;
}
if (m_optional_components) if (m_optional_components)
{
Mallocator.dispose(m_optional_components); Mallocator.dispose(m_optional_components);
m_optional_components = null;
}
if (jobs) if (jobs)
{
Mallocator.dispose(jobs); Mallocator.dispose(jobs);
jobs = null;
}
if (m_read_only_components) if (m_read_only_components)
{
Mallocator.dispose(m_read_only_components); Mallocator.dispose(m_read_only_components);
m_read_only_components = null;
}
if (m_writable_components) if (m_writable_components)
{
Mallocator.dispose(m_writable_components); Mallocator.dispose(m_writable_components);
m_writable_components = null;
}
if (m_readonly_dependencies) if (m_readonly_dependencies)
{
Mallocator.dispose(m_readonly_dependencies); Mallocator.dispose(m_readonly_dependencies);
m_readonly_dependencies = null;
}
if (m_writable_dependencies) if (m_writable_dependencies)
{
Mallocator.dispose(m_writable_dependencies); Mallocator.dispose(m_writable_dependencies);
m_writable_dependencies = null;
}
if (m_event_callers) if (m_event_callers)
{
Mallocator.dispose(m_event_callers); Mallocator.dispose(m_event_callers);
m_event_callers = null;
}
if (m_system_pointer) if (m_system_pointer)
{
Mallocator.dispose(m_system_pointer); Mallocator.dispose(m_system_pointer);
m_system_pointer = null;
}
} }
struct EventCaller struct EventCaller