diff --git a/MCGalaxy/Commands/Information/CmdBlocks.cs b/MCGalaxy/Commands/Information/CmdBlocks.cs index 266a4e679..7edea26f6 100644 --- a/MCGalaxy/Commands/Information/CmdBlocks.cs +++ b/MCGalaxy/Commands/Information/CmdBlocks.cs @@ -84,7 +84,7 @@ namespace MCGalaxy.Commands.Info { internal static string FormatBlockName(string block) { - BlockPerms perms = BlockPerms.List[Block.Byte(block)]; + BlockPerms perms = BlockPerms.List[Block.Byte(block)]; return Group.GetColor(perms.MinRank) + block; } diff --git a/MCGalaxy/Commands/Information/CmdMapInfo.cs b/MCGalaxy/Commands/Information/CmdMapInfo.cs index 6490f802b..0c2c4a5a4 100644 --- a/MCGalaxy/Commands/Information/CmdMapInfo.cs +++ b/MCGalaxy/Commands/Information/CmdMapInfo.cs @@ -76,9 +76,11 @@ namespace MCGalaxy.Commands.Info { DateTime createTime = File.GetCreationTimeUtc(LevelInfo.MapPath(data.Name)); TimeSpan createDelta = DateTime.UtcNow - createTime; - if (Directory.Exists(ServerConfig.BackupDirectory + "/" + data.Name)) { - int latest = Directory.GetDirectories(ServerConfig.BackupDirectory + "/" + data.Name).Length; - DateTime backupTime = File.GetCreationTimeUtc(LevelInfo.BackupPath(data.Name, latest.ToString())); + string backupPath = LevelInfo.BackupBasePath(data.Name); + + if (Directory.Exists(backupPath)) { + int latest = LevelInfo.LatestBackup(data.Name); + DateTime backupTime = File.GetCreationTimeUtc(LevelInfo.BackupFilePath(data.Name, latest.ToString())); TimeSpan backupDelta = DateTime.UtcNow - backupTime; Player.Message(p, " Created {2} ago, last backup ({1} ago): &a{0}", latest, backupDelta.Shorten(), createDelta.Shorten()); diff --git a/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs b/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs index 358c4e634..43dc2fa87 100644 --- a/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs +++ b/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs @@ -43,7 +43,7 @@ namespace MCGalaxy.Commands.Moderation { } bool DoRestore(Player p, Vec3S32[] marks, object state, ExtBlock block) { - string path = LevelInfo.BackupPath(p.level.name, (string)state); + string path = LevelInfo.BackupFilePath(p.level.name, (string)state); Level source = IMapImporter.Formats[0].Read(path, "templevel", false); RestoreSelectionDrawOp op = new RestoreSelectionDrawOp(); diff --git a/MCGalaxy/Commands/World/CmdLoad.cs b/MCGalaxy/Commands/World/CmdLoad.cs index e05ee777a..ea2da54f2 100644 --- a/MCGalaxy/Commands/World/CmdLoad.cs +++ b/MCGalaxy/Commands/World/CmdLoad.cs @@ -80,13 +80,13 @@ namespace MCGalaxy.Commands.World { if (level != null) return level; Player.Message(p, "Loading backup failed."); - string backupPath = ServerConfig.BackupDirectory; + string backupPath = LevelInfo.BackupBasePath(name); - if (Directory.Exists(backupPath + "/" + name)) { - int num = Directory.GetDirectories(backupPath + "/" + name).Length; - Logger.Log(LogType.Warning, "Attempting to load latest backup of {0}, number {1} instead.", name, num); + if (Directory.Exists(backupPath)) { + int latest = LevelInfo.LatestBackup(name); + Logger.Log(LogType.Warning, "Attempting to load latest backup of {0}, number {1} instead.", name, latest); - string path = LevelInfo.BackupPath(name, num.ToString()); + string path = LevelInfo.BackupFilePath(name, latest.ToString()); level = Level.Load(name, path); if (level == null) Player.Message(p, "Loading latest backup failed as well."); diff --git a/MCGalaxy/Commands/World/CmdMuseum.cs b/MCGalaxy/Commands/World/CmdMuseum.cs index 466c5d8a9..c6af4d20a 100644 --- a/MCGalaxy/Commands/World/CmdMuseum.cs +++ b/MCGalaxy/Commands/World/CmdMuseum.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.World { public override void Use(Player p, string message) { string[] args = message.SplitSpaces(); string path = args.Length == 1 ? LevelInfo.MapPath(args[0]) : - LevelInfo.BackupPath(args[0], args[1]); + LevelInfo.BackupFilePath(args[0], args[1]); if (!File.Exists(path)) { Player.Message(p, "Level or backup could not be found."); return; } diff --git a/MCGalaxy/Commands/World/CmdRestore.cs b/MCGalaxy/Commands/World/CmdRestore.cs index 2afba54bb..7fc43bf9d 100644 --- a/MCGalaxy/Commands/World/CmdRestore.cs +++ b/MCGalaxy/Commands/World/CmdRestore.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.World { } if (!LevelInfo.ValidateAction(p, lvl.name, "restore a backup of this level")) return; - if (File.Exists(LevelInfo.BackupPath(lvl.name, args[0]))) { + if (File.Exists(LevelInfo.BackupFilePath(lvl.name, args[0]))) { try { DoRestore(lvl, args[0]); } catch (Exception ex) { @@ -59,7 +59,7 @@ namespace MCGalaxy.Commands.World { static void DoRestore(Level lvl, string backup) { lock (lvl.saveLock) { - File.Copy(LevelInfo.BackupPath(lvl.name, backup), LevelInfo.MapPath(lvl.name), true); + File.Copy(LevelInfo.BackupFilePath(lvl.name, backup), LevelInfo.MapPath(lvl.name), true); lvl.SaveChanges = false; } @@ -73,17 +73,18 @@ namespace MCGalaxy.Commands.World { } static void OutputBackups(Player p) { - if (!Directory.Exists(ServerConfig.BackupDirectory + "/" + p.level.name)) { + string backupPath = LevelInfo.BackupBasePath(p.level.name); + if (!Directory.Exists(backupPath)) { Player.Message(p, p.level.ColoredName + " %Shas no backups yet."); return; } - string[] dirs = Directory.GetDirectories(ServerConfig.BackupDirectory + "/" + p.level.name); + string[] dirs = Directory.GetDirectories(backupPath); Player.Message(p, p.level.ColoredName + " %Shas &b" + dirs.Length + " %Sbackups."); int count = 0; StringBuilder custom = new StringBuilder(); - foreach (string s in dirs) { - string name = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); + foreach (string path in dirs) { + string name = LevelInfo.BackupNameFrom(path); int num; if (int.TryParse(name, out num)) continue; diff --git a/MCGalaxy/Levels/Level.cs b/MCGalaxy/Levels/Level.cs index 1227d35c2..464836386 100644 --- a/MCGalaxy/Levels/Level.cs +++ b/MCGalaxy/Levels/Level.cs @@ -274,11 +274,12 @@ namespace MCGalaxy { public int Backup(bool Forced = false, string backupName = "") { if (!backedup || Forced) { - string dir = Path.Combine(ServerConfig.BackupDirectory, name); - int backupNum = NextBackup(dir); + string backupPath = LevelInfo.BackupBasePath(name); + if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath); + int next = LevelInfo.LatestBackup(name) + 1; - string path = Path.Combine(dir, backupNum.ToString()); - if (backupName.Length > 0) path = Path.Combine(dir, backupName); + string path = Path.Combine(backupPath, next.ToString()); + if (backupName.Length > 0) path = Path.Combine(backupPath, backupName); Directory.CreateDirectory(path); string backup = Path.Combine(path, name + ".lvl"); @@ -286,7 +287,7 @@ namespace MCGalaxy { try { File.Copy(current, backup, true); backedup = true; - return backupNum; + return next; } catch (Exception e) { Logger.LogError(e); Logger.Log(LogType.Warning, "FAILED TO INCREMENTAL BACKUP :" + name); @@ -296,24 +297,6 @@ namespace MCGalaxy { Logger.Log(LogType.SystemActivity, "Level unchanged, skipping backup"); return -1; } - - int NextBackup(string dir) { - if (Directory.Exists(dir)) { - int max = 0; - string[] backups = Directory.GetDirectories(dir); - foreach (string s in backups) { - string name = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); - int num; - - if (!int.TryParse(name, out num)) continue; - max = Math.Max(num, max); - } - return max + 1; - } else { - Directory.CreateDirectory(dir); - return 1; - } - } public static Level Load(string name) { return Load(name, LevelInfo.MapPath(name)); } diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index aaa336b2a..b21400bfa 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -44,8 +44,9 @@ namespace MCGalaxy { MoveIfExists(BotsFile.BotsPath(src), BotsFile.BotsPath(dst)); + // TODO: Should we move backups still try { - MoveBackups(src, dst); + //MoveBackups(src, dst); } catch { } @@ -86,29 +87,25 @@ namespace MCGalaxy { } } - static bool DirectoryEmpty(string dir) { - if (!Directory.Exists(dir)) return true; - if (Directory.GetDirectories(dir).Length > 0) return false; - if (Directory.GetFiles(dir).Length > 0) return false; - return true; - } - - static void MoveBackups(string src, string dst) { - for (int i = 1; ; i++) { - string oldDir = LevelInfo.BackupPath(src, i.ToString()); - string newDir = LevelInfo.BackupPath(dst, i.ToString()); - - if (File.Exists(oldDir + src + ".lvl")) { - Directory.CreateDirectory(newDir); - File.Move(oldDir + src + ".lvl", newDir + dst + ".lvl"); - if (DirectoryEmpty(oldDir)) Directory.Delete(oldDir); - } else { - if (DirectoryEmpty(ServerConfig.BackupDirectory + "/" + src + "/")) - Directory.Delete(ServerConfig.BackupDirectory + "/" + src + "/"); - break; - } + /*static void MoveBackups(string src, string dst) { + string srcBase = LevelInfo.BackupBasePath(src); + string dstBase = LevelInfo.BackupBasePath(dst); + if (!Directory.Exists(srcBase)) return; + Directory.CreateDirectory(dstBase); + + string[] backups = Directory.GetDirectories(srcBase); + for (int i = 0; i < backups.Length; i++) { + string name = LevelInfo.BackupNameFrom(backups[i]); + string srcFile = LevelInfo.BackupFilePath(src, name); + string dstFile = LevelInfo.BackupFilePath(dst, name); + string dstDir = LevelInfo.BackupDirPath(dst, name); + + Directory.CreateDirectory(dstDir); + File.Move(srcFile, dstFile); + Directory.Delete(backups[i]); } - } + Directory.Delete(srcBase); + }*/ /// Deletes the .lvl (and related) files and database tables. diff --git a/MCGalaxy/Levels/LevelInfo.cs b/MCGalaxy/Levels/LevelInfo.cs index 6789cf289..06be8e346 100644 --- a/MCGalaxy/Levels/LevelInfo.cs +++ b/MCGalaxy/Levels/LevelInfo.cs @@ -46,7 +46,7 @@ namespace MCGalaxy { } public static bool ExistsBackup(string name, string backup) { - return File.Exists(BackupPath(name, backup)); + return File.Exists(BackupFilePath(name, backup)); } @@ -65,11 +65,42 @@ namespace MCGalaxy { return "levels/prev/" + name.ToLower() + ".lvl.prev"; } - /// Relative path of a level's backup map file - public static string BackupPath(string name, string backup) { - return ServerConfig.BackupDirectory + "/" + name + "/" + backup + "/" + name + ".lvl"; + + /// Relative path of a level's backup folder + public static string BackupBasePath(string name) { + return ServerConfig.BackupDirectory + "/" + name; } + /// Relative path of a level's backup map directory + public static string BackupDirPath(string name, string backup) { + return BackupBasePath(name) + "/" + backup; + } + + /// Relative path of a level's backup map file + public static string BackupFilePath(string name, string backup) { + return BackupDirPath(name, backup) + "/" + name + ".lvl"; + } + + public static string BackupNameFrom(string path) { + return path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1); + } + + public static int LatestBackup(string name) { + string dir = BackupBasePath(name); + string[] backups = Directory.GetDirectories(dir); + int latest = 0; + + foreach (string path in backups) { + string backupName = BackupNameFrom(path); + int num; + + if (!int.TryParse(backupName, out num)) continue; + latest = Math.Max(num, latest); + } + return latest; + } + + /// Relative path of a level's property file public static string PropertiesPath(string name) { return "levels/level properties/" + name + ".properties";