Minor changes to hot reload code

This commit is contained in:
2025-02-23 21:05:13 -05:00
parent 051939f827
commit 7c8f8a3bb1

View File

@@ -6,10 +6,6 @@
#include <Core/HotReload/HotReload.h>
#include <Core/Memory/Allocator.h>
#define MAX_ATTEMPT 256
#define MAX_DLL_ID 32767
#define TEMP_DLL_BUFFER_SIZE_FOR_ID 6 // MAX_DLL_ID + _
namespace Juliet
{
namespace
@@ -27,6 +23,9 @@ namespace Juliet
return lastWriteTime;
}
constexpr size_t kMaxAttempts = 256;
constexpr size_t kMaxDLLID = 256;
constexpr size_t kTempDLLBufferSizeForID = 6; // ID numbers + \0
} // namespace
void LoadCode(HotReloadCode& code)
@@ -52,37 +51,34 @@ namespace Juliet
const char* basePath = GetBasePath();
size_t basePathLength = strlen(basePath);
StringBuffer<char> tempDllPath = {};
const size_t tempDllMaxBufferSize = basePathLength + code.TransientDLLName.Size + TEMP_DLL_BUFFER_SIZE_FOR_ID;
tempDllPath.Data = static_cast<char*>(Calloc(tempDllMaxBufferSize, sizeof(char)));
char idToStr[TEMP_DLL_BUFFER_SIZE_FOR_ID];
for (uint32 attempt = 0; attempt < MAX_ATTEMPT; ++attempt)
const size_t tempDllMaxBufferSize = basePathLength + code.TransientDLLName.Size + kTempDLLBufferSizeForID;
auto tempDllPath = static_cast<char*>(Calloc(tempDllMaxBufferSize, sizeof(char)));
for (uint32 attempt = 0; attempt < kMaxAttempts; ++attempt)
{
// int to char
int idLength = snprintf(idToStr, sizeof(idToStr), "%u", code.UniqueID);
char idToStr[kTempDLLBufferSizeForID];
int idLength = snprintf(idToStr, sizeof(idToStr), "%u", code.UniqueID);
int writtenSize = snprintf(tempDllPath.Data, tempDllMaxBufferSize, "%s%u_%s", basePath, code.UniqueID,
int writtenSize = snprintf(tempDllPath, tempDllMaxBufferSize, "%s%u_%s", basePath, code.UniqueID,
code.TransientDLLName.Data);
if (writtenSize < static_cast<int>(basePathLength + idLength + code.TransientDLLName.Size))
{
SafeFree(tempDllPath.Data);
SafeFree(tempDllPath);
Log(LogLevel::Error, LogCategory::Core, "Cannot create temp full path");
return;
}
tempDllPath.Size = writtenSize + 1;
if (++code.UniqueID >= MAX_DLL_ID)
if (++code.UniqueID >= kMaxDLLID)
{
code.UniqueID = 0;
}
if (CopyFileA(dllName, tempDllPath.Data, false))
if (CopyFileA(dllName, tempDllPath, false))
{
break;
}
}
code.Dll = LoadDynamicLibrary(tempDllPath.Data);
code.Dll = LoadDynamicLibrary(tempDllPath);
if (code.Dll)
{
code.IsValid = true;
@@ -99,8 +95,7 @@ namespace Juliet
}
}
SafeFree(tempDllPath.Data);
tempDllPath = {};
SafeFree(tempDllPath);
}
if (!code.IsValid)