-system name getter

-added error checking for event handling in registerSystem
-now only valid handleEvent() functions are taken by system during register
This commit is contained in:
Mergul 2019-08-10 15:35:36 +00:00
parent f27e4c30ad
commit dfdb56d501
3 changed files with 43 additions and 17 deletions

View file

@ -69,7 +69,7 @@ export class EntityManager
if(system.m_components)Mallocator.instance.dispose(system.m_components); if(system.m_components)Mallocator.instance.dispose(system.m_components);
if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components); if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components);
if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components); if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components);
if(system.name)Mallocator.instance.dispose(system.name); if(system.m_name)Mallocator.instance.dispose(system.m_name);
if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers); if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers);
if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer); if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer);
@ -344,22 +344,33 @@ export class EntityManager
data_system.handleEvent(input, *cast(Type*) data.event); data_system.handleEvent(input, *cast(Type*) data.event);
} }
static void setEventCallers(Sys)(ref System system) void setEventCallers(Sys)(ref System system)
{ {
enum event_handlers_num = __traits(getOverloads, Sys, "handleEvent").length; enum event_handlers_num = __traits(getOverloads, Sys, "handleEvent").length;
system.m_event_callers = Mallocator.instance.makeArray!( System.EventCaller[] callers = (cast(System.EventCaller*)alloca(event_handlers_num * System.EventCaller.sizeof))[0..event_handlers_num];
System.EventCaller)(event_handlers_num); int i = 0;
foreach (j, func; __traits(getOverloads, Sys, "handleEvent")) foreach (j, func; __traits(getOverloads, Sys, "handleEvent"))
{ {
alias EventParamType = Parameters!(__traits(getOverloads, alias Params = Parameters!(__traits(getOverloads,
Sys, "handleEvent")[j])[1]; Sys, "handleEvent")[j]);
system.m_event_callers[j].callback = cast( static if(Params.length == 2 && is(Params[0] == __traits(getMember, Sys, "EventInput")))
{
alias EventParamType = Params[1];
enum EventName = Unqual!(EventParamType).stringof;
ushort evt = events_map.get(cast(char[]) EventName, ushort.max);
assert(evt != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing event \""~EventName~"\".");
callers[i].callback = cast(
void*)&callEventHandler!(EventParamType); void*)&callEventHandler!(EventParamType);
system.m_event_callers[j].id = EventParamType.event_id; callers[i].id = EventParamType.event_id;
i++;
} }
} }
system.m_event_callers = Mallocator.instance.makeArray(callers[0..i]);
}
static if (__traits(hasMember, Sys, "handleEvent")) static if (__traits(hasMember, Sys, "handleEvent"))
{ {
setEventCallers!(Sys)(system); setEventCallers!(Sys)(system);
@ -736,8 +747,8 @@ export class EntityManager
} }
else else
{ {
system.name = Mallocator.instance.makeArray(Sys.stringof); system.m_name = Mallocator.instance.makeArray(Sys.stringof);
systems_map.add(system.name, cast(ushort) systems.length); systems_map.add(system.m_name, cast(ushort) systems.length);
system.m_id = cast(ushort)(systems.length); system.m_id = cast(ushort)(systems.length);
@ -2302,9 +2313,9 @@ export class EntityManager
caller.exclusion = null; caller.exclusion = null;
/*import std.stdio; /*import std.stdio;
write("Exclusive systems for system ", caller.system.name, ": "); write("Exclusive systems for system ", caller.system.m_name, ": ");
foreach (ex; exclusion[0 .. index]) foreach (ex; exclusion[0 .. index])
write(ex.system.name, " "); write(ex.system.m_name, " ");
writeln();*/ writeln();*/
} }
@ -2362,9 +2373,9 @@ export class EntityManager
caller.dependencies = null; caller.dependencies = null;
/*import std.stdio; /*import std.stdio;
write("Dependencies for system ", caller.system.name, ": "); write("Dependencies for system ", caller.system.m_name, ": ");
foreach (ex; caller.dependencies) foreach (ex; caller.dependencies)
write(ex.system.name, " "); write(ex.system.m_name, " ");
writeln();*/ writeln();*/
} }
} }

View file

@ -74,6 +74,14 @@ struct System
return m_id; return m_id;
} }
/************************************************************************************************************************
*Get system name.
*/
export const (char)[] name() nothrow @nogc
{
return cast(const (char)[])m_name;
}
struct EventCaller struct EventCaller
{ {
ushort id; ushort id;
@ -97,7 +105,7 @@ package:
int m_pass; int m_pass;
///system name ///system name
char[] name; char[] m_name;
///required components ///required components
ushort[] m_components; ushort[] m_components;

View file

@ -376,6 +376,11 @@ struct TestSystem2
//TestComp* tt; //TestComp* tt;
} }
void handleEvent(EventInput input)
{
}
void handleEvent(EventInput input, ref TestEvent event) void handleEvent(EventInput input, ref TestEvent event)
{ {
input.test.bg = event.a; input.test.bg = event.a;
@ -571,9 +576,11 @@ int main()
//foreach(j; 0..1_000)gEM.addEntity(tmpl); //foreach(j; 0..1_000)gEM.addEntity(tmpl);
gEM.beginRegister(); gEM.beginRegister();
//gEM.registerSystem!TestSystem2(0); gEM.registerSystem!TestSystem2(0);
gEM.endRegister(); gEM.endRegister();
System* sys = EntityManager.instance.getSystem(TestSystem2.system_id);
//gEM.generateDependencies(); //gEM.generateDependencies();
//assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+24)) == EntityID(1,1)); //assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+24)) == EntityID(1,1));