private static bool BackupGrid(long playerId, string path, GridBackupPlugin plugin, string gridName, long entityId, List <MyObjectBuilder_CubeGrid> objectBuilders)
        {
            try {
                string pathForPlayer = plugin.CreatePathForPlayer(path, playerId);

                string pathForGrid   = plugin.CreatePathForGrid(pathForPlayer, gridName, entityId);
                string fileName      = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".sbc";
                string fileNameDaily = DAILY_PRAEFIX + "_" + DateTime.Now.ToString("yyyy_MM_dd") + ".sbc";

                if (plugin.Config.NumberOfDailyBackupSaves > 0)
                {
                    string dailyFile = Path.Combine(pathForGrid, fileNameDaily);

                    if (!File.Exists(dailyFile))
                    {
                        GridManager.SaveGrid(dailyFile, gridName, plugin.Config.KeepOriginalOwner, plugin.Config.BackupProjections, objectBuilders);
                    }
                }

                string pathForFile = Path.Combine(pathForGrid, fileName);

                bool saved = GridManager.SaveGrid(pathForFile, gridName, plugin.Config.KeepOriginalOwner, plugin.Config.BackupProjections, objectBuilders);

                if (saved)
                {
                    CleanUpDirectory(plugin, pathForGrid);
                }

                return(saved);
            } catch (Exception e) {
                Log.Error(e, "Error on Export Grid!");
                return(false);
            }
        }
Esempio n. 2
0
        public static void FindPathToRestore(GridBackupPlugin plugin,
                                             List <DirectoryInfo> matchingGrids, string gridNameOrEntityId, int backupNumber,
                                             out string path, out bool gridFound, out bool outOfBounds)
        {
            gridFound   = false;
            outOfBounds = false;
            path        = null;

            DirectoryInfo gridDir = FindFolderName(matchingGrids, gridNameOrEntityId);

            if (gridDir == null)
            {
                return;
            }

            path      = gridDir.FullName;
            gridFound = true;

            FileInfo[] fileList = gridDir.GetFiles("*.*", SearchOption.TopDirectoryOnly);

            List <FileInfo> query = new List <FileInfo>(fileList.OrderByDescending(f => f.CreationTime));

            if (backupNumber > query.Count || backupNumber < 1)
            {
                outOfBounds = true;
                return;
            }

            FileInfo file = query[backupNumber - 1];

            path = Path.Combine(path, file.Name);
        }
Esempio n. 3
0
        public static void AddListEntriesToSb(GridBackupPlugin plugin, StringBuilder sb,
                                              long playerIdentity, int startIndex, bool outputPlayerName,
                                              out int nextIndex)
        {
            nextIndex = startIndex;

            string path = plugin.CreatePath();

            path = plugin.CreatePathForPlayer(path, playerIdentity);

            DirectoryInfo gridDir = new DirectoryInfo(path);

            DirectoryInfo[] dirList = gridDir.GetDirectories("*", SearchOption.TopDirectoryOnly);

            if (outputPlayerName && dirList.Length > 0)
            {
                var identity = PlayerUtils.GetIdentityById(playerIdentity);

                sb.AppendLine(identity.DisplayName);
            }

            foreach (var file in dirList)
            {
                string dateString = GenerateDateString(file);

                sb.AppendLine((nextIndex++) + "      " + file.Name + " - " + dateString);
            }

            if (outputPlayerName && dirList.Length > 0)
            {
                sb.AppendLine();
            }
        }
Esempio n. 4
0
        internal static void DeleteBackupsOlderThan(GridBackupPlugin plugin, int deleteBackupsOlderThanDays)
        {
            if (deleteBackupsOlderThanDays <= 0)
            {
                return;
            }

            MyAPIGateway.Parallel.StartBackground(() => {
                Log.Info("Start deleting backups older than " + deleteBackupsOlderThanDays + " days were deleted.");

                string path = plugin.CreatePath();

                DirectoryInfo dir = new DirectoryInfo(path);
                var directoryList = dir.GetDirectories("*", SearchOption.TopDirectoryOnly);

                var checkTime = DateTime.Now.AddDays(-deleteBackupsOlderThanDays);

                foreach (var playerDir in directoryList)
                {
                    var gridList = playerDir.GetDirectories("*", SearchOption.TopDirectoryOnly);

                    foreach (var gridDir in gridList)
                    {
                        try {
                            var fileList = gridDir.GetFiles("*.*", SearchOption.TopDirectoryOnly);

                            foreach (var file in fileList)
                            {
                                if (file.CreationTime < checkTime)
                                {
                                    file.Delete();
                                }
                            }

                            if (gridDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly).Length == 0)
                            {
                                gridDir.Delete(false);
                            }
                        } catch (Exception e) {
                            Log.Error(e, "Error on deleting backups older than " + deleteBackupsOlderThanDays + " days!");
                        }
                    }

                    if (playerDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly).Length == 0)
                    {
                        playerDir.Delete(false);
                    }
                }

                Log.Info("Backups older than " + deleteBackupsOlderThanDays + " days were deleted.");
            });
        }
Esempio n. 5
0
        public static List <DirectoryInfo> FindRelevantGrids(GridBackupPlugin plugin,
                                                             IEnumerable <long> playerIdentities)
        {
            List <DirectoryInfo> matchingGrids = new List <DirectoryInfo>();

            foreach (long playerIdentity in playerIdentities)
            {
                string path = plugin.CreatePath();
                path = plugin.CreatePathForPlayer(path, playerIdentity);

                DirectoryInfo   gridDir = new DirectoryInfo(path);
                DirectoryInfo[] dirList = gridDir.GetDirectories("*", SearchOption.TopDirectoryOnly);

                matchingGrids.AddRange(dirList);
            }

            return(matchingGrids);
        }
        private static void CleanUpDirectory(GridBackupPlugin plugin, string pathForGrid)
        {
            DirectoryInfo dir = new DirectoryInfo(pathForGrid);

            FileInfo[] fileList = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);

            var query = fileList.OrderByDescending(file => file.CreationTime);
            int numberOfFilesToKeep      = plugin.Config.NumberOfBackupSaves;
            int numberOfDailyFilesToKeep = plugin.Config.NumberOfDailyBackupSaves;

            List <FileInfo> dailyFiles = new List <FileInfo>();

            int i = 0;

            foreach (var file in query)
            {
                if (file.Name.StartsWith(DAILY_PRAEFIX))
                {
                    dailyFiles.Add(file);
                    continue;
                }

                if (i++ >= numberOfFilesToKeep)
                {
                    file.Delete();
                }
            }

            i = 0;
            foreach (var file in dailyFiles)
            {
                if (i++ >= numberOfDailyFilesToKeep)
                {
                    file.Delete();
                }
            }
        }
 public BackupQueue(GridBackupPlugin Plugin)
 {
     this.Plugin = Plugin;
 }
        public static bool BackupSingleGridStatic(long playerId, List <MyObjectBuilder_CubeGrid> grids, string path, GridBackupPlugin plugin)
        {
            MyObjectBuilder_CubeGrid biggestGrid = null;
            int  blockOnBiggestGrid = 0;
            long blockCount         = 0;

            foreach (var grid in grids)
            {
                int blocksOnGrid = grid.CubeBlocks != null ? grid.CubeBlocks.Count : 0;

                blockCount += blocksOnGrid;

                if (biggestGrid == null || blocksOnGrid > blockOnBiggestGrid)
                {
                    biggestGrid        = grid;
                    blockOnBiggestGrid = blocksOnGrid;
                }
            }

            if (biggestGrid == null)
            {
                Log.Warn("Could not find biggest grid in list for manual backups!");
                return(false);
            }

            /* To little blocks... ignore */
            if (blockCount < plugin.Config.MinBlocksForBackup)
            {
                return(true);
            }

            long   entityId    = biggestGrid.EntityId;
            string displayName = biggestGrid.DisplayName;

            return(BackupGrid(playerId, path, plugin, displayName, entityId, grids));
        }
        public static bool BackupSingleGridStatic(long playerId, List <MyCubeGrid> grids,
                                                  string path, HashSet <long> alreadyExportedGrids, GridBackupPlugin plugin, bool background = true)
        {
            MyCubeGrid biggestGrid = null;

            long blockCount = 0;

            foreach (var grid in grids)
            {
                long count = grid.BlocksCount;

                blockCount += count;

                if (biggestGrid == null || biggestGrid.BlocksCount < count)
                {
                    biggestGrid = grid;
                }
            }

            long entityId = biggestGrid.EntityId;

            if (alreadyExportedGrids != null)
            {
                if (alreadyExportedGrids.Contains(entityId))
                {
                    return(false);
                }

                alreadyExportedGrids.Add(entityId);
            }

            /* To little blocks... ignore */
            if (blockCount < plugin.Config.MinBlocksForBackup)
            {
                return(true);
            }

            List <MyObjectBuilder_CubeGrid> objectBuilders = new List <MyObjectBuilder_CubeGrid>();

            foreach (MyCubeGrid grid in grids)
            {
                /* What else should it be? LOL? */
                if (!(grid.GetObjectBuilder() is MyObjectBuilder_CubeGrid objectBuilder))
                {
                    throw new ArgumentException(grid + " has a ObjectBuilder thats not for a CubeGrid");
                }

                objectBuilders.Add(objectBuilder);
            }

            if (background)
            {
                MyAPIGateway.Parallel.StartBackground(() => {
                    BackupGrid(playerId, path, plugin, biggestGrid.DisplayName, entityId, objectBuilders);
                });
            }
            else
            {
                return(BackupGrid(playerId, path, plugin, biggestGrid.DisplayName, entityId, objectBuilders));
            }

            return(true);
        }
 public BackupControl(GridBackupPlugin plugin) : this()
 {
     Plugin      = plugin;
     DataContext = plugin.Config;
 }