/// <summary> /// Saves Tag to the specified File (creates it if it doesn't exist) /// </summary> /// <param name="filename">filename of file to save tag to</param> public void Save(string filename) { FileStream File = new FileStream(filename, FileMode.Create); BinaryWriter binWriter = new BinaryWriter(File); Info header = new Info(); this.Filename = filename; header.type = this.Type; File.Position = Info.Size; //header.cacheInformationOffset = (int)File.Position; //binWriter.Write(IsCached); //binWriter.Write(Padding.GetBytes(File.Position, 4)); //binWriter.Write(RawOffsets.Length); //foreach (int i in RawOffsets) // binWriter.Write(i); //binWriter.Write(StringIdOffsets.Length); //foreach (int i in StringIdOffsets) // binWriter.Write(i); //binWriter.Write(TagReferenceOffsets.Length); //foreach (int i in TagReferenceOffsets) // binWriter.Write(i); //binWriter.Write(PointerOffsets.Length); //foreach (int i in PointerOffsets) // binWriter.Write(i); header.metaOffset = (int)File.Position; binWriter.Write(TagStream.ToArray()); header.metaLength = (int)File.Position - header.metaOffset; if (ResourceStream.Length > 0) { binWriter.Write(Padding.GetBytes(File.Position, 512)); header.rawOffset = (int)File.Position; binWriter.Write(ResourceStream.ToArray()); header.rawLength = (int)File.Position - header.rawOffset; header.rawReferencesCount = ResourceInformation.Length; header.rawReferencesOffset = (int)File.Position; for (int i = 0; i < ResourceInformation.Length; i++) { binWriter.Write(ResourceInformation[i].Address); binWriter.Write(ResourceInformation[i].Length); } } if (Strings.Count > 0) { binWriter.Write(Padding.GetBytes(File.Position, 512)); header.stringReferencesCount = Strings.Count; header.stringReferencesOffset = (int)File.Position; for (int i = 0; i < Strings.Count; i++) { binWriter.Write(Encoding.UTF8.GetBytes(Strings[i])); binWriter.Write(byte.MinValue); } if (Strings.Count > 1) File.Position -= 1; header.stringReferencesLength = (int)File.Position - header.stringReferencesOffset; } if (TagReferences.Count > 0) { binWriter.Write(Padding.GetBytes(File.Position, 512)); header.idReferencesOffset = (int)File.Position; header.idReferencesCount = TagReferences.Count; for (int i = 0; i < TagReferences.Count; i++) { if (TagReferences[i] == "{THIS}") { binWriter.Write(Encoding.UTF8.GetBytes(this.Filename)); } else { binWriter.Write(Encoding.UTF8.GetBytes(TagReferences[i])); } binWriter.Write(byte.MinValue); } File.Position -= 1; header.idReferencesLength = (int)File.Position - header.idReferencesOffset; } binWriter.Write(Padding.GetBytes(File.Position, 512)); File.Position = 0; binWriter.Write(header.ToByteArray()); binWriter.Flush(); File.Close(); }