Skip to content

Commit

Permalink
Simplified writers by passing around binary writer instead of stream
Browse files Browse the repository at this point in the history
  • Loading branch information
notscuffed committed Jan 8, 2020
1 parent 382bb8d commit 27cd984
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 91 deletions.
32 changes: 14 additions & 18 deletions RePKG.Application/Texture/Writer/TexFrameInfoContainerWriter.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
using System;
using System.IO;
using System.Text;
using RePKG.Application.Exceptions;
using RePKG.Core.Texture;

namespace RePKG.Application.Texture
{
public class TexFrameInfoContainerWriter : ITexFrameInfoContainerWriter
{
public void WriteToStream(TexFrameInfoContainer frameInfoContainer, Stream stream)
public void WriteTo(BinaryWriter writer, TexFrameInfoContainer frameInfoContainer)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
if (frameInfoContainer == null) throw new ArgumentNullException(nameof(frameInfoContainer));
if (stream == null) throw new ArgumentNullException(nameof(stream));

using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
writer.WriteNString(frameInfoContainer.Magic);
writer.Write(frameInfoContainer.Frames.Length);

switch (frameInfoContainer.Magic)
{
writer.WriteNString(frameInfoContainer.Magic);
writer.Write(frameInfoContainer.Frames.Length);

switch (frameInfoContainer.Magic)
{
case "TEXS0002":
WriteV2(frameInfoContainer, writer);
break;
case "TEXS0002":
WriteV2(frameInfoContainer, writer);
break;

case "TEXS0003":
WriteV3(frameInfoContainer, writer);
break;
case "TEXS0003":
WriteV3(frameInfoContainer, writer);
break;

default:
throw new UnknownTexFrameInfoContainerMagicException(frameInfoContainer.Magic);
}
default:
throw new UnknownTexFrameInfoContainerMagicException(frameInfoContainer.Magic);
}
}

Expand Down
24 changes: 10 additions & 14 deletions RePKG.Application/Texture/Writer/TexHeaderWriter.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
using System;
using System.IO;
using System.Text;
using RePKG.Core.Texture;

namespace RePKG.Application.Texture
{
public class TexHeaderWriter : ITexHeaderWriter
{
public void WriteToStream(TexHeader header, Stream stream)
public void WriteTo(BinaryWriter writer, TexHeader header)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
if (header == null) throw new ArgumentNullException(nameof(header));
if (stream == null) throw new ArgumentNullException(nameof(stream));

using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
{
writer.Write((int) header.Format);
writer.Write((int) header.Flags);
writer.Write(header.TextureWidth);
writer.Write(header.TextureHeight);
writer.Write(header.ImageWidth);
writer.Write(header.ImageHeight);
writer.Write(header.UnkInt0);
}

writer.Write((int) header.Format);
writer.Write((int) header.Flags);
writer.Write(header.TextureWidth);
writer.Write(header.TextureHeight);
writer.Write(header.ImageWidth);
writer.Write(header.ImageHeight);
writer.Write(header.UnkInt0);
}
}
}
40 changes: 18 additions & 22 deletions RePKG.Application/Texture/Writer/TexImageContainerWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Text;
using RePKG.Application.Exceptions;
using RePKG.Core.Texture;

Expand All @@ -15,40 +14,37 @@ public TexImageContainerWriter(ITexImageWriter texImageWriter)
_texImageWriter = texImageWriter;
}

public void WriteToStream(TexImageContainer imageContainer, Stream stream)
public void WriteTo(BinaryWriter writer, TexImageContainer imageContainer)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
if (imageContainer == null) throw new ArgumentNullException(nameof(imageContainer));
if (stream == null) throw new ArgumentNullException(nameof(stream));

using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
{
writer.WriteNString(imageContainer.Magic);
writer.WriteNString(imageContainer.Magic);

switch (imageContainer.Magic)
{
case "TEXB0001":
WriteV1(imageContainer, writer);
break;
switch (imageContainer.Magic)
{
case "TEXB0001":
WriteV1(imageContainer, writer);
break;

case "TEXB0002":
WriteV2(imageContainer, writer);
break;
case "TEXB0002":
WriteV2(imageContainer, writer);
break;

case "TEXB0003":
WriteV3(imageContainer, writer);
break;
case "TEXB0003":
WriteV3(imageContainer, writer);
break;

default:
throw new UnknownTexImageContainerMagicException(imageContainer.Magic);
}
default:
throw new UnknownTexImageContainerMagicException(imageContainer.Magic);
}
}

public void WriteImagesToStream(Tex tex, Stream stream)
public void WriteImagesTo(BinaryWriter writer, Tex tex)
{
foreach (var image in tex.ImagesContainer.Images)
{
_texImageWriter.WriteToStream(tex, image, stream);
_texImageWriter.WriteTo(writer, tex, image);
}
}

Expand Down
20 changes: 8 additions & 12 deletions RePKG.Application/Texture/Writer/TexImageWriter.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System;
using System.IO;
using System.Text;
using RePKG.Core.Texture;

namespace RePKG.Application.Texture
{
public class TexImageWriter : ITexImageWriter
{
public void WriteToStream(Tex tex, TexImage image, Stream stream)
public void WriteTo(BinaryWriter writer, Tex tex, TexImage image)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
if (tex == null) throw new ArgumentNullException(nameof(tex));
if (image == null) throw new ArgumentNullException(nameof(image));
if (stream == null) throw new ArgumentNullException(nameof(stream));

Action<BinaryWriter, TexMipmap> mipmapWriter;
Action<BinaryWriter, TexMipmap> mipmapWriter;

switch (tex.ImagesContainer.ImageContainerVersion)
{
case TexImageContainerVersion.Version1:
Expand All @@ -28,14 +27,11 @@ public void WriteToStream(Tex tex, TexImage image, Stream stream)
throw new ArgumentOutOfRangeException();
}

using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
writer.Write(image.Mipmaps.Length);

foreach (var mipmap in image.Mipmaps)
{
writer.Write(image.Mipmaps.Length);

foreach (var mipmap in image.Mipmaps)
{
mipmapWriter(writer, mipmap);
}
mipmapWriter(writer, mipmap);
}
}

Expand Down
28 changes: 14 additions & 14 deletions RePKG.Application/Texture/Writer/TexWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.IO;
using System.Text;
using RePKG.Application.Exceptions;
using RePKG.Core.Texture;

Expand All @@ -21,27 +21,27 @@ public TexWriter(
_texFrameInfoContainerWriter = texFrameInfoContainerWriter;
}

public void WriteToStream(Tex tex, Stream stream)
public void WriteTo(BinaryWriter writer, Tex tex)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
if (tex == null) throw new ArgumentNullException(nameof(tex));

if (tex.Magic1 != "TEXV0005")
throw new UnknownTexHeaderMagicException(nameof(tex.Magic1), tex.Magic1);

if (tex.Magic2 != "TEXI0001")
throw new UnknownTexHeaderMagicException(nameof(tex.Magic2), tex.Magic2);

using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
{
writer.WriteNString(tex.Magic1);
writer.WriteNString(tex.Magic2);
}

_texHeaderWriter.WriteToStream(tex.Header, stream);
_texImageContainerWriter.WriteToStream(tex.ImagesContainer, stream);

_texImageContainerWriter.WriteImagesToStream(tex, stream);
writer.WriteNString(tex.Magic1);
writer.WriteNString(tex.Magic2);

_texHeaderWriter.WriteTo(writer, tex.Header);
_texImageContainerWriter.WriteTo(writer, tex.ImagesContainer);

_texImageContainerWriter.WriteImagesTo(writer, tex);

if (tex.IsGif)
_texFrameInfoContainerWriter.WriteToStream(tex.FrameInfoContainer, stream);
_texFrameInfoContainerWriter.WriteTo(writer, tex.FrameInfoContainer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace RePKG.Core.Texture
{
public interface ITexFrameInfoContainerWriter
{
void WriteToStream(TexFrameInfoContainer frameInfoContainer, Stream stream);
void WriteTo(BinaryWriter writer, TexFrameInfoContainer frameInfoContainer);
}
}
2 changes: 1 addition & 1 deletion RePKG.Core/Texture/Interfaces/Writer/ITexHeaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace RePKG.Core.Texture
{
public interface ITexHeaderWriter
{
void WriteToStream(TexHeader header, Stream stream);
void WriteTo(BinaryWriter writer, TexHeader header);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace RePKG.Core.Texture
{
public interface ITexImageContainerWriter
{
void WriteToStream(TexImageContainer imageContainer, Stream stream);
void WriteImagesToStream(Tex tex, Stream stream);
void WriteTo(BinaryWriter writer, TexImageContainer imageContainer);
void WriteImagesTo(BinaryWriter writer, Tex tex);
}
}
2 changes: 1 addition & 1 deletion RePKG.Core/Texture/Interfaces/Writer/ITexImageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace RePKG.Core.Texture
{
public interface ITexImageWriter
{
void WriteToStream(Tex tex, TexImage mipmap, Stream stream);
void WriteTo(BinaryWriter writer, Tex tex, TexImage mipmap);
}
}
4 changes: 3 additions & 1 deletion RePKG.Core/Texture/Interfaces/Writer/ITexWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.IO;

namespace RePKG.Core.Texture
{
public interface ITexWriter
{

void WriteTo(BinaryWriter writer, Tex tex);
}
}
12 changes: 7 additions & 5 deletions RePKG.Tests/TexWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System.Text;
using NUnit.Framework;
using RePKG.Application.Texture;
using RePKG.Core.Texture;

namespace RePKG.Tests
{
public class TexWriterTests
{
private TexReader _reader;
private TexWriter _writer;
private ITexReader _reader;
private ITexWriter _writer;

[SetUp]
public void Setup()
Expand Down Expand Up @@ -63,9 +64,10 @@ public void TestWriteAndRead(string name)
var tex = _reader.ReadFrom(reader);

// Write tex
var outputMemoryStream = new MemoryStream(inputBytes.Length);
_writer.WriteToStream(tex, outputMemoryStream);
var outputBytes = outputMemoryStream.ToArray();
var memoryStream = new MemoryStream(inputBytes.Length);
var writer = new BinaryWriter(memoryStream, Encoding.UTF8);
_writer.WriteTo(writer, tex);
var outputBytes = memoryStream.ToArray();

// Verify
Assert.AreEqual(inputBytes.Length, outputBytes.Length, "Written tex size doesn't match input size");
Expand Down

0 comments on commit 27cd984

Please sign in to comment.