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));
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());

View File

@ -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();

View File

@ -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.");

View File

@ -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;
}

View File

@ -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;

View File

@ -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);
@ -297,24 +298,6 @@ namespace MCGalaxy {
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, string path) {

View File

@ -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) {
string srcBase = LevelInfo.BackupBasePath(src);
string dstBase = LevelInfo.BackupBasePath(dst);
if (!Directory.Exists(srcBase)) return;
Directory.CreateDirectory(dstBase);
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());
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);
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;
}
}
Directory.CreateDirectory(dstDir);
File.Move(srcFile, dstFile);
Directory.Delete(backups[i]);
}
Directory.Delete(srcBase);
}*/
/// <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) {
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";
}
/// <summary> Relative path of a level's backup map file </summary>
public static string BackupPath(string name, string backup) {
return ServerConfig.BackupDirectory + "/" + name + "/" + backup + "/" + name + ".lvl";
/// <summary> Relative path of a level's backup folder </summary>
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>
public static string PropertiesPath(string name) {
return "levels/level properties/" + name + ".properties";