瀏覽代碼

Added methods for opening dynamic libraries to OS

Karroffel 8 年之前
父節點
當前提交
2281942fb3
共有 5 個文件被更改,包括 68 次插入0 次删除
  1. 4 0
      core/os/os.h
  2. 30 0
      drivers/unix/os_unix.cpp
  3. 4 0
      drivers/unix/os_unix.h
  4. 26 0
      platform/windows/os_windows.cpp
  5. 4 0
      platform/windows/os_windows.h

+ 4 - 0
core/os/os.h

@@ -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);

+ 30 - 0
drivers/unix/os_unix.cpp

@@ -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)

+ 4 - 0
drivers/unix/os_unix.h

@@ -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();

+ 26 - 0
platform/windows/os_windows.cpp

@@ -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;

+ 4 - 0
platform/windows/os_windows.h

@@ -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();