Fix /mi showing wrong latest backup time, if a map has a custom named restore.

This commit is contained in:
UnknownShadow200 2017-10-02 12:19:04 +11:00
parent 7c502839fd
commit 1648ea7d11
9 changed files with 81 additions and 67 deletions

View File

@ -76,9 +76,11 @@ namespace MCGalaxy.Commands.Info {
DateTime createTime = File.GetCreationTimeUtc(LevelInfo.MapPath(data.Name)); DateTime createTime = File.GetCreationTimeUtc(LevelInfo.MapPath(data.Name));
TimeSpan createDelta = DateTime.UtcNow - createTime; TimeSpan createDelta = DateTime.UtcNow - createTime;
if (Directory.Exists(ServerConfig.BackupDirectory + "/" + data.Name)) { string backupPath = LevelInfo.BackupBasePath(data.Name);
int latest = Directory.GetDirectories(ServerConfig.BackupDirectory + "/" + data.Name).Length;
DateTime backupTime = File.GetCreationTimeUtc(LevelInfo.BackupPath(data.Name, latest.ToString())); 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; TimeSpan backupDelta = DateTime.UtcNow - backupTime;
Player.Message(p, " Created {2} ago, last backup ({1} ago): &a{0}", Player.Message(p, " Created {2} ago, last backup ({1} ago): &a{0}",
latest, backupDelta.Shorten(), createDelta.Shorten()); latest, backupDelta.Shorten(), createDelta.Shorten());

View File

@ -43,7 +43,7 @@ namespace MCGalaxy.Commands.Moderation {
} }
bool DoRestore(Player p, Vec3S32[] marks, object state, ExtBlock block) { 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); Level source = IMapImporter.Formats[0].Read(path, "templevel", false);
RestoreSelectionDrawOp op = new RestoreSelectionDrawOp(); RestoreSelectionDrawOp op = new RestoreSelectionDrawOp();

View File

@ -80,13 +80,13 @@ namespace MCGalaxy.Commands.World {
if (level != null) return level; if (level != null) return level;
Player.Message(p, "Loading backup failed."); Player.Message(p, "Loading backup failed.");
string backupPath = ServerConfig.BackupDirectory; string backupPath = LevelInfo.BackupBasePath(name);
if (Directory.Exists(backupPath + "/" + name)) { if (Directory.Exists(backupPath)) {
int num = Directory.GetDirectories(backupPath + "/" + name).Length; int latest = LevelInfo.LatestBackup(name);
Logger.Log(LogType.Warning, "Attempting to load latest backup of {0}, number {1} instead.", name, num); 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); level = Level.Load(name, path);
if (level == null) if (level == null)
Player.Message(p, "Loading latest backup failed as well."); Player.Message(p, "Loading latest backup failed as well.");

View File

@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.World {
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
string[] args = message.SplitSpaces(); string[] args = message.SplitSpaces();
string path = args.Length == 1 ? LevelInfo.MapPath(args[0]) : 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)) { if (!File.Exists(path)) {
Player.Message(p, "Level or backup could not be found."); return; Player.Message(p, "Level or backup could not be found."); return;
} }

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.World {
} }
if (!LevelInfo.ValidateAction(p, lvl.name, "restore a backup of this level")) return; 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 { try {
DoRestore(lvl, args[0]); DoRestore(lvl, args[0]);
} catch (Exception ex) { } catch (Exception ex) {
@ -59,7 +59,7 @@ namespace MCGalaxy.Commands.World {
static void DoRestore(Level lvl, string backup) { static void DoRestore(Level lvl, string backup) {
lock (lvl.saveLock) { 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; lvl.SaveChanges = false;
} }
@ -73,17 +73,18 @@ namespace MCGalaxy.Commands.World {
} }
static void OutputBackups(Player p) { 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; 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."); Player.Message(p, p.level.ColoredName + " %Shas &b" + dirs.Length + " %Sbackups.");
int count = 0; int count = 0;
StringBuilder custom = new StringBuilder(); StringBuilder custom = new StringBuilder();
foreach (string s in dirs) { foreach (string path in dirs) {
string name = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); string name = LevelInfo.BackupNameFrom(path);
int num; int num;
if (int.TryParse(name, out num)) continue; if (int.TryParse(name, out num)) continue;

View File

@ -274,11 +274,12 @@ namespace MCGalaxy {
public int Backup(bool Forced = false, string backupName = "") { public int Backup(bool Forced = false, string backupName = "") {
if (!backedup || Forced) { if (!backedup || Forced) {
string dir = Path.Combine(ServerConfig.BackupDirectory, name); string backupPath = LevelInfo.BackupBasePath(name);
int backupNum = NextBackup(dir); if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath);
int next = LevelInfo.LatestBackup(name) + 1;
string path = Path.Combine(dir, backupNum.ToString()); string path = Path.Combine(backupPath, next.ToString());
if (backupName.Length > 0) path = Path.Combine(dir, backupName); if (backupName.Length > 0) path = Path.Combine(backupPath, backupName);
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
string backup = Path.Combine(path, name + ".lvl"); string backup = Path.Combine(path, name + ".lvl");
@ -286,7 +287,7 @@ namespace MCGalaxy {
try { try {
File.Copy(current, backup, true); File.Copy(current, backup, true);
backedup = true; backedup = true;
return backupNum; return next;
} catch (Exception e) { } catch (Exception e) {
Logger.LogError(e); Logger.LogError(e);
Logger.Log(LogType.Warning, "FAILED TO INCREMENTAL BACKUP :" + name); Logger.Log(LogType.Warning, "FAILED TO INCREMENTAL BACKUP :" + name);
@ -297,24 +298,6 @@ namespace MCGalaxy {
return -1; 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)); } public static Level Load(string name) { return Load(name, LevelInfo.MapPath(name)); }
public static Level Load(string name, string path) { public static Level Load(string name, string path) {

View File

@ -44,8 +44,9 @@ namespace MCGalaxy {
MoveIfExists(BotsFile.BotsPath(src), MoveIfExists(BotsFile.BotsPath(src),
BotsFile.BotsPath(dst)); BotsFile.BotsPath(dst));
// TODO: Should we move backups still
try { try {
MoveBackups(src, dst); //MoveBackups(src, dst);
} catch { } catch {
} }
@ -86,29 +87,25 @@ namespace MCGalaxy {
} }
} }
static bool DirectoryEmpty(string dir) { /*static void MoveBackups(string src, string dst) {
if (!Directory.Exists(dir)) return true; string srcBase = LevelInfo.BackupBasePath(src);
if (Directory.GetDirectories(dir).Length > 0) return false; string dstBase = LevelInfo.BackupBasePath(dst);
if (Directory.GetFiles(dir).Length > 0) return false; if (!Directory.Exists(srcBase)) return;
return true; Directory.CreateDirectory(dstBase);
}
static void MoveBackups(string src, string dst) { string[] backups = Directory.GetDirectories(srcBase);
for (int i = 1; ; i++) { for (int i = 0; i < backups.Length; i++) {
string oldDir = LevelInfo.BackupPath(src, i.ToString()); string name = LevelInfo.BackupNameFrom(backups[i]);
string newDir = LevelInfo.BackupPath(dst, i.ToString()); string srcFile = LevelInfo.BackupFilePath(src, name);
string dstFile = LevelInfo.BackupFilePath(dst, name);
string dstDir = LevelInfo.BackupDirPath(dst, name);
if (File.Exists(oldDir + src + ".lvl")) { Directory.CreateDirectory(dstDir);
Directory.CreateDirectory(newDir); File.Move(srcFile, dstFile);
File.Move(oldDir + src + ".lvl", newDir + dst + ".lvl"); Directory.Delete(backups[i]);
if (DirectoryEmpty(oldDir)) Directory.Delete(oldDir);
} else {
if (DirectoryEmpty(ServerConfig.BackupDirectory + "/" + src + "/"))
Directory.Delete(ServerConfig.BackupDirectory + "/" + src + "/");
break;
}
}
} }
Directory.Delete(srcBase);
}*/
/// <summary> Deletes the .lvl (and related) files and database tables. /// <summary> Deletes the .lvl (and related) files and database tables.

View File

@ -46,7 +46,7 @@ namespace MCGalaxy {
} }
public static bool ExistsBackup(string name, string backup) { 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"; return "levels/prev/" + name.ToLower() + ".lvl.prev";
} }
/// <summary> Relative path of a level's backup map file </summary>
public static string BackupPath(string name, string backup) { /// <summary> Relative path of a level's backup folder </summary>
return ServerConfig.BackupDirectory + "/" + name + "/" + backup + "/" + name + ".lvl"; public static string BackupBasePath(string name) {
return ServerConfig.BackupDirectory + "/" + name;
} }
/// <summary> Relative path of a level's backup map directory </summary>
public static string BackupDirPath(string name, string backup) {
return BackupBasePath(name) + "/" + backup;
}
/// <summary> Relative path of a level's backup map file </summary>
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;
}
/// <summary> Relative path of a level's property file </summary> /// <summary> Relative path of a level's property file </summary>
public static string PropertiesPath(string name) { public static string PropertiesPath(string name) {
return "levels/level properties/" + name + ".properties"; return "levels/level properties/" + name + ".properties";