Skip to content

Commit

Permalink
Merge pull request #134 from psiberx/master
Browse files Browse the repository at this point in the history
Various changes
  • Loading branch information
wopss authored Jul 10, 2024
2 parents 5492034 + 59a9f07 commit 84cea2b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
9 changes: 9 additions & 0 deletions include/RED4ext/Buffer-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ RED4EXT_INLINE RED4ext::RawBuffer::~RawBuffer()
}
}

RED4EXT_INLINE void RED4ext::RawBuffer::Resize(uint32_t aSize)
{
if (data && allocator[0])
{
data = reinterpret_cast<RawBufferAllocator*>(&allocator)->ReallocAligned(data, aSize, alignment);
size = aSize;
}
}

RED4EXT_INLINE RED4ext::SharedPtr<RED4ext::DeferredDataBufferToken> RED4ext::DeferredDataBuffer::LoadAsync()
{
using LoadBufferAsync_t = JobHandle* (*)(DeferredDataBuffer*, JobHandle*, int64_t);
Expand Down
8 changes: 5 additions & 3 deletions include/RED4ext/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace RED4ext
{
struct RawBufferAllocator
{
virtual void sub_00() = 0; // 08
virtual void Free(void* aData) = 0; // 08
virtual Memory::IAllocator GetAllocator() = 0; // 10
virtual void* ReallocAligned(void* aData, uint32_t aSize, uint32_t aAlignment) = 0; // 00
virtual void Free(void* aData) = 0; // 08
virtual Memory::IAllocator GetAllocator() = 0; // 10

void* unk08;
};
Expand All @@ -29,6 +29,8 @@ struct RawBuffer
RawBuffer(RawBuffer&&) = default;
~RawBuffer();

void Resize(uint32_t aSize);

void* data; // 00
uint32_t size; // 08
uint32_t alignment; // 0C
Expand Down
12 changes: 10 additions & 2 deletions include/RED4ext/CName.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ struct CName
{
}

constexpr CName(const char* aName) noexcept
constexpr CName(const char* aName, size_t aLength = 0) noexcept
: hash(0)
{
constexpr CName None = FNV1a64("None");

hash = FNV1a64(aName);
if (aLength <= 0)
{
hash = FNV1a64(aName);
}
else
{
hash = FNV1a64(reinterpret_cast<const uint8_t*>(aName), aLength);
}

if (hash == None)
{
hash = 0;
Expand Down
15 changes: 12 additions & 3 deletions include/RED4ext/NativeTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ struct TweakDBID
name.tdbOffsetBE[2] = 0;
}

constexpr TweakDBID(const char* aName) noexcept
constexpr TweakDBID(const char* aName, size_t aLength = 0) noexcept
{
name.hash = CRC32(aName, 0);
name.length = static_cast<uint8_t>(std::char_traits<char>::length(aName));
if (aLength <= 0)
{
name.hash = CRC32(aName, 0);
name.length = static_cast<uint8_t>(std::char_traits<char>::length(aName));
}
else
{
name.hash = CRC32(reinterpret_cast<const uint8_t*>(aName), aLength, 0);
name.length = static_cast<uint8_t>(aLength);
}

name.tdbOffsetBE[0] = 0;
name.tdbOffsetBE[1] = 0;
name.tdbOffsetBE[2] = 0;
Expand Down
32 changes: 20 additions & 12 deletions include/RED4ext/ResourcePath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ struct ResourcePath
{
}

constexpr ResourcePath(const char* aPath) noexcept
: hash(HashSanitized(aPath))
constexpr ResourcePath(const char* aPath, size_t aLength = 0) noexcept
: hash(HashSanitized(aPath, aLength))
{
}

Expand Down Expand Up @@ -67,7 +67,7 @@ struct ResourcePath
return hash == 0;
}

static constexpr uint64_t HashSanitized(const char* aPath)
static constexpr uint64_t HashSanitized(const char* aPath, size_t aLength = 0)
{
// Sanitations:
// 1. Discard opening and closing quotes (singles and doubles)
Expand All @@ -79,10 +79,15 @@ struct ResourcePath
if (!aPath || *aPath == '\0')
return 0;

constexpr size_t MaxLength = 216;
char buffer[MaxLength];
constexpr size_t MaxPathLength = 216;

char* out = buffer;
if (aLength <= 0 || aLength > MaxPathLength)
{
aLength = MaxPathLength;
}

char buffer[MaxPathLength + 1];
size_t pos = 0;
const char* in = aPath;

if (*in == '"' || *in == '\'')
Expand All @@ -95,25 +100,28 @@ struct ResourcePath
{
if (*in == '/' || *in == '\\')
{
*out = '\\';
++out;
buffer[pos] = '\\';
++pos;
++in;

while (*in == '/' || *in == '\\')
++in;
}
else
{
*out = (*in >= 'A' && *in <= 'Z') ? *in + ('a' - 'A') : *in;
++out;
buffer[pos] = (*in >= 'A' && *in <= 'Z') ? *in + ('a' - 'A') : *in;
++pos;
++in;
}

if (pos == aLength)
break;
}

if (out == buffer)
if (pos == 0)
return 0;

*out = '\0';
buffer[pos] = '\0';

return RED4ext::FNV1a64(buffer);
}
Expand Down

0 comments on commit 84cea2b

Please sign in to comment.