/// <summary> /// Concludes this editing session by combining all data files, finishing off with an /// instance of <see cref="EndSessionEvent"/>. /// </summary> internal void EndSession() { // Pick up the files that relate to the session string endFolder = Path.GetDirectoryName(m_FileName); uint[] fileNumbers = GetFileNumbers(endFolder, m_Data.EditSequence); // Create an end session event EndSessionEvent endEvent = new EndSessionEvent(m_Project.AllocateId()); string endFile = Path.Combine(endFolder, ProjectDatabase.GetDataFileName(endEvent.EditSequence)); using (StreamWriter sw = File.CreateText(endFile)) { foreach (uint fileNum in fileNumbers) { string fileName = Path.Combine(endFolder, ProjectDatabase.GetDataFileName(fileNum)); string s = File.ReadAllText(fileName); sw.Write(s); } // And finish off with the end event string endText = EditSerializer.GetSerializedString <Change>(DataField.Edit, endEvent); sw.Write(endText); } // Get rid of the files that we've just combined foreach (uint fileNum in fileNumbers) { string fileName = Path.Combine(endFolder, ProjectDatabase.GetDataFileName(fileNum)); File.Delete(fileName); } }
/// <summary> /// Writes the data for some sort of change to this project's data folder. /// </summary> /// <param name="c">The change to write</param> internal void WriteChange(Change c) { string dataFolder = Path.Combine(m_Container.FolderName, m_Id.ToString().ToUpper()); string dataFile = Path.Combine(dataFolder, ProjectDatabase.GetDataFileName(c.EditSequence)); string changeText = EditSerializer.GetSerializedString <Change>(DataField.Edit, c); File.WriteAllText(dataFile, changeText); }
/// <summary> /// Saves an editing operation as part of this session (writes a file to the project folder). /// </summary> /// <param name="edit">The edit to save</param> /// <param name="editString">A serialized version of the edit to save</param> /// <remarks>This method is called directly when dealing with instances of <see cref="UpdateOperation"/>, /// as the serialized string must be obtained before the update is applied. In all other cases, you /// should use the <see cref="SaveOperation"/> to save an edit. /// </remarks> internal void SaveEditString(Operation edit, string editString) { string dataFolder = Path.GetDirectoryName(m_FileName); string editFileName = Path.Combine(dataFolder, ProjectDatabase.GetDataFileName(m_Project.LastItemId)); File.WriteAllText(editFileName, editString); // Remember the edit as part of the session edit.FileNumber = m_Project.LastItemId; AddOperation(edit); }
/// <summary> /// Rolls back the last operation in this session. The operation will be removed from /// the session's operation list. /// </summary> /// <returns>True if an edit was undone.</returns> internal bool Rollback() { // Return if there is nothing to rollback. if (m_Operations.Count == 0) { return(false); } // Get the tail operation int index = m_Operations.Count - 1; Operation op = m_Operations[index]; // Move the data file to the project undo folder uint fileNum = op.FileNumber; if (fileNum == 0) { throw new ApplicationException("Edit does not appear to belong to the current editing session"); } string name = ProjectDatabase.GetDataFileName(fileNum); string sourceFileName = Path.Combine(m_Project.ProjectFolder, name); string destFileName = Path.Combine(m_Project.GetUndoFolder(), name); File.Move(sourceFileName, destFileName); // Remember the sequence number of the edit we're rolling back uint editSequence = op.EditSequence; // Remove the edit and undo it m_Operations.RemoveAt(index); this.MapModel.RemoveEdit(op); m_Project.SetLastItem(op.EditSequence - 1); op.Undo(); return(true); }
void LoadDataFiles(string folderName, uint[] fileNums) { Trace.Write("Reading data..."); EditDeserializer ed = new EditDeserializer(this); Session lastSession = null; IdManager idMan = m_MapModel.IdManager; foreach (uint fileNum in fileNums) { string editFile = Path.Combine(folderName, ProjectDatabase.GetDataFileName(fileNum)); using (TextReader tr = File.OpenText(editFile)) { TextEditReader er = new TextEditReader(tr); // Ignore any empty files altogether while (er.HasNext) { ed.SetReader(er); Change edit = Change.Deserialize(ed); if (edit is NewProjectEvent) { m_ProjectInfo = (NewProjectEvent)edit; // If the project settings don't have default entity types, initialize them with // the layer defaults. This covers a case where the settings file has been lost, and // automatically re-created by ProjectSettings.CreateInstance. var layer = EnvironmentContainer.FindLayerById(m_ProjectInfo.LayerId); m_Settings.SetEntityTypeDefaults(layer); } else if (edit is NewSessionEvent) { lastSession = new Session(this, (NewSessionEvent)edit, editFile); m_MapModel.AddSession(lastSession); } else if (edit is EndSessionEvent) { Debug.Assert(lastSession != null); lastSession.EndTime = edit.When; } else if (edit is IdAllocation) { if (idMan != null) { IdAllocation alloc = (IdAllocation)edit; IdGroup g = idMan.FindGroupById(alloc.GroupId); g.AddIdPacket(alloc); // Remember that allocations have been made in the session (bit of a hack // to ensure the session isn't later removed if no edits are actually // performed). lastSession.AddAllocation(alloc); } } else { Debug.Assert(edit is Operation); Debug.Assert(lastSession != null); lastSession.AddOperation((Operation)edit); } } } } if (m_ProjectInfo == null) { throw new ApplicationException("Could not locate the project creation event"); } // Apply any forward references ed.ApplyForwardRefs(); // Remember the highest internal ID used by the project SetLastItem(fileNums[fileNums.Length - 1]); }