mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-08-13 03:29:16 -04:00
Merge pull request #2282 from encounter/mods-data-dir
Add HostService.data_dir
This commit is contained in:
@@ -166,6 +166,8 @@ struct LoadedMod {
|
||||
std::filesystem::path dir;
|
||||
// Stable UTF-8 storage for HostService::mod_dir.
|
||||
std::string dirUtf8;
|
||||
// Stable UTF-8 storage for HostService::data_dir.
|
||||
std::string dataDirUtf8;
|
||||
|
||||
uint32_t searchDirIndex = 0;
|
||||
// Native lib is dlopen'd in place and stays resident for the session. Reload is unsupported.
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#include "registry.hpp"
|
||||
#include "slot_map.hpp"
|
||||
|
||||
#include "dusk/main.h"
|
||||
#include "dusk/mods/loader/loader.hpp"
|
||||
#include "dusk/mods/log_buffer.hpp"
|
||||
#include "dusk/mods/manifest.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <borealis/io.hpp>
|
||||
#include <borealis/version.h>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
namespace dusk::mods::svc {
|
||||
namespace {
|
||||
@@ -68,6 +72,41 @@ const char* host_native_dir(ModContext* context) {
|
||||
return mod != nullptr ? mod->nativeDirUtf8.c_str() : "";
|
||||
}
|
||||
|
||||
ModResult host_data_dir(ModContext* context, const char** outPath) {
|
||||
if (outPath == nullptr) {
|
||||
return MOD_INVALID_ARGUMENT;
|
||||
}
|
||||
*outPath = nullptr;
|
||||
|
||||
auto* mod = mod_from_context(context);
|
||||
if (mod == nullptr) {
|
||||
return MOD_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (mod->dataDirUtf8.empty()) {
|
||||
namespace fs = std::filesystem;
|
||||
std::error_code ec;
|
||||
const fs::path path = fs::absolute(dusk::ConfigPath / "mod_data" / mod->metadata.id, ec);
|
||||
if (ec) {
|
||||
log::write(mod->metadata.id, LOG_LEVEL_ERROR,
|
||||
"failed to resolve persistent mod data directory: {}", ec.message());
|
||||
return MOD_ERROR;
|
||||
}
|
||||
|
||||
fs::create_directories(path, ec);
|
||||
if (ec) {
|
||||
log::write(mod->metadata.id, LOG_LEVEL_ERROR,
|
||||
"failed to create persistent mod data directory {}: {}",
|
||||
borealis::io::fs_path_to_string(path), ec.message());
|
||||
return MOD_ERROR;
|
||||
}
|
||||
mod->dataDirUtf8 = borealis::io::fs_path_to_string(path);
|
||||
}
|
||||
|
||||
*outPath = mod->dataDirUtf8.c_str();
|
||||
return MOD_OK;
|
||||
}
|
||||
|
||||
struct LifecycleWatcher {
|
||||
ModLifecycleFn fn = nullptr;
|
||||
void* userData = nullptr;
|
||||
@@ -148,6 +187,7 @@ constinit HostService s_hostService{
|
||||
.watch_mod_lifecycle = host_watch_mod_lifecycle,
|
||||
.unwatch_mod_lifecycle = host_unwatch_mod_lifecycle,
|
||||
.native_dir = host_native_dir,
|
||||
.data_dir = host_data_dir,
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user