I develop a game server. Currently I use a switch :
import protocol.messages.connection.Message1;
import protocol.messages.connection.Message2;
import protocol.messages.queues.Message3;
import ......
import protocol.messages.NetworkMessage;
class ProtocolMessageManager
{
public static NetworkMessage GetInstance(uint id)
{
switch(id)
{
case Message1.Id:
return new Message1();
case Message2.Id:
return new Message2();
.....
default:
return null;
}
}
}
what I want to do :
import protocol.messages.NetworkMessage;
class ProtocolMessageManager
{
private static TypeInfo_Class[uint] m_types;
public static void Init()
{
foreach(mod; ModuleInfo)
{
foreach(TypeInfo_Class lc; mod.localClasses)
{
if(lc.name.indexOf("protocol.messages") != -1)
{
NetworkMessage c =
cast(NetworkMessage)lc.create();
ProtocolMessageManager.m_types[c.MessageId] = lc;
}
}
}
}
public static NetworkMessage GetInstance(string id)
{
auto v = (id in ProtocolMessageManager.m_types);
if (v !is null)
return
cast(NetworkMessage)ProtocolMessageManager.m_types[id].create();
else
return null;
}
}