public void Save(String filename,StreamProgress.ProgressFunction pfunction) { String getextension = Path.GetExtension(filename); SaveFormatInfo sfi = null; if(getextension.Equals(".XBLF",StringComparison.OrdinalIgnoreCase)) { sfi = new XmlSaveFormatInfo(); } else { sfi = new BinarySaveFormatInfo(); } Save(filename,sfi,pfunction); }
public static EditorSet FromStream(Stream inputstream, StreamProgress.ProgressFunction pcfunc) { string tempfile = BCBlockGameState.GetTempFile(".BLF"); FileStream writeto = new FileStream(tempfile, FileMode.Create); byte[] membytes = new byte[inputstream.Length-inputstream.Position]; inputstream.Read(membytes, 0, membytes.Length); writeto.Write(membytes, 0, membytes.Length); writeto.Close(); EditorSet readset = FromFile(tempfile, pcfunc); File.Delete(tempfile); return readset; }
public void Save(Stream Target,SaveFormatInfo Format,StreamProgress.ProgressFunction pfunction) { SerializationInfo sinfo; try { Stream outstream = Target; { using (StreamProgress sp = new StreamProgress(outstream, pfunction)) { if (Format is BinarySaveFormatInfo) { BinarySaveFormatInfo BinInfo = Format as BinarySaveFormatInfo; //write out whether this is compressed. IFormatter binformatter = BCBlockGameState.getFormatter<EditorSet>(BCBlockGameState.DataSaveFormats.Format_Binary); outstream.WriteByte((byte)(BinInfo.CompressionType != CompressionTypeConstants.Compression_None ? 1 : 0)); Stream targetstream; switch (BinInfo.CompressionType) { case CompressionTypeConstants.Compression_GZip: targetstream = new GZipStream(outstream, CompressionMode.Compress); break; case CompressionTypeConstants.Compression_Deflate: targetstream = new DeflateStream(outstream, CompressionMode.Compress, CompressionLevel.BestCompression); break; default: targetstream = outstream; break; } using (targetstream) { Cursor.Current = Cursors.WaitCursor; binformatter.Serialize(targetstream, this); targetstream.Close(); Cursor.Current = Cursors.Default; } } else if(Format is XmlSaveFormatInfo) { Cursor.Current = Cursors.WaitCursor; //almost forgot to change this code to save to XML properly. the SOAP stuff, we have to remove at some stage... XElement BuildElement = this.GetXmlData("EditorSet",null); XDocument buildDocument = new XDocument(BuildElement); buildDocument.Save(outstream,SaveOptions.OmitDuplicateNamespaces); /*IFormatter xmlformat = BCBlockGameState.getFormatter<EditorSet>(BCBlockGameState.DataSaveFormats.Format_XML); Cursor.Current = Cursors.WaitCursor; xmlformat.Serialize(outstream,this);*/ } } } } catch (Exception e) { Debug.Print("Exception in EditorSet.Save:" + e.ToString()); } finally { Cursor.Current = Cursors.Default; } }
public static EditorSet FromFile(String filename,StreamProgress.ProgressFunction pcfunc) { Debug.Print("loading EditorSet from file:" + filename); if (filename == null) throw new ArgumentException("filename cannot be null"); //System.Web.Script.Serialization.JavaScriptSerializer oSerializer using (FileStream inputstream = new FileStream(filename, FileMode.Open)) { FileStream pcs = inputstream; if (Path.GetExtension(filename).ToUpper() == ".BLF") { IFormatter useformatter = BCBlockGameState.getFormatter<EditorSet>(BCBlockGameState.DataSaveFormats.Format_Binary); bool donocompressheader = false; Stream acquirefrom = null; using (StreamProgress sp = new StreamProgress(pcs, pcfunc)) { tryagain: try { //read in a bool. this determines whether it is compressed or not. //bool readbool = new BinaryReader(inputstream).ReadBoolean(); if (!donocompressheader) { CompressionTypeConstants compression = (CompressionTypeConstants)inputstream.ReadByte(); Debug.Print("EditSet::FromFile- file compression header:" + compression); switch (compression) { case CompressionTypeConstants.Compression_GZip: acquirefrom = new GZipStream(inputstream, CompressionMode.Decompress); break; case CompressionTypeConstants.Compression_Deflate: acquirefrom = new DeflateStream(inputstream, CompressionMode.Decompress); break; default: acquirefrom = inputstream; break; } } else { acquirefrom = inputstream; inputstream.Seek(0, SeekOrigin.Begin); } using (acquirefrom) { EditorSet returnthis = (EditorSet)useformatter.Deserialize(acquirefrom); if (returnthis.CreateData.SavedSounds != null) Debug.Print("returning EditorSet that contains " + returnthis.CreateData.SavedSounds.Count.ToString() + " Sounds."); return returnthis; } } catch (SerializationException se) { Trace.WriteLine("SerializationException:" + se.ToString()); donocompressheader = true; goto tryagain; } finally { if (acquirefrom != null) acquirefrom.Close(); } } } else if(Path.GetExtension(filename).ToUpper() == ".XBLF") { //uses CustomXmlFormatter to deserialize, and no compression. using (StreamProgress sp = new StreamProgress(pcs, pcfunc)) { XDocument loaddocument = XDocument.Load(inputstream); EditorSet returnthis = new EditorSet(loaddocument.Root,null); return returnthis; /*IFormatter useformatter = BCBlockGameState.getFormatter<EditorSet>(BCBlockGameState.DataSaveFormats.Format_XML); EditorSet returnthis = (EditorSet)useformatter.Deserialize(inputstream); if (returnthis.CreateData.SavedSounds != null) Debug.Print("returning EditorSet that contains " + returnthis.CreateData.SavedSounds.Count.ToString() + " Sounds."); return returnthis;*/ } } else { throw new Exception("Unsupported File Extension, \"" + Path.GetExtension(filename) + "\""); } } return null; }
public void Save(String sTarget,SaveFormatInfo Format,StreamProgress.ProgressFunction pfunction) { //if(!File.Exists(sTarget)) throw new FileNotFoundException(sTarget); using (FileStream fs = new FileStream(sTarget, FileMode.Create)) { Save(fs, Format, pfunction); } }