1
0
mirror of synced 2024-12-03 11:37:21 +01:00
Switch-Toolbox/Switch_FileFormatsMain/FileFormats/GFBMDL/GFBMDL.cs

277 lines
9.6 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-20 23:20:06 +02:00
DrawableContainer.Name = FileName;
2019-06-13 23:31:35 +02:00
header = new Header();
header.Read(new FileReader(stream));
2019-06-20 23:20:06 +02:00
DrawableContainer.Drawables.Add(header.Skeleton);
}
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>();
public void Read(FileReader reader)
{
2019-06-20 23:20:06 +02:00
Skeleton = new STSkeleton();
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)
{
reader.Seek(BoneDataOffset, SeekOrigin.Begin);
uint Count = reader.ReadUInt32();
Console.WriteLine($"BoneCount {Count}");
for (int i = 0; i < Count; i++)
{
var bone = new Bone(Skeleton);
bone.Read(reader);
Skeleton.bones.Add(bone);
}
}
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;
uint BoneInfoOffset = reader.ReadUInt32();
//Read the info section for position data
reader.SeekBegin(InfoPosition - BoneInfoOffset);
BoneInfo = new BoneInfo();
BoneInfo.Read(reader);
if (BoneInfo.NamePosition != 0)
{
reader.SeekBegin(DataPosition + BoneInfo.NamePosition);
uint NameLength = reader.ReadUInt32();
Text = reader.ReadString((int)NameLength);
}
if (BoneInfo.RotationPosition != 0)
{
reader.SeekBegin(DataPosition + BoneInfo.RotationPosition);
float RotationX = reader.ReadSingle();
float RotationY = reader.ReadSingle();
float RotationZ = reader.ReadSingle();
rotation = new float[] { RotationX,RotationY, RotationZ };
}
if (BoneInfo.TranslationPosition != 0)
{
reader.SeekBegin(DataPosition + BoneInfo.RotationPosition);
float TranslateX = reader.ReadSingle();
float TranslateY = reader.ReadSingle();
float TranslateZ = reader.ReadSingle();
position = new float[] { TranslateX, TranslateY, TranslateZ };
}
if (BoneInfo.ScalePosition != 0)
{
reader.SeekBegin(DataPosition + BoneInfo.ScalePosition);
float ScaleX = reader.ReadSingle();
float ScaleY = reader.ReadSingle();
float ScaleZ = reader.ReadSingle();
scale = new float[] { ScaleX, ScaleY, ScaleZ };
}
if (BoneInfo.ParentPosition != 0)
{
reader.SeekBegin(DataPosition + BoneInfo.ParentPosition);
parentIndex = reader.ReadInt32();
}
Console.WriteLine("BONE " + Text);
//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; }
internal ushort IsVisablePosition { get; set; }
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
IsVisablePosition = reader.ReadUInt16(); //Points to byte. 0 or 1 for visibilty
ScalePosition = reader.ReadUInt16();
RotationPosition = reader.ReadUInt16();
TranslationPosition = reader.ReadUInt16();
Unknown4Position = reader.ReadUInt16(); //Padding
Unknown5Position = reader.ReadUInt16(); //Padding
}
2019-06-13 23:31:35 +02:00
}
public class Material
{
}
}
}