1
0
mirror of synced 2024-09-24 03:28:21 +02:00

Improve gfpak hash calculating based on pkNX

This commit is contained in:
KillzXGaming 2019-11-24 20:47:41 -05:00
parent 1c787477d1
commit 589d4ea39e
3 changed files with 17 additions and 37 deletions

View File

@ -203,19 +203,12 @@ namespace FirstPlugin
fileEntry.FolderHash = FolderFiles[i];
fileEntry.FileName = GetString(hashes[i], fileEntry.FileData);
string suffix = hashes[i].ToString("X").GetLast(6);
ulong suffix64 = Convert.ToUInt64(suffix, 16);
if (HashList.ContainsKey(suffix64))
{
fileEntry.FileName = HashList[suffix64];
}
string baseName = Path.GetFileName(fileEntry.FileName.Replace("\r", ""));
switch (Utils.GetExtension(fileEntry.FileName))
{
case ".gfbanm":
// fileEntry.OpenFileFormatOnLoad = true;
fileEntry.FileName = $"Animations/{baseName}";
break;
case ".gfbanmcfg":
@ -228,6 +221,7 @@ namespace FirstPlugin
fileEntry.FileName = $"PokeConfigs/{baseName}";
break;
case ".bntx":
// fileEntry.OpenFileFormatOnLoad = true;
fileEntry.FileName = $"Textures/{baseName}";
break;
case ".bnsh":
@ -268,7 +262,7 @@ namespace FirstPlugin
{
string HashString = hashStr.TrimEnd();
ulong hash = FNV64A1.CalculateSuffix(HashString);
ulong hash = FNV64A1.Calculate(HashString);
if (!hashList.ContainsKey(hash))
hashList.Add(hash, HashString);
@ -278,7 +272,7 @@ namespace FirstPlugin
string[] hashPaths = HashString.Split('/');
for (int i = 0; i < hashPaths?.Length; i++)
{
hash = FNV64A1.CalculateSuffix(hashPaths[i]);
hash = FNV64A1.Calculate(hashPaths[i]);
if (!hashList.ContainsKey(hash))
hashList.Add(hash, HashString);
}
@ -293,7 +287,7 @@ namespace FirstPlugin
string baseName = Path.GetFileNameWithoutExtension(FileName);
string pokeStrFile = hashStr.Replace("pm0000_00", baseName);
ulong hash = FNV64A1.CalculateSuffix(pokeStrFile);
ulong hash = FNV64A1.Calculate(pokeStrFile);
if (!hashList.ContainsKey(hash))
hashList.Add(hash, pokeStrFile);
}
@ -302,7 +296,7 @@ namespace FirstPlugin
{
string pokeStr = hashStr.Replace("pm0000", $"pm{i.ToString("D4")}");
ulong hash = FNV64A1.CalculateSuffix(pokeStr);
ulong hash = FNV64A1.Calculate(pokeStr);
if (!hashList.ContainsKey(hash))
hashList.Add(hash, pokeStr);
}
@ -314,7 +308,12 @@ namespace FirstPlugin
if (ext == ".bntx" || ext == ".bfres" || ext == ".bnsh" || ext == ".bfsha")
return GetBinaryHeaderName(Data) + ext;
else
return $"{Hash.ToString("X")}{ext}";
{
if (HashList.ContainsKey(Hash))
return HashList[Hash];
else
return $"{Hash.ToString("X")}{ext}";
}
}
public void Write(FileWriter writer)

View File

@ -126,6 +126,8 @@ In the event that the tool cannot compile, check references. All the libraries u
- Kuriimu for some IO and file parsing help.
- Skyth and Radfordhound for PAC documentation.
- Ac_K for ASTC decoder c# port from Ryujinx.
- pkNX and kwsch for Fnv hashing and useful pkmn code/structure references.
- Dragonation for useful code on the structure for some flatbuffers in pokemon switch
## Resources
- [TreeView Icons by icons8](https://icons8.com/)

View File

@ -12,32 +12,11 @@ namespace Toolbox.Library
return Calculate(Encoding.Default.GetBytes(text));
}
public static ulong CalculateSuffix(string text) {
return CalculateSuffix(Encoding.Default.GetBytes(text));
}
public static ulong CalculateSuffix(byte[] bytes)
{
const ulong fnv64Basis = 0x222645;
const ulong fnv64Prime = 0x0001b3;
const ulong mask = 0xffffff;
ulong hash = fnv64Basis & mask;
for (var i = 0; i < bytes.Length; i++)
{
hash = hash ^ bytes[i];
hash *= fnv64Prime;
hash = hash & mask;
}
return hash;
}
//https://gist.github.com/rasmuskl/3786618
//https://github.com/kwsch/pkNX/blob/e8cd9cc30feb0a6f6e8cc1f6f6e04288aef0a8cb/pkNX.Containers/Misc/FnvHash.cs
public static ulong Calculate(byte[] bytes)
{
const ulong fnv64Offset = 14695981039346656037;
const ulong fnv64Prime = 0x100000001b3;
const ulong fnv64Offset = 0xCBF29CE484222645;
const ulong fnv64Prime = 0x00000100000001b3;
ulong hash = fnv64Offset;
for (var i = 0; i < bytes.Length; i++)