Symbol Registration

ryuukk_ ryuukk.dev at gmail.com
Wed Sep 25 17:32:10 UTC 2024


I did something like that once, i agree it should be greatly 
simplified


```D
     alias THIS_MODULE = game.states.gameplay;
     static foreach (member; __traits(allMembers, THIS_MODULE))
     {
         static foreach (attr; __traits(getAttributes, 
__traits(getMember, THIS_MODULE, member)))
         {
             static if (is(typeof(attr) == PacketType))
             {
                 if(type == attr)
                 {
                     found = true;
                     handled = true;

                     // check if it has params
                     static if (is(typeof( __traits(getMember, 
THIS_MODULE, member)) params == __parameters))
                     {
                         // LINFO("Packet Handler found: {} {}", 
attr, member.ptr);
                         // LINFO("Param: {}", 
params[0].stringof.ptr);

                         // get type of the packet (from the 
function parameter)
                         alias ptype = typeof(*params[0]);
                         static if (__traits(hasMember, ptype, 
"write") == false) static assert(0);

                         auto data = ptype();

                         static if (__traits(hasMember, ptype, 
"allocator"))
                         {
                             data.allocator = 
state.ctx.engine.arena.allocator();
                         }

                         data.read(&reader);
                         __traits(getMember, THIS_MODULE, 
member)(&data);
                     }
                 }
             }
         }
     }
```


```D
@(PacketType.RECONNECT)
void handle_reconnect(ReconnectPacket* rec)
{
     disconnect();
     state.net_info.host = rec.host;
     state.net_info.port = rec.port;
     state.net_info.connect_timer = 1.0;
}
``

I can never remember how to do it, never



More information about the dip.ideas mailing list