/// <summary>
        /// 获取管理的模块
        /// </summary>
        /// <typeparam name="T">模块的类型</typeparam>
        /// <returns>返回指定模块实例</returns>
        public T GetModule <T>() where T : class
        {
            Type t_InterfaceType = typeof(T);

            if (!t_InterfaceType.IsInterface) //如果不是接口
            {
                throw new GameBoxFrameworkException(string.Format("你只能通过接口来获取系统内建模块,{0} 不是内建模块对应的接口.", t_InterfaceType.FullName));
            }
            if (!t_InterfaceType.FullName.StartsWith("GameBoxFramework.")) //如果接口的命名空间开头不为内建系统命名空间
            {
                throw new GameBoxFrameworkException(string.Format("{0} 不是系统内建的模块.", t_InterfaceType.FullName));
            }
            var    t_NamespaceSplits = t_InterfaceType.Namespace.Split('.');
            string t_ModuleName      = string.Format("{0}.{1}.{2}", "GameBoxFramework", t_NamespaceSplits[t_NamespaceSplits.Length - 1], t_InterfaceType.Name.Substring(1));
            Type   t_ModuleType      = Type.GetType(t_ModuleName);

            if (t_ModuleType == null) //如果存在这个内建模块类型
            {
                throw new GameBoxFrameworkException(string.Format("找不到系统内建模块 '{0}'.", t_ModuleName));
            }
            var t_TargetModule = IListDataStructure.GetNode(module => null != module && module.GetType() == t_ModuleType);

            if (null == t_TargetModule)                                              //还没有存在这个模块
            {
                t_TargetModule = (BaseModule)Activator.CreateInstance(t_ModuleType); //创建该模块
                if (t_TargetModule == null)
                {
                    throw new GameBoxFrameworkException(string.Format("创建 '{0}' 模块失败.", t_ModuleType.GetType().FullName));
                }
                else
                {
                    IListDataStructure.AddNode(t_TargetModule); //添加进数据结构
                    t_TargetModule.OnInit(this);                //初始化模块
                    t_TargetModule.OnStart(this);               //启动模块
                }
            }
            else //已经存在这个模块
            {
                t_TargetModule.Weight++; //该模块权值刷新
                IListDataStructure.Sort(); //重新排序模块,已确保权值高的模块优先被搜索到,提高效率
            }
            //return (T)Convert.ChangeType(t_TargetModule,typeof(T)); //将 BaseModlue 转换成 模块对应的接口
            return(t_TargetModule as T);
        }
Esempio n. 2
0
 /// <summary>
 /// 获取状态机
 /// </summary>
 /// <param name="t_FSMName">状态机的名字</param>
 /// <returns>获取到的状态机实例</returns>
 public IFSM GetFSM(string t_FSMName)
 {
     return(IListDataStructure.GetNode(fsm => fsm.m_FSMName == t_FSMName));
 }