diff --git a/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileReader.cs b/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileReader.cs index 056a3006..4714c4b3 100644 --- a/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileReader.cs +++ b/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileReader.cs @@ -9,7 +9,7 @@ using PckStudio.Classes.Utils; namespace PckStudio.Classes.IO.GRF { - internal class GRFFileReader + internal class GRFFileReader : StreamDataReader { internal List TagNames; internal GRFFile _file; @@ -18,6 +18,9 @@ namespace PckStudio.Classes.IO.GRF return new GRFFileReader().read(stream); } + private GRFFileReader() : base(false) + { } + private GRFFile read(Stream stream) { stream = ReadHeader(stream); @@ -144,35 +147,10 @@ namespace PckStudio.Classes.IO.GRF return new ValueTuple(GetTagName(stream), ReadString(stream)); } - internal byte[] ReadBytes(Stream stream, int count) - { - byte[] buffer = new byte[count]; - stream.Read(buffer, 0, count); - return buffer; - } - - internal int ReadInt(Stream stream) - { - byte[] buffer = ReadBytes(stream, 4); - if (BitConverter.IsLittleEndian) - Array.Reverse(buffer); - return BitConverter.ToInt32(buffer, 0); - } - - internal short ReadShort(Stream stream) - { - byte[] buffer = ReadBytes(stream, 2); - if (BitConverter.IsLittleEndian) - Array.Reverse(buffer); - return BitConverter.ToInt16(buffer, 0); - } - internal string ReadString(Stream stream) { short stringLength = ReadShort(stream); - byte[] buffer = ReadBytes(stream, stringLength); - return Encoding.ASCII.GetString(buffer); + return ReadString(stream, stringLength, Encoding.ASCII); } - } } diff --git a/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileWriter.cs b/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileWriter.cs index b69a3d6b..ca979a09 100644 --- a/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileWriter.cs +++ b/MinecraftUSkinEditor/Classes/IO/GRF/GRFFileWriter.cs @@ -5,12 +5,13 @@ using System.IO; using System.Linq; using System.Text; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; -using PckStudio.Classes.Utils; +using PckStudio.Classes; using PckStudio.Classes.Utils.grf; +using PckStudio.Classes.Utils; namespace PckStudio.Classes.IO.GRF { - public class GRFFileWriter + internal class GRFFileWriter : StreamDataWriter { internal readonly GRFFile _grfFile; internal List LUT; @@ -20,7 +21,7 @@ namespace PckStudio.Classes.IO.GRF instance.write(stream); } - private GRFFileWriter(GRFFile grfFile) + private GRFFileWriter(GRFFile grfFile) : base(false) { if (grfFile.IsWorld) throw new NotImplementedException("World grf saving is currently unsupported"); @@ -73,7 +74,7 @@ namespace PckStudio.Classes.IO.GRF return result; } - private byte[] CompressRle(byte[] buffer) => RLE.Encode(buffer).ToArray(); + private byte[] CompressRle(byte[] buffer) => Utils.RLE.Encode(buffer).ToArray(); private void MakeAndWriteCrc(Stream stream, byte[] data) { @@ -151,31 +152,10 @@ namespace PckStudio.Classes.IO.GRF WriteInt(stream, i); } - static internal void WriteInt(Stream stream, int value) - { - byte[] bytes = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - WriteBytes(stream, bytes); - } - - static internal void WriteShort(Stream stream, short value) - { - byte[] bytes = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - WriteBytes(stream, bytes); - } - static internal void WriteString(Stream stream, string s) { WriteShort(stream, (short)s.Length); WriteBytes(stream, Encoding.ASCII.GetBytes(s)); } - - static internal void WriteBytes(Stream stream, byte[] bytes) - { - stream.Write(bytes, 0, bytes.Length); - } } } diff --git a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs index 19baff08..6b8f4974 100644 --- a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs +++ b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.IO; using System.Text; using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; namespace PckStudio.Classes.IO.LOC { - internal class LOCFileReader + internal class LOCFileReader : StreamDataReader { internal LOCFile _file; @@ -15,7 +16,7 @@ namespace PckStudio.Classes.IO.LOC return new LOCFileReader().ReadFile(stream); } - private LOCFileReader() + private LOCFileReader() : base(false) { _file = new LOCFile(); } @@ -70,29 +71,10 @@ namespace PckStudio.Classes.IO.LOC return keys; } - internal short ReadShort(Stream stream) - { - byte[] bytes = new byte[2]; - stream.Read(bytes, 0, bytes.Length); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - return BitConverter.ToInt16(bytes, 0); - } - internal int ReadInt(Stream stream) - { - byte[] bytes = new byte[4]; - stream.Read(bytes, 0, bytes.Length); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - return BitConverter.ToInt32(bytes, 0); - } - internal string ReadString(Stream stream) { int length = ReadShort(stream); - byte[] buffer = new byte[length]; - stream.Read(buffer, 0, length); - return Encoding.UTF8.GetString(buffer, 0, length); + return ReadString(stream, length, Encoding.UTF8); } } } \ No newline at end of file diff --git a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs index d4a9539a..18d85fd2 100644 --- a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs +++ b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs @@ -1,4 +1,5 @@ using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; using System; using System.Collections.Generic; using System.IO; @@ -7,7 +8,7 @@ using System.Text; namespace PckStudio.Classes.IO.LOC { - internal class LOCFileWriter + internal class LOCFileWriter : StreamDataWriter { internal LOCFile _locfile; public static void Write(Stream stream, LOCFile file, int type = 2) @@ -15,7 +16,7 @@ namespace PckStudio.Classes.IO.LOC new LOCFileWriter(file).WriteToStream(stream, type); } - private LOCFileWriter(LOCFile file) + private LOCFileWriter(LOCFile file) : base(false) { _locfile = file; } @@ -64,26 +65,10 @@ namespace PckStudio.Classes.IO.LOC } } - internal void WriteShort(Stream stream, short value) - { - byte[] bytes = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - stream.Write(bytes, 0, 2); - } - internal void WriteInt(Stream stream, int value) - { - byte[] bytes = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - stream.Write(bytes, 0, 4); - } - internal void WriteString(Stream stream, string s) { WriteShort(stream, (short)s.Length); - byte[] buffer = Encoding.UTF8.GetBytes(s); - stream.Write(buffer, 0, s.Length); + WriteString(stream, s, Encoding.UTF8); } } } diff --git a/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileReader.cs b/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileReader.cs index 55294119..a7b3bf88 100644 --- a/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileReader.cs +++ b/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileReader.cs @@ -1,4 +1,5 @@ using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; using System; using System.Collections.Generic; using System.IO; @@ -7,26 +8,24 @@ using System.Windows; namespace PckStudio.Classes.IO { - internal class PCKFileReader + internal class PCKFileReader : StreamDataReader { - internal bool isLittleEndian = false; internal PCKFile _file; - public static PCKFile Read(Stream s, bool isLittleEndian) + public static PCKFile Read(Stream stream, bool isLittleEndian) { - return new PCKFileReader(isLittleEndian).ReadFileFromStream(s); + return new PCKFileReader(isLittleEndian).ReadFileFromStream(stream); } - private PCKFileReader(bool isLittleEndian) + private PCKFileReader(bool isLittleEndian) : base(isLittleEndian) { - this.isLittleEndian = isLittleEndian; } - private PCKFile ReadFileFromStream(Stream s) + private PCKFile ReadFileFromStream(Stream stream) { - _file = new PCKFile(ReadInt(s)); - ReadMetaData(s); - ReadFileEntries(s); + _file = new PCKFile(ReadInt(stream)); + ReadMetaData(stream); + ReadFileEntries(stream); return _file; } @@ -71,23 +70,12 @@ namespace PckStudio.Classes.IO stream.Read(file_entry.data, 0, file_entry.size); } } - - internal int ReadInt(Stream stream) - { - byte[] buffer = new byte[4]; - stream.Read(buffer, 0, 4); - if (BitConverter.IsLittleEndian && !isLittleEndian) - Array.Reverse(buffer); - return BitConverter.ToInt32(buffer, 0); - } - internal string ReadString(Stream stream) { int len = ReadInt(stream); - byte[] stringBuffer = new byte[len * 2]; - stream.Read(stringBuffer, 0, len * 2); - ReadInt(stream); - return Encoding.BigEndianUnicode.GetString(stringBuffer, 0, len * 2); + string s = ReadString(stream, len * 2, Encoding.BigEndianUnicode); + ReadInt(stream); // padding + return s; } } } diff --git a/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileWriter.cs b/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileWriter.cs index 11c7810f..f4505115 100644 --- a/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileWriter.cs +++ b/MinecraftUSkinEditor/Classes/IO/PCK/PCKFileWriter.cs @@ -1,4 +1,5 @@ using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; using System; using System.Collections.Generic; using System.IO; @@ -8,9 +9,8 @@ using System.Threading.Tasks; namespace PckStudio.Classes.IO { - internal class PCKFileWriter + internal class PCKFileWriter : StreamDataWriter { - internal bool isLittleEndian = false; internal PCKFile _file; public static void Write(Stream stream, PCKFile file, bool isLittleEndian) @@ -18,10 +18,9 @@ namespace PckStudio.Classes.IO new PCKFileWriter(file, isLittleEndian).WriteToStream(stream); } - private PCKFileWriter(PCKFile file, bool isLittleEndian) + private PCKFileWriter(PCKFile file, bool isLittleEndian) : base(isLittleEndian) { _file = file; - this.isLittleEndian = isLittleEndian; } private void WriteToStream(Stream stream) @@ -31,20 +30,11 @@ namespace PckStudio.Classes.IO WriteFileEntries(stream); } - internal void WriteInt(Stream stream, int value) - { - byte[] buffer = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian && !isLittleEndian) - Array.Reverse(buffer); - stream.Write(buffer, 0, 4); - } - internal void WriteString(Stream stream, string s) { WriteInt(stream, s.Length); - byte[] byteString = Encoding.BigEndianUnicode.GetBytes(s); - stream.Write(byteString, 0, byteString.Length); - WriteInt(stream, 0); + WriteString(stream, s, Encoding.BigEndianUnicode); + WriteInt(stream, 0); // padding } internal void WriteMetaEntries(Stream stream) diff --git a/MinecraftUSkinEditor/Classes/Utils/StreamDataReader.cs b/MinecraftUSkinEditor/Classes/Utils/StreamDataReader.cs new file mode 100644 index 00000000..41209787 --- /dev/null +++ b/MinecraftUSkinEditor/Classes/Utils/StreamDataReader.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PckStudio.Classes.Utils +{ + public class StreamDataReader + { + private static bool isLittleEndian; + protected StreamDataReader(bool useLittleEndian) + { + isLittleEndian = useLittleEndian; + } + + protected static string ReadString(Stream stream, int length, Encoding encoding) + { + byte[] buffer = ReadBytes(stream, length); + return encoding.GetString(buffer); + } + + protected static byte[] ReadBytes(Stream stream, int count) + { + byte[] buffer = new byte[count]; + stream.Read(buffer, 0, count); + return buffer; + } + + protected static ushort ReadUShort(Stream stream) => (ushort)ReadShort(stream); + protected static short ReadShort(Stream stream) + { + byte[] bytes = ReadBytes(stream, 2); + if (BitConverter.IsLittleEndian && !isLittleEndian) + Array.Reverse(bytes); + return BitConverter.ToInt16(bytes, 0); + } + + protected static uint ReadUInt(Stream stream) => (uint)ReadInt(stream); + protected static int ReadInt(Stream stream) + { + byte[] buffer = ReadBytes(stream, 4); + if (BitConverter.IsLittleEndian && !isLittleEndian) + Array.Reverse(buffer); + return BitConverter.ToInt32(buffer, 0); + } + + protected static ulong ReadULong(Stream stream) => (ulong)ReadLong(stream); + protected static long ReadLong(Stream stream) + { + byte[] buffer = ReadBytes(stream, 8); + if (BitConverter.IsLittleEndian && !isLittleEndian) + Array.Reverse(buffer); + return BitConverter.ToInt64(buffer, 0); + } + } +} diff --git a/MinecraftUSkinEditor/Classes/Utils/StreamDataWriter.cs b/MinecraftUSkinEditor/Classes/Utils/StreamDataWriter.cs new file mode 100644 index 00000000..58315025 --- /dev/null +++ b/MinecraftUSkinEditor/Classes/Utils/StreamDataWriter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PckStudio.Classes.Utils +{ + internal class StreamDataWriter + { + private static bool useLittleEndian; + + protected StreamDataWriter(bool isLittleEndian) + { + useLittleEndian = isLittleEndian; + } + + protected static void WriteUShort(Stream stream, ushort value) => WriteShort(stream, (short)value); + + protected static void WriteShort(Stream stream, short value) + { + byte[] bytes = BitConverter.GetBytes(value); + if (BitConverter.IsLittleEndian && !useLittleEndian) + Array.Reverse(bytes); + WriteBytes(stream, bytes, 2); + } + + protected static void WriteUInt(Stream stream, uint value) => WriteInt(stream, (int)value); + protected static void WriteInt(Stream stream, int value) + { + byte[] buffer = BitConverter.GetBytes(value); + if (BitConverter.IsLittleEndian && !useLittleEndian) + Array.Reverse(buffer); + WriteBytes(stream, buffer, 4); + } + + protected static void WriteULong(Stream stream, ulong value) => WriteLong(stream, (long)value); + protected static void WriteLong(Stream stream, long value) + { + byte[] buffer = BitConverter.GetBytes(value); + if (BitConverter.IsLittleEndian && !useLittleEndian) + Array.Reverse(buffer); + WriteBytes(stream, buffer, 8); + } + + protected static void WriteString(Stream stream, string s, Encoding encoding) + => WriteBytes(stream, encoding.GetBytes(s)); + + protected static void WriteBytes(Stream stream, byte[] bytes) => WriteBytes(stream, bytes, bytes.Length); + protected static void WriteBytes(Stream stream, byte[] bytes, int count) + { + stream.Write(bytes, 0, count); + } + + } +} diff --git a/MinecraftUSkinEditor/PckStudio.csproj b/MinecraftUSkinEditor/PckStudio.csproj index acac8923..988d3dc6 100644 --- a/MinecraftUSkinEditor/PckStudio.csproj +++ b/MinecraftUSkinEditor/PckStudio.csproj @@ -154,6 +154,8 @@ + +