Skip to content

Commit

Permalink
feat: load only ncm3-compatible plugins for ncm3
Browse files Browse the repository at this point in the history
close #433
  • Loading branch information
std-microblock committed Oct 27, 2023
1 parent 79057b6 commit 23fd03f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void from_json(const nlohmann::json& j, PluginManifest& p) {
p.preview = j.value("preview", "unknown");
p.injects = j.value("injects", std::map<std::string, std::vector<std::map<std::string, std::string>>>());
p.startup_script = j.value("startup_script", "startup_script.js");
p.ncm3Compatible = j.value("ncm3-compatible", false);

p.hijacks.clear();
if (j.count("hijacks")) {
Expand Down Expand Up @@ -242,6 +243,7 @@ void PluginManager::extractPackedPlugins() {
}

fs::create_directories(datapath + L"/plugins_runtime");
static const bool isNCM3 = util::getNCMExecutableVersion().major == 3;

for (auto file : fs::directory_iterator(datapath + L"/plugins")) {
BNString path = file.path().wstring();
Expand All @@ -256,7 +258,10 @@ void PluginManager::extractPackedPlugins() {
util::read_to_string(datapath + L"/plugins_runtime/tmp/manifest.json"));
modManifest.get_to(manifest);

if (std::ranges::find(disable_list, manifest.slug) != disable_list.end()) {
if (std::ranges::find(disable_list, manifest.slug) != disable_list.end() || (
!isNCM3 ||
manifest.ncm3Compatible // duplicated / not ncm3 / ncm3-compatible
)) {
fs::remove_all(datapath.utf8() + "/plugins_runtime/tmp");
continue;
}
Expand Down Expand Up @@ -348,15 +353,15 @@ std::vector<std::shared_ptr<Plugin>> PluginManager::loadInPath(const std::wstrin
PluginManifest manifest;
json.get_to(manifest);

std::optional<std::filesystem::path> packed_file_path=std::nullopt;
std::optional<std::filesystem::path> packed_file_path = std::nullopt;
auto plugin_meta_path = file.path() / ".plugin.path.meta";
if (fs::exists(plugin_meta_path)) packed_file_path = util::read_to_string(plugin_meta_path);
plugins.push_back(std::make_shared<Plugin>(manifest, file.path(), packed_file_path));
}
}
catch (std::exception& e) {
util::write_file_text(datapath.utf8() + "log.log",
std::string("\n[" + file.path().string() + "]Plugin Native load Error: ") + (e.
std::string("\n[" + file.path().string() + "] Plugin Native load Error: ") + (e.
what()), true);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct PluginManifest {
std::string betterncm_version;
std::string preview;
std::string startup_script;
bool ncm3Compatible;

std::map<std::string, std::vector<std::map<std::string, std::string>>> injects;
HijackVersionMap hijacks;
Expand Down
6 changes: 5 additions & 1 deletion src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ std::wstring util::utf8_to_wstring(const std::string& utf8) {
}

semver::version util::getNCMExecutableVersion() {
static std::optional<semver::version> cached;
if (cached.has_value()) return cached.value();

DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = nullptr;
Expand All @@ -305,11 +308,12 @@ semver::version util::getNCMExecutableVersion() {
if (size) {
auto verInfo = (VS_FIXEDFILEINFO*)lpBuffer;
if (verInfo->dwSignature == 0xfeef04bd) {
return semver::version{
cached = semver::version{
static_cast<uint8_t>((verInfo->dwFileVersionMS >> 16) & 0xffff),
static_cast<uint8_t>((verInfo->dwFileVersionMS >> 0) & 0xffff),
static_cast<uint8_t>((verInfo->dwFileVersionLS >> 16) & 0xffff)
};
return cached.value();
}
}
}
Expand Down

0 comments on commit 23fd03f

Please sign in to comment.