Beispiel #1
0
 public MinecraftStoreService(ConsoleApplicationWrapper <MinecraftMessageParser> wrapper, StoreRepository storeRepository, UserRepository userRepository,
                              IServiceProvider serviceProvider, IOptions <ApplicationSettings> options)
 {
     _wrapper             = wrapper;
     _storeRepository     = storeRepository;
     _userRepository      = userRepository;
     _serviceProvider     = serviceProvider;
     _applicationSettings = options.Value;
 }
Beispiel #2
0
        public async Task CreateWorldBackup(ConsoleApplicationWrapper <MinecraftMessageParser> wrapper, string worldPath, string tempPath, bool fullCopy, bool archive = true)
        {
            if (fullCopy)
            {
                if (Directory.Exists(tempPath))
                {
                    wrapper.AddEphemeralMessage("Clearing local world backup directory...\t", null);

                    Directory.Delete(tempPath, true);
                    Directory.CreateDirectory(tempPath);

                    Console.WriteLine("Done!");
                }
                else
                {
                    Directory.CreateDirectory(tempPath);
                }


                if (Directory.Exists(worldPath))
                {
                    wrapper.AddEphemeralMessage("Attempting to create full world backup...\t", null);

                    CopyDirectory(worldPath, tempPath);

                    Console.WriteLine("Done!");
                }
                else
                {
                    wrapper.AddEphemeralMessage("Invalid world directory. Could not create full world backup!", null);
                }
            }
            else
            {
                wrapper.AddEphemeralMessage("Creating backup...", null);
                wrapper.AddEphemeralMessage("Holding world saving...", null);

                // Send tellraw message 1/2
                wrapper.SendInput("say Creating backup...", null);

                wrapper.SendInput("save hold", null);
                var holdStart = DateTime.UtcNow;

                var matchPattern = new Regex("^(" + Path.GetFileName(worldPath) + @"[\/]{1})");
                var files        = new List <string> ();

                while (files.Count == 0)
                {
                    wrapper.SendInput("save query", null);
                    await Task.Delay(2000);

                    files = wrapper.GetStdoutSinceTime(holdStart)
                            .Where(item => matchPattern.IsMatch(item))
                            .ToList();
                }

                Regex           fileListRegex = new Regex("(" + Path.GetFileName(worldPath) + @"[\/]{1}.+?)\:{1}(\d+)");
                MatchCollection matches       = fileListRegex.Matches(string.Join(Environment.NewLine, files));

                string[,] sourceFiles = new string[matches.Count, 2];

                for (int i = 0; i < matches.Count; i++)
                {
                    sourceFiles[i, 0] = matches[i].Groups[1].Value.Replace(Path.GetFileName(worldPath), "");
                    sourceFiles[i, 1] = matches[i].Groups[2].Value;
                }

                wrapper.AddEphemeralMessage($"Copying {sourceFiles.GetLength ( 0 )} files... ", null);

                // ACTUAL COPYING BEGINS HERE
                if (!Directory.Exists(Path.Combine(tempPath, "db")))
                {
                    Directory.CreateDirectory(Path.Combine(tempPath, "db"));
                }

                for (uint i = 0; i < sourceFiles.GetLength(0); i++)
                {
                    var subdir =
                        File.Exists(Path.Join(worldPath, "db", Path.GetFileName(sourceFiles[i, 0])))
                        ? $"{Path.DirectorySeparatorChar}db" : "";

                    string filePath   = Path.Join(worldPath, subdir, Path.GetFileName(sourceFiles[i, 0]));
                    string targetPath = tempPath + sourceFiles[i, 0];

                    wrapper.AddEphemeralMessage($"Copying from '{filePath}' to '{targetPath}'", null);

                    WriteFileCopy(filePath, targetPath, int.Parse(sourceFiles[i, 1]));
                }

                #region FILE INTEGRITY CHECK

                wrapper.AddEphemeralMessage("Veryfing file-integrity... ", null);

                string[] sourceDbFiles = Directory.GetFiles(worldPath + "/db/");
                string[] targetDbFiles = Directory.GetFiles(tempPath + "/db/");

                foreach (string tFile in targetDbFiles)
                {
                    bool found = false;
                    foreach (string sFile in sourceDbFiles)
                    {
                        if (Path.GetFileName(tFile) == Path.GetFileName(sFile))
                        {
                            found = true;
                            break;
                        }
                    }

                    // File isn't in the source world directory anymore, delete!
                    if (!found)
                    {
                        File.Delete(tFile);
                    }
                }

                #endregion

                Console.WriteLine("Resuming world saving...");

                wrapper.SendInput("save resume", null);
                var resumeRegex = new Regex("^Changes to the (level|world) are resumed.?");

                while (!wrapper.StandardOutput.Any(item => resumeRegex.IsMatch(item)))
                {
                    await Task.Delay(500);
                }

                string tellrawMsg = "Finished creating backup!";

                // Archive
                if (archive)
                {
                    wrapper.AddEphemeralMessage("Archiving world backup...", null);
                    if (ArchiveBackup(tempPath, _applicationSettings.ArchivePath, _applicationSettings.BackupsToKeep, _applicationSettings.WorldName, fullCopy ? "FULL" : ""))
                    {
                        wrapper.AddEphemeralMessage("Archiving done!", null);
                    }
                    else
                    {
                        wrapper.AddEphemeralMessage("Archiving failed!", null);
                        tellrawMsg = "Could not archive backup!";
                    }
                }

                // Send tellraw message 2/2
                wrapper.SendInput($"say {tellrawMsg}", null);

                wrapper.AddEphemeralMessage("Backup done!", null);
            }
        }
 public WhiteListService(ILogger <WhiteListService> logger, IOptions <ApplicationSettings> options, ConsoleApplicationWrapper <MinecraftMessageParser> wrapper)
 {
     _logger = logger;
     _applicationSettings = options.Value;
     _wrapper             = wrapper;
 }
 public BackupService(IOptions <ApplicationSettings> options, ConsoleApplicationWrapper <MinecraftMessageParser> wrapper, ScheduledTaskService scheduledTaskService)
 {
     _applicationSettings  = options.Value;
     _wrapper              = wrapper;
     _scheduledTaskService = scheduledTaskService;
 }