mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-03-05 12:33:02 +00:00
chore: call common djb2 implementations in game hashing funcs
This commit is contained in:
@@ -1,28 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "IW3.h"
|
||||
#include "Utils/Djb2.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace IW3
|
||||
{
|
||||
class Common
|
||||
{
|
||||
public:
|
||||
static constexpr uint32_t R_HashString(const char* string, const uint32_t hash)
|
||||
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
|
||||
{
|
||||
const char* v2 = string; // edx@1
|
||||
char v3 = *string; // cl@1
|
||||
uint32_t result = hash;
|
||||
|
||||
for (; *v2; v3 = *v2)
|
||||
{
|
||||
++v2;
|
||||
result = 33 * result ^ (v3 | 0x20);
|
||||
}
|
||||
return result;
|
||||
return djb2_xor_nocase(str, hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* string)
|
||||
{
|
||||
// Using djb2 with a 0 starting value makes a worse hash func apparently
|
||||
// but who am I to judge
|
||||
return R_HashString(string, 0u);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,26 +2,8 @@
|
||||
|
||||
#include "Utils/Pack.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
int Common::StringTable_HashString(const char* str)
|
||||
{
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
auto result = 0;
|
||||
auto offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
const auto c = tolower(str[offset++]);
|
||||
result = c + 31 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2])
|
||||
{
|
||||
return PackedTexCoords{pack32::Vec2PackTexCoordsVU(in)};
|
||||
|
||||
@@ -1,33 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "IW4.h"
|
||||
#include "Utils/Djb2.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
namespace IW4
|
||||
{
|
||||
class Common
|
||||
{
|
||||
public:
|
||||
static constexpr uint32_t R_HashString(const char* string, const uint32_t hash)
|
||||
static constexpr int StringTable_HashString(const char* str)
|
||||
{
|
||||
const char* v2 = string; // edx@1
|
||||
char v3 = *string; // cl@1
|
||||
uint32_t result = hash;
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
for (; *v2; v3 = *v2)
|
||||
{
|
||||
++v2;
|
||||
result = 33 * result ^ (v3 | 0x20);
|
||||
}
|
||||
return result;
|
||||
// Lets do djb2 with 31 instead of 33 because why not
|
||||
// and leave out the starting value while we are at it
|
||||
uint32_t hash = 0;
|
||||
for (char c = *str; c; c = *++str)
|
||||
hash = hash * 31 + std::tolower(c);
|
||||
|
||||
return static_cast<int>(hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
|
||||
{
|
||||
return djb2_xor_nocase(str, hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* string)
|
||||
{
|
||||
// Using djb2 with a 0 starting value makes a worse hash func apparently
|
||||
// but who am I to judge
|
||||
return R_HashString(string, 0u);
|
||||
}
|
||||
|
||||
static int StringTable_HashString(const char* str);
|
||||
|
||||
static PackedTexCoords Vec2PackTexCoords(const float (&in)[2]);
|
||||
static PackedUnitVec Vec3PackUnitVec(const float (&in)[3]);
|
||||
static GfxColor Vec4PackGfxColor(const float (&in)[4]);
|
||||
|
||||
@@ -2,26 +2,8 @@
|
||||
|
||||
#include "Utils/Pack.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
using namespace IW5;
|
||||
|
||||
int Common::StringTable_HashString(const char* str)
|
||||
{
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
auto result = 0;
|
||||
auto offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
const auto c = tolower(str[offset++]);
|
||||
result = c + 31 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2])
|
||||
{
|
||||
return PackedTexCoords{pack32::Vec2PackTexCoordsVU(in)};
|
||||
|
||||
@@ -1,26 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "IW5.h"
|
||||
#include "Utils/Djb2.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
namespace IW5
|
||||
{
|
||||
class Common
|
||||
{
|
||||
public:
|
||||
static int StringTable_HashString(const char* str);
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, uint32_t hash)
|
||||
static constexpr int StringTable_HashString(const char* str)
|
||||
{
|
||||
for (const auto* pos = str; *pos; pos++)
|
||||
{
|
||||
hash = 33 * hash ^ (*pos | 0x20);
|
||||
}
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
return hash;
|
||||
// Lets do djb2 with 31 instead of 33 because why not
|
||||
// and leave out the starting value while we are at it
|
||||
uint32_t hash = 0;
|
||||
for (char c = *str; c; c = *++str)
|
||||
hash = hash * 31 + std::tolower(c);
|
||||
|
||||
return static_cast<int>(hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
|
||||
{
|
||||
return djb2_xor_nocase(str, hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* string)
|
||||
{
|
||||
// Using djb2 with a 0 starting value makes a worse hash func apparently
|
||||
// but who am I to judge
|
||||
return R_HashString(string, 0u);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,62 +2,8 @@
|
||||
|
||||
#include "Utils/Pack.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
using namespace T5;
|
||||
|
||||
int Common::Com_HashKey(const char* str, const int maxLen)
|
||||
{
|
||||
if (str == nullptr)
|
||||
return 0;
|
||||
|
||||
int hash = 0;
|
||||
for (int i = 0; i < maxLen; i++)
|
||||
{
|
||||
if (str[i] == '\0')
|
||||
break;
|
||||
|
||||
hash += str[i] * (0x77 + i);
|
||||
}
|
||||
|
||||
return hash ^ ((hash ^ (hash >> 10)) >> 10);
|
||||
}
|
||||
|
||||
int Common::Com_HashString(const char* str)
|
||||
{
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
auto result = 0x1505;
|
||||
auto offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
const auto c = tolower(str[offset++]);
|
||||
result = c + 33 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int Common::Com_HashString(const char* str, const int len)
|
||||
{
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
int result = 0x1505;
|
||||
int offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
if (len > 0 && offset >= len)
|
||||
break;
|
||||
|
||||
const int c = tolower(str[offset++]);
|
||||
result = c + 33 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2])
|
||||
{
|
||||
return PackedTexCoords{pack32::Vec2PackTexCoordsVU(in)};
|
||||
|
||||
@@ -1,28 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "T5.h"
|
||||
#include "Utils/Djb2.h"
|
||||
|
||||
namespace T5
|
||||
{
|
||||
class Common
|
||||
{
|
||||
public:
|
||||
static int Com_HashKey(const char* str, int maxLen);
|
||||
static int Com_HashString(const char* str);
|
||||
static int Com_HashString(const char* str, int len);
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, uint32_t hash)
|
||||
static constexpr int Com_HashKey(const char* str, const int maxLen)
|
||||
{
|
||||
for (const auto* pos = str; *pos; pos++)
|
||||
if (str == nullptr)
|
||||
return 0;
|
||||
|
||||
int hash = 0;
|
||||
for (int i = 0; i < maxLen; i++)
|
||||
{
|
||||
hash = 33 * hash ^ (*pos | 0x20);
|
||||
if (str[i] == '\0')
|
||||
break;
|
||||
|
||||
hash += str[i] * (0x77 + i);
|
||||
}
|
||||
|
||||
return hash;
|
||||
return hash ^ ((hash ^ (hash >> 10)) >> 10);
|
||||
}
|
||||
|
||||
static constexpr int Com_HashString(const char* str)
|
||||
{
|
||||
// Hashing aesthetics seem to be a thing
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
return static_cast<int>(djb2_lower(str));
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
|
||||
{
|
||||
return djb2_xor_nocase(str, hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* string)
|
||||
{
|
||||
// Using djb2 with a 0 starting value makes a worse hash func apparently
|
||||
// but who am I to judge
|
||||
return R_HashString(string, 0u);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "T6.h"
|
||||
#include "Utils/Djb2.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
@@ -28,51 +29,22 @@ namespace T6
|
||||
|
||||
static constexpr int Com_HashString(const char* str)
|
||||
{
|
||||
// Hashing aesthetics seem to be a thing
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
auto result = 0x1505;
|
||||
auto offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
const auto c = tolower(str[offset++]);
|
||||
result = c + 33 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
return static_cast<int>(djb2_lower(str));
|
||||
}
|
||||
|
||||
static constexpr int Com_HashString(const char* str, const int len)
|
||||
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
|
||||
{
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
int result = 0x1505;
|
||||
int offset = 0;
|
||||
while (str[offset])
|
||||
{
|
||||
if (len > 0 && offset >= len)
|
||||
break;
|
||||
|
||||
const int c = tolower(str[offset++]);
|
||||
result = c + 33 * result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* str, uint32_t hash)
|
||||
{
|
||||
for (const auto* pos = str; *pos; pos++)
|
||||
{
|
||||
hash = 33 * hash ^ (*pos | 0x20);
|
||||
}
|
||||
|
||||
return hash;
|
||||
return djb2_xor_nocase(str, hash);
|
||||
}
|
||||
|
||||
static constexpr uint32_t R_HashString(const char* string)
|
||||
{
|
||||
// Using djb2 with a 0 starting value makes a worse hash func apparently
|
||||
// but who am I to judge
|
||||
return R_HashString(string, 0u);
|
||||
}
|
||||
|
||||
@@ -81,6 +53,9 @@ namespace T6
|
||||
if (!str || !*str)
|
||||
return 0;
|
||||
|
||||
// Seems to be somewhat based on sdbm
|
||||
// http://www.cse.yorku.ca/~oz/hash.html
|
||||
|
||||
auto result = 0x1505;
|
||||
auto offset = 0u;
|
||||
|
||||
|
||||
100
src/Common/Utils/Djb2.h
Normal file
100
src/Common/Utils/Djb2.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
|
||||
// This header contains multiple varying implementations of the DJB2 algorithm:
|
||||
// http://www.cse.yorku.ca/~oz/hash.html
|
||||
|
||||
constexpr uint32_t DJB2_STARTING_VALUE = 5381;
|
||||
|
||||
static constexpr uint32_t djb2(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
{
|
||||
// hash * 33 + c
|
||||
hash = ((hash << 5) + hash) + c;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2(const char* str)
|
||||
{
|
||||
return djb2(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_nocase(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
{
|
||||
// Or with 0x20 makes the string case-insensitive
|
||||
// but also messes up non-letters
|
||||
hash = ((hash << 5) + hash) + (c | 0x20);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_nocase(const char* str)
|
||||
{
|
||||
return djb2_nocase(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_lower(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
hash = ((hash << 5) + hash) + std::tolower(c);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_lower(const char* str)
|
||||
{
|
||||
return djb2_lower(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
{
|
||||
hash = ((hash << 5) + hash) ^ c;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor(const char* str)
|
||||
{
|
||||
return djb2_xor(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor_nocase(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
{
|
||||
// Or with 0x20 makes the string case-insensitive
|
||||
// but also messes up non-letters
|
||||
hash = ((hash << 5) + hash) ^ (c | 0x20);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor_nocase(const char* str)
|
||||
{
|
||||
return djb2_xor_nocase(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor_lower(const char* str, uint32_t hash)
|
||||
{
|
||||
for (char c = *str; c; c = *++str)
|
||||
hash = ((hash << 5) + hash) ^ std::tolower(c);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static constexpr uint32_t djb2_xor_lower(const char* str)
|
||||
{
|
||||
return djb2_xor_lower(str, DJB2_STARTING_VALUE);
|
||||
}
|
||||
Reference in New Issue
Block a user