Add ModelReader and ModelFile class

This commit is contained in:
miku-666
2022-10-15 14:03:13 +02:00
parent 67a17d4d6e
commit f14dd4fe4e
3 changed files with 195 additions and 0 deletions

View File

@@ -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<Model> Models { get; } = new List<Model>();
public void AddModel(Model model)
{
Models.Add(model);
}
bool Contains(string name) => Models.FindIndex(m => m.name == name) > -1;
/// <exception cref="ModelNotFoundException"></exception>
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<Part> parts { get; } = new List<Part>();
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<Box> Boxes { get; } = new List<Box>();
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);
}
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -139,6 +139,7 @@
<Compile Include="Classes\API\PCKCenter\SaveLocalJSON.cs" />
<Compile Include="Classes\FileTypes\ARCFile.cs" />
<Compile Include="Classes\FileTypes\CSMBFile.cs" />
<Compile Include="Classes\FileTypes\ModelFile.cs" />
<Compile Include="Classes\FileTypes\PCKAudioFile.cs" />
<Compile Include="Classes\FileTypes\Bink.cs" />
<Compile Include="Classes\FileTypes\COLFile.cs" />
@@ -148,6 +149,7 @@
<Compile Include="Classes\IO\ARC\ARCFileReader.cs" />
<Compile Include="Classes\IO\CSMB\CSMBFileReader.cs" />
<Compile Include="Classes\IO\CSMB\CSMBFileWriter.cs" />
<Compile Include="Classes\IO\Model\ModelReader.cs" />
<Compile Include="Classes\IO\PCK\PCKAudioFileReader.cs" />
<Compile Include="Classes\IO\PCK\PCKAudioFileWriter.cs" />
<Compile Include="Classes\IO\COL\COLFileReader.cs" />