/// <summary> /// 通过名字创建一个业务模块 /// 先通过名字反射出Class,如果不存在 /// 则通过扫描Lua文件目录加载LuaModule /// </summary> /// <param name="name">业务模块的名字</param> /// <param name="args"></param> /// <returns></returns> public BusinessModule CreateModule(string name, object args = null) { if (m_mapModules.ContainsKey(name)) { Log.Error("CreateModule() The Module<{0}> Has Existed!"); return(null); } BusinessModule module = null; Type type = Type.GetType(m_domain + "." + name); if (type != null) { module = Activator.CreateInstance(type) as BusinessModule; } else { module = new LuaModule(name); } m_mapModules.Add(name, module); //处理预监听的事件 if (m_mapPreListenEvents.ContainsKey(name)) { EventTable mgrEvent = null; mgrEvent = m_mapPreListenEvents[name]; m_mapPreListenEvents.Remove(name); module.SetEventTable(mgrEvent); } module.Create(args); //处理缓存的消息 if (m_mapCacheMessage.ContainsKey(name)) { List <MessageObject> list = m_mapCacheMessage[name]; for (int i = 0; i < list.Count; i++) { MessageObject msgobj = list[i]; module.HandleMessage(msgobj.msg, msgobj.args); } m_mapCacheMessage.Remove(name); } return(module); }
/// <summary> /// 监听指定模块的指定事件 /// </summary> /// <param name="target"></param> /// <param name="type"></param> /// <returns></returns> public ModuleEvent Event(string target, string type) { ModuleEvent evt = null; BusinessModule module = GetModule(target); if (module != null) { evt = module.Event(type); } else { //预创建事件 EventTable table = GetPreEventTable(target); evt = table.GetEvent(type); Log.Warning("Event() target不存在!将预监听事件! target:{0}, event:{1}", target, type); } return(evt); }
/// <summary> /// 释放一个由ModuleManager创建的模块 /// 遵守谁创建谁释放的原则 /// </summary> /// <param name="module"></param> public void ReleaseModule(BusinessModule module) { if (module != null) { if (m_mapModules.ContainsKey(module.Name)) { m_mapModules.Remove(module.Name); module.Release(); } else { Log.Error("ReleaseModule() 模块不是由ModuleManager创建的! name = " + module.Name); } } else { Log.Error("ReleaseModule() module = null!"); } }
private void SendMessage_Internal(string target, string msg, object[] args) { BusinessModule module = GetModule(target); if (module != null) { module.HandleMessage(msg, args); } else { //将消息缓存起来 List <MessageObject> list = GetCacheMessageList(target); MessageObject obj = new MessageObject(); obj.target = target; obj.msg = msg; obj.args = args; list.Add(obj); Log.Warning("SendMessage() target不存在!将消息缓存起来! target:{0}, msg:{1}, args:{2}", target, msg, args); } }