forked from emoose/MBINCompiler
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HashMap and other new types
- Loading branch information
1 parent
43ac519
commit f0f8247
Showing
18 changed files
with
667 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.