Skip to content

Commit

Permalink
FilesystemModuleResolver: Rename RegisterModule[Directory|File]
Browse files Browse the repository at this point in the history
to RegisterDirectory and RegisterFile
  • Loading branch information
SirLynix committed Oct 14, 2024
1 parent 9eff51a commit 533ba25
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 126 deletions.
4 changes: 2 additions & 2 deletions include/CNZSL/FilesystemModuleResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ CNZSL_API nzslFilesystemModuleResolver* nzslFilesystemModuleResolverCreate(void)
CNZSL_API void nzslFilesystemModuleResolverDestroy(nzslFilesystemModuleResolver* resolverPtr);
CNZSL_API const char* nzslFilesystemModuleResolverGetLastError(const nzslFilesystemModuleResolver* resolverPtr);

CNZSL_API void nzslFilesystemModuleResolverRegisterDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen);
CNZSL_API void nzslFilesystemModuleResolverRegisterFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen);
CNZSL_API void nzslFilesystemModuleResolverRegisterModule(nzslFilesystemModuleResolver* resolverPtr, const nzslModule* module);
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen);
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromSource(nzslFilesystemModuleResolver* resolverPtr, const char* source, size_t sourceLen);
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions include/NZSL/FilesystemModuleResolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ namespace nzsl
~FilesystemModuleResolver();

void RegisterArchive(const Archive& archive);
void RegisterModule(const std::filesystem::path& realPath);
void RegisterDirectory(const std::filesystem::path& realPath, bool watchDirectory = false);
void RegisterFile(const std::filesystem::path& realPath);
void RegisterModule(std::string_view moduleSource);
void RegisterModule(Ast::ModulePtr module);
void RegisterModuleDirectory(const std::filesystem::path& realPath, bool watchDirectory = false);

Ast::ModulePtr Resolve(const std::string& moduleName) override;

Expand Down
41 changes: 21 additions & 20 deletions src/CNZSL/FilesystemModuleResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <CNZSL/FilesystemModuleResolver.h>
#include <CNZSL/Structs/FilesystemModuleResolver.hpp>
#include <CNZSL/Structs/Module.hpp>
#include <NazaraUtils/PathUtils.hpp>
#include <fmt/format.h>
#include <string_view>
#include <filesystem>
Expand All @@ -25,75 +26,75 @@ extern "C"
delete resolverPtr;
}

CNZSL_API void nzslFilesystemModuleResolverRegisterModule(nzslFilesystemModuleResolver* resolverPtr, const nzslModule* module)
CNZSL_API void nzslFilesystemModuleResolverRegisterDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen)
{
assert(resolverPtr);

try
{
resolverPtr->resolver->RegisterModule(module->module);
resolverPtr->resolver->RegisterDirectory(std::filesystem::path{ sourcePath, sourcePath + sourcePathLen });
}
catch(const std::exception& e)
catch (const std::exception& e)
{
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModule failed: {}", e.what());
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterDirectory failed: {}", e.what());
}
catch(...)
catch (...)
{
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModule failed with unknown error";
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterDirectory failed with unknown error";
}
}

CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen)
CNZSL_API void nzslFilesystemModuleResolverRegisterFile(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen)
{
assert(resolverPtr);

try
{
resolverPtr->resolver->RegisterModule(std::filesystem::path{ sourcePath, sourcePath + sourcePathLen });
resolverPtr->resolver->RegisterFile(Nz::Utf8Path(std::string_view(sourcePath, sourcePathLen)));
}
catch(const std::exception& e)
catch (const std::exception& e)
{
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleFromFile failed: {}", e.what());
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterFile failed: {}", e.what());
}
catch(...)
catch (...)
{
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleFromFile failed with unknown error";
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterFile failed with unknown error";
}
}

CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromSource(nzslFilesystemModuleResolver* resolverPtr, const char* source, size_t sourceLen)
CNZSL_API void nzslFilesystemModuleResolverRegisterModule(nzslFilesystemModuleResolver* resolverPtr, const nzslModule* module)
{
assert(resolverPtr);

try
{
resolverPtr->resolver->RegisterModule(std::string_view{ source, sourceLen });
resolverPtr->resolver->RegisterModule(module->module);
}
catch(const std::exception& e)
{
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleFromSource failed: {}", e.what());
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModule failed: {}", e.what());
}
catch(...)
{
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleFromSource failed with unknown error";
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModule failed with unknown error";
}
}

CNZSL_API void nzslFilesystemModuleResolverRegisterModuleDirectory(nzslFilesystemModuleResolver* resolverPtr, const char* sourcePath, size_t sourcePathLen)
CNZSL_API void nzslFilesystemModuleResolverRegisterModuleFromSource(nzslFilesystemModuleResolver* resolverPtr, const char* source, size_t sourceLen)
{
assert(resolverPtr);

try
{
resolverPtr->resolver->RegisterModuleDirectory(std::filesystem::path{ sourcePath, sourcePath + sourcePathLen });
resolverPtr->resolver->RegisterModule(std::string_view{ source, sourceLen });
}
catch(const std::exception& e)
{
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleDirectory failed: {}", e.what());
resolverPtr->lastError = fmt::format("nzslFilesystemModuleResolverRegisterModuleFromSource failed: {}", e.what());
}
catch(...)
{
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleDirectory failed with unknown error";
resolverPtr->lastError = "nzslFilesystemModuleResolverRegisterModuleFromSource failed with unknown error";
}
}

Expand Down
144 changes: 70 additions & 74 deletions src/NZSL/FilesystemModuleResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,81 @@ namespace nzsl
}
}

void FilesystemModuleResolver::RegisterModule(const std::filesystem::path& realPath)
void FilesystemModuleResolver::RegisterDirectory(const std::filesystem::path& realPath, bool watchDirectory)
{
if (!std::filesystem::is_directory(realPath))
return;

if (watchDirectory)
{
#ifdef NZSL_EFSW
if (!m_fileWatcher)
{
m_fileWatcher = efsw_create(0);
efsw_watch(m_fileWatcher);
}

auto FileSystemCallback = [](efsw_watcher /*watcher*/, efsw_watchid /*watchid*/, const char* dir, const char* filename, efsw_action action, const char* oldFileName, void* param)
{
FilesystemModuleResolver* resolver = static_cast<FilesystemModuleResolver*>(param);

switch (action)
{
case EFSW_ADD:
resolver->OnFileAdded(dir, filename);
break;

case EFSW_DELETE:
resolver->OnFileRemoved(dir, filename);
break;

case EFSW_MODIFIED:
resolver->OnFileUpdated(dir, filename);
break;

case EFSW_MOVED:
resolver->OnFileMoved(dir, filename, (oldFileName) ? oldFileName : std::string_view());
break;
}
};

efsw_addwatch(m_fileWatcher, Nz::PathToString(realPath).c_str(), FileSystemCallback, 1, this);
#else
throw std::runtime_error("nzsl was built without filesystem watch feature");
#endif
}

for (const auto& entry : std::filesystem::recursive_directory_iterator(realPath))
{
if (entry.is_regular_file() && CheckExtension(Nz::PathToString(entry.path())))
{
try
{
RegisterFile(entry.path());
}
catch (const std::exception& e)
{
throw std::runtime_error(fmt::format("failed to register module {}: {}", Nz::PathToString(entry.path()), e.what()));
}
}
}
}

void FilesystemModuleResolver::RegisterFile(const std::filesystem::path& realPath)
{
Ast::ModulePtr module;
try
{
std::uintmax_t filesize = std::filesystem::file_size(realPath);
if (filesize == 0)
return; //< ignore empty files

std::ifstream inputFile(realPath, std::ios::in | std::ios::binary);
if (!inputFile)
throw std::runtime_error("failed to open " + Nz::PathToString(realPath));

inputFile.seekg(0, std::ios::end);

std::streamsize length = inputFile.tellg();
if (length == 0)
return; //< ignore empty files

inputFile.seekg(0, std::ios::beg);

std::vector<char> content(Nz::SafeCast<std::size_t>(length));
if (!inputFile.read(&content[0], length))
std::vector<char> content(Nz::SafeCast<std::size_t>(filesize));
if (!inputFile.read(&content[0], Nz::SafeCast<std::size_t>(filesize)))
throw std::runtime_error("failed to read " + Nz::PathToString(realPath));

std::string ext = Nz::PathToString(realPath.extension());
Expand Down Expand Up @@ -126,66 +182,6 @@ namespace nzsl
m_modules.emplace(std::move(moduleName), std::move(module));
}

void FilesystemModuleResolver::RegisterModuleDirectory(const std::filesystem::path& realPath, bool watchDirectory)
{
if (!std::filesystem::is_directory(realPath))
return;

if (watchDirectory)
{
#ifdef NZSL_EFSW
if (!m_fileWatcher)
{
m_fileWatcher = efsw_create(0);
efsw_watch(m_fileWatcher);
}

auto FileSystemCallback = [](efsw_watcher /*watcher*/, efsw_watchid /*watchid*/, const char* dir, const char* filename, efsw_action action, const char* oldFileName, void* param)
{
FilesystemModuleResolver* resolver = static_cast<FilesystemModuleResolver*>(param);

switch (action)
{
case EFSW_ADD:
resolver->OnFileAdded(dir, filename);
break;

case EFSW_DELETE:
resolver->OnFileRemoved(dir, filename);
break;

case EFSW_MODIFIED:
resolver->OnFileUpdated(dir, filename);
break;

case EFSW_MOVED:
resolver->OnFileMoved(dir, filename, (oldFileName) ? oldFileName : std::string_view());
break;
}
};

efsw_addwatch(m_fileWatcher, Nz::PathToString(realPath).c_str(), FileSystemCallback, 1, this);
#else
throw std::runtime_error("nzsl was built without filesystem watch feature");
#endif
}

for (const auto& entry : std::filesystem::recursive_directory_iterator(realPath))
{
if (entry.is_regular_file() && CheckExtension(Nz::PathToString(entry.path())))
{
try
{
RegisterModule(entry.path());
}
catch (const std::exception& e)
{
throw std::runtime_error(fmt::format("failed to register module {}: {}", Nz::PathToString(entry.path()), e.what()));
}
}
}
}

Ast::ModulePtr FilesystemModuleResolver::Resolve(const std::string& moduleName)
{
auto it = m_modules.find(moduleName);
Expand All @@ -204,7 +200,7 @@ namespace nzsl

try
{
RegisterModule(filepath);
RegisterFile(filepath);
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -260,7 +256,7 @@ namespace nzsl

try
{
RegisterModule(filepath);
RegisterFile(filepath);
}
catch (const std::exception& e)
{
Expand All @@ -281,6 +277,6 @@ namespace nzsl
});
};

return EndsWith(filename, ModuleExtension) || EndsWith(filename, BinaryModuleExtension);
return EndsWith(filename, ModuleExtension) || EndsWith(filename, BinaryModuleExtension) || EndsWith(filename, ArchiveExtension);
}
}
Loading

0 comments on commit 533ba25

Please sign in to comment.