2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
using CodeImp.DoomBuilder.Windows;
|
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using CodeImp.DoomBuilder.Actions;
|
2012-07-23 21:28:23 +00:00
|
|
|
using CodeImp.DoomBuilder.GZBuilder.Data; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Editing
|
|
|
|
{
|
|
|
|
public class UndoSnapshot
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
2009-06-11 21:21:20 +00:00
|
|
|
private MemoryStream recstream;
|
2009-04-19 18:07:22 +00:00
|
|
|
private string filename;
|
|
|
|
private string description;
|
|
|
|
private int ticketid; // For safe withdrawing
|
|
|
|
private volatile bool storeondisk;
|
|
|
|
private volatile bool isondisk;
|
|
|
|
private bool isdisposed;
|
2012-07-28 20:36:28 +00:00
|
|
|
//private Dictionary<string, MemoryStream> customdata;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2009-05-01 20:31:17 +00:00
|
|
|
public string Description { get { return description; } set { description = value; } }
|
2009-04-19 18:07:22 +00:00
|
|
|
public int TicketID { get { return ticketid; } }
|
|
|
|
internal bool StoreOnDisk { get { return storeondisk; } set { storeondisk = value; } }
|
|
|
|
public bool IsOnDisk { get { return isondisk; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2009-06-11 21:21:20 +00:00
|
|
|
internal UndoSnapshot(string description, MemoryStream recstream, int ticketid)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
2009-06-11 21:21:20 +00:00
|
|
|
if(recstream == null) General.Fail("Argument cannot be null!");
|
2009-04-19 18:07:22 +00:00
|
|
|
this.ticketid = ticketid;
|
|
|
|
this.description = description;
|
2009-06-11 21:21:20 +00:00
|
|
|
this.recstream = recstream;
|
2009-04-19 18:07:22 +00:00
|
|
|
this.filename = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor
|
2009-06-11 21:21:20 +00:00
|
|
|
internal UndoSnapshot(UndoSnapshot info, MemoryStream recstream)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
2009-06-11 21:21:20 +00:00
|
|
|
if(recstream == null) General.Fail("Argument cannot be null!");
|
2009-04-19 18:07:22 +00:00
|
|
|
this.ticketid = info.ticketid;
|
|
|
|
this.description = info.description;
|
2009-06-11 21:21:20 +00:00
|
|
|
this.recstream = recstream;
|
2009-04-19 18:07:22 +00:00
|
|
|
this.filename = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposer
|
|
|
|
internal void Dispose()
|
|
|
|
{
|
|
|
|
lock(this)
|
|
|
|
{
|
|
|
|
isdisposed = true;
|
2009-06-11 21:21:20 +00:00
|
|
|
if(recstream != null) recstream.Dispose();
|
|
|
|
recstream = null;
|
2009-04-19 18:07:22 +00:00
|
|
|
if(isondisk) File.Delete(filename);
|
|
|
|
isondisk = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This returns the map data
|
2009-06-11 21:21:20 +00:00
|
|
|
internal MemoryStream GetStream()
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
lock(this)
|
|
|
|
{
|
|
|
|
// Restore into memory if needed
|
|
|
|
if(isondisk) RestoreFromFile();
|
|
|
|
|
2009-06-11 21:21:20 +00:00
|
|
|
// Return the buffer
|
|
|
|
return recstream;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This moves the snapshot from memory to harddisk
|
|
|
|
internal void WriteToFile()
|
|
|
|
{
|
|
|
|
lock(this)
|
|
|
|
{
|
|
|
|
if(isdisposed) return;
|
|
|
|
if(isondisk) return;
|
|
|
|
isondisk = true;
|
|
|
|
|
|
|
|
// Compress data
|
2009-06-11 21:21:20 +00:00
|
|
|
recstream.Seek(0, SeekOrigin.Begin);
|
2012-07-23 21:28:23 +00:00
|
|
|
MemoryStream outstream = SharpCompressHelper.CompressStream(recstream); //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Make temporary file
|
|
|
|
filename = General.MakeTempFilename(General.Map.TempPath, "snapshot");
|
|
|
|
|
|
|
|
// Write data to file
|
|
|
|
File.WriteAllBytes(filename, outstream.ToArray());
|
|
|
|
|
|
|
|
// Remove data from memory
|
2009-06-11 21:21:20 +00:00
|
|
|
recstream.Dispose();
|
|
|
|
recstream = null;
|
2009-04-19 18:07:22 +00:00
|
|
|
outstream.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This loads the snapshot from harddisk into memory
|
|
|
|
internal void RestoreFromFile()
|
|
|
|
{
|
|
|
|
lock(this)
|
|
|
|
{
|
|
|
|
if(isdisposed) return;
|
|
|
|
if(!isondisk) return;
|
|
|
|
isondisk = false;
|
|
|
|
|
|
|
|
// Read the file data
|
|
|
|
MemoryStream instream = new MemoryStream(File.ReadAllBytes(filename));
|
|
|
|
|
|
|
|
// Decompress data
|
|
|
|
instream.Seek(0, SeekOrigin.Begin);
|
2012-07-23 21:28:23 +00:00
|
|
|
MemoryStream outstream = SharpCompressHelper.DecompressStream(instream); //mxd
|
2009-06-11 21:21:20 +00:00
|
|
|
recstream = new MemoryStream(outstream.ToArray());
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
instream.Dispose();
|
|
|
|
File.Delete(filename);
|
|
|
|
filename = null;
|
2012-07-23 21:28:23 +00:00
|
|
|
outstream.Dispose();
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|