ENH: network: refine current path logic
only use the same path as binary for current path JIRA: STUDIO-7875 Change-Id: I5523e3b7e20b0f24de50c8d295f54b984693165a
This commit is contained in:
parent
f88467cfa4
commit
62b98f783d
|
@ -145,6 +145,31 @@ NetworkAgent::~NetworkAgent()
|
||||||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", this %1%, network_agent=%2%, destroy_agent_ptr=%3%, ret %4%")%this %network_agent %destroy_agent_ptr %ret;
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", this %1%, network_agent=%2%, destroy_agent_ptr=%3%, ret %4%")%this %network_agent %destroy_agent_ptr %ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string NetworkAgent::get_libpath_in_current_directory(std::string library_name)
|
||||||
|
{
|
||||||
|
std::string lib_path;
|
||||||
|
#if defined(_MSC_VER) || defined(_WIN32)
|
||||||
|
wchar_t file_name[512];
|
||||||
|
DWORD ret = GetModuleFileNameW(NULL, file_name, 512);
|
||||||
|
if (!ret) {
|
||||||
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", GetModuleFileNameW return error, can not Load Library for %1%") % library_name;
|
||||||
|
return lib_path;
|
||||||
|
}
|
||||||
|
int size_needed = ::WideCharToMultiByte(0, 0, file_name, wcslen(file_name), nullptr, 0, nullptr, nullptr);
|
||||||
|
std::string file_name_string(size_needed, 0);
|
||||||
|
::WideCharToMultiByte(0, 0, file_name, wcslen(file_name), file_name_string.data(), size_needed, nullptr, nullptr);
|
||||||
|
|
||||||
|
std::size_t found = file_name_string.find("bambu-studio.exe");
|
||||||
|
if (found == (file_name_string.size() - 16)) {
|
||||||
|
lib_path = library_name + ".dll";
|
||||||
|
lib_path = file_name_string.replace(found, 16, lib_path);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
|
return lib_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int NetworkAgent::initialize_network_module(bool using_backup)
|
int NetworkAgent::initialize_network_module(bool using_backup)
|
||||||
{
|
{
|
||||||
//int ret = -1;
|
//int ret = -1;
|
||||||
|
@ -159,7 +184,7 @@ int NetworkAgent::initialize_network_module(bool using_backup)
|
||||||
|
|
||||||
//first load the library
|
//first load the library
|
||||||
#if defined(_MSC_VER) || defined(_WIN32)
|
#if defined(_MSC_VER) || defined(_WIN32)
|
||||||
library = plugin_folder.string() + "/" + std::string(BAMBU_NETWORK_LIBRARY) + ".dll";
|
library = plugin_folder.string() + "\\" + std::string(BAMBU_NETWORK_LIBRARY) + ".dll";
|
||||||
wchar_t lib_wstr[128];
|
wchar_t lib_wstr[128];
|
||||||
memset(lib_wstr, 0, sizeof(lib_wstr));
|
memset(lib_wstr, 0, sizeof(lib_wstr));
|
||||||
::MultiByteToWideChar(CP_UTF8, NULL, library.c_str(), strlen(library.c_str())+1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
::MultiByteToWideChar(CP_UTF8, NULL, library.c_str(), strlen(library.c_str())+1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
||||||
|
@ -172,9 +197,15 @@ int NetworkAgent::initialize_network_module(bool using_backup)
|
||||||
}*/
|
}*/
|
||||||
if (!netwoking_module) {
|
if (!netwoking_module) {
|
||||||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", try load library directly from current directory");
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", try load library directly from current directory");
|
||||||
library = std::string(BAMBU_NETWORK_LIBRARY) + ".dll";
|
|
||||||
|
std::string library_path = get_libpath_in_current_directory(std::string(BAMBU_NETWORK_LIBRARY));
|
||||||
|
if (library_path.empty()) {
|
||||||
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", can not get path in current directory for %1%") % BAMBU_NETWORK_LIBRARY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", current path %1%")%library_path;
|
||||||
memset(lib_wstr, 0, sizeof(lib_wstr));
|
memset(lib_wstr, 0, sizeof(lib_wstr));
|
||||||
::MultiByteToWideChar(CP_UTF8, NULL, library.c_str(), strlen(library.c_str())+1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
::MultiByteToWideChar(CP_UTF8, NULL, library_path.c_str(), strlen(library_path.c_str())+1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
||||||
netwoking_module = LoadLibrary(lib_wstr);
|
netwoking_module = LoadLibrary(lib_wstr);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
@ -455,9 +486,14 @@ void* NetworkAgent::get_bambu_source_entry()
|
||||||
source_module = LoadLibrary(lib_wstr);
|
source_module = LoadLibrary(lib_wstr);
|
||||||
if (!source_module) {
|
if (!source_module) {
|
||||||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", try load BambuSource directly from current directory");
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", try load BambuSource directly from current directory");
|
||||||
library = std::string(BAMBU_SOURCE_LIBRARY) + ".dll";
|
std::string library_path = get_libpath_in_current_directory(std::string(BAMBU_SOURCE_LIBRARY));
|
||||||
|
if (library_path.empty()) {
|
||||||
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", can not get path in current directory for %1%") % BAMBU_SOURCE_LIBRARY;
|
||||||
|
return source_module;
|
||||||
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", current path %1%")%library_path;
|
||||||
memset(lib_wstr, 0, sizeof(lib_wstr));
|
memset(lib_wstr, 0, sizeof(lib_wstr));
|
||||||
::MultiByteToWideChar(CP_UTF8, NULL, library.c_str(), strlen(library.c_str()) + 1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
::MultiByteToWideChar(CP_UTF8, NULL, library_path.c_str(), strlen(library_path.c_str()) + 1, lib_wstr, sizeof(lib_wstr) / sizeof(lib_wstr[0]));
|
||||||
source_module = LoadLibrary(lib_wstr);
|
source_module = LoadLibrary(lib_wstr);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -115,6 +115,7 @@ class NetworkAgent
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static std::string get_libpath_in_current_directory(std::string library_name);
|
||||||
static int initialize_network_module(bool using_backup = false);
|
static int initialize_network_module(bool using_backup = false);
|
||||||
static int unload_network_module();
|
static int unload_network_module();
|
||||||
#if defined(_MSC_VER) || defined(_WIN32)
|
#if defined(_MSC_VER) || defined(_WIN32)
|
||||||
|
|
Loading…
Reference in New Issue