1
0
mirror of synced 2024-12-02 11:07:26 +01:00
Switch-Toolbox/Switch_FileFormatsMain/FileFormats/GFBMDL/GFBMDL.cs

310 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-06-13 23:31:35 +02:00
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Switch_Toolbox;
using System.Windows.Forms;
using Switch_Toolbox.Library;
2019-06-13 23:31:35 +02:00
using Switch_Toolbox.Library.IO;
2019-06-20 23:20:06 +02:00
using Switch_Toolbox.Library.Forms;
namespace FirstPlugin
{
public class GFBMDL : TreeNodeFile, IFileFormat
{
public FileType FileType { get; set; } = FileType.Model;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Graphic Model" };
public string[] Extension { get; set; } = new string[] { "*.gfbmdl" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
using (var reader = new Switch_Toolbox.Library.IO.FileReader(stream, true))
{
reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
bool IsMatch = reader.ReadUInt32() == 0x20000000;
reader.Position = 0;
return IsMatch;
}
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
2019-06-20 23:20:06 +02:00
Viewport viewport
{
get
{
var editor = LibraryGUI.Instance.GetObjectEditor();
return editor.GetViewport();
}
set
{
var editor = LibraryGUI.Instance.GetObjectEditor();
editor.LoadViewport(value);
}
}
bool DrawablesLoaded = false;
public override void OnClick(TreeView treeView)
{
if (Runtime.UseOpenGL)
{
if (viewport == null)
{
viewport = new Viewport(ObjectEditor.GetDrawableContainers());
viewport.Dock = DockStyle.Fill;
}
if (!DrawablesLoaded)
{
ObjectEditor.AddContainer(DrawableContainer);
DrawablesLoaded = true;
}
viewport.ReloadDrawables(DrawableContainer);
LibraryGUI.Instance.LoadEditor(viewport);
viewport.Text = Text;
}
}
2019-06-13 23:31:35 +02:00
public Header header;
2019-06-20 23:20:06 +02:00
public DrawableContainer DrawableContainer = new DrawableContainer();
2019-06-13 23:31:35 +02:00
public void Load(System.IO.Stream stream)
{
2019-06-21 00:01:06 +02:00
Text = FileName;
2019-06-20 23:20:06 +02:00
DrawableContainer.Name = FileName;
2019-06-13 23:31:35 +02:00
header = new Header();
2019-06-21 00:01:06 +02:00
header.Read(new FileReader(stream), this);
2019-06-20 23:20:06 +02:00
}
public void Unload()
{
}
public byte[] Save()
{
2019-06-13 23:31:35 +02:00
var mem = new System.IO.MemoryStream();
header.Write(new FileWriter(mem));
return mem.ToArray();
}
2019-06-13 23:31:35 +02:00
public class Header
{
2019-06-20 23:20:06 +02:00
public STSkeleton Skeleton { get; set; }
2019-06-13 23:31:35 +02:00
public uint Version { get; set; }
public float[] Boundings { get; set; }
public List<string> TextureMaps = new List<string>();
public List<string> Materials = new List<string>();
2019-06-21 00:01:06 +02:00
public void Read(FileReader reader, GFBMDL Root)
2019-06-13 23:31:35 +02:00
{
2019-06-20 23:20:06 +02:00
Skeleton = new STSkeleton();
2019-06-21 00:01:06 +02:00
Root.DrawableContainer.Drawables.Add(Skeleton);
2019-06-20 23:20:06 +02:00
2019-06-13 23:31:35 +02:00
reader.SetByteOrder(false);
Version = reader.ReadUInt32();
Boundings = reader.ReadSingles(9);
long TextureOffset = reader.ReadOffset(true, typeof(uint));
long MaterialOffset = reader.ReadOffset(true, typeof(uint));
long UnknownOffset = reader.ReadOffset(true, typeof(uint));
long Unknown2Offset = reader.ReadOffset(true, typeof(uint));
long ShaderOffset = reader.ReadOffset(true, typeof(uint));
long VisGroupOffset = reader.ReadOffset(true, typeof(uint));
2019-06-20 23:20:06 +02:00
long VertexDataOffset = reader.ReadOffset(true, typeof(uint));
2019-06-13 23:31:35 +02:00
long BoneDataOffset = reader.ReadOffset(true, typeof(uint));
2019-06-13 23:31:35 +02:00
if (TextureOffset != 0)
{
reader.Seek(TextureOffset, SeekOrigin.Begin);
uint Count = reader.ReadUInt32();
TextureMaps = reader.ReadNameOffsets(Count, true, typeof(uint), true);
}
if (MaterialOffset != 0)
{
reader.Seek(MaterialOffset, SeekOrigin.Begin);
uint Count = reader.ReadUInt32();
Materials = reader.ReadNameOffsets(Count, true, typeof(uint));
}
2019-06-20 23:20:06 +02:00
if (BoneDataOffset != 0)
{
2019-06-21 00:01:06 +02:00
Root.Nodes.Add("Skeleton");
2019-06-20 23:20:06 +02:00
reader.Seek(BoneDataOffset, SeekOrigin.Begin);
uint Count = reader.ReadUInt32();
for (int i = 0; i < Count; i++)
{
var bone = new Bone(Skeleton);
bone.Read(reader);
Skeleton.bones.Add(bone);
}
2019-06-21 00:01:06 +02:00
foreach (var bone in Skeleton.bones)
{
if (bone.Parent == null)
Root.Nodes[0].Nodes.Add(bone);
}
2019-06-20 23:20:06 +02:00
}
2019-06-21 00:01:06 +02:00
Skeleton.update();
Skeleton.reset();
2019-06-13 23:31:35 +02:00
}
public void Write(FileWriter writer)
{
writer.Write(Version);
}
}
2019-06-20 23:20:06 +02:00
public class Bone : STBone
{
internal BoneInfo BoneInfo { get; set; }
public Bone(STSkeleton skeleton) : base(skeleton) { }
public void Read(FileReader reader)
{
long DataPosition = reader.Position;
var BoneDataOffset = reader.ReadOffset(true, typeof(uint));
reader.SeekBegin(BoneDataOffset);
long InfoPosition = reader.Position;
2019-06-21 00:01:06 +02:00
int BoneInfoOffset = reader.ReadInt32();
2019-06-20 23:20:06 +02:00
//Read the info section for position data
reader.SeekBegin(InfoPosition - BoneInfoOffset);
BoneInfo = new BoneInfo();
BoneInfo.Read(reader);
2019-06-21 00:01:06 +02:00
RotationType = BoneRotationType.Euler;
Checked = true;
2019-06-20 23:20:06 +02:00
if (BoneInfo.NamePosition != 0)
{
2019-06-21 00:01:06 +02:00
reader.SeekBegin(InfoPosition + BoneInfo.NamePosition);
2019-06-20 23:20:06 +02:00
uint NameLength = reader.ReadUInt32();
Text = reader.ReadString((int)NameLength);
}
if (BoneInfo.RotationPosition != 0)
{
2019-06-21 00:01:06 +02:00
reader.SeekBegin(InfoPosition + BoneInfo.RotationPosition);
2019-06-20 23:20:06 +02:00
float RotationX = reader.ReadSingle();
float RotationY = reader.ReadSingle();
float RotationZ = reader.ReadSingle();
rotation = new float[] { RotationX,RotationY, RotationZ };
2019-06-21 00:01:06 +02:00
Console.WriteLine($"RotationX {RotationX} RotationY {RotationY} RotationX {RotationX} ");
2019-06-20 23:20:06 +02:00
}
if (BoneInfo.TranslationPosition != 0)
{
2019-06-21 00:01:06 +02:00
reader.SeekBegin(InfoPosition + BoneInfo.TranslationPosition);
2019-06-20 23:20:06 +02:00
float TranslateX = reader.ReadSingle();
float TranslateY = reader.ReadSingle();
float TranslateZ = reader.ReadSingle();
position = new float[] { TranslateX, TranslateY, TranslateZ };
2019-06-21 00:01:06 +02:00
Console.WriteLine($"TranslateX {TranslateX} TranslateY {TranslateY} TranslateZ {TranslateZ} ");
2019-06-20 23:20:06 +02:00
}
if (BoneInfo.ScalePosition != 0)
{
2019-06-21 00:01:06 +02:00
reader.SeekBegin(InfoPosition + BoneInfo.ScalePosition);
2019-06-20 23:20:06 +02:00
float ScaleX = reader.ReadSingle();
float ScaleY = reader.ReadSingle();
float ScaleZ = reader.ReadSingle();
scale = new float[] { ScaleX, ScaleY, ScaleZ };
2019-06-21 00:01:06 +02:00
Console.WriteLine($"ScaleX {ScaleX} ScaleY {ScaleX} ScaleZ {ScaleX} ");
2019-06-20 23:20:06 +02:00
}
if (BoneInfo.ParentPosition != 0)
{
2019-06-21 00:01:06 +02:00
reader.SeekBegin(InfoPosition + BoneInfo.ParentPosition);
2019-06-20 23:20:06 +02:00
parentIndex = reader.ReadInt32();
}
2019-06-21 00:01:06 +02:00
Console.WriteLine($"parentIndex {parentIndex} {Text}");
2019-06-20 23:20:06 +02:00
//Seek back to next bone in array
reader.SeekBegin(DataPosition + sizeof(uint));
}
}
//A section that stores position info for bone data
public class BoneInfo
2019-06-13 23:31:35 +02:00
{
2019-06-20 23:20:06 +02:00
internal ushort SectionSize { get; set; }
internal ushort NamePosition { get; set; }
internal ushort UnknownPosition { get; set; }
internal ushort Unknown2Position { get; set; }
internal ushort ParentPosition { get; set; }
internal ushort Unknown3Position { get; set; }
2019-06-21 00:01:06 +02:00
internal ushort IsVisiblePosition { get; set; }
2019-06-20 23:20:06 +02:00
internal ushort ScalePosition { get; set; }
internal ushort RotationPosition { get; set; }
internal ushort TranslationPosition { get; set; }
internal ushort Unknown4Position { get; set; }
internal ushort Unknown5Position { get; set; }
2019-06-13 23:31:35 +02:00
2019-06-20 23:20:06 +02:00
public void Read(FileReader reader)
{
SectionSize = reader.ReadUInt16();
NamePosition = reader.ReadUInt16();
UnknownPosition = reader.ReadUInt16();
Unknown2Position = reader.ReadUInt16();
ParentPosition = reader.ReadUInt16();
Unknown3Position = reader.ReadUInt16(); //Padding
2019-06-21 00:01:06 +02:00
IsVisiblePosition = reader.ReadUInt16(); //Points to byte. 0 or 1 for visibilty
2019-06-20 23:20:06 +02:00
ScalePosition = reader.ReadUInt16();
RotationPosition = reader.ReadUInt16();
TranslationPosition = reader.ReadUInt16();
Unknown4Position = reader.ReadUInt16(); //Padding
Unknown5Position = reader.ReadUInt16(); //Padding
2019-06-21 00:01:06 +02:00
/* Console.WriteLine($"SectionSize {SectionSize}");
Console.WriteLine($"NamePosition {NamePosition}");
Console.WriteLine($"UnknownPosition {UnknownPosition}");
Console.WriteLine($"Unknown2Position {Unknown2Position}");
Console.WriteLine($"ParentPosition {ParentPosition}");
Console.WriteLine($"Unknown3Position {Unknown3Position}");
Console.WriteLine($"IsVisiblePosition {IsVisiblePosition}");
Console.WriteLine($"ScalePosition {ScalePosition}");
Console.WriteLine($"RotationPosition {RotationPosition}");
Console.WriteLine($"TranslationPosition {TranslationPosition}");
Console.WriteLine($"Unknown4Position {Unknown4Position}");
Console.WriteLine($"Unknown5Position {Unknown5Position}");*/
2019-06-20 23:20:06 +02:00
}
2019-06-13 23:31:35 +02:00
}
public class Material
{
}
}
}