mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-09-27 10:51:22 +00:00
Implement support for loading custom plugin binaries
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#if defined(_MSC_VER) || defined(_WIN32)
|
||||
#include <Windows.h>
|
||||
#else
|
||||
@@ -282,6 +284,52 @@ void NetworkAgent::remove_legacy_library()
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> NetworkAgent::scan_plugin_versions()
|
||||
{
|
||||
std::vector<std::string> discovered_versions;
|
||||
std::string data_dir_str = data_dir();
|
||||
boost::filesystem::path plugin_folder = boost::filesystem::path(data_dir_str) / "plugins";
|
||||
|
||||
if (!boost::filesystem::is_directory(plugin_folder)) {
|
||||
return discovered_versions;
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER) || defined(_WIN32)
|
||||
std::string prefix = std::string(BAMBU_NETWORK_LIBRARY) + "_";
|
||||
std::string extension = ".dll";
|
||||
#elif defined(__WXMAC__)
|
||||
std::string prefix = std::string("lib") + std::string(BAMBU_NETWORK_LIBRARY) + "_";
|
||||
std::string extension = ".dylib";
|
||||
#else
|
||||
std::string prefix = std::string("lib") + std::string(BAMBU_NETWORK_LIBRARY) + "_";
|
||||
std::string extension = ".so";
|
||||
#endif
|
||||
|
||||
boost::system::error_code ec;
|
||||
for (auto& entry : boost::filesystem::directory_iterator(plugin_folder, ec)) {
|
||||
if (ec) {
|
||||
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": error iterating directory: " << ec.message();
|
||||
break;
|
||||
}
|
||||
if (!boost::filesystem::is_regular_file(entry.status()))
|
||||
continue;
|
||||
|
||||
std::string filename = entry.path().filename().string();
|
||||
|
||||
if (filename.rfind(prefix, 0) != 0)
|
||||
continue;
|
||||
if (filename.size() <= extension.size() ||
|
||||
filename.compare(filename.size() - extension.size(), extension.size(), extension) != 0)
|
||||
continue;
|
||||
|
||||
std::string version = filename.substr(prefix.size(),
|
||||
filename.size() - prefix.size() - extension.size());
|
||||
discovered_versions.push_back(version);
|
||||
}
|
||||
|
||||
return discovered_versions;
|
||||
}
|
||||
|
||||
int NetworkAgent::initialize_network_module(bool using_backup, const std::string& version)
|
||||
{
|
||||
clear_load_error();
|
||||
@@ -1770,3 +1818,63 @@ int NetworkAgent::get_model_mall_rating_result(int job_id, std::string &rating_r
|
||||
}
|
||||
|
||||
} //namespace
|
||||
|
||||
std::vector<BBL::NetworkLibraryVersionInfo> BBL::get_all_available_versions()
|
||||
{
|
||||
std::vector<NetworkLibraryVersionInfo> result;
|
||||
std::set<std::string> known_base_versions;
|
||||
std::set<std::string> all_known_versions;
|
||||
|
||||
for (size_t i = 0; i < AVAILABLE_NETWORK_VERSIONS_COUNT; ++i) {
|
||||
result.push_back(NetworkLibraryVersionInfo::from_static(AVAILABLE_NETWORK_VERSIONS[i]));
|
||||
known_base_versions.insert(AVAILABLE_NETWORK_VERSIONS[i].version);
|
||||
all_known_versions.insert(AVAILABLE_NETWORK_VERSIONS[i].version);
|
||||
}
|
||||
|
||||
std::vector<std::string> discovered = Slic3r::NetworkAgent::scan_plugin_versions();
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> suffixed_versions;
|
||||
|
||||
for (const auto& version : discovered) {
|
||||
if (all_known_versions.count(version) > 0)
|
||||
continue;
|
||||
|
||||
std::string base = extract_base_version(version);
|
||||
std::string suffix = extract_suffix(version);
|
||||
|
||||
if (suffix.empty())
|
||||
continue;
|
||||
|
||||
if (known_base_versions.count(base) == 0)
|
||||
continue;
|
||||
|
||||
suffixed_versions.emplace_back(base, version);
|
||||
all_known_versions.insert(version);
|
||||
}
|
||||
|
||||
std::sort(suffixed_versions.begin(), suffixed_versions.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
if (a.first != b.first) return a.first > b.first;
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
for (const auto& [base, full] : suffixed_versions) {
|
||||
size_t insert_pos = 0;
|
||||
for (size_t i = 0; i < result.size(); ++i) {
|
||||
if (result[i].base_version == base) {
|
||||
insert_pos = i + 1;
|
||||
while (insert_pos < result.size() &&
|
||||
result[insert_pos].base_version == base) {
|
||||
++insert_pos;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string sfx = extract_suffix(full);
|
||||
result.insert(result.begin() + insert_pos,
|
||||
NetworkLibraryVersionInfo::from_discovered(full, base, sfx));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -120,6 +120,7 @@ public:
|
||||
static bool versioned_library_exists(const std::string& version);
|
||||
static bool legacy_library_exists();
|
||||
static void remove_legacy_library();
|
||||
static std::vector<std::string> scan_plugin_versions();
|
||||
static int initialize_network_module(bool using_backup = false, const std::string& version = "");
|
||||
static int unload_network_module();
|
||||
static bool is_network_module_loaded();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
extern std::string g_log_folder;
|
||||
extern std::string g_log_start_time;
|
||||
@@ -329,6 +330,48 @@ inline const char* get_latest_network_version() {
|
||||
return AVAILABLE_NETWORK_VERSIONS[0].version;
|
||||
}
|
||||
|
||||
struct NetworkLibraryVersionInfo {
|
||||
std::string version;
|
||||
std::string base_version;
|
||||
std::string suffix;
|
||||
std::string display_name;
|
||||
std::string url_override;
|
||||
bool is_latest;
|
||||
std::string warning;
|
||||
bool is_discovered;
|
||||
|
||||
static NetworkLibraryVersionInfo from_static(const NetworkLibraryVersion& v) {
|
||||
return {
|
||||
v.version,
|
||||
v.version,
|
||||
"",
|
||||
v.display_name,
|
||||
v.url_override ? v.url_override : "",
|
||||
v.is_latest,
|
||||
v.warning ? v.warning : "",
|
||||
false
|
||||
};
|
||||
}
|
||||
|
||||
static NetworkLibraryVersionInfo from_discovered(const std::string& full_version,
|
||||
const std::string& base,
|
||||
const std::string& sfx) {
|
||||
return {full_version, base, sfx, full_version, "", false, "", true};
|
||||
}
|
||||
};
|
||||
|
||||
inline std::string extract_base_version(const std::string& full_version) {
|
||||
auto pos = full_version.find('-');
|
||||
return (pos == std::string::npos) ? full_version : full_version.substr(0, pos);
|
||||
}
|
||||
|
||||
inline std::string extract_suffix(const std::string& full_version) {
|
||||
auto pos = full_version.find('-');
|
||||
return (pos == std::string::npos) ? "" : full_version.substr(pos + 1);
|
||||
}
|
||||
|
||||
std::vector<NetworkLibraryVersionInfo> get_all_available_versions();
|
||||
|
||||
struct NetworkLibraryLoadError {
|
||||
bool has_error = false;
|
||||
std::string message;
|
||||
|
||||
Reference in New Issue
Block a user