Skip to content

Commit

Permalink
Implement HashMap and other new types
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyman192 committed Jul 25, 2024
1 parent 43ac519 commit f0f8247
Show file tree
Hide file tree
Showing 18 changed files with 667 additions and 104 deletions.
6 changes: 3 additions & 3 deletions Tools/auto_extract/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@
# TODO: If the GUID changes we need to raise an important message so that we may
# fix it manually.
DONT_OVERRIDE = [
# 'TkAnimNodeFrameData',
'TkAnimNodeFrameData',
# 'TkAnimNodeFrameHalfData',
# 'TkGeometryData',
# 'TkMeshData',
'TkGeometryData',
'TkMeshData',
]

SUMMARY_FILE = op.join(op.dirname(__file__), 'summary.txt')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\GcResource.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\GcSeed.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\INMSString.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\INMSVariableLengthString.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\IHashMap.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x10.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x20.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x20A.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace libMBIN.NMS.Toolkit
{
[NMS(GUID = 0xB2E78D75B9088DB6, NameHash = 0x270204EDCEE4DB62)]
[NMS(GUID = 0xB2E78D75B9088DB6, NameHash = 0xADF5F9C3)]
public class TkAnimNodeFrameData : NMSTemplate
{
[NMS(Index = 0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

namespace libMBIN.NMS.Toolkit
{
[NMS(GUID = 0x7E2C6C00A113D11F, NameHash = 0xA74EA06001E7577E)]
[NMS(GUID = 0x32F6AE7B03222A1F, NameHash = 0x819C3220)]
public class TkGeometryData : NMSTemplate
{
[NMS(Index = 18)]
[NMS(Index = 20)]
/* 0x000 */ public TkVertexLayout SmallVertexLayout;
[NMS(Index = 17)]
[NMS(Index = 19)]
/* 0x020 */ public TkVertexLayout VertexLayout;
[NMS(Index = 12)]
/* 0x040 */ public List<int> BoundHullVertEd;
[NMS(Index = 16)]
/* 0x050 */ public List<Vector4f> BoundHullVerts;
[NMS(Index = 11)]
/* 0x060 */ public List<int> BoundHullVertSt;
[NMS(Index = 19)]
[NMS(Index = 21)]
/* 0x070 */ public List<int> IndexBuffer;
[NMS(Index = 4)]
/* 0x080 */ public List<TkJointBindingData> JointBindings;
Expand All @@ -39,18 +39,22 @@ public class TkGeometryData : NMSTemplate
/* 0x0F0 */ public List<int> MeshVertREnd;
[NMS(Index = 9)]
/* 0x100 */ public List<int> MeshVertRStart;
[NMS(Index = 18)]
/* 0x110 */ public List<VariableSizeString> ProcGenNodeNames;
[NMS(Index = 17)]
/* 0x120 */ public List<int> ProcGenParentId;
[NMS(Index = 8)]
/* 0x110 */ public List<int> SkinMatrixLayout;
[NMS(Index = 20)]
/* 0x120 */ public List<TkMeshMetaData> StreamMetaDataArray;
/* 0x130 */ public List<int> SkinMatrixLayout;
[NMS(Index = 22)]
/* 0x140 */ public List<TkMeshMetaData> StreamMetaDataArray;
[NMS(Index = 3)]
/* 0x130 */ public int CollisionIndexCount;
/* 0x150 */ public int CollisionIndexCount;
[NMS(Index = 1)]
/* 0x134 */ public int IndexCount;
/* 0x154 */ public int IndexCount;
[NMS(Index = 2)]
/* 0x138 */ public int Indices16Bit;
/* 0x158 */ public int Indices16Bit;
[NMS(Index = 0)]
/* 0x13C */ public int VertexCount;
/* 0x15C */ public int VertexCount;

// TODO: add the list ending to this??
public override bool CustomSerialize(BinaryWriter writer, Type field, object fieldData, NMSAttribute settings, FieldInfo fieldInfo, ref List<Tuple<long, object>> additionalData, ref int addtDataIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

namespace libMBIN.NMS.Toolkit
{
[NMS(GUID = 0xDF5154A393B47BBB, NameHash = 0xF671716161E708E3)]
[NMS(GUID = 0x70272DC777DDD6BD, NameHash = 0x18D05F06)]
public class TkMeshData : NMSTemplate
{
[NMS(Index = 0)]
/* 0x00 */ public VariableSizeString IdString;
[NMS(Index = 4)]
/* 0x00 */ public byte[] MeshDataStream;
/* 0x10 */ public byte[] MeshDataStream;
[NMS(Index = 1)]
/* 0x10 */ public ulong Hash;
/* 0x20 */ public ulong Hash;
[NMS(Index = 3)]
/* 0x18 */ public int IndexDataSize;
/* 0x28 */ public int IndexDataSize;
[NMS(Index = 2)]
/* 0x1C */ public int VertexDataSize;
[NMS(Index = 0, Size = 0x80, Padding = 0xFE)]
/* 0x20 */ public string IdString;
/* 0x2C */ public int VertexDataSize;

public override object CustomDeserialize( BinaryReader reader, Type field, NMSAttribute settings, FieldInfo fieldInfo ) {
var fieldName = fieldInfo.Name;
Expand Down
2 changes: 1 addition & 1 deletion libMBIN/Source/Common/BinaryStreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Align( this BinaryReader reader, int alignBy ) {

public static void Align( this BinaryWriter writer, int alignBy, string name, byte paddingByte = 0 ) {
long offset = writer.BaseStream.Position;

if (alignBy == 0) return;
long mod = offset % alignBy;
if ( mod != 0 ) {
if (paddingByte != 0) {
Expand Down
54 changes: 47 additions & 7 deletions libMBIN/Source/NMS/BaseTypes/HashMap.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace libMBIN.NMS {

[NMS(Size = 0x30)]
public class HashMap<T> : NMSTemplate
[NMS(Size = 0x30, Alignment = 0x8)]
public class HashMap<T> : NMSTemplate, IEnumerable<T>, IHashMap
{
/* 0x00 */ public ulong Offset;
/* 0x08 */ public uint Count;
/* 0x0C */ public uint EndPaddingLShift; // This is the size of the end padding blob. Ie. size = 8 << EndPaddingLShift
// To write this value. We calculate the smallest `n` such that (8 << n) > 8 * Count
public List<T> Elements; // The actual elements of the HashMap.
/* 0x00 */ private ulong Offset;
/* 0x08 */ public uint Count {
get {
return (uint)Elements.Count;
}
}

/* 0x0C */ public int EndPaddingLShift {
get {
int shifts = 62;
while ((shifts > 0) && (1 << (64 - shifts)) * 0.8 < Count) shifts -= 1;
return 64 - shifts;
}
}

// It looks like there are then 4 uint64's which correspond to something... They seem to be empty in the mbins though.
[NMS(Size = 0x20)]
/* 0x10 */ public byte[] EndPadding;
/* 0x10 */ private byte[] EndPadding;

public object GetElements() {
return Elements;
}

// IMPORTANT: If any further constructors are added with one argument, some code in NMSTemplate will break!
// See DeserializeEXml method.
public HashMap() {
Elements = new List<T>();
}

public HashMap(List<T> lst) {
Elements = lst;
}

public IEnumerator<T> GetEnumerator() {
return Elements.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}

public static implicit operator HashMap<T>(List<T> lst) => new HashMap<T>(lst);
public static implicit operator List<T>(HashMap<T> hm) => hm.Elements;
}
}
50 changes: 44 additions & 6 deletions libMBIN/Source/NMS/BaseTypes/HashedString.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
namespace libMBIN.NMS {

[NMS(Size = 0x18)]
public class HashedString : NMSTemplate
public class HashedString : NMSTemplate, INMSVariableLengthString
{
public ulong Offset;
public uint Length;
public uint Padding0xC;
public uint Hash;
public uint Padding0x14;
public string Value;
private ulong Offset;
private uint Length;
private uint Padding0xC = 0xAAAAAA01;
private uint Padding0x14 = 0xAAAAAAAA;
public string String {
get => StringValue();
set => Value = value;
}

public string StringValue()
{
return Value;
}
/// <summary>
/// Jenkins Hashing function:
/// https://en.wikipedia.org/wiki/Jenkins_hash_function
/// </summary>
/// <returns></returns>
public uint Hash()
{
uint hash = 0;
if( Value != null ) {
foreach( char c in Value ) {
hash += char.ToUpper(c);
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
}
return hash;
}

public HashedString(string str) {
Value = str;
}

public HashedString() { }

public static implicit operator HashedString ( string str ) => new HashedString { Value = str };
public static implicit operator string ( HashedString str ) => str.Value;
}
}
9 changes: 9 additions & 0 deletions libMBIN/Source/NMS/BaseTypes/IHashMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace libMBIN.NMS
{
public interface IHashMap {
object GetElements();

uint Count { get; }
int EndPaddingLShift { get; }
}
}
11 changes: 11 additions & 0 deletions libMBIN/Source/NMS/BaseTypes/INMSVariableLengthString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using libMBIN.NMS.Toolkit;
using libMBIN.NMS.GameComponents;

namespace libMBIN.NMS
{
public interface INMSVariableLengthString: INMSString
{
string StringValue();
string String { get; set; }
}
}
7 changes: 6 additions & 1 deletion libMBIN/Source/NMS/BaseTypes/OptionalVariableSizeString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
namespace libMBIN.NMS
{
[NMS(Size = 0x10, Alignment = 0x1)]
public class OptionalVariableSizeString : NMSTemplate, INMSString
public class OptionalVariableSizeString : NMSTemplate, INMSVariableLengthString
{
public string Value;

public string String {
get => StringValue();
set => Value = value;
}

public string StringValue()
{
return this.Value;
Expand Down
7 changes: 6 additions & 1 deletion libMBIN/Source/NMS/BaseTypes/VariableSizeString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace libMBIN.NMS
{
[NMS(Size = 0x10, Alignment = 0x1)]
public class VariableSizeString : NMSTemplate, INMSString
public class VariableSizeString : NMSTemplate, INMSVariableLengthString
{
public string Value;

Expand All @@ -13,6 +13,11 @@ public string StringValue()
return this.Value;
}

public string String {
get => StringValue();
set => Value = value;
}

/// <summary>
/// Returns the value held by this string.
/// </summary>
Expand Down
Loading

0 comments on commit f0f8247

Please sign in to comment.