Skip to content

Commit

Permalink
Keep all .lvl.prev files in a separate levels/prev directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jun 9, 2016
1 parent 27eae5f commit be59b95
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Levels/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,17 @@ public void Save(bool Override = false, bool clearPhysics = false)
{
if (!Directory.Exists("levels")) Directory.CreateDirectory("levels");
if (!Directory.Exists("levels/level properties")) Directory.CreateDirectory("levels/level properties");

if (changed || !File.Exists(path) || Override || (physicschanged && clearPhysics))
{
if (!Directory.Exists("levels/prev")) Directory.CreateDirectory("levels/prev");

if (changed || !File.Exists(path) || Override || (physicschanged && clearPhysics)) {
if (clearPhysics)
ClearPhysics();

if (File.Exists(path)) {
if (File.Exists(path + ".prev"))
File.Delete(path + ".prev");
File.Copy(path, path + ".prev");
string prevPath = LevelInfo.PrevPath(name);
if (File.Exists(prevPath))
File.Delete(prevPath);
File.Copy(path, prevPath, true);
File.Delete(path);
}
LvlFile.Save(this, path + ".backup");
Expand All @@ -560,9 +561,7 @@ public void Save(bool Override = false, bool clearPhysics = false)
Server.s.Log(string.Format("SAVED: Level \"{0}\". ({1}/{2}/{3})", name, players.Count,
PlayerInfo.Online.Count, Server.players));
changed = false;
}
else
{
} else {
Server.s.Log("Skipping level save for " + name + ".");
}
} catch (Exception e) {
Expand Down
4 changes: 4 additions & 0 deletions Levels/LevelInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static string LevelPath(string name) {
return "levels/" + name.ToLower() + ".lvl";
}

public static string PrevPath(string name) {
return "levels/prev/" + name.ToLower() + ".lvl.prev";
}

public static string BackupPath(string name, string backup) {
return Server.backupLocation + "/" + name + "/" + backup + "/" + name + ".lvl";
}
Expand Down
24 changes: 24 additions & 0 deletions Server/Server.Tasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ static void AutoLoadLineProcessor(string key, string value) {
}
}
}

void MovePreviousLevelFiles() {
if (!Directory.Exists("levels")) return;
try {
string[] files = Directory.GetFiles("levels", "*.prev");
if (files.Length == 0) return;
if (!Directory.Exists("levels/prev"))
Directory.CreateDirectory("levels/prev");

foreach (string file in files) {
string name = Path.GetFileName(file);
string newFile = "levels/prev/" + name;

try {
File.Move(file, newFile);
} catch (Exception ex) {
Server.s.Log("Error while trying to move .lvl.prev file");
Server.ErrorLog(ex);
}
}
} catch (Exception ex) {
Server.ErrorLog(ex);
}
}

void SetupSocket() {
Log("Creating listening socket on port " + port + "... ");
Expand Down
1 change: 1 addition & 0 deletions Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ public void Start() {
Plugin.Load();
ml.Queue(LoadPlayerLists);
ml.Queue(LoadAutoloadCommands);
ml.Queue(MovePreviousLevelFiles);
ml.Queue(SetupSocket);

ml.Queue(InitTimers);
Expand Down

0 comments on commit be59b95

Please sign in to comment.