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; }
//this is a weaker test than TestSerialisationIsInvertible //but has been left here as it's sometimes handy for debugging private void TestBinaryUnkSerialisationIsInvertible(string xiFilename) { Console.Out.WriteLine("Testing file {0}", xiFilename); using (Stream inStr = File.OpenRead(xiFilename)) { FileChunk deser = new FileChunk(); deser.Deserialise(inStr); MemoryStream memStr = new MemoryStream(); inStr.Seek(0, SeekOrigin.Begin); DebugOutputStreamWithExpectations outStr = new DebugOutputStreamWithExpectations(inStr, memStr); deser.Serialise(outStr); inStr.Seek(0, SeekOrigin.Begin); memStr.Seek(0, SeekOrigin.Begin); Assert.IsTrue(StreamUtils.StreamsAreEqual(inStr, memStr)); } }