From 7c8f8a3bb18acbeb5c17b688a0a09a94b355dd0e Mon Sep 17 00:00:00 2001 From: Patedam Date: Sun, 23 Feb 2025 21:05:13 -0500 Subject: [PATCH] Minor changes to hot reload code --- .../Core/HotReload/Win32/Win32HotReload.cpp | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp b/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp index af1992d..8b3ff3b 100644 --- a/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp +++ b/Juliet/src/Core/HotReload/Win32/Win32HotReload.cpp @@ -6,10 +6,6 @@ #include #include -#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 tempDllPath = {}; - const size_t tempDllMaxBufferSize = basePathLength + code.TransientDLLName.Size + TEMP_DLL_BUFFER_SIZE_FOR_ID; - tempDllPath.Data = static_cast(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(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(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)