Also load plugins from location relative to the executable
This commit is contained in:
parent
34eaba54a2
commit
5b76990e97
6 changed files with 147 additions and 7 deletions
|
@ -4,7 +4,7 @@
|
|||
#include "library/loadlib.hpp"
|
||||
|
||||
void handle_post_loadlibrary();
|
||||
void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err) = NULL);
|
||||
void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err, bool system) = NULL);
|
||||
void with_loaded_library(const loadlib::module& l);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
std::set<std::string> enumerate_directory(const std::string& dir, const std::string& match);
|
||||
std::string get_absolute_path(const std::string& relative);
|
||||
|
|
14
include/library/running-executable.hpp
Normal file
14
include/library/running-executable.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef _library__running_executable__hpp__included__
|
||||
#define _library__running_executable__hpp__included__
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Get full path of current executable.
|
||||
*
|
||||
* Returns: Full path of current executable.
|
||||
* Throws std::runtime_error: Operation not supported!
|
||||
*/
|
||||
std::string running_executable();
|
||||
|
||||
#endif
|
|
@ -4,6 +4,7 @@
|
|||
#include "core/dispatch.hpp"
|
||||
#include "core/misc.hpp"
|
||||
#include "library/directory.hpp"
|
||||
#include "library/running-executable.hpp"
|
||||
#include "library/loadlib.hpp"
|
||||
#include "library/opus.hpp"
|
||||
#include <stdexcept>
|
||||
|
@ -27,6 +28,36 @@ namespace
|
|||
name = path.substr(p + 1);
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string get_system_library_dir()
|
||||
{
|
||||
std::string path;
|
||||
try {
|
||||
path = running_executable();
|
||||
} catch(...) {
|
||||
return "";
|
||||
}
|
||||
#if __WIN32__ || __WIN64__
|
||||
const char* sep = "\\/";
|
||||
#else
|
||||
const char* sep = "/";
|
||||
#endif
|
||||
size_t p = path.find_last_of(sep);
|
||||
if(p >= path.length())
|
||||
path = ".";
|
||||
else if(p == 0)
|
||||
path = "/";
|
||||
else
|
||||
path = path.substr(0, p);
|
||||
#if !__WIN32__ && !__WIN64__
|
||||
//If executable is in /bin, translate library path to corresponding /lib/lsnes.
|
||||
regex_results r = regex("(.*)/bin", path);
|
||||
if(r) path = r[1] + "/lib/lsnes";
|
||||
else
|
||||
#endif
|
||||
path = path + "/plugins";
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_post_loadlibrary()
|
||||
|
@ -48,11 +79,12 @@ void with_loaded_library(const loadlib::module& l)
|
|||
}
|
||||
}
|
||||
|
||||
void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err))
|
||||
namespace
|
||||
{
|
||||
try {
|
||||
void load_libraries(std::set<std::string> libs, bool system,
|
||||
void(*on_error)(const std::string& libname, const std::string& err, bool system))
|
||||
{
|
||||
std::string extension = loadlib::library::extension();
|
||||
auto libs = enumerate_directory(get_config_path() + "/autoload", ".*");
|
||||
for(auto i : libs) {
|
||||
if(i.length() < extension.length() + 1)
|
||||
continue;
|
||||
|
@ -67,10 +99,20 @@ void autoload_libraries(void(*on_error)(const std::string& libname, const std::s
|
|||
std::string x = "Can't load '" + i + "': " + e.what();
|
||||
|
||||
if(on_error)
|
||||
on_error(get_name(i), e.what());
|
||||
on_error(get_name(i), e.what(), system);
|
||||
messages << x << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err, bool system))
|
||||
{
|
||||
try {
|
||||
auto libs = enumerate_directory(get_config_path() + "/autoload", ".*");
|
||||
load_libraries(libs, false, on_error);
|
||||
libs = enumerate_directory(get_system_library_dir(), ".*");
|
||||
load_libraries(libs, true, on_error);
|
||||
handle_post_loadlibrary();
|
||||
} catch(std::exception& e) {
|
||||
messages << e.what() << std::endl;
|
||||
|
|
82
src/library/running-executable.cpp
Normal file
82
src/library/running-executable.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include "running-executable.hpp"
|
||||
#include "directory.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace
|
||||
{
|
||||
#if __unix__
|
||||
#include <unistd.h>
|
||||
std::string readlink_generic(std::string path)
|
||||
{
|
||||
char buf[8192];
|
||||
if(readlink(path.c_str(), buf, sizeof(buf)) > 0)
|
||||
return buf;
|
||||
throw std::runtime_error("Can't determine executable path");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if __linux__
|
||||
#include <unistd.h>
|
||||
std::string _running_executable()
|
||||
{
|
||||
return readlink_generic("/proc/self/exe");
|
||||
}
|
||||
#elif __APPLE__
|
||||
#include <mach-o/dyld.h>
|
||||
std::string _running_executable()
|
||||
{
|
||||
char buf[8192];
|
||||
uint32_t size = 8192;
|
||||
_NSGetExecutablePath(buf, &size);
|
||||
return buf;
|
||||
}
|
||||
#elif __WIN32__ || __WIN64__
|
||||
#include "windows.h"
|
||||
std::string _running_executable()
|
||||
{
|
||||
char buf[8192];
|
||||
GetModuleFileName(NULL, buf, 8191);
|
||||
return buf;
|
||||
}
|
||||
#elif __OpenBSD__
|
||||
std::string _running_executable()
|
||||
{
|
||||
//TODO: This isn't doable?
|
||||
throw std::runtime_error("running_executable() unsupported");
|
||||
}
|
||||
#elif __FreeBSD__
|
||||
#include <sys/param.h>
|
||||
#include <sys/sysctl.h>
|
||||
std::string _running_executable()
|
||||
{
|
||||
char buf[8192];
|
||||
int args[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
|
||||
size_t len = 8192;
|
||||
if(sysctl(args, 4, buf, &len, NULL, 0) < 0)
|
||||
throw std::runtime_error("Can't determine executable path");
|
||||
return buf;
|
||||
}
|
||||
#elif __NetBSD__
|
||||
std::string _running_executable()
|
||||
{
|
||||
return readlink_generic("/proc/curproc/exe");
|
||||
}
|
||||
#elif __sun__
|
||||
#include <stdlib.h>
|
||||
std::string _running_executable()
|
||||
{
|
||||
return getexecname();
|
||||
}
|
||||
#else
|
||||
//Unsupported
|
||||
std::string _running_executable()
|
||||
{
|
||||
throw std::runtime_error("running_executable() unsupported");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string running_executable()
|
||||
{
|
||||
return get_absolute_path(_running_executable());
|
||||
}
|
|
@ -427,10 +427,11 @@ bool lsnes_app::OnInit()
|
|||
controls.set_ports(ports);
|
||||
|
||||
std::string cfgpath = get_config_path();
|
||||
autoload_libraries([](const std::string& libname, const std::string& error) {
|
||||
autoload_libraries([](const std::string& libname, const std::string& error, bool system) {
|
||||
show_message_ok(NULL, "Error loading plugin " + libname, "Error loading '" + libname + "'\n\n" +
|
||||
error, wxICON_EXCLAMATION);
|
||||
wxeditor_plugin_manager_notify_fail(libname);
|
||||
if(!system)
|
||||
wxeditor_plugin_manager_notify_fail(libname);
|
||||
});
|
||||
messages << "Saving per-user data to: " << get_config_path() << std::endl;
|
||||
messages << "--- Loading configuration --- " << std::endl;
|
||||
|
|
Loading…
Add table
Reference in a new issue