From 8f89e79afeb7d5a39b9d4ba3b22893d2bbc03a11 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 24 Dec 2016 08:02:05 +1100 Subject: [PATCH] DB: Fix /deletelvl and /renamelvl with binary BlockDB files --- MCGalaxy/Database/BlockDB/BlockDB.cs | 2 +- MCGalaxy/Database/BlockDB/BlockDBFile.cs | 17 +++++++++++++++++ MCGalaxy/Levels/LevelActions.cs | 6 +++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/MCGalaxy/Database/BlockDB/BlockDB.cs b/MCGalaxy/Database/BlockDB/BlockDB.cs index c9f4f01fc..7c23a1c94 100644 --- a/MCGalaxy/Database/BlockDB/BlockDB.cs +++ b/MCGalaxy/Database/BlockDB/BlockDB.cs @@ -35,7 +35,7 @@ namespace MCGalaxy.DB { public static DateTime Epoch = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// The path of this BlockDB's backing file on disc. - public string FilePath { get { return "blockdb/" + MapName + ".cbdb"; } } + public string FilePath { get { return BlockDBFile.FilePath(MapName); } } /// The path of this BlockDB's temp backing file on disc for resizing. public string TempPath { get { return "blockdb/" + MapName + ".temp"; } } diff --git a/MCGalaxy/Database/BlockDB/BlockDBFile.cs b/MCGalaxy/Database/BlockDB/BlockDBFile.cs index 2e2433bf0..eae40d6f8 100644 --- a/MCGalaxy/Database/BlockDB/BlockDBFile.cs +++ b/MCGalaxy/Database/BlockDB/BlockDBFile.cs @@ -27,6 +27,8 @@ namespace MCGalaxy.DB { const int entrySize = 16; const int bulkEntries = 256; + public static string FilePath(string map) { return "blockdb/" + map + ".cbdb"; } + public static void WriteHeader(Stream s, Vec3U16 dims) { byte[] header = new byte[entrySize * 4]; NetUtils.WriteAscii("CBDB_MCG", header, 0); @@ -136,6 +138,21 @@ namespace MCGalaxy.DB { } } + + /// Deletes the backing file on disc if it exists. + public static void DeleteBackingFile(string map) { + string path = FilePath(map); + if (!File.Exists(path)) return; + File.Delete(path); + } + + /// Moves the backing file on disc if it exists. + public static void MoveBackingFile(string srcMap, string dstMap) { + string srcPath = FilePath(srcMap), dstPath = FilePath(dstMap); + if (!File.Exists(srcPath)) return; + if (File.Exists(dstPath)) File.Delete(dstPath); + File.Move(srcPath, dstPath); + } public static void ResizeBackingFile(BlockDB db) { Server.s.Log("Resizing BlockDB for " + db.MapName, true); diff --git a/MCGalaxy/Levels/LevelActions.cs b/MCGalaxy/Levels/LevelActions.cs index 18f972f62..c62219cd9 100644 --- a/MCGalaxy/Levels/LevelActions.cs +++ b/MCGalaxy/Levels/LevelActions.cs @@ -18,6 +18,7 @@ using System; using System.IO; using MCGalaxy.Bots; +using MCGalaxy.DB; using MCGalaxy.SQL; using MCGalaxy.Util; @@ -60,6 +61,7 @@ namespace MCGalaxy { } BotsFile.MoveBots(src, dst); RenameDatabaseTables(src, dst); + BlockDBFile.MoveBackingFile(src, dst); } static void RenameDatabaseTables(string src, string dst) { @@ -135,9 +137,10 @@ namespace MCGalaxy { if (File.Exists("blockdefs/lvl_" + name + ".json")) File.Delete("blockdefs/lvl_" + name + ".json"); } catch {} - + BotsFile.DeleteBots(name); DeleteDatabaseTables(name); + BlockDBFile.DeleteBackingFile(name); } static void DeleteDatabaseTables(string name) { @@ -159,6 +162,7 @@ namespace MCGalaxy { } public static void Replace(Level old, Level lvl) { + LevelDB.SaveBlockDB(old); LevelInfo.Loaded.Remove(old); LevelInfo.Loaded.Add(lvl);