Clear undo buffer when undo buffer is saved. Breaks /redo.

This commit is contained in:
UnknownShadow200 2016-08-19 22:20:17 +10:00
parent 36465e8f2e
commit 6b7b3cd191
5 changed files with 28 additions and 25 deletions

View File

@ -74,7 +74,6 @@ namespace MCGalaxy.Drawing.Ops {
buffer.CheckIfSend(true);
node = node.Prev;
}
return;
}
}
}

View File

@ -123,14 +123,9 @@ namespace MCGalaxy {
ecos.money = money;
Economy.UpdateEcoStats(ecos);
}
Server.zombie.SaveZombieStats(this);
try {
SaveUndo(this);
} catch (Exception e) {
Server.s.Log("Error saving undo data.");
Server.ErrorLog(e);
}
SaveUndo(this);
}
#region == GLOBAL MESSAGES ==

View File

@ -39,25 +39,30 @@ namespace MCGalaxy.Util {
/// <summary> Used to sychronise clearing an undo cache. </summary>
public ReaderWriterLockSlim ClearLock = new ReaderWriterLockSlim();
/// <summary> Used to sychronise adding to an undo cache. </summary>
public object AddLock = new object();
public const int TimeDeltaMax = (1 << 13) - 1;
/// <summary> Appends an item to the cache. </summary>
public void Add(Level lvl, Player.UndoPos item) {
DateTime time = Server.StartTime.AddTicks(item.timeDelta * TimeSpan.TicksPerSecond);
if (Tail == null) {
Tail = UndoCacheNode.Make(lvl, time); Head = Tail;
}
lock (AddLock) {
DateTime time = Server.StartTime.AddTicks(item.timeDelta * TimeSpan.TicksPerSecond);
if (Tail == null) {
Tail = UndoCacheNode.Make(lvl, time); Head = Tail;
}
if (lvl.name != Tail.MapName || lvl.Width != Tail.Width || lvl.Height != Tail.Height ||
lvl.Length != Tail.Length || Math.Abs((time - Tail.BaseTime).TotalSeconds) > TimeDeltaMax) {
UndoCacheNode node = UndoCacheNode.Make(lvl, time);
Tail.Next = node; node.Prev = Tail;
Tail = node;
if (lvl.name != Tail.MapName || lvl.Width != Tail.Width || lvl.Height != Tail.Height ||
lvl.Length != Tail.Length || Math.Abs((time - Tail.BaseTime).TotalSeconds) > TimeDeltaMax) {
UndoCacheNode node = UndoCacheNode.Make(lvl, time);
Tail.Next = node; node.Prev = Tail;
Tail = node;
}
short timeDiff = (short)(time - Tail.BaseTime).TotalSeconds;
Tail.Items.Add(UndoCacheItem.Make(Tail, timeDiff, ref item));
Count++;
}
short timeDiff = (short)(time - Tail.BaseTime).TotalSeconds;
Tail.Items.Add(UndoCacheItem.Make(Tail, timeDiff, ref item));
Count++;
}
/// <summary> Removes all items from the cache and resets the state to default. </summary>
@ -81,7 +86,7 @@ namespace MCGalaxy.Util {
public int Width, Height, Length;
public DateTime BaseTime;
public UndoCacheNode Prev;
public UndoCacheNode Prev, Next;
public List<UndoCacheItem> Items = new List<UndoCacheItem>();
public static UndoCacheNode Make(Level lvl, DateTime time) {

View File

@ -64,6 +64,11 @@ namespace MCGalaxy.Util {
using (IDisposable locker = cache.ClearLock.AccquireReadLock()) {
NewFormat.SaveUndoData(cache, path);
}
using (IDisposable locker = cache.ClearLock.AccquireWriteLock()) {
lock (cache.AddLock)
cache.Clear();
}
}
public static void UndoPlayer(Player p, string target, Vec3S32[] marks, DateTime start, ref bool FoundUser) {

View File

@ -70,12 +70,11 @@ namespace MCGalaxy.Util {
}
protected override void SaveUndoData(UndoCache buffer, string path) {
// TODO: should be writing Head first??
using (FileStream fs = File.Create(path)) {
BinaryWriter w = new BinaryWriter(fs);
long entriesPos = 0;
ChunkHeader last = default(ChunkHeader);
UndoCacheNode node = buffer.Tail;
UndoCacheNode node = buffer.Head;
while (node != null) {
List<UndoCacheItem> items = node.Items;
@ -95,7 +94,7 @@ namespace MCGalaxy.Util {
}
if (last.Entries > 0)
WriteChunkEntries(w, last.Entries, entriesPos);
node = node.Prev;
node = node.Next;
}
}
}