Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
notscuffed committed Jan 10, 2020
1 parent fdc4c9a commit 8da267e
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 62 deletions.
4 changes: 4 additions & 0 deletions RePKG.Application/Exceptions/EnumNotValidException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace RePKG.Application.Exceptions
{
/// <summary>
/// Thrown when enum raw value doesn't match any label
/// </summary>
/// <typeparam name="T">The enum</typeparam>
public class EnumNotValidException<T> : Exception where T : Enum
{
public EnumNotValidException(T @enum) : base($"Invalid value '{@enum}' for enum '{typeof(T)}'")
Expand Down
20 changes: 20 additions & 0 deletions RePKG.Application/Exceptions/UnknownMagicException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace RePKG.Application.Exceptions
{
/// <summary>
/// Thrown on magic values that have not been tested before
/// </summary>
public class UnknownMagicException : Exception
{
public UnknownMagicException(string source, string magic) : base(
$"Unknown magic: '{magic}' in '{source}'")
{
}

public UnknownMagicException(string source, string property, string magic) : base(
$"Unknown magic: '{magic}' in '{source}:{property}'")
{
}
}
}

This file was deleted.

10 changes: 0 additions & 10 deletions RePKG.Application/Exceptions/UnknownTexHeaderMagicException.cs

This file was deleted.

This file was deleted.

6 changes: 4 additions & 2 deletions RePKG.Application/Exceptions/UnsafeTexException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace RePKG.Application.Exceptions
{
// Thrown on unorganic values
// For example when entry count is way higher than usual
/// <summary>
/// Thrown on unorganic values.
/// For example when entry count is way higher than usual
/// </summary>
public class UnsafeTexException : Exception
{
public UnsafeTexException(string reason) : base($"Unsafe TEX detected, reason: {reason}")
Expand Down
3 changes: 2 additions & 1 deletion RePKG.Application/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal static class Extensions
public static string ReadNString(this BinaryReader reader, int maxLength = -1)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
var builder = new StringBuilder(16);

var builder = new StringBuilder(maxLength <= 0 ? 16 : maxLength);
var c = reader.ReadChar();

while (c != '\0' && (maxLength == -1 || builder.Length < maxLength))
Expand Down
4 changes: 2 additions & 2 deletions RePKG.Application/Package/PackageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Core.Package.Package ReadFromStream(Stream stream)
}
}

private void ReadEntries(List<PackageEntry> list, BinaryReader reader)
private static void ReadEntries(List<PackageEntry> list, BinaryReader reader)
{
var entryCount = reader.ReadInt32();

Expand All @@ -52,7 +52,7 @@ private void ReadEntries(List<PackageEntry> list, BinaryReader reader)
}
}

private void PopulateEntriesWithData(int dataStart, List<PackageEntry> entries, BinaryReader reader)
private static void PopulateEntriesWithData(int dataStart, List<PackageEntry> entries, BinaryReader reader)
{
foreach (var entry in entries)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@

// most of the algorithms and data used in this Class-file has been ported from LibSquish!
// http://code.google.com/p/libsquish/
// ReSharper disable InconsistentNaming

using System;
using RePKG.Core.Texture;

namespace RePKG.Core.Texture
namespace RePKG.Application.Texture.Helpers
{
public static class DXT
{
[Flags]
public enum DXTFlags
{
DXT1 = 1,
DXT3 = 1 << 1,
DXT5 = 1 << 2,
// Additional Enums not implemented :o
}

private static void Decompress(byte[] rgba, byte[] block, int blockIndex, DXTFlags flags)
{
// get the block locations
Expand Down
5 changes: 4 additions & 1 deletion RePKG.Application/Texture/TexFrameInfoContainerReader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using RePKG.Application.Exceptions;
using RePKG.Core.Texture;
Expand All @@ -8,6 +9,8 @@ public class TexFrameInfoContainerReader : ITexFrameInfoContainerReader
{
public TexFrameInfoContainer ReadFrom(BinaryReader reader)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));

var container = new TexFrameInfoContainer
{
Magic = reader.ReadNString(maxLength: 16)
Expand All @@ -29,7 +32,7 @@ public TexFrameInfoContainer ReadFrom(BinaryReader reader)
break;

default:
throw new UnknownTexFrameInfoContainerMagicException(container.Magic);
throw new UnknownMagicException(nameof(TexFrameInfoContainerReader), container.Magic);
}

for (var i = 0; i < frameCount; i++)
Expand Down
3 changes: 3 additions & 0 deletions RePKG.Application/Texture/TexHeaderReader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using RePKG.Core.Texture;

Expand All @@ -7,6 +8,8 @@ public class TexHeaderReader : ITexHeaderReader
{
public TexHeader ReadFrom(BinaryReader reader)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));

var header = new TexHeader
{
Format = (TexFormat) reader.ReadInt32(),
Expand Down
5 changes: 4 additions & 1 deletion RePKG.Application/Texture/TexImageContainerReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public TexImageContainerReader(ITexImageReader texImageReader)

public TexImageContainer ReadFrom(BinaryReader reader, TexFormat texFormat)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
texFormat.AssertValid();

var container = new TexImageContainer
{
Magic = reader.ReadNString(maxLength: 16)
Expand All @@ -38,7 +41,7 @@ public TexImageContainer ReadFrom(BinaryReader reader, TexFormat texFormat)
break;

default:
throw new UnknownTexImageContainerMagicException(container.Magic);
throw new UnknownMagicException(nameof(TexImageContainerReader), container.Magic);
}

container.ImageContainerVersion = (TexImageContainerVersion) Convert.ToInt32(container.Magic.Substring(4));
Expand Down
4 changes: 4 additions & 0 deletions RePKG.Application/Texture/TexImageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public TexImage ReadFrom(
TexImageContainer container,
TexFormat texFormat)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
if (container == null) throw new ArgumentNullException(nameof(container));
texFormat.AssertValid();

var mipmapCount = reader.ReadInt32();

if (mipmapCount > Constants.MaximumMipmapCount)
Expand Down
3 changes: 3 additions & 0 deletions RePKG.Application/Texture/TexJsonInfoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json;
using RePKG.Core.Texture;

Expand All @@ -7,6 +8,8 @@ public class TexJsonInfoGenerator : ITexJsonInfoGenerator
{
public string GenerateInfo(Tex tex)
{
if (tex == null) throw new ArgumentNullException(nameof(tex));

return JsonConvert.SerializeObject(new
{
bleedtransparentcolors = true,
Expand Down
10 changes: 7 additions & 3 deletions RePKG.Application/Texture/TexMipmapDecompressor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using K4os.Compression.LZ4;
using RePKG.Application.Texture.Helpers;
using RePKG.Core.Texture;

namespace RePKG.Application.Texture
Expand All @@ -7,6 +9,8 @@ public class TexMipmapDecompressor : ITexMipmapDecompressor
{
public void DecompressMipmap(TexMipmap mipmap)
{
if (mipmap == null) throw new ArgumentNullException(nameof(mipmap));

if (mipmap.IsLZ4Compressed)
{
mipmap.Bytes = Lz4Decompress(mipmap.Bytes, mipmap.DecompressedBytesCount);
Expand All @@ -19,15 +23,15 @@ public void DecompressMipmap(TexMipmap mipmap)
switch (mipmap.Format)
{
case MipmapFormat.CompressedDXT5:
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT5);
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXTFlags.DXT5);
mipmap.Format = MipmapFormat.RGBA8888;
break;
case MipmapFormat.CompressedDXT3:
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT3);
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXTFlags.DXT3);
mipmap.Format = MipmapFormat.RGBA8888;
break;
case MipmapFormat.CompressedDXT1:
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT1);
mipmap.Bytes = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXTFlags.DXT1);
mipmap.Format = MipmapFormat.RGBA8888;
break;
}
Expand Down
7 changes: 5 additions & 2 deletions RePKG.Application/Texture/TexReader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using RePKG.Application.Exceptions;
using RePKG.Core.Texture;
Expand All @@ -22,15 +23,17 @@ public TexReader(

public Tex ReadFrom(BinaryReader reader)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));

var tex = new Tex {Magic1 = reader.ReadNString(maxLength: 16)};

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

tex.Magic2 = reader.ReadNString(maxLength: 16);

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

tex.Header = _texHeaderReader.ReadFrom(reader);
tex.ImagesContainer = _texImageContainerReader.ReadFrom(reader, tex.Header.Format);
Expand Down
4 changes: 4 additions & 0 deletions RePKG.Application/Texture/TexToImageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TexToImageConverter
{
public ImageResult ConvertToImage(Tex tex)
{
if (tex == null) throw new ArgumentNullException(nameof(tex));

if (tex.IsGif)
return ConvertToGif(tex);

Expand Down Expand Up @@ -52,6 +54,8 @@ public ImageResult ConvertToImage(Tex tex)

public MipmapFormat GetConvertedFormat(Tex tex)
{
if (tex == null) throw new ArgumentNullException(nameof(tex));

if (tex.IsGif)
return MipmapFormat.ImageGIF;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void WriteTo(BinaryWriter writer, TexFrameInfoContainer frameInfoContaine
break;

default:
throw new UnknownTexFrameInfoContainerMagicException(frameInfoContainer.Magic);
throw new UnknownMagicException(nameof(TexFrameInfoContainerWriter), frameInfoContainer.Magic);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void WriteTo(BinaryWriter writer, TexImageContainer imageContainer)
break;

default:
throw new UnknownTexImageContainerMagicException(imageContainer.Magic);
throw new UnknownMagicException(nameof(TexImageContainerWriter), imageContainer.Magic);
}
}

Expand Down
4 changes: 2 additions & 2 deletions RePKG.Application/Texture/Writer/TexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public void WriteTo(BinaryWriter writer, Tex tex)
if (tex == null) throw new ArgumentNullException(nameof(tex));

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

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

writer.WriteNString(tex.Magic1);
writer.WriteNString(tex.Magic2);
Expand Down
1 change: 1 addition & 0 deletions RePKG.Core/RePKG.Core.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=texture_005Cenums/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=texture_005Chelpers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=texture_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=texture_005Cinterfaces_005Cwriter/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
12 changes: 12 additions & 0 deletions RePKG.Core/Texture/Enums/DXTFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace RePKG.Core.Texture
{
[Flags]
public enum DXTFlags
{
DXT1 = 1,
DXT3 = 1 << 1,
DXT5 = 1 << 2,
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion RePKG.Core/Texture/Tex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Tex
public TexFrameInfoContainer FrameInfoContainer { get; set; }

public bool IsGif => HasFlag(TexFlags.IsGif);
public TexImage FirstImage => ImagesContainer?.Images?.FirstOrDefault();
public TexImage FirstImage => ImagesContainer?.Images.FirstOrDefault();

public bool HasFlag(TexFlags flag)
{
Expand Down
2 changes: 1 addition & 1 deletion RePKG.Core/Texture/TexImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class TexImage
{
public List<TexMipmap> Mipmaps { get; } = new List<TexMipmap>();

public TexMipmap FirstMipmap => Mipmaps?.FirstOrDefault();
public TexMipmap FirstMipmap => Mipmaps.FirstOrDefault();
}
}

0 comments on commit 8da267e

Please sign in to comment.