conversion to arena of filesystem

This commit is contained in:
2026-08-16 22:30:40 -04:00
parent 262d91dd49
commit a50e3c75cb
5 changed files with 21 additions and 28 deletions
+11 -18
View File
@@ -26,15 +26,13 @@ namespace Juliet
String GetBasePath() String GetBasePath()
{ {
if (!IsValid(CachedBasePath)) Assert(IsValid(CachedBasePath));
{
CachedBasePath = Platform::GetBasePath();
}
return CachedBasePath; return CachedBasePath;
} }
String GetAssetBasePath() String GetAssetBasePath()
{ {
Assert(IsValid(CachedAssetBasePath));
return CachedAssetBasePath; return CachedAssetBasePath;
} }
@@ -50,7 +48,7 @@ namespace Juliet
return {}; return {};
} }
snprintf(buffer, totalSize, "%s%s", CStr(CachedAssetBasePath), CStr(filename)); juliet_snprintf(buffer, totalSize, "%s%s", CStr(CachedAssetBasePath), CStr(filename));
return { buffer, totalSize - 1 }; return { buffer, totalSize - 1 };
} }
@@ -63,8 +61,10 @@ namespace Juliet
return Platform::IsAbsolutePath(path); return Platform::IsAbsolutePath(path);
} }
void InitFilesystem() void InitFilesystem(NonNullPtr<Arena> arena)
{ {
CachedBasePath = Platform::GetBasePath(arena);
String basePath = GetBasePath(); String basePath = GetBasePath();
Assert(IsValid(basePath)); Assert(IsValid(basePath));
@@ -76,15 +76,14 @@ namespace Juliet
for (const char* candidate : kCandidates) for (const char* candidate : kCandidates)
{ {
char probePath[512]; char probePath[512];
snprintf(probePath, sizeof(probePath), "%s%s", CStr(basePath), candidate); juliet_snprintf(probePath, sizeof(probePath), "%s%s", CStr(basePath), candidate);
if (DirectoryExists(probePath)) if (DirectoryExists(probePath))
{ {
size_t len = strlen(probePath); size_t len = strlen(probePath);
auto* buffer = static_cast<char*>(Calloc(len + 1, sizeof(char))); if (char* buffer = ArenaPushArray<char>(arena, len + 1 JULIET_DEBUG_PARAM("CachedAssetBasePath")))
if (buffer)
{ {
snprintf(buffer, len + 1, "%s", probePath); juliet_snprintf(buffer, len + 1, "%s", probePath);
CachedAssetBasePath = { buffer, len }; CachedAssetBasePath = { buffer, len };
Log(LogLevel::Message, LogCategory::Core, "Asset base path: %s", buffer); Log(LogLevel::Message, LogCategory::Core, "Asset base path: %s", buffer);
} }
@@ -96,16 +95,10 @@ namespace Juliet
} }
void ShutdownFilesystem() void ShutdownFilesystem()
{
if (IsValid(CachedBasePath))
{ {
CachedBasePath.Size = 0; CachedBasePath.Size = 0;
SafeFree(CachedBasePath.Str); CachedBasePath.Str = nullptr;
}
if (IsValid(CachedAssetBasePath))
{
CachedAssetBasePath.Size = 0; CachedAssetBasePath.Size = 0;
SafeFree(CachedAssetBasePath.Str); CachedAssetBasePath.Str = nullptr;
}
} }
} // namespace Juliet } // namespace Juliet
@@ -2,6 +2,6 @@
namespace Juliet::Platform namespace Juliet::Platform
{ {
extern String GetBasePath(); extern String GetBasePath(NonNullPtr<Arena> arena);
extern bool IsAbsolutePath(String path); extern bool IsAbsolutePath(String path);
} // namespace Juliet::Platform } // namespace Juliet::Platform
@@ -2,6 +2,6 @@
namespace Juliet namespace Juliet
{ {
extern void InitFilesystem(); extern void InitFilesystem(NonNullPtr<Arena> arena);
extern void ShutdownFilesystem(); extern void ShutdownFilesystem();
} // namespace Juliet } // namespace Juliet
@@ -7,12 +7,12 @@
namespace Juliet::Platform namespace Juliet::Platform
{ {
String GetBasePath() String GetBasePath(NonNullPtr<Arena> arena)
{ {
// Allocate a buffer that could fit the module size. // Allocate a buffer that could fit the module size.
// Max Path is a good start but could be bigger if the path include long path prefix // Max Path is a good start but could be bigger if the path include long path prefix
size_t bufferSize = MAX_PATH; size_t bufferSize = MAX_PATH;
auto buffer = static_cast<char*>(Calloc(MAX_PATH, sizeof(char))); auto buffer = ArenaPushArray<char>(arena, MAX_PATH JULIET_DEBUG_PARAM("BasePath buffer"));
if (buffer == nullptr) if (buffer == nullptr)
{ {
return {}; return {};
@@ -26,8 +26,9 @@ namespace Juliet::Platform
// If the module filename length is bigger than the buffer size, we need to reallocate a bigger buffer // If the module filename length is bigger than the buffer size, we need to reallocate a bigger buffer
if (moduleFilenameLength >= bufferSize - 1) if (moduleFilenameLength >= bufferSize - 1)
{ {
char* addedBuffer = ArenaPushArray<char>(arena, bufferSize JULIET_DEBUG_PARAM("BasePath buffer"));
Assert(addedBuffer != nullptr);
bufferSize *= 2; bufferSize *= 2;
buffer = static_cast<char*>(Realloc(buffer, bufferSize * sizeof(char)));
} }
else else
{ {
@@ -37,7 +38,6 @@ namespace Juliet::Platform
if (moduleFilenameLength == 0) if (moduleFilenameLength == 0)
{ {
SafeFree(buffer);
Log(LogLevel::Error, LogCategory::Core, "Filesystem: Cannot locate executable path"); Log(LogLevel::Error, LogCategory::Core, "Filesystem: Cannot locate executable path");
} }
+1 -1
View File
@@ -160,7 +160,7 @@ namespace Juliet
UnitTest::RunUnitTests(); UnitTest::RunUnitTests();
#endif #endif
InitFilesystem(); InitFilesystem(EngineInstance.PlatformArena);
JulietInit(flags); JulietInit(flags);
} }