Exemplo n.º 1
0
        public virtual void Init()
        {
            try
            {
                EditorTemplates.RUNTIME_IN_SERVER = true;
                RPGServerTemplateManager.Instance.ToString();
                if (ZoneFactory == null)
                {
                    log.Info("********************************************************");
                    log.Info("# 初始化战斗编辑器扩展 ");
                    log.Info("********************************************************");
                    DataFactory = ReflectionUtil.CreateInterface <ZoneDataFactory>(GlobalConfig.ZoneDataFactory);
                    ZoneFactory = ReflectionUtil.CreateInterface <ZoneHostFactory>(GlobalConfig.InstanceZoneFactory, GlobalConfig.GameEditorRoot);
                    log.Info(" 战斗编辑器插件 : " + ZoneFactory);
                }
                if (NodeConfig == null)
                {
                    log.Info("********************************************************");
                    log.Info("# 加载配置文件");
                    log.Info("********************************************************");
                    NodeConfig = ZoneFactory.GetServerConfig();
                    var node_cfg = GlobalConfig.ZoneNodeConfig;
                    if (node_cfg != null)
                    {
                        log.Info(node_cfg.ToString());
                        node_cfg.LoadFields(NodeConfig);
                    }
                }
                if (DataRoot == null)
                {
                    log.Info("********************************************************");
                    log.Info("# 加载模板数据");
                    log.Info("********************************************************");
                    try
                    {
                        DataRoot         = DataFactory.CreateEditorTemplates(GlobalConfig.BattleDataRoot);
                        DataRoot.Verbose = true;
                        DataRoot.LoadAllTemplates();
                        DataRoot.CacheAllScenes();
                        Templates = DataRoot.Templates;
                    }
                    catch (Exception err)
                    {
                        throw new Exception("EditorTemplates init error : " + err.Message + "\n" + err.StackTrace, err);
                    }

                    if (CACHE_ALL_VOXEL)
                    {
                        GC.Collect();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.WaitForFullGCApproach();
                        GC.WaitForFullGCComplete();
                        log.Info("********************************************************");
                        log.Info("# 缓存体素");
                        log.Info("********************************************************");
                        var list   = new HashMap <string, SceneData>();
                        var caches = new ConcurrentDictionary <string, VoxelWorld>();
                        foreach (var sd in DataRoot.CacheAllScenes())
                        {
                            if (string.IsNullOrEmpty(sd.VoxelFileName) is false)
                            {
                                list.TryAdd(GlobalConfig.GameEditorRoot + sd.VoxelFileName, sd);
                            }
                        }
                        var cacheTasks = new ActionBlock <KeyValuePair <string, SceneData> >(run_CacheScene, new ExecutionDataflowBlockOptions()
                        {
                            MaxDegreeOfParallelism    = Environment.ProcessorCount - 1,
                            SingleProducerConstrained = false,
                        });
                        var watch = Stopwatch.StartNew();
                        foreach (var path in list)
                        {
                            cacheTasks.Post(path);
                        }
                        void run_CacheScene(KeyValuePair <string, SceneData> sd)
                        {
                            try
                            {
                                log.Info($"Cache voxel data : {sd.Key}");
                                var wx = VoxelWorld.LoadFromFile(sd.Key);
                                caches.TryAdd(sd.Key, wx);
                                log.Info($"Cache voxel data : {sd.Key} : OK ({caches.Count}/{list.Count})");
                            }
                            catch (Exception err)
                            {
                                log.Error($"Load Voxel Error : >>>{sd.Value}<<< {sd.Key}");
                                log.Error(err);
                            }
                        }
                        cacheTasks.Complete();
                        cacheTasks.Completion.Wait();
                        watch.Stop();
                        log.Warn($"Load Voxel Complete Use : {watch.Elapsed}");
                        VoxelWorldManager.Instance.CacheAll(caches);
                    }

                    log.Info("********************************************************");
                    log.Info("# 从地图配置表重新构建场景传送点");
                    log.Info("********************************************************");
                    FillSceneTransport();
                    SceneGrapAstar = new MapSceneGrapAstar(RPGServerTemplateManager.Instance.AllMapTemplates);
                }
                {
                    log.Info("********************************************************");
                    log.Info("# 战斗编解码器");
                    log.Info("********************************************************");
                    if (DataFactory.MessageCodec is MessageFactoryGenerator)
                    {
                        log.Info(DataFactory.MessageCodec);
                    }
                }
                log.Info("********************************************************");
            }
            catch
            {
                log.Error("检查战斗编辑器是否重新保存");

                throw;
            }
        }