diff --git a/MinecraftUSkinEditor/Classes/FileTypes/COLFile.cs b/MinecraftUSkinEditor/Classes/FileTypes/COLFile.cs index 822cfbb7..3259da6e 100644 --- a/MinecraftUSkinEditor/Classes/FileTypes/COLFile.cs +++ b/MinecraftUSkinEditor/Classes/FileTypes/COLFile.cs @@ -11,112 +11,29 @@ namespace PckStudio.Classes.FileTypes { public class ColorEntry { - public string name; + public readonly string name; public uint color; + + public ColorEntry(string name, uint color) + { + this.name = name; + this.color = color; + } } + public class ExtendedColorEntry : ColorEntry { public uint rgbcolor; public uint unk; + + public ExtendedColorEntry(string name, uint color, uint rgbcolor, uint unk) : base(name, color) + { + this.rgbcolor = rgbcolor; + this.unk = unk; + } } public List entries = new List(); public List waterEntries = new List(); - public void Open(Stream stream) - { - int has_water_colors = ReadInt(stream); - int color_entries = ReadInt(stream); - for (int i = 0; i < color_entries; i++) - { - ColorEntry entry = new ColorEntry(); - entry.name = ReadString(stream); - entry.color = ReadUint(stream); - entries.Add(entry); - } - if (has_water_colors == 1) - { - int water_color_entries = ReadInt(stream); - for (int i = 0; i < water_color_entries; i++) - { - ExtendedColorEntry entry = new ExtendedColorEntry(); - entry.name = ReadString(stream); - entry.color = ReadUint(stream); - entry.rgbcolor = ReadUint(stream); - entry.unk = ReadUint(stream); - waterEntries.Add(entry); - } - } - } - - - private string ReadString(Stream stream) - { - byte[] bytes = new byte[2]; - stream.Read(bytes, 0, 2); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - short strlen = BitConverter.ToInt16(bytes, 0); - byte[] stringBuffer = new byte[strlen]; - stream.Read(stringBuffer, 0, strlen); - return Encoding.UTF8.GetString(stringBuffer, 0, strlen); - } - - private int ReadInt(Stream stream) - { - return (int)ReadUint(stream); - } - - private uint ReadUint(Stream stream) - { - byte[] bytes = new byte[4]; - stream.Read(bytes, 0, 4); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - return BitConverter.ToUInt32(bytes, 0); - } - - private void WriteUint(Stream stream, uint value) - { - byte[] bytes = BitConverter.GetBytes(value); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - stream.Write(bytes, 0, 4); - } - private void WriteInt(Stream stream, int value) - { - WriteUint(stream, (uint)value); - } - - private void WriteString(Stream stream, string s) - { - byte[] bytes = BitConverter.GetBytes((short)s.Length); - if (BitConverter.IsLittleEndian) - Array.Reverse(bytes); - byte[] stringBuffer = Encoding.UTF8.GetBytes(s); - stream.Write(bytes, 0, 2); - stream.Write(stringBuffer, 0, s.Length); - } - - public void Save(Stream stream) - { - WriteInt(stream, Convert.ToInt32(waterEntries.Count > 0)); - WriteInt(stream, entries.Count); - foreach (var colorEntry in entries) - { - WriteString(stream, colorEntry.name); - WriteUint(stream, colorEntry.color); - } - if (waterEntries.Count > 0) - { - WriteInt(stream, waterEntries.Count); - foreach (var colorEntry in waterEntries) - { - WriteString(stream, colorEntry.name); - WriteUint(stream, colorEntry.color); - WriteUint(stream, colorEntry.rgbcolor); - WriteUint(stream, colorEntry.unk); - } - } - } } } diff --git a/MinecraftUSkinEditor/Classes/IO/COL/COLFileReader.cs b/MinecraftUSkinEditor/Classes/IO/COL/COLFileReader.cs new file mode 100644 index 00000000..696a60f0 --- /dev/null +++ b/MinecraftUSkinEditor/Classes/IO/COL/COLFileReader.cs @@ -0,0 +1,51 @@ +using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; +using System.IO; +using System.Text; +using static PckStudio.Classes.FileTypes.COLFile; + +namespace PckStudio.Classes.IO.COL +{ + internal class COLFileReader : StreamDataReader + { + public static COLFile Read(Stream stream) + { + return new COLFileReader().ReadFromStream(stream); + } + + private COLFileReader() : base(false) + {} + + private COLFile ReadFromStream(Stream stream) + { + COLFile colourFile = new COLFile(); + int has_water_colors = ReadInt(stream); + int color_entries = ReadInt(stream); + for (int i = 0; i < color_entries; i++) + { + string name = ReadString(stream); + uint color = ReadUInt(stream); + colourFile.entries.Add(new ColorEntry(name, color)); + } + if (has_water_colors > 0) + { + int water_color_entries = ReadInt(stream); + for (int i = 0; i < water_color_entries; i++) + { + string name = ReadString(stream); + uint color = ReadUInt(stream); + uint rgbcolor = ReadUInt(stream); + uint unk = ReadUInt(stream); + colourFile.waterEntries.Add(new ExtendedColorEntry(name, color, rgbcolor, unk)); + } + } + return colourFile; + } + + private string ReadString(Stream stream) + { + short strlen = ReadShort(stream); + return ReadString(stream, strlen, Encoding.ASCII); + } + } +} diff --git a/MinecraftUSkinEditor/Classes/IO/COL/COLFileWriter.cs b/MinecraftUSkinEditor/Classes/IO/COL/COLFileWriter.cs new file mode 100644 index 00000000..4e4bce3b --- /dev/null +++ b/MinecraftUSkinEditor/Classes/IO/COL/COLFileWriter.cs @@ -0,0 +1,47 @@ +using PckStudio.Classes.FileTypes; +using PckStudio.Classes.Utils; +using System; +using System.IO; +using System.Text; + +namespace PckStudio.Classes.IO.COL +{ + internal class COLFileWriter : StreamDataWriter + { + public static void Write(Stream stream, COLFile file) + { + new COLFileWriter().WriteToStream(stream, file); + } + + private COLFileWriter() : base(false) + {} + + private void WriteToStream(Stream stream, COLFile colourFile) + { + WriteInt(stream, Convert.ToInt32(colourFile.waterEntries.Count > 0)); + WriteInt(stream, colourFile.entries.Count); + foreach (var colorEntry in colourFile.entries) + { + WriteString(stream, colorEntry.name); + WriteUInt(stream, colorEntry.color); + } + if (colourFile.waterEntries.Count > 0) + { + WriteInt(stream, colourFile.waterEntries.Count); + foreach (var colorEntry in colourFile.waterEntries) + { + WriteString(stream, colorEntry.name); + WriteUInt(stream, colorEntry.color); + WriteUInt(stream, colorEntry.rgbcolor); + WriteUInt(stream, colorEntry.unk); + } + } + } + + private void WriteString(Stream stream, string s) + { + WriteShort(stream, (short)s.Length); + WriteString(stream, s, Encoding.ASCII); + } + } +} diff --git a/MinecraftUSkinEditor/Forms/Editor/COLEditor.cs b/MinecraftUSkinEditor/Forms/Editor/COLEditor.cs index 7316a535..d3ac2e99 100644 --- a/MinecraftUSkinEditor/Forms/Editor/COLEditor.cs +++ b/MinecraftUSkinEditor/Forms/Editor/COLEditor.cs @@ -10,25 +10,33 @@ using System.Threading.Tasks; using System.Windows.Forms; using MetroFramework.Forms; using PckStudio.Classes.FileTypes; +using PckStudio.Classes.IO.COL; namespace PckStudio.Forms.Editor { public partial class COLEditor : MetroForm { - COLFile cf = new COLFile(); - public byte[] data { get; private set; } = new byte[0]; + COLFile colurfile; - public COLEditor(COLFile colFile) + private readonly PCKFile.FileData _file; + + public COLEditor(PCKFile.FileData file) { InitializeComponent(); - cf = colFile; - foreach (var obj in cf.entries) + _file = file; + + using(var stream = new MemoryStream(file.data)) + { + colurfile = COLFileReader.Read(stream); + } + + foreach (var obj in colurfile.entries) { TreeNode tn = new TreeNode(obj.name); tn.Tag = obj; treeView1.Nodes.Add(tn); } - foreach (var obj in cf.waterEntries) + foreach (var obj in colurfile.waterEntries) { TreeNode tn = new TreeNode(obj.name); tn.Tag = obj; @@ -71,8 +79,8 @@ namespace PckStudio.Forms.Editor { using (var stream = new MemoryStream()) { - cf.Save(stream); - data = stream.ToArray(); + COLFileWriter.Write(stream, colurfile); + _file.SetData(stream.ToArray()); } } @@ -100,9 +108,9 @@ namespace PckStudio.Forms.Editor public void treeView1_KeyDown(object sender, KeyEventArgs e) { var node = treeView1.SelectedNode; - if (e.KeyCode == Keys.Delete && node.Tag is COLFile.ColorEntry) + if (e.KeyCode == Keys.Delete && node.Tag is COLFile.ColorEntry colorInfo) { - cf.entries.Remove((COLFile.ColorEntry)node.Tag); + colurfile.entries.Remove(colorInfo); if (treeView1.Nodes.Count > 0) treeView1.Nodes.Remove(node); } } @@ -112,7 +120,7 @@ namespace PckStudio.Forms.Editor var node = treeView2.SelectedNode; if (e.KeyCode == Keys.Delete && node.Tag is COLFile.ExtendedColorEntry) { - cf.waterEntries.Remove((COLFile.ExtendedColorEntry)node.Tag); + colurfile.waterEntries.Remove((COLFile.ExtendedColorEntry)node.Tag); if (treeView2.Nodes.Count > 0) treeView2.Nodes.Remove(node); } } diff --git a/MinecraftUSkinEditor/MainForm.cs b/MinecraftUSkinEditor/MainForm.cs index 26f94ea5..008ed510 100644 --- a/MinecraftUSkinEditor/MainForm.cs +++ b/MinecraftUSkinEditor/MainForm.cs @@ -623,14 +623,8 @@ namespace PckStudio MessageBoxIcon.Error); return; } - COLFile colFile = new COLFile(); - using (var stream = new MemoryStream(file.data)) - { - colFile.Open(stream); - } - COLEditor diag = new COLEditor(colFile); - if (diag.ShowDialog(this) == DialogResult.OK && diag.data.Length > 0) - file.SetData(diag.data); + COLEditor diag = new COLEditor(file); + diag.ShowDialog(this); diag.Dispose(); break; } @@ -2802,14 +2796,8 @@ namespace PckStudio // Checks to see if selected minefile is a col file if (file.type == 9 && file.filepath == "colours.col") // .col file { - COLFile colFile = new COLFile(); - using (var stream = new MemoryStream(file.data)) - { - colFile.Open(stream); - } - using (COLEditor diag = new COLEditor(colFile)) - if (diag.ShowDialog(this) == DialogResult.OK && diag.data.Length > 0) - file.SetData(diag.data); + using COLEditor diag = new COLEditor(file); + diag.ShowDialog(this); } } diff --git a/MinecraftUSkinEditor/PckStudio.csproj b/MinecraftUSkinEditor/PckStudio.csproj index b5a97d46..1a5b8732 100644 --- a/MinecraftUSkinEditor/PckStudio.csproj +++ b/MinecraftUSkinEditor/PckStudio.csproj @@ -144,6 +144,8 @@ + +