Skip to content

Commit

Permalink
refactor: make mod manager auto release
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 19, 2024
1 parent 915f576 commit ff93ced
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src-client/ll/core/main_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
} // namespace ll

BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ulReasonForCall, LPVOID /*lpReserved*/) {
if (ulReasonForCall == DLL_PROCESS_DETACH) {
mod::ModRegistrar::getInstance().releaseAllMods();
}
if (ulReasonForCall != DLL_PROCESS_ATTACH) return TRUE;

::ll::setGamingStatus(::ll::GamingStatus::Default);
Expand Down
5 changes: 5 additions & 0 deletions src-server/ll/core/main_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
) {
setGamingStatus(GamingStatus::Stopping);
mod::ModRegistrar::getInstance().disableAllMods();

command::CommandRegistrar::getInstance().clear();

mod::ModRegistrar::getInstance().releaseAllMods();

origin();
}

Expand Down
2 changes: 1 addition & 1 deletion src-test/common/ExecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CoroTask<Expected<int>> coroutine() {
);
parbegin = std::chrono::steady_clock::now();
std::vector<ll::coro::CoroTask<int>> tasks{};
for (size_t i = 0; i < 10000000; i++) {
for (size_t i = 0; i < 1000000; i++) {
tasks.emplace_back(val1());
}
auto vec = co_await collectAll(std::move(tasks));
Expand Down
2 changes: 2 additions & 0 deletions src/ll/api/mod/Mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Mod::Mod(Manifest manifest) : mImpl(std::make_unique<Impl>()) {
}
Mod::~Mod() = default;

void Mod::release() noexcept { mImpl.reset(); }

Manifest const& Mod::getManifest() const { return mImpl->manifest; }

std::string const& Mod::getName() const { return getManifest().name; }
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/mod/Mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ LLAPI std::filesystem::path const& getModsRoot();

class ModManager;
class Mod {

public:
enum class State {
Enabled,
Expand Down Expand Up @@ -58,6 +57,8 @@ class Mod {
LLAPI void onDisable(CallbackFn func) noexcept;

protected:
LLAPI void release() noexcept;

LLAPI void setState(State state) const;

// is callback set
Expand Down
9 changes: 9 additions & 0 deletions src/ll/api/mod/ModManagerRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ ModManagerRegistry& ModManagerRegistry::getInstance() {
return instance;
}

Expected<> ModManagerRegistry::releaseManagers() noexcept try {
std::lock_guard lock(impl->modMtx);
impl->loadedMods.clear();
impl->managers.clear();
return {};
} catch (...) {
return makeExceptionError();
}

Expected<> ModManagerRegistry::loadMod(Manifest manifest) noexcept try {
std::lock_guard lock(impl->modMtx);
if (hasManager(manifest.type)) {
Expand Down
2 changes: 2 additions & 0 deletions src/ll/api/mod/ModManagerRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ModManagerRegistry {
ModManagerRegistry();
~ModManagerRegistry();

Expected<> releaseManagers() noexcept;

Expected<> loadMod(Manifest manifest) noexcept;

Expected<> unloadMod(std::string_view name) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/mod/NativeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NativeMod::NativeMod(Manifest manifest, sys_utils::HandleT handle)
: Mod(std::move(manifest)),
mImpl(std::make_unique<Impl>(handle)) {}

NativeMod::~NativeMod() = default;
NativeMod::~NativeMod() { release(); }

sys_utils::DynamicLibrary& NativeMod::getDynamicLibrary() { return mImpl->lib; }

Expand Down
15 changes: 13 additions & 2 deletions src/ll/core/mod/ModRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ std::vector<std::string> ModRegistrar::getSortedModNames() const {
}

void ModRegistrar::enableAllMods() noexcept try {
auto names = getSortedModNames();
std::lock_guard lock(impl->mutex);
auto names = getSortedModNames();
if (names.empty()) {
return;
}
Expand Down Expand Up @@ -289,7 +290,8 @@ void ModRegistrar::enableAllMods() noexcept try {
error_utils::printCurrentException(getLogger());
}
void ModRegistrar::disableAllMods() noexcept try {
auto names = getSortedModNames();
std::lock_guard lock(impl->mutex);
auto names = getSortedModNames();
if (!names.empty()) {
getLogger().info("Disabling mods..."_tr());
for (auto& name : std::ranges::reverse_view(names)) {
Expand All @@ -305,6 +307,15 @@ void ModRegistrar::disableAllMods() noexcept try {
} catch (...) {
error_utils::printCurrentException(getLogger());
}
void ModRegistrar::releaseAllMods() noexcept try {
std::lock_guard lock(impl->mutex);
if (auto res = ModManagerRegistry::getInstance().releaseManagers(); !res) {
res.error().log(getLogger(), io::LogLevel::Warn);
}
impl->deps.clear();
} catch (...) {
error_utils::printCurrentException(getLogger());
}

Expected<> ModRegistrar::loadMod(std::string_view name) noexcept {
std::lock_guard lock(impl->mutex);
Expand Down
2 changes: 2 additions & 0 deletions src/ll/core/mod/ModRegistrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ModRegistrar {

void disableAllMods() noexcept;

void releaseAllMods() noexcept;

Expected<> loadMod(std::string_view name) noexcept;

Expected<> unloadMod(std::string_view name) noexcept;
Expand Down

0 comments on commit ff93ced

Please sign in to comment.