Skip to content

Commit 58dacf1

Browse files
authored
Merge pull request #609 from monkeyman192/more_5.0
Implement HashMap and other new types
2 parents 43ac519 + f0f8247 commit 58dacf1

18 files changed

+667
-104
lines changed

Tools/auto_extract/extractor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@
157157
# TODO: If the GUID changes we need to raise an important message so that we may
158158
# fix it manually.
159159
DONT_OVERRIDE = [
160-
# 'TkAnimNodeFrameData',
160+
'TkAnimNodeFrameData',
161161
# 'TkAnimNodeFrameHalfData',
162-
# 'TkGeometryData',
163-
# 'TkMeshData',
162+
'TkGeometryData',
163+
'TkMeshData',
164164
]
165165

166166
SUMMARY_FILE = op.join(op.dirname(__file__), 'summary.txt')

Tools/auto_extract/templates/default/libMBIN-Shared.projitems.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\GcResource.cs" />
3939
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\GcSeed.cs" />
4040
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\INMSString.cs" />
41+
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\INMSVariableLengthString.cs" />
42+
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\IHashMap.cs" />
4143
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x10.cs" />
4244
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x20.cs" />
4345
<Compile Include="$(MSBuildThisFileDirectory)Source\NMS\BaseTypes\NMSString0x20A.cs" />

Tools/auto_extract/templates/default/static_templates/TkAnimNodeFrameData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace libMBIN.NMS.Toolkit
99
{
10-
[NMS(GUID = 0xB2E78D75B9088DB6, NameHash = 0x270204EDCEE4DB62)]
10+
[NMS(GUID = 0xB2E78D75B9088DB6, NameHash = 0xADF5F9C3)]
1111
public class TkAnimNodeFrameData : NMSTemplate
1212
{
1313
[NMS(Index = 0)]

Tools/auto_extract/templates/default/static_templates/TkGeometryData.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66

77
namespace libMBIN.NMS.Toolkit
88
{
9-
[NMS(GUID = 0x7E2C6C00A113D11F, NameHash = 0xA74EA06001E7577E)]
9+
[NMS(GUID = 0x32F6AE7B03222A1F, NameHash = 0x819C3220)]
1010
public class TkGeometryData : NMSTemplate
1111
{
12-
[NMS(Index = 18)]
12+
[NMS(Index = 20)]
1313
/* 0x000 */ public TkVertexLayout SmallVertexLayout;
14-
[NMS(Index = 17)]
14+
[NMS(Index = 19)]
1515
/* 0x020 */ public TkVertexLayout VertexLayout;
1616
[NMS(Index = 12)]
1717
/* 0x040 */ public List<int> BoundHullVertEd;
1818
[NMS(Index = 16)]
1919
/* 0x050 */ public List<Vector4f> BoundHullVerts;
2020
[NMS(Index = 11)]
2121
/* 0x060 */ public List<int> BoundHullVertSt;
22-
[NMS(Index = 19)]
22+
[NMS(Index = 21)]
2323
/* 0x070 */ public List<int> IndexBuffer;
2424
[NMS(Index = 4)]
2525
/* 0x080 */ public List<TkJointBindingData> JointBindings;
@@ -39,18 +39,22 @@ public class TkGeometryData : NMSTemplate
3939
/* 0x0F0 */ public List<int> MeshVertREnd;
4040
[NMS(Index = 9)]
4141
/* 0x100 */ public List<int> MeshVertRStart;
42+
[NMS(Index = 18)]
43+
/* 0x110 */ public List<VariableSizeString> ProcGenNodeNames;
44+
[NMS(Index = 17)]
45+
/* 0x120 */ public List<int> ProcGenParentId;
4246
[NMS(Index = 8)]
43-
/* 0x110 */ public List<int> SkinMatrixLayout;
44-
[NMS(Index = 20)]
45-
/* 0x120 */ public List<TkMeshMetaData> StreamMetaDataArray;
47+
/* 0x130 */ public List<int> SkinMatrixLayout;
48+
[NMS(Index = 22)]
49+
/* 0x140 */ public List<TkMeshMetaData> StreamMetaDataArray;
4650
[NMS(Index = 3)]
47-
/* 0x130 */ public int CollisionIndexCount;
51+
/* 0x150 */ public int CollisionIndexCount;
4852
[NMS(Index = 1)]
49-
/* 0x134 */ public int IndexCount;
53+
/* 0x154 */ public int IndexCount;
5054
[NMS(Index = 2)]
51-
/* 0x138 */ public int Indices16Bit;
55+
/* 0x158 */ public int Indices16Bit;
5256
[NMS(Index = 0)]
53-
/* 0x13C */ public int VertexCount;
57+
/* 0x15C */ public int VertexCount;
5458

5559
// TODO: add the list ending to this??
5660
public override bool CustomSerialize(BinaryWriter writer, Type field, object fieldData, NMSAttribute settings, FieldInfo fieldInfo, ref List<Tuple<long, object>> additionalData, ref int addtDataIndex) {

Tools/auto_extract/templates/default/static_templates/TkMeshData.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
namespace libMBIN.NMS.Toolkit
88
{
9-
[NMS(GUID = 0xDF5154A393B47BBB, NameHash = 0xF671716161E708E3)]
9+
[NMS(GUID = 0x70272DC777DDD6BD, NameHash = 0x18D05F06)]
1010
public class TkMeshData : NMSTemplate
1111
{
12+
[NMS(Index = 0)]
13+
/* 0x00 */ public VariableSizeString IdString;
1214
[NMS(Index = 4)]
13-
/* 0x00 */ public byte[] MeshDataStream;
15+
/* 0x10 */ public byte[] MeshDataStream;
1416
[NMS(Index = 1)]
15-
/* 0x10 */ public ulong Hash;
17+
/* 0x20 */ public ulong Hash;
1618
[NMS(Index = 3)]
17-
/* 0x18 */ public int IndexDataSize;
19+
/* 0x28 */ public int IndexDataSize;
1820
[NMS(Index = 2)]
19-
/* 0x1C */ public int VertexDataSize;
20-
[NMS(Index = 0, Size = 0x80, Padding = 0xFE)]
21-
/* 0x20 */ public string IdString;
21+
/* 0x2C */ public int VertexDataSize;
2222

2323
public override object CustomDeserialize( BinaryReader reader, Type field, NMSAttribute settings, FieldInfo fieldInfo ) {
2424
var fieldName = fieldInfo.Name;

libMBIN/Source/Common/BinaryStreamExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Align( this BinaryReader reader, int alignBy ) {
1818

1919
public static void Align( this BinaryWriter writer, int alignBy, string name, byte paddingByte = 0 ) {
2020
long offset = writer.BaseStream.Position;
21-
21+
if (alignBy == 0) return;
2222
long mod = offset % alignBy;
2323
if ( mod != 0 ) {
2424
if (paddingByte != 0) {
Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
15
namespace libMBIN.NMS {
26

3-
[NMS(Size = 0x30)]
4-
public class HashMap<T> : NMSTemplate
7+
[NMS(Size = 0x30, Alignment = 0x8)]
8+
public class HashMap<T> : NMSTemplate, IEnumerable<T>, IHashMap
59
{
6-
/* 0x00 */ public ulong Offset;
7-
/* 0x08 */ public uint Count;
8-
/* 0x0C */ public uint EndPaddingLShift; // This is the size of the end padding blob. Ie. size = 8 << EndPaddingLShift
9-
// To write this value. We calculate the smallest `n` such that (8 << n) > 8 * Count
10+
public List<T> Elements; // The actual elements of the HashMap.
11+
/* 0x00 */ private ulong Offset;
12+
/* 0x08 */ public uint Count {
13+
get {
14+
return (uint)Elements.Count;
15+
}
16+
}
17+
18+
/* 0x0C */ public int EndPaddingLShift {
19+
get {
20+
int shifts = 62;
21+
while ((shifts > 0) && (1 << (64 - shifts)) * 0.8 < Count) shifts -= 1;
22+
return 64 - shifts;
23+
}
24+
}
1025

1126
// It looks like there are then 4 uint64's which correspond to something... They seem to be empty in the mbins though.
1227
[NMS(Size = 0x20)]
13-
/* 0x10 */ public byte[] EndPadding;
28+
/* 0x10 */ private byte[] EndPadding;
29+
30+
public object GetElements() {
31+
return Elements;
32+
}
33+
34+
// IMPORTANT: If any further constructors are added with one argument, some code in NMSTemplate will break!
35+
// See DeserializeEXml method.
36+
public HashMap() {
37+
Elements = new List<T>();
38+
}
39+
40+
public HashMap(List<T> lst) {
41+
Elements = lst;
42+
}
43+
44+
public IEnumerator<T> GetEnumerator() {
45+
return Elements.GetEnumerator();
46+
}
47+
48+
IEnumerator IEnumerable.GetEnumerator() {
49+
return this.GetEnumerator();
50+
}
51+
52+
public static implicit operator HashMap<T>(List<T> lst) => new HashMap<T>(lst);
53+
public static implicit operator List<T>(HashMap<T> hm) => hm.Elements;
1454
}
1555
}
Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
11
namespace libMBIN.NMS {
22

33
[NMS(Size = 0x18)]
4-
public class HashedString : NMSTemplate
4+
public class HashedString : NMSTemplate, INMSVariableLengthString
55
{
6-
public ulong Offset;
7-
public uint Length;
8-
public uint Padding0xC;
9-
public uint Hash;
10-
public uint Padding0x14;
6+
public string Value;
7+
private ulong Offset;
8+
private uint Length;
9+
private uint Padding0xC = 0xAAAAAA01;
10+
private uint Padding0x14 = 0xAAAAAAAA;
11+
public string String {
12+
get => StringValue();
13+
set => Value = value;
14+
}
15+
16+
public string StringValue()
17+
{
18+
return Value;
19+
}
20+
/// <summary>
21+
/// Jenkins Hashing function:
22+
/// https://en.wikipedia.org/wiki/Jenkins_hash_function
23+
/// </summary>
24+
/// <returns></returns>
25+
public uint Hash()
26+
{
27+
uint hash = 0;
28+
if( Value != null ) {
29+
foreach( char c in Value ) {
30+
hash += char.ToUpper(c);
31+
hash += hash << 10;
32+
hash ^= hash >> 6;
33+
}
34+
hash += hash << 3;
35+
hash ^= hash >> 11;
36+
hash += hash << 15;
37+
}
38+
return hash;
39+
}
40+
41+
public HashedString(string str) {
42+
Value = str;
43+
}
44+
45+
public HashedString() { }
46+
47+
public static implicit operator HashedString ( string str ) => new HashedString { Value = str };
48+
public static implicit operator string ( HashedString str ) => str.Value;
1149
}
1250
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace libMBIN.NMS
2+
{
3+
public interface IHashMap {
4+
object GetElements();
5+
6+
uint Count { get; }
7+
int EndPaddingLShift { get; }
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using libMBIN.NMS.Toolkit;
2+
using libMBIN.NMS.GameComponents;
3+
4+
namespace libMBIN.NMS
5+
{
6+
public interface INMSVariableLengthString: INMSString
7+
{
8+
string StringValue();
9+
string String { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)