1 /*************************************************************************/
2 /*  gdnative.h                                                           */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifndef GDNATIVE_H
32 #define GDNATIVE_H
33 
34 #include "core/io/resource_loader.h"
35 #include "core/io/resource_saver.h"
36 #include "core/os/thread_safe.h"
37 #include "core/resource.h"
38 
39 #include "gdnative/gdnative.h"
40 #include "gdnative_api_struct.gen.h"
41 
42 #include "core/io/config_file.h"
43 
44 class GDNativeLibraryResourceLoader;
45 class GDNative;
46 
47 class GDNativeLibrary : public Resource {
48 	GDCLASS(GDNativeLibrary, Resource);
49 
50 	static Map<String, Vector<Ref<GDNative> > > loaded_libraries;
51 
52 	friend class GDNativeLibraryResourceLoader;
53 	friend class GDNative;
54 
55 	Ref<ConfigFile> config_file;
56 
57 	String current_library_path;
58 	Vector<String> current_dependencies;
59 
60 	bool singleton;
61 	bool load_once;
62 	String symbol_prefix;
63 	bool reloadable;
64 
65 public:
66 	GDNativeLibrary();
67 	~GDNativeLibrary();
68 
69 	virtual bool _set(const StringName &p_name, const Variant &p_property);
70 	virtual bool _get(const StringName &p_name, Variant &r_property) const;
71 	virtual void _get_property_list(List<PropertyInfo> *p_list) const;
72 
get_config_file()73 	_FORCE_INLINE_ Ref<ConfigFile> get_config_file() { return config_file; }
74 
75 	void set_config_file(Ref<ConfigFile> p_config_file);
76 
77 	// things that change per-platform
78 	// so there are no setters for this
get_current_library_path()79 	_FORCE_INLINE_ String get_current_library_path() const {
80 		return current_library_path;
81 	}
get_current_dependencies()82 	_FORCE_INLINE_ Vector<String> get_current_dependencies() const {
83 		return current_dependencies;
84 	}
85 
86 	// things that are a property of the library itself, not platform specific
should_load_once()87 	_FORCE_INLINE_ bool should_load_once() const {
88 		return load_once;
89 	}
is_singleton()90 	_FORCE_INLINE_ bool is_singleton() const {
91 		return singleton;
92 	}
get_symbol_prefix()93 	_FORCE_INLINE_ String get_symbol_prefix() const {
94 		return symbol_prefix;
95 	}
96 
is_reloadable()97 	_FORCE_INLINE_ bool is_reloadable() const {
98 		return reloadable;
99 	}
100 
set_load_once(bool p_load_once)101 	_FORCE_INLINE_ void set_load_once(bool p_load_once) {
102 		config_file->set_value("general", "load_once", p_load_once);
103 		load_once = p_load_once;
104 	}
set_singleton(bool p_singleton)105 	_FORCE_INLINE_ void set_singleton(bool p_singleton) {
106 		config_file->set_value("general", "singleton", p_singleton);
107 		singleton = p_singleton;
108 	}
set_symbol_prefix(String p_symbol_prefix)109 	_FORCE_INLINE_ void set_symbol_prefix(String p_symbol_prefix) {
110 		config_file->set_value("general", "symbol_prefix", p_symbol_prefix);
111 		symbol_prefix = p_symbol_prefix;
112 	}
113 
set_reloadable(bool p_reloadable)114 	_FORCE_INLINE_ void set_reloadable(bool p_reloadable) {
115 		config_file->set_value("general", "reloadable", p_reloadable);
116 		reloadable = p_reloadable;
117 	}
118 
119 	static void _bind_methods();
120 };
121 
122 struct GDNativeCallRegistry {
123 	static GDNativeCallRegistry *singleton;
124 
get_singletonGDNativeCallRegistry125 	inline static GDNativeCallRegistry *get_singleton() {
126 		return singleton;
127 	}
128 
GDNativeCallRegistryGDNativeCallRegistry129 	inline GDNativeCallRegistry() :
130 			native_calls() {}
131 
132 	Map<StringName, native_call_cb> native_calls;
133 
134 	void register_native_call_type(StringName p_call_type, native_call_cb p_callback);
135 
136 	Vector<StringName> get_native_call_types();
137 };
138 
139 class GDNative : public Reference {
140 	GDCLASS(GDNative, Reference);
141 
142 	Ref<GDNativeLibrary> library;
143 
144 	void *native_handle;
145 
146 	bool initialized;
147 
148 public:
149 	GDNative();
150 	~GDNative();
151 
152 	static void _bind_methods();
153 
154 	void set_library(Ref<GDNativeLibrary> p_library);
155 	Ref<GDNativeLibrary> get_library() const;
156 
157 	bool is_initialized() const;
158 
159 	bool initialize();
160 	bool terminate();
161 
162 	Variant call_native(StringName p_native_call_type, StringName p_procedure_name, Array p_arguments = Array());
163 
164 	Error get_symbol(StringName p_procedure_name, void *&r_handle, bool p_optional = true) const;
165 };
166 
167 class GDNativeLibraryResourceLoader : public ResourceFormatLoader {
168 public:
169 	virtual RES load(const String &p_path, const String &p_original_path, Error *r_error);
170 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
171 	virtual bool handles_type(const String &p_type) const;
172 	virtual String get_resource_type(const String &p_path) const;
173 };
174 
175 class GDNativeLibraryResourceSaver : public ResourceFormatSaver {
176 public:
177 	virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags);
178 	virtual bool recognize(const RES &p_resource) const;
179 	virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
180 };
181 
182 #endif // GDNATIVE_H
183