From 4dcca3ff0a71f770542320654793936d74fe625d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 20 Jan 2020 07:49:51 +1100 Subject: [PATCH] Fix copying a level not saving to disc first. You used to potentially lose a few recent changes in the copied map if the source level hadn't been saved for a while --- MCGalaxy/Commands/World/CmdCopyLVL.cs | 3 +-- MCGalaxy/Levels/LevelActions.cs | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/MCGalaxy/Commands/World/CmdCopyLVL.cs b/MCGalaxy/Commands/World/CmdCopyLVL.cs index 6678a0b0b..1dca13c2d 100644 --- a/MCGalaxy/Commands/World/CmdCopyLVL.cs +++ b/MCGalaxy/Commands/World/CmdCopyLVL.cs @@ -44,10 +44,9 @@ namespace MCGalaxy.Commands.World { string dst = args[1]; if (!Formatter.ValidMapName(p, dst)) return; - if (LevelInfo.MapExists(dst)) { p.Message("Level \"" + dst + "\" already exists."); return; } try { - LevelActions.CopyLevel(src, dst); + if (!LevelActions.CopyLevel(p, src, dst)) return; } catch (IOException) { p.Message("Level %W" + dst + " %Salready exists!"); return; } diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index 8cf2eb1ca..45f77ab51 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -173,18 +173,31 @@ namespace MCGalaxy { Server.mainLevel = lvl; } - public static void CopyLevel(string src, string dst) { + /// Copies a map and related metadata. + /// Backups and BlockDB are NOT copied. + public static bool CopyLevel(Player p, string src, string dst) { + if (LevelInfo.MapExists(dst)) { + p.Message("%WLevel \"{0}\" already exists.", dst); return false; + } + + // Make sure any changes to live map are saved first + Level lvl = LevelInfo.FindExact(src); + if (lvl != null && !lvl.Save(true)) { + p.Message("%WUnable to save {0}! Some recent block changes may not be copied.", src); + } + File.Copy(LevelInfo.MapPath(src), LevelInfo.MapPath(dst)); DoAll(src, dst, action_copy); CopyDatabaseTables(src, dst); + return true; } static void CopyDatabaseTables(string src, string dst) { object srcLocker = ThreadSafeCache.DBCache.GetLocker(src); - object dstLockder = ThreadSafeCache.DBCache.GetLocker(dst); + object dstLocker = ThreadSafeCache.DBCache.GetLocker(dst); lock (srcLocker) - lock (dstLockder) + lock (dstLocker) { if (Database.TableExists("Portals" + src)) { Database.Backend.CreateTable("Portals" + dst, LevelDB.createPortals);