Skip to content

Commit 4cdcf36

Browse files
committed
refactor: optimize plugin load
1 parent 6d54cc2 commit 4cdcf36

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/lse/PluginManager.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <ll/api/plugin/Plugin.h>
99
#include <ll/api/plugin/PluginManager.h>
1010
#include <memory>
11-
#include <stdexcept>
1211

1312
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_LUA
1413

@@ -57,7 +56,8 @@ auto PluginManager::load(ll::plugin::Manifest manifest) -> bool {
5756
auto& logger = getSelfPluginInstance().getLogger();
5857

5958
if (hasPlugin(manifest.name)) {
60-
throw std::runtime_error("plugin already loaded");
59+
logger.error("plugin already loaded");
60+
return false;
6161
}
6262

6363
auto plugin = std::make_shared<Plugin>(manifest);
@@ -67,11 +67,13 @@ auto PluginManager::load(ll::plugin::Manifest manifest) -> bool {
6767

6868
logger.info("loading plugin {}", manifest.name);
6969

70-
auto pluginDir = std::filesystem::canonical(ll::plugin::getPluginsRoot() / ll::string_utils::str2wstr(manifest.name));
70+
auto pluginDir =
71+
std::filesystem::canonical(ll::plugin::getPluginsRoot() / ll::string_utils::str2wstr(manifest.name));
7172
auto entryPath = pluginDir / ll::string_utils::str2wstr(manifest.entry);
7273

7374
if (!::PluginManager::loadPlugin(ll::string_utils::u8str2str(entryPath.u8string()), false, true)) {
74-
throw std::runtime_error(fmt::format("failed to load plugin {}", manifest.name));
75+
logger.error("failed to load plugin {}", manifest.name);
76+
return false;
7577
}
7678
return true;
7779
});
@@ -82,18 +84,24 @@ auto PluginManager::load(ll::plugin::Manifest manifest) -> bool {
8284
logger.info("unloading plugin {}", pluginName);
8385

8486
if (!::PluginManager::unloadPlugin(pluginName)) {
85-
throw std::runtime_error(fmt::format("failed to unload plugin {}", pluginName));
87+
logger.error("failed to unload plugin {}", pluginName);
88+
return false;
8689
}
8790

8891
return true;
8992
});
9093

94+
plugin->onEnable([](ll::plugin::Plugin& plugin) { return true; });
95+
plugin->onDisable([](ll::plugin::Plugin& plugin) { return true; });
96+
9197
if (!plugin->onLoad()) {
92-
throw std::runtime_error(fmt::format("failed to load plugin {}", manifest.name));
98+
logger.error("failed to load plugin {}", manifest.name);
99+
return false;
93100
}
94101

95102
if (!addPlugin(manifest.name, plugin)) {
96-
throw std::runtime_error(fmt::format("failed to register plugin {}", manifest.name));
103+
logger.error("failed to register plugin {}", manifest.name);
104+
return false;
97105
}
98106

99107
return true;
@@ -105,14 +113,16 @@ auto PluginManager::unload(std::string_view name) -> bool {
105113
auto plugin = std::static_pointer_cast<Plugin>(getPlugin(name));
106114

107115
if (!plugin->onUnload()) {
108-
throw std::runtime_error(fmt::format("failed to unload plugin {}", name));
116+
logger.error("failed to unload plugin {}", name);
117+
return false;
109118
}
110119

111120
if (!erasePlugin(name)) {
112-
throw std::runtime_error(fmt::format("failed to unregister plugin {}", name));
121+
logger.error("failed to unregister plugin {}", name);
122+
return false;
113123
}
114124

115-
return false;
125+
return true;
116126
}
117127

118128
} // namespace lse

0 commit comments

Comments
 (0)