Skip to content

Commit

Permalink
Backups also save associated file with map (bots/blockdefs/env)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 24, 2018
1 parent 42bcb10 commit 50c2140
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdMapInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void FromOfflineLevel(string name) {
Width = dims.X; Height = dims.Y; Length = dims.Z;
BlockDBEntries = BlockDBFile.CountEntries(name);

path = LevelInfo.PropertiesPath(name);
path = LevelInfo.PropsPath(name);
LevelConfig cfg = new LevelConfig();
cfg.Reset(Height);
LevelConfig.Load(path, cfg);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdWorlds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void RetrieveProps(string level, out LevelPermission visit,
build = LevelPermission.Guest;
loadOnGoto = true;

string propsPath = LevelInfo.PropertiesPath(level);
string propsPath = LevelInfo.PropsPath(level);
SearchArgs args = new SearchArgs();
if (!PropertiesFile.Read(propsPath, ref args, ProcessLine)) return;

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Levels/AccessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void DoChange(Player p, Level lvl, string msg) {
}

void Update(Level lvl) {
cfg.Save(LevelInfo.PropertiesPath(lvlName));
cfg.Save(LevelInfo.PropsPath(lvlName));
if (lvl == null) return;
if (IsVisit && lvl == Server.mainLevel) return;
Player[] players = PlayerInfo.Online.Items;
Expand Down
21 changes: 6 additions & 15 deletions MCGalaxy/Levels/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public bool InBound(ushort x, ushort y, ushort z) {

public static void SaveSettings(Level lvl) {
if (lvl.IsMuseum) return; // museums do not save properties
string path = LevelInfo.PropertiesPath(lvl.MapName);
string path = LevelInfo.PropsPath(lvl.MapName);
lvl.Config.Save(path);
}

Expand Down Expand Up @@ -278,24 +278,15 @@ void SaveCore(string path) {
public int Backup(bool Forced = false, string backupName = "") {
if (!backedup || Forced) {
string backupPath = LevelInfo.BackupBasePath(name);
if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath);
if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath);
int next = LevelInfo.LatestBackup(name) + 1;
if (backupName.Length == 0) backupName = next.ToString();

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");
string current = LevelInfo.MapPath(name);
try {
File.Copy(current, backup, true);
backedup = true;
return next;
} catch (Exception e) {
Logger.LogError(e);
if (!LevelActions.Backup(name, backupName)) {
Logger.Log(LogType.Warning, "FAILED TO INCREMENTAL BACKUP :" + name);
return -1;
}
return next;
}
Logger.Log(LogType.SystemActivity, "Level unchanged, skipping backup");
return -1;
Expand Down Expand Up @@ -343,7 +334,7 @@ public static Level Load(string name, string path) {

public static void LoadMetadata(Level lvl) {
try {
string propsPath = LevelInfo.PropertiesPath(lvl.MapName);
string propsPath = LevelInfo.PropsPath(lvl.MapName);
bool propsExisted = LevelConfig.Load(propsPath, lvl.Config);

if (propsExisted) {
Expand Down
83 changes: 53 additions & 30 deletions MCGalaxy/Levels/LevelActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ permissions and limitations under the Licenses.
*/
using System;
using System.IO;
using MCGalaxy.Blocks;
using MCGalaxy.Bots;
using MCGalaxy.DB;
using MCGalaxy.SQL;
Expand All @@ -26,6 +27,26 @@ namespace MCGalaxy {

public static class LevelActions {

static string BlockPropsLvlPath(string map) { return BlockProps.PropsPath("_" + map); }
static string BlockPropsOldPath(string map) { return BlockProps.PropsPath("lvl" + map); }
static string BlockDefsPath(string map) { return "blockdefs/lvl_" + map + ".json"; }

public static bool Backup(string map, string backupName) {
string basePath = LevelInfo.BackupBasePath(map);
if (!Directory.Exists(basePath)) Directory.CreateDirectory(basePath);
string path = Path.Combine(basePath, backupName);
Directory.CreateDirectory(path);

bool lvl = DoAction(LevelInfo.MapPath(map), Path.Combine(path, map + ".lvl"), action_copy);
bool props = DoAction(LevelInfo.PropsPath(map), Path.Combine(path, "map.properties"), action_copy);
bool defs = DoAction(BlockDefsPath(map), Path.Combine(path, "blockdefs.json"), action_copy);
bool blkOld = DoAction(BlockPropsOldPath(map), Path.Combine(path, "blockprops.txt"), action_copy);
bool blkCur = DoAction(BlockPropsLvlPath(map), Path.Combine(path, "blockprops.txt"), action_copy);
bool bots = DoAction(BotsFile.BotsPath(map), Path.Combine(path, "bots.json"), action_copy);

return lvl && props && defs && blkOld && blkCur && bots;
}

/// <summary> Renames the .lvl (and related) files and database tables. Does not unload. </summary>
public static void Rename(string src, string dst) {
File.Move(LevelInfo.MapPath(src), LevelInfo.MapPath(dst));
Expand Down Expand Up @@ -88,42 +109,42 @@ static void RenameDatabaseTables(string src, string dst) {

public const string DeleteFailedMessage = "Unable to delete the level, because it could not be unloaded. A game may currently be running on it.";
/// <summary> Deletes the .lvl (and related) files and database tables. Unloads level if it is loaded. </summary>
public static bool Delete(string name) {
Level lvl = LevelInfo.FindExact(name);
public static bool Delete(string map) {
Level lvl = LevelInfo.FindExact(map);
if (lvl != null && !lvl.Unload()) return false;

if (!Directory.Exists("levels/deleted"))
Directory.CreateDirectory("levels/deleted");

if (File.Exists(LevelInfo.DeletedPath(name))) {
if (File.Exists(LevelInfo.DeletedPath(map))) {
int num = 0;
while (File.Exists(LevelInfo.DeletedPath(name + num))) num++;
while (File.Exists(LevelInfo.DeletedPath(map + num))) num++;

File.Move(LevelInfo.MapPath(name), LevelInfo.DeletedPath(name + num));
File.Move(LevelInfo.MapPath(map), LevelInfo.DeletedPath(map + num));
} else {
File.Move(LevelInfo.MapPath(name), LevelInfo.DeletedPath(name));
File.Move(LevelInfo.MapPath(map), LevelInfo.DeletedPath(map));
}

DoAll(name, "", action_delete);
DeleteDatabaseTables(name);
BlockDBFile.DeleteBackingFile(name);
DoAll(map, "", action_delete);
DeleteDatabaseTables(map);
BlockDBFile.DeleteBackingFile(map);
return true;
}

static void DeleteDatabaseTables(string name) {
if (Database.Backend.TableExists("Block" + name))
Database.Backend.DeleteTable("Block" + name);
static void DeleteDatabaseTables(string map) {
if (Database.Backend.TableExists("Block" + map))
Database.Backend.DeleteTable("Block" + map);

object locker = ThreadSafeCache.DBCache.GetLocker(name);
object locker = ThreadSafeCache.DBCache.GetLocker(map);
lock (locker) {
if (Database.TableExists("Portals" + name)) {
Database.Backend.DeleteTable("Portals" + name);
if (Database.TableExists("Portals" + map)) {
Database.Backend.DeleteTable("Portals" + map);
}
if (Database.TableExists("Messages" + name)) {
Database.Backend.DeleteTable("Messages" + name);
if (Database.TableExists("Messages" + map)) {
Database.Backend.DeleteTable("Messages" + map);
}
if (Database.TableExists("Zone" + name)) {
Database.Backend.DeleteTable("Zone" + name);
if (Database.TableExists("Zone" + map)) {
Database.Backend.DeleteTable("Zone" + map);
}
}
}
Expand Down Expand Up @@ -203,22 +224,22 @@ public static void ReloadMap(Player p, Player who, bool showMessage) {
static void DoAll(string src, string dst, byte action) {
DoAction(LevelInfo.MapPath(src) + ".backup",
LevelInfo.MapPath(dst) + ".backup", action);
DoAction("levels/level properties/" + src + ".properties",
"levels/level properties/" + dst + ".properties", action);
DoAction(LevelInfo.PropsPath(src),
LevelInfo.PropsPath(dst), action);
DoAction("levels/level properties/" + src,
"levels/level properties/" + dst + ".properties", action);
DoAction("blockdefs/lvl_" + src + ".json",
"blockdefs/lvl_" + dst + ".json", action);
DoAction("blockprops/lvl_" + src + ".txt",
"blockprops/lvl_" + dst + ".txt", action);
DoAction("blockprops/_" + src + ".txt", "" +
"blockprops/_" + dst + ".txt", action);
LevelInfo.PropsPath(dst), action);
DoAction(BlockDefsPath(src),
BlockDefsPath(dst), action);
DoAction(BlockPropsOldPath(src),
BlockPropsOldPath(dst), action);
DoAction(BlockPropsLvlPath(src),
BlockPropsLvlPath(dst), action);
DoAction(BotsFile.BotsPath(src),
BotsFile.BotsPath(dst), action);
}

static void DoAction(string src, string dst, byte action) {
if (!File.Exists(src)) return;
static bool DoAction(string src, string dst, byte action) {
if (!File.Exists(src)) return true;
try {
if (action == action_delete) {
File.Delete(src);
Expand All @@ -227,8 +248,10 @@ static void DoAction(string src, string dst, byte action) {
} else if (action == action_copy) {
File.Copy(src, dst, true);
}
return true;
} catch (Exception ex) {
Logger.LogError(ex);
return false;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Levels/LevelInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ public static int LatestBackup(string name) {


/// <summary> Relative path of a level's property file </summary>
public static string PropertiesPath(string name) {
public static string PropsPath(string name) {
return "levels/level properties/" + name + ".properties";
}

public static LevelConfig GetConfig(string map, out Level lvl) {
lvl = FindExact(map);
if (lvl != null) return lvl.Config;

string propsPath = PropertiesPath(map);
string propsPath = PropsPath(map);
LevelConfig cfg = new LevelConfig();
cfg.Reset(0);
LevelConfig.Load(propsPath, cfg);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Player/PlayerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static bool GotoMap(Player p, string name) {
}

static bool LoadOfflineLevel(Player p, string name) {
string propsPath = LevelInfo.PropertiesPath(name);
string propsPath = LevelInfo.PropsPath(name);
LevelConfig cfg = new LevelConfig();
LevelConfig.Load(propsPath, cfg);

Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Server/Tasks/UpgradeTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal static void UpgradeOldBlacklist(SchedulerTask task) {

if (names.Count > 0) {
string lvlName = Path.GetFileNameWithoutExtension(files[i]);
string propsPath = LevelInfo.PropertiesPath(lvlName);
string propsPath = LevelInfo.PropsPath(lvlName);
using (StreamWriter w = new StreamWriter(propsPath, true)) {
w.WriteLine("VisitBlacklist = " + names.Join());
}
Expand Down Expand Up @@ -116,7 +116,7 @@ internal static void CombineEnvFiles(SchedulerTask task) {

static void Combine(string envFile) {
string name = Path.GetFileNameWithoutExtension(envFile);
string propsPath = LevelInfo.PropertiesPath(name);
string propsPath = LevelInfo.PropsPath(name);

List<string> lines = new List<string>();
if (File.Exists(propsPath)) {
Expand Down

0 comments on commit 50c2140

Please sign in to comment.