private void newToolStripMenuItem_Click(object sender, EventArgs e) { NewForm lForm = new NewForm(this); if (lForm.ShowDialog() == DialogResult.OK) { //===================================================================== // Load the file from the CD image //===================================================================== CDImage lCDImage = new CDImage(new FileInfo(lForm.CDImageTextBox.Text)); MMCD.Course lCourse = (MMCD.Course)lForm.CourseDropDown.SelectedItem; byte[] lLevelBinary = lCDImage.Extract(lCourse.CDOffset, lCourse.CDLength); MemoryStream lLevelStream = new MemoryStream(lLevelBinary); Level lNewLevel = new Level(lLevelStream); //===================================================================== // Check that the whole file has been read //===================================================================== if (lLevelStream.Length != lLevelStream.Position) { throw new DeserialisationException(string.Format("Deserialisation terminated early at byte {0} of {1}", lLevelStream.Position, lLevelStream.Length)); } //===================================================================== // Create a new VersionList based on this level, and set it up //===================================================================== VersionList lVersionList = new VersionList(lNewLevel, lCourse.CourseName, lCourse.FileName); RootChunk = lVersionList; mCurrentFile = null; } }
private void SaveInternal(eSaveMode xiSaveMode, string xiFilename) { if (RootChunk == null) { MessageBox.Show("Can't save: no file is open"); return; } string lExceptionWhen = "saving file"; try { long lPreviousSize = -1; if (xiSaveMode == eSaveMode.Binary && File.Exists(xiFilename)) { lPreviousSize = new FileInfo(xiFilename).Length; } using (FileStream fs = File.Create(xiFilename)) { lExceptionWhen = "serialising the file"; if (xiSaveMode == eSaveMode.Binary) { if (RootChunk is VersionList) { CurrentLevel.Serialise(fs); } else { RootChunk.Serialise(fs); } } else if (xiSaveMode == eSaveMode.Xml) { XmlSerializer xs = new XmlSerializer(typeof(Chunk)); if (RootChunk is VersionList) { xs.Serialize(fs, CurrentLevel); } else { xs.Serialize(fs, RootChunk); } } else if (xiSaveMode == eSaveMode.Mmv) { if (RootChunk is VersionList) { VersionList lVersionList = (VersionList)RootChunk; lVersionList.AddLevel(CurrentLevel); RecursiveAddChunkNode(ChunkTreeView.Nodes[0].Nodes, 1, lVersionList.GetLastVersion()); } else if (RootChunk is Level) { VersionList lVersionList = new VersionList( (Level)RootChunk, Path.GetFileNameWithoutExtension(xiFilename), null); RootChunk = lVersionList; } RootChunk.Serialise(fs); } } if (lPreviousSize != -1 && lPreviousSize != new FileInfo(xiFilename).Length) { MessageBox.Show("WARNING: The size of your level has changed. Please check it's not too large, and check MMEd for bugs that have allowed the size to change.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception err) { Trace.WriteLine(err); MessageBox.Show(string.Format("Exception occurred while {0}: {1}", lExceptionWhen, err.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } mLocalSettings.LastSavedFile = xiFilename; mLocalSettings.LastSavedMode = xiSaveMode; mCurrentFile = xiFilename; mCurrentFileMode = xiSaveMode; }
private void LoadInternal(eOpenType xiOpenType, string xiFilename) { Chunk lNewRootChunk = null; string lExceptionWhen = "opening file"; try { using (FileStream fs = File.OpenRead(xiFilename)) { lExceptionWhen = "deserialising the file"; switch (xiOpenType) { case eOpenType.LevelBinary: lNewRootChunk = new Level(fs); break; case eOpenType.UnknownBinary: lNewRootChunk = new FileChunk(fs); break; case eOpenType.Mmv: lNewRootChunk = new VersionList(fs); break; case eOpenType.Xml: XmlSerializer xs = new XmlSerializer(typeof(Chunk)); lNewRootChunk = (Chunk)xs.Deserialize(fs); break; default: throw new Exception("unreachable case"); } if (fs.Length != fs.Position) { //check the whole file has been read throw new DeserialisationException(string.Format("Deserialisation terminated early at byte {0} of {1}", fs.Position, fs.Length)); } } } catch (Exception err) { Trace.WriteLine(err); MessageBox.Show(string.Format("Exception occurred while {0}: {1}", lExceptionWhen, err.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // level loaded OK, now fill tree: RootChunk = lNewRootChunk; mLocalSettings.LastOpenedFile = xiFilename; mLocalSettings.LastOpenedType = xiOpenType; mCurrentFile = xiFilename; mCurrentFileMode = xiOpenType == eOpenType.Mmv ? eSaveMode.Mmv : xiOpenType == eOpenType.Xml ? eSaveMode.Xml : eSaveMode.Binary; }