/// <summary> /// Backup the project, only works if loaded from a file /// </summary> public static void Backup() { CANAPEProject currProject = CurrentProject; if ((currProject != null) && (currProject._fileName != null)) { string tempName = Path.ChangeExtension(currProject._fileName, ".autobak.canape"); try { using (Stream stm = File.Open(tempName, FileMode.Create, FileAccess.ReadWrite)) { WriteFileVersion(GeneralUtils.GetCanapeVersion(), stm); BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File)); formatter.Serialize(stm, currentProject); } } catch (Exception) { // We didn't save, delete file try { File.Delete(tempName); } catch (IOException) { } catch (UnauthorizedAccessException) { } } } }
/// <summary> /// Load a project from a file /// </summary> /// <param name="stm">The stream</param> /// <param name="fileName">The filename to use (can be null)</param> /// <param name="verifyVersion">Set true to verify the version being opened match this canape</param> public static void Load(Stream stm, string fileName, bool verifyVersion) { // If an empty stream if (stm.Length == 0) { New(); } else { Version ver = ReadFileHeader(stm); bool compressed = true; if (verifyVersion) { if (ver.CompareTo(GeneralUtils.GetCanapeVersion()) != 0) { throw new InvalidVersionException(ver); } } if (stm.ReadByte() == 0x1F) { compressed = true; } else { compressed = false; } stm.Position = stm.Position - 1; using (Stream inStream = compressed ? new GZipStream(stm, CompressionMode.Decompress, false) : stm) { BinaryFormatter formatter = CreateFormatter(ver); CANAPEProject newProject = (CANAPEProject)formatter.Deserialize(inStream); newProject._fileName = fileName; newProject._globalMeta = new MetaDictionary(); currentProject = newProject; if (ProjectLoaded != null) { ProjectLoaded.Invoke(currentProject, new EventArgs()); } } } }
/// <summary> /// Load a project from a file /// </summary> /// <param name="stm">The stream</param> /// <param name="fileName">The filename to use (can be null)</param> /// <param name="verifyVersion">Set true to verify the version being opened match this canape</param> /// <param name="secure">Attemps to make the load secure, not likely to succeed</param> public static void Load(Stream stm, string fileName, bool verifyVersion, bool secure) { // If an empty stream if (stm.Length == 0) { New(); } else { Version ver = ReadFileHeader(stm); bool compressed = true; if (verifyVersion) { if (ver.CompareTo(GeneralUtils.GetCanapeVersion()) != 0) { throw new InvalidVersionException(ver); } } if (stm.ReadByte() == 0x1F) { compressed = true; } else { compressed = false; } stm.Position = stm.Position - 1; using (Stream inStream = compressed ? new GZipStream(stm, CompressionMode.Decompress, false) : stm) { BinaryFormatter formatter = CreateFormatter(ver, secure); CANAPEProject newProject = null; // Note that all this is going to do is prevent anything during // direct load of objects and scripts, it won't do anything against anything else if (secure) { try { PermissionSet ps = new PermissionSet(PermissionState.None); ps.PermitOnly(); newProject = (CANAPEProject)formatter.UnsafeDeserialize(inStream, null); } finally { CodeAccessPermission.RevertPermitOnly(); } } else { newProject = (CANAPEProject)formatter.Deserialize(inStream); } newProject._fileName = fileName; newProject._globalMeta = new MetaDictionary(); currentProject = newProject; if (ProjectLoaded != null) { ProjectLoaded.Invoke(currentProject, new EventArgs()); } } } }
/// <summary> /// Create a new document /// </summary> public static void New() { currentProject = new CANAPEProject(); currentProject._isNew = true; }