Added methods for opening dynamic libraries to OS

This commit is contained in:
Karroffel 2017-03-08 02:50:13 +01:00
parent 92b6c8095a
commit 2281942fb3
5 changed files with 68 additions and 0 deletions

View File

@ -177,6 +177,10 @@ public:
virtual void set_borderless_window(int p_borderless) {}
virtual bool get_borderless_window() { return 0; }
virtual Error open_dynamic_library(const String p_path, void* &p_library_handle) { return ERR_UNAVAILABLE; };
virtual Error close_dynamic_library(void* p_library_handle) { return ERR_UNAVAILABLE; };
virtual Error get_dynamic_library_symbol_handle(void* p_library_handle, const String p_name, void* &p_symbol_handle) { return ERR_UNAVAILABLE; };
virtual void set_keep_screen_on(bool p_enabled);
virtual bool is_keep_screen_on() const;
virtual void set_low_processor_usage_mode(bool p_enabled);

View File

@ -54,6 +54,7 @@
#endif
#include "global_config.h"
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
@ -435,6 +436,35 @@ String OS_Unix::get_locale() const {
return locale;
}
Error OS_Unix::open_dynamic_library(const String p_path, void* &p_library_handle) {
p_library_handle = dlopen(p_path.utf8().get_data(), RTLD_NOW);
if(!p_library_handle) {
ERR_EXPLAIN("Can't open dynamic library: " + p_path + ". Error: " + dlerror());
ERR_FAIL_V(ERR_CANT_OPEN);
}
return OK;
}
Error OS_Unix::close_dynamic_library(void* p_library_handle) {
if(dlclose(p_library_handle)) {
return FAILED;
}
return OK;
}
Error OS_Unix::get_dynamic_library_symbol_handle(void* p_library_handle, const String p_name, void* &p_symbol_handle) {
char* error;
dlerror(); // Clear existing errors
p_symbol_handle = dlsym(p_library_handle, p_name.utf8().get_data());
if ((error = dlerror()) != NULL) {
ERR_EXPLAIN("Can't resolve symbol " + p_name + ". Error: " + dlerror());
ERR_FAIL_V(ERR_CANT_RESOLVE);
}
return OK;
}
Error OS_Unix::set_cwd(const String &p_cwd) {
if (chdir(p_cwd.utf8().get_data()) != 0)

View File

@ -82,6 +82,10 @@ public:
//virtual VideoMode get_video_mode() const;
//virtual void get_fullscreen_mode_list(List<VideoMode> *p_list) const;
virtual Error open_dynamic_library(const String p_path, void* &p_library_handle);
virtual Error close_dynamic_library(void* p_library_handle);
virtual Error get_dynamic_library_symbol_handle(void* p_library_handle, const String p_name, void* &p_symbol_handle);
virtual Error set_cwd(const String &p_cwd);
virtual String get_name();

View File

@ -1582,6 +1582,32 @@ bool OS_Windows::get_borderless_window() {
return video_mode.borderless_window;
}
Error open_dynamic_library(const String p_path, void* &p_library_handle) {
p_library_handle = (void *) LoadLibrary(p_path.utf8().get_data());
if (!p_library_handle) {
ERR_EXPLAIN("Can't open dynamic library: " + p_path + ". Error: " + String::num(GetLastError()));
ERR_FAIL_V(ERR_CANT_OPEN);
}
return OK;
}
Error close_dynamic_library(void* p_library_handle) {
if (!FreeLibrary((HMODULE) p_library_handle)) {
return FAILED;
}
return OK;
}
Error get_dynamic_library_symbol_handle(void* p_library_handle, const String p_name, void* &p_symbol_handle) {
char *error;
p_symbol_handle = (void *) GetProcAddress((HMODULE) p_library_handle, p_name.utf8().get_data());
if (!p_symbol_handle) {
ERR_EXPLAIN("Can't resolve symbol " + p_name + ". Error: " + String::num(GetLastError()));
ERR_FAIL_V(ERR_CANT_RESOLVE);
}
return OK;
}
void OS_Windows::request_attention() {
FLASHWINFO info;

View File

@ -222,6 +222,10 @@ public:
virtual void set_borderless_window(int p_borderless);
virtual bool get_borderless_window();
virtual Error open_dynamic_library(const String p_path, void* &p_library_handle);
virtual Error close_dynamic_library(void* p_library_handle);
virtual Error get_dynamic_library_symbol_handle(void* p_library_handle, const String p_name, void* &p_symbol_handle);
virtual MainLoop *get_main_loop() const;
virtual String get_name();