Skip to content

Commit

Permalink
add basic model parsing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
krogenth committed Aug 1, 2023
1 parent b1b928e commit 8f26572
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/G2DataGUI.Common/Data/Maps/Maps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Threading.Tasks;
using G2DataGUI.Common.Data.Errors;
using G2DataGUI.Common.Data.Models;

namespace G2DataGUI.Common.Data.Maps;

Expand Down Expand Up @@ -36,6 +37,12 @@ private void ReadMaps()
}

CollectionRefreshed?.Invoke(this, EventArgs.Empty);

using var modelFile = File.Open(
"F:\\programming\\languages\\c#\\programs\\G2DataGUI\\build\\Debug\\net7.0\\models\\2a00.chr_model_1.nj",
FileMode.Open,
FileAccess.Read);
NJCM model = NJCM.ReadNJCM(modelFile, 0);
}
catch (Exception ex)
{
Expand Down
6 changes: 6 additions & 0 deletions src/G2DataGUI.Common/Data/Models/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace G2DataGUI.Common.Data.Models;

public class Model
{

}
7 changes: 7 additions & 0 deletions src/G2DataGUI.Common/Data/Models/Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using G2DataGUI.Common.Data.Common;

namespace G2DataGUI.Common.Data.Models;

public class Models : BaseContainer
{
}
38 changes: 38 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJCM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using System.Linq;
using G2DataGUI.IO.Streams;

namespace G2DataGUI.Common.Data.Models;

public class NJCM
{
public uint ByteLength { get; private set; }
public NJSBone BaseBone { get; private set; } = null;

/// <summary>
/// Represents the ASCII string "NJCM"
/// </summary>
public static byte[] Identifier => new byte[] { 0x4E, 0x4A, 0x43, 0x4D };

/// <summary>
/// Offsets do not account for Identifier or ByteLength
/// </summary>
public static long DataOffset => 0x08;

public static NJCM ReadNJCM(Stream reader, long instanceOffset)
{
var identifier = reader.ReadRawByteArray((uint)Identifier.Length);
if (!Identifier.SequenceEqual(identifier))
{
return null;
}

NJCM njcm = new()
{
ByteLength = reader.ReadRawUInt(),
BaseBone = NJSBone.ReadNJSObject(reader, instanceOffset + DataOffset),
};

return njcm;
}
}
57 changes: 57 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJSBone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;
using G2DataGUI.Common.Data.Common;
using G2DataGUI.IO.Streams;

namespace G2DataGUI.Common.Data.Models;

public class NJSBone
{
public uint Flags { get; set; }
public NJSMesh Model { get; set; } = null;
public Vector3 Position { get; set; }
public Vector3 Angle { get; set; }
public Vector3 Scale { get; set; }
public NJSBone Child { get; set; } = null;
public NJSBone Sibling { get; set; } = null;

public static NJSBone ReadNJSObject(Stream reader, long instanceOffset)
{
NJSBone obj = new()
{
Flags = reader.ReadRawUInt(),
};

var modelOffset = reader.ReadRawUInt();
if (modelOffset > 0)
{
long position = reader.Position;
reader.Seek(instanceOffset + modelOffset, SeekOrigin.Begin);
obj.Model = NJSMesh.ReadNJSModel(reader, instanceOffset);
reader.Seek(position, SeekOrigin.Begin);
}

obj.Position = Vector3.ReadVector3(reader);
obj.Angle = Vector3.ReadVector3(reader);
obj.Scale = Vector3.ReadVector3(reader);

var childOffset = reader.ReadRawUInt();
if (childOffset > 0)
{
long position = reader.Position;
reader.Seek(instanceOffset + childOffset, SeekOrigin.Begin);
obj.Child = ReadNJSObject(reader, instanceOffset);
reader.Seek(position, SeekOrigin.Begin);
}

var siblingOffset = reader.ReadRawUInt();
if (siblingOffset > 0)
{
long position = reader.Position;
reader.Seek(instanceOffset + siblingOffset, SeekOrigin.Begin);
obj.Sibling = ReadNJSObject(reader, instanceOffset);
reader.Seek(position, SeekOrigin.Begin);
}

return obj;
}
}
32 changes: 32 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJSMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using G2DataGUI.Common.Data.Common;
using G2DataGUI.IO.Streams;

namespace G2DataGUI.Common.Data.Models;

public class NJSMesh
{
public NJSVertexList VertexList { get; set; } = null;
// indices
public Vector3 Center { get; set; }
public float Radius { get; set; }

public static NJSMesh ReadNJSModel(Stream reader, long instanceOffset)
{
NJSMesh model = new();

var vertexOffset = reader.ReadRawUInt();
if (vertexOffset > 0)
{
var position = reader.Position;
reader.Seek(instanceOffset + vertexOffset, SeekOrigin.Begin);
model.VertexList = NJSVertexList.ReadNJSVertexList(reader);
reader.Seek(position, SeekOrigin.Begin);
}

model.Center = Vector3.ReadVector3(reader);
model.Radius = reader.ReadRawFloat();

return model;
}
}
52 changes: 52 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJSVertexList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.IO;
using G2DataGUI.Common.Data.Common;
using G2DataGUI.IO.Streams;

namespace G2DataGUI.Common.Data.Models;

public class NJSVertexList
{
/// <summary>
/// Always 0x2900?
/// </summary>
public short Format { get; set; }

/// <summary>
/// No idea
/// </summary>
public short Unknown1 { get; set; }

/// <summary>
/// Always 0x0000?
/// </summary>
public short Flags { get; set; }

/// <summary>
/// Number of Vertices
/// </summary>
public short Count { get; set; }

/// <summary>
/// Should end with a 0xFF000000?
/// </summary>
public List<Vector3> VertexList { get; set; } = new List<Vector3>();

public static NJSVertexList ReadNJSVertexList(Stream reader)
{
NJSVertexList list = new()
{
Format = reader.ReadRawShort(),
Unknown1 = reader.ReadRawShort(),
Flags = reader.ReadRawShort(),
Count = reader.ReadRawShort(),
};

for (int index = 0; index < list.Count; index++)
{
list.VertexList.Add(Vector3.ReadVector3(reader));
}

return list;
}
}
6 changes: 6 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJTL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace G2DataGUI.Common.Data.Models;

public class NJTL
{

}
10 changes: 10 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NMDM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G2DataGUI.Common.Data.Models;
internal class NMDM
{
}

0 comments on commit 8f26572

Please sign in to comment.