diff --git a/src/addoncreator/Addon/MinifiedMediaAddonFileInfo.cs b/src/addoncreator/Addon/MinifiedMediaAddonFileInfo.cs index 484b33a..92e42b3 100644 --- a/src/addoncreator/Addon/MinifiedMediaAddonFileInfo.cs +++ b/src/addoncreator/Addon/MinifiedMediaAddonFileInfo.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using TagLib; using File = System.IO.File; @@ -26,6 +23,7 @@ namespace GarrysMod.AddonCreator.Addon /// Creates a new instance using the given addon file. /// /// The addon file, supposedly a media file + /// The extension of this media file public MinifiedMediaAddonFileInfo(AddonFileInfo file, string extension) { _tempFile = Path.GetTempFileName(); @@ -37,6 +35,8 @@ namespace GarrysMod.AddonCreator.Addon var newTempFile = Path.Combine( dirName, Path.GetFileNameWithoutExtension(_tempFile) + "." + extension); + if (File.Exists(newTempFile)) + File.Delete(newTempFile); File.Move(_tempFile, newTempFile); _tempFile = newTempFile; @@ -74,8 +74,7 @@ namespace GarrysMod.AddonCreator.Addon } } - using (var s = new FileStream(_tempFile, FileMode.Open, FileAccess.Read)) - using (var tags = TagLib.File.Create(new StreamFileAbstraction(_tempFile, s, s))) + using (var tags = TagLib.File.Create(new FileAbstraction(_tempFile))) { if (tags.PossiblyCorrupt && !IgnoreCorrupted) { diff --git a/src/addoncreator/FileAbstraction.cs b/src/addoncreator/FileAbstraction.cs new file mode 100644 index 0000000..870e53d --- /dev/null +++ b/src/addoncreator/FileAbstraction.cs @@ -0,0 +1,44 @@ +using System.IO; +using File = TagLib.File; + +namespace GarrysMod.AddonCreator +{ + class FileAbstraction : File.IFileAbstraction + { + private readonly string path; + private readonly MemoryStream realWriteStream; + public FileAbstraction(string path) + { + this.path = path; + realWriteStream = new MemoryStream(); + WriteStream = new NonClosingStream(realWriteStream); + ReadStream = new FileStream(path, FileMode.Open, FileAccess.Read); + + ReadStream.CopyTo(realWriteStream); + ReadStream.Position = realWriteStream.Position = 0; + + Name = Path.GetFileName(path); + } + + public string Name { get; private set; } + + public Stream ReadStream { get; private set; } + + public Stream WriteStream { get; private set; } + + public void CloseStream(Stream stream) + { + ReadStream.Dispose(); + + if (!((NonClosingStream) WriteStream).Written) + return; + + realWriteStream.Position = 0; + using (var s = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) + { + realWriteStream.CopyTo(s); + } + realWriteStream.Dispose(); + } + } +} diff --git a/src/addoncreator/GarrysMod.AddonCreator.csproj b/src/addoncreator/GarrysMod.AddonCreator.csproj index d894780..020e956 100644 --- a/src/addoncreator/GarrysMod.AddonCreator.csproj +++ b/src/addoncreator/GarrysMod.AddonCreator.csproj @@ -69,8 +69,10 @@ + + diff --git a/src/addoncreator/NonClosingStream.cs b/src/addoncreator/NonClosingStream.cs new file mode 100644 index 0000000..13cff66 --- /dev/null +++ b/src/addoncreator/NonClosingStream.cs @@ -0,0 +1,76 @@ +using System.IO; + +namespace GarrysMod.AddonCreator +{ + class NonClosingStream : Stream + { + private readonly Stream _stream; + + public NonClosingStream(Stream stream) + { + _stream = stream; + } + + public override bool CanRead + { + get { return _stream.CanRead; } + } + + public override bool CanSeek + { + get { return _stream.CanSeek; } + } + + public override bool CanWrite + { + get { return _stream.CanWrite; } + } + + public override long Length + { + get { return _stream.Length; } + } + + public override long Position + { + get { return _stream.Position; } + set { _stream.Position = value; } + } + + public override void Flush() + { + _stream.Flush(); + } + + public override void Close() + { + } + + protected override void Dispose(bool disposing) + { + } + + public override long Seek(long offset, SeekOrigin origin) + { + return _stream.Seek(offset, origin); + } + + public override void SetLength(long value) + { + _stream.SetLength(value); + } + + public override int Read(byte[] buffer, int offset, int count) + { + return _stream.Read(buffer, offset, count); + } + + public override void Write(byte[] buffer, int offset, int count) + { + _stream.Write(buffer, offset, count); + Written = true; + } + + public bool Written { get; private set; } + } +}