From f14dd4fe4ea46f06aada670a811c0c4a8a6f0dff Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Sat, 15 Oct 2022 14:03:13 +0200 Subject: [PATCH] Add ModelReader and ModelFile class --- PCK-Studio/Classes/FileTypes/ModelFile.cs | 116 +++++++++++++++++++++ PCK-Studio/Classes/IO/Model/ModelReader.cs | 77 ++++++++++++++ PCK-Studio/PckStudio.csproj | 2 + 3 files changed, 195 insertions(+) create mode 100644 PCK-Studio/Classes/FileTypes/ModelFile.cs create mode 100644 PCK-Studio/Classes/IO/Model/ModelReader.cs diff --git a/PCK-Studio/Classes/FileTypes/ModelFile.cs b/PCK-Studio/Classes/FileTypes/ModelFile.cs new file mode 100644 index 00000000..260407c5 --- /dev/null +++ b/PCK-Studio/Classes/FileTypes/ModelFile.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Documents; + +namespace PckStudio.Classes.FileTypes +{ + [Serializable] + internal class ModelNotFoundException : Exception + { + public ModelNotFoundException() + { + } + + public ModelNotFoundException(string message) : base(message) + { + } + + public ModelNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + + protected ModelNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } + public class ModelFile + { + public List Models { get; } = new List(); + + public void AddModel(Model model) + { + Models.Add(model); + } + + bool Contains(string name) => Models.FindIndex(m => m.name == name) > -1; + + /// + Model GetModelByName(string name) + { + return Contains(name) ? Models.First(m => m.name.Equals(name)) : throw new ModelNotFoundException(nameof(name)); + } + + public struct Model + { + public readonly string name; + public Size textureSize; + public List parts { get; } = new List(); + + public Model(string name, int textureWidth, int textureHeight) + { + this.name = name; + textureSize = new Size(textureWidth, textureHeight); + } + + public struct Part + { + string name; + (float x, float y, float z) position; + (float yaw, float pitch, float roll) rotation; + List Boxes { get; } = new List(); + + public struct Box + { + (float x, float y, float z) Position; + (int width, int height, int length) Size; + float U, V; + float Scale; + bool Mirror; + + public Box((float x, float y, float z) position, + (int width, int height, int length) size, + float u, float v, float scale, bool mirror) + { + Position = position; + Size = size; + U = u; + V = v; + Scale = scale; + Mirror = mirror; + } + } + + public Part(string name, + (float x, float y, float z) pos, + (float yaw, float pitch, float roll) rot) : this(name) + { + position = pos; + rotation = rot; + } + + public Part(string name) + { + this.name = name; + } + + public void AddBox((float x, float y, float z) position, + (int width, int height, int length) size, + float u, float v, float scale, bool mirror) + { + Boxes.Add(new Box(position, size, u, v, scale, mirror)); + } + + } + + public void AddPart(Part part) + { + parts.Add(part); + } + } + } +} diff --git a/PCK-Studio/Classes/IO/Model/ModelReader.cs b/PCK-Studio/Classes/IO/Model/ModelReader.cs new file mode 100644 index 00000000..5c00479d --- /dev/null +++ b/PCK-Studio/Classes/IO/Model/ModelReader.cs @@ -0,0 +1,77 @@ +using PckStudio.Classes.FileTypes; +using System; +using System.Diagnostics; +using System.IO; +using System.Text; + +namespace PckStudio.Classes.IO.Model +{ + public class ModelReader : StreamDataReader + { + public static ModelFile Read(Stream stream, bool useLittleEndian = false) + { + return new ModelReader(useLittleEndian).ReadFromStream(stream); + } + + private ModelReader(bool useLittleEndian) : base(useLittleEndian) + { + } + + private ModelFile ReadFromStream(Stream stream) + { + var modelFile = new ModelFile(); + int version = ReadInt(stream); + int modelCount = ReadInt(stream); + for (; 0 < modelCount; modelCount--) + { + string name = ReadString(stream); + int width = ReadInt(stream); + int height = ReadInt(stream); + var model = new ModelFile.Model(name, width, height); + + int partCount = ReadInt(stream); + for (; 0 < partCount; partCount--) + { + string partname = ReadString(stream); + float x = ReadFloat(stream); + float y = ReadFloat(stream); + float z = ReadFloat(stream); + + float yaw = ReadFloat(stream); + float pitch = ReadFloat(stream); + float roll = ReadFloat(stream); + var part = new ModelFile.Model.Part(partname, (x, y, z), (yaw, pitch, roll)); + if (version > 0) + { + float _1 = ReadFloat(stream); + float _2 = ReadFloat(stream); + float _3 = ReadFloat(stream); + Debug.WriteLine("[{0}]: {1}, {2}, {3}", nameof(ModelReader), _1, _2, _3); + } + + int boxCount = ReadInt(stream); + for (; 0 < boxCount; boxCount--) + { + var pos = (ReadFloat(stream), ReadFloat(stream), ReadFloat(stream)); + var size = (ReadInt(stream), ReadInt(stream), ReadInt(stream)); + float u = ReadFloat(stream), v = ReadFloat(stream); + float scale = ReadFloat(stream); + bool mirrored = ReadBool(stream); + part.AddBox(pos, size, u, v, scale, mirrored); + } + model.AddPart(part); + } + modelFile.AddModel(model); + + } + return modelFile; + } + + private string ReadString(Stream stream) + { + short length = ReadShort(stream); + return ReadString(stream, length, Encoding.ASCII); + } + + } +} diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index a51cd73b..07b6c1ea 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -139,6 +139,7 @@ + @@ -148,6 +149,7 @@ +