예제 #1
0
        public static void init(MCCSAPI api)
        {
            Mapi             = api;
            TimerTick        = new System.Timers.Timer();
            RunPath          = FileSys.GetPath();
            BakcupPath       = RunPath + BakcupPath;        //备份后存档存放的文件夹的绝对路径
            Profile.HomeDire = BakcupPath;
            GetGameMap();
            CheckDeploy();                                  //检查配置文件是否存在,不存在则打开窗口进行配置

            //TimerTick.Interval = 1000 * 1 * 10;           //1H执行一次 第一次执行就在这个时间后 单位ms
            TimerTick.Elapsed += new System.Timers.ElapsedEventHandler(OnTick);
            //GetGameMap();
            //监听事件:玩家加入世界
            api.addAfterActListener("onLoadName", e =>
            {
                HavePlayer = true;
                return(true);
            });

            //监听事件:后台指令输出信息
            api.addBeforeActListener("onServerCmdOutput", e =>
            {
                var ex        = (ServerCmdOutputEvent)BaseEvent.getFrom(e);
                string output = ex.output;
                String t_tmp  = DateTime.Now.Ticks.ToString();
                //
                if (PrepareBackup)
                {
                    if (output.IndexOf("Data saved. Files are now ready to be copied") != -1 || output.IndexOf("数据已保存。文件现已可供复制") != -1)
                    {
                        Console.WriteLine("已获取备份文件列表,准备备份");
                        new Thread(() =>
                        {
                            string savepath = string.Format("{0}\\{1}\\", Profile.HomeDire, DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"));
                            //备份
                            int count = BackupDB(output.Split(new char[] { '\n' })[1], Profile.Zip? Temp + @"\" + t_tmp + @"\" : savepath);
                            if (Profile.Zip)
                            {
                                try
                                {
                                    Directory.CreateDirectory(savepath);
                                    ZipFile.CreateFromDirectory(Temp + @"\" + t_tmp + @"\" + MapDirName, savepath + MapDirName + ".zip");
                                }
                                catch (Exception error)
                                {
                                    Console.WriteLine("[BackupMap Error] 执行压缩失败,将备份的文件夹复制到备份目录,错误:{0}", error);
                                    Folder.Copy(Temp + @"\" + t_tmp + @"\" + MapDirName, savepath + @"\");
                                }
                                Directory.Delete(Temp + @"\" + t_tmp, true);
                            }
                            if (count != 0)
                            {
                                if (count == -1)
                                {
                                    Console.WriteLine("因备份失败而终止");
                                    PrepareBackup = false;
                                    SeedCMD(SaveResume);
                                    return;
                                }
                                Console.WriteLine("备份结束有{0}个文件备份失败", count);
                            }
                            else
                            {
                                Console.WriteLine("存档全部复制成功");
                            }


                            //备份之后执行的操作
                            if (Profile.run != null && Profile.run != "0" && File.Exists(Profile.run))
                            {
                                bool hasspace = (savepath + MapDirName).IndexOf(" ") != -1;

                                if (Profile.Zip)
                                {
                                    Process.Start(Profile.run, String.Format("{0} {1}", "zip", hasspace? "\"" + savepath + MapDirName + ".zip\"" : savepath + MapDirName + ".zip"));
                                }
                                else
                                {
                                    Process.Start(Profile.run, String.Format("{0} {1}", "dir", hasspace?"\"" + savepath + MapDirName + "\"" : savepath + MapDirName));
                                }
                            }
                            //Thread.Sleep(1000 * 2);
                            SeedCMD(SaveResume);
                            PrepareBackup = false;
                            //告诉插件 存档存储已恢复 可以再次备份
                        }).Start();

                        return(false);                                       //禁止在控制面板上显示这个回显
                    }
                    else
                    {
                        // TODO:当出现还没有准备完成的时候执行
                    }
                }

                return(true);
            });

            api.addBeforeActListener("onServerCmd", e =>
            {
                var ex = (ServerCmdEvent)BaseEvent.getFrom(e);
                if (ex.cmd.ToLower() == "backupmap")
                {
                    if (PrepareBackup)
                    {
                        Console.WriteLine("上次备份尚未结束");
                    }
                    else
                    {
                        StartBackup();  //手动备份
                    }
                    return(false);
                }
                return(true);
            });
        }