From 4f8b279c633878a2b2a696f32526ae56c8694d71 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 20 Aug 2016 00:16:15 +1000 Subject: [PATCH] [Major WIP] Rewrite the highlighting bit of the code, still need to fix some the undo bit. --- Player/Undo/UndoFile.cs | 83 +++++++++++++++++++++++-------------- Player/Undo/UndoFileBin.cs | 6 ++- Player/Undo/UndoFileCBin.cs | 5 ++- Player/Undo/UndoFileText.cs | 6 ++- 4 files changed, 63 insertions(+), 37 deletions(-) diff --git a/Player/Undo/UndoFile.cs b/Player/Undo/UndoFile.cs index 27b079042..35ab12b5c 100644 --- a/Player/Undo/UndoFile.cs +++ b/Player/Undo/UndoFile.cs @@ -30,10 +30,15 @@ namespace MCGalaxy.Util { public static UndoFile NewFormat = new UndoFileCBin(); protected class UndoEntriesArgs { - public Player Player; + public Player Player; public byte[] Temp; public bool Stop; public DateTime StartRange; + + public UndoEntriesArgs(Player p, DateTime start) { + Player = p; + StartRange = start; + } } protected abstract void SaveUndoData(List buffer, string path); @@ -42,7 +47,7 @@ namespace MCGalaxy.Util { protected abstract IEnumerable GetEntries(Stream s, UndoEntriesArgs args); - protected abstract string Extension { get; } + protected abstract string Ext { get; } public static void SaveUndo(Player p) { if (p == null || p.UndoBuffer.Count < 1) return; @@ -59,7 +64,7 @@ namespace MCGalaxy.Util { Directory.CreateDirectory(playerDir); int numFiles = Directory.GetFiles(playerDir).Length; - string path = Path.Combine(playerDir, numFiles + NewFormat.Extension); + string path = Path.Combine(playerDir, numFiles + NewFormat.Ext); UndoCache cache = p.UndoBuffer; using (IDisposable locker = cache.ClearLock.AccquireReadLock()) { @@ -89,7 +94,7 @@ namespace MCGalaxy.Util { return; string[] files = Directory.GetFiles(path); Array.Sort(files, CompareFiles); - byte[] temp = null; + UndoEntriesArgs args = new UndoFile.UndoEntriesArgs(p, start); for (int i = files.Length - 1; i >= 0; i--) { path = files[i]; @@ -98,15 +103,18 @@ namespace MCGalaxy.Util { continue; UndoFile format = null; - if (path.EndsWith(TxtFormat.Extension)) format = TxtFormat; - if (path.EndsWith(BinFormat.Extension)) format = BinFormat; - if (path.EndsWith(NewFormat.Extension)) format = NewFormat; + if (path.EndsWith(TxtFormat.Ext)) format = TxtFormat; + if (path.EndsWith(BinFormat.Ext)) format = BinFormat; + if (path.EndsWith(NewFormat.Ext)) format = NewFormat; if (format == null) continue; - if (highlight) { - if (!format.HighlightEntry(p, path, ref temp, start)) break; - } else { - if (!format.UndoEntry(p, path, marks, ref temp, start)) break; + using (Stream s = File.OpenRead(path)) { + if (highlight) { + DoHighlight(s, format, args); + } else { + if (!format.UndoEntry(p, path, marks, ref temp, start)) break; + } + if (args.Stop) break; } } FoundUser = true; @@ -122,8 +130,8 @@ namespace MCGalaxy.Util { return aNum.CompareTo(bNum); } - protected internal static void UndoBlock(Player pl, Level lvl, Player.UndoPos P, - int timeDelta, BufferedBlockSender buffer) { + protected internal static void UndoBlock(Player pl, Level lvl, Player.UndoPos P, + int timeDelta, BufferedBlockSender buffer) { byte lvlTile = lvl.GetTile(P.x, P.y, P.z); if (lvlTile == P.newtype || Block.Convert(lvlTile) == Block.water || Block.Convert(lvlTile) == Block.lava || lvlTile == Block.grass) { @@ -146,23 +154,32 @@ namespace MCGalaxy.Util { if (diffBlock) { buffer.Add(lvl.PosToInt(P.x, P.y, P.z), P.newtype, P.newExtType); buffer.CheckIfSend(false); - } + } lvl.SetTile(P.x, P.y, P.z, P.newtype); if (P.newtype == Block.custom_block) lvl.SetExtTile(P.x, P.y, P.z, P.newExtType); - } + } } } - protected static void HighlightBlock(Player p, Level lvl, byte type, byte newType, - ushort x, ushort y, ushort z) { - byte block = (newType == Block.air + static void DoHighlight(Stream s, UndoFile format, UndoEntriesArgs args) { + BufferedBlockSender buffer = new BufferedBlockSender(args.Player); + Level lvl = args.Player.level; + + foreach (Player.UndoPos P in format.GetEntries(s, args)) { + byte type = P.type, newType = P.newtype; + byte block = (newType == Block.air || Block.Convert(type) == Block.water || type == Block.waterstill - || Block.Convert(type) == Block.lava || type == Block.lavastill) + || Block.Convert(type) == Block.lava || type == Block.lavastill) ? Block.red : Block.green; - p.SendBlockchange(x, y, z, block); + + buffer.Add(lvl.PosToInt(P.x, P.y, P.z), block, 0); + buffer.CheckIfSend(false); + } + buffer.CheckIfSend(true); } + public static void CreateDefaultDirectories() { if (!Directory.Exists(undoDir)) Directory.CreateDirectory(undoDir); @@ -177,23 +194,27 @@ namespace MCGalaxy.Util { static void UpgradeFiles(string dir, string name) { string path = Path.Combine(dir, name); - if (!Directory.Exists(path)) - return; + if (!Directory.Exists(path)) return; string[] files = Directory.GetFiles(path); List buffer = new List(); + UndoEntriesArgs args = new UndoEntriesArgs(null, DateTime.MinValue); for (int i = 0; i < files.Length; i++) { path = files[i]; - if (path.EndsWith(BinFormat.Extension)) { - buffer.Clear(); - BinFormat.ReadUndoData(buffer, path); - } else if (path.EndsWith(TxtFormat.Extension)) { - buffer.Clear(); - TxtFormat.ReadUndoData(buffer, path); - } + if (!path.EndsWith(BinFormat.Ext) && !path.EndsWith(TxtFormat.Ext)) continue; - string newPath = Path.ChangeExtension(path, NewFormat.Extension); - NewFormat.SaveUndoData(buffer, newPath); + IEnumerable data = null; + using (FileStream s = File.OpenRead(path)) { + data = path.EndsWith(BinFormat.Ext) + ? BinFormat.GetEntries(s, args) + : TxtFormat.GetEntries(s, args); + + foreach (Player.UndoPos pos in data) + buffer.Add(pos); + buffer.Reverse(); + string newPath = Path.ChangeExtension(path, NewFormat.Ext); + NewFormat.SaveUndoData(buffer, newPath); + } File.Delete(path); } } diff --git a/Player/Undo/UndoFileBin.cs b/Player/Undo/UndoFileBin.cs index 672fa9a57..4a7d151b4 100644 --- a/Player/Undo/UndoFileBin.cs +++ b/Player/Undo/UndoFileBin.cs @@ -24,7 +24,7 @@ namespace MCGalaxy.Util { public sealed class UndoFileBin : UndoFile { - protected override string Extension { get { return ".unbin"; } } + protected override string Ext { get { return ".unbin"; } } const int entrySize = 12; protected override void SaveUndoData(List buffer, string path) { @@ -37,7 +37,7 @@ namespace MCGalaxy.Util { protected override IEnumerable GetEntries(Stream s, UndoEntriesArgs args) { List list = new List(); - Player.UndoPos pos = default(Player.UndoPos); + Player.UndoPos pos; bool super = args.Player == null || args.Player.ircNick != null; DateTime start = args.StartRange; @@ -48,6 +48,7 @@ namespace MCGalaxy.Util { bool inRange = chunk.BaseTime.AddTicks(65536 * TimeSpan.TicksPerSecond) >= start; if (!inRange) { args.Stop = true; yield break; } if (!super && !args.Player.level.name.CaselessEq(chunk.LevelName)) continue; + pos.mapName = chunk.LevelName; s.Seek(chunk.DataPosition, SeekOrigin.Begin); if (args.Temp == null) @@ -59,6 +60,7 @@ namespace MCGalaxy.Util { int offset = j * entrySize; DateTime time = chunk.BaseTime.AddTicks(U16(temp, offset + 0) * TimeSpan.TicksPerSecond); if (time < start) { args.Stop = true; yield break; } + pos.timeDelta = (int)time.Subtract(Server.StartTime).TotalSeconds; pos.x = U16(temp, offset + 2); pos.y = U16(temp, offset + 4); diff --git a/Player/Undo/UndoFileCBin.cs b/Player/Undo/UndoFileCBin.cs index 11ef15b9f..ab4f76751 100644 --- a/Player/Undo/UndoFileCBin.cs +++ b/Player/Undo/UndoFileCBin.cs @@ -25,7 +25,7 @@ namespace MCGalaxy.Util { public sealed class UndoFileCBin : UndoFile { - protected override string Extension { get { return ".uncbin"; } } + protected override string Ext { get { return ".uncbin"; } } const int entrySize = 8; protected override void SaveUndoData(List buffer, string path) { @@ -100,7 +100,7 @@ namespace MCGalaxy.Util { protected override IEnumerable GetEntries(Stream s, UndoEntriesArgs args) { List list = new List(); - Player.UndoPos pos = default(Player.UndoPos); + Player.UndoPos pos; UndoCacheItem item = default(UndoCacheItem); bool super = args.Player == null || args.Player.ircNick != null; DateTime start = args.StartRange; @@ -125,6 +125,7 @@ namespace MCGalaxy.Util { item.Flags = U16(temp, offset + 0); DateTime time = chunk.BaseTime.AddTicks((item.Flags & 0x3FFF) * TimeSpan.TicksPerSecond); if (time < start) { args.Stop = true; yield break; } + pos.timeDelta = (int)time.Subtract(Server.StartTime).TotalSeconds; int index = I32(temp, offset + 2); pos.x = (ushort)(index % chunk.Width); diff --git a/Player/Undo/UndoFileText.cs b/Player/Undo/UndoFileText.cs index d21c87402..5232f9374 100644 --- a/Player/Undo/UndoFileText.cs +++ b/Player/Undo/UndoFileText.cs @@ -24,7 +24,7 @@ namespace MCGalaxy.Util { public sealed class UndoFileText : UndoFile { - protected override string Extension { get { return ".undo"; } } + protected override string Ext { get { return ".undo"; } } protected override void SaveUndoData(List buffer, string path) { throw new NotSupportedException("Text undo files have been deprecated"); @@ -35,7 +35,8 @@ namespace MCGalaxy.Util { } protected override IEnumerable GetEntries(Stream s, UndoEntriesArgs args) { - Player.UndoPos pos = default(Player.UndoPos); + Player.UndoPos pos; + pos.newExtType = 0; pos.extType = 0; string[] lines = new StreamReader(s).ReadToEnd().Split(' '); Player p = args.Player; bool super = p == null || p.ircNick != null; @@ -47,6 +48,7 @@ namespace MCGalaxy.Util { string timeRaw = lines[(i * 7) - 3].Replace('&', ' '); DateTime time = DateTime.Parse(timeRaw, CultureInfo.InvariantCulture); if (time < start) { args.Stop = true; yield break; } + pos.timeDelta = (int)time.Subtract(Server.StartTimeLocal).TotalSeconds; string map = lines[(i * 7) - 7]; if (!super && !p.level.name.CaselessEq(map)) continue;