dl_script.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*************************************************************************/
  2. /* dl_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "dl_script.h"
  31. #include "global_config.h"
  32. #include "global_constants.h"
  33. #include "io/file_access_encrypted.h"
  34. #include "os/file_access.h"
  35. #include "os/os.h"
  36. #include "scene/main/scene_main_loop.h"
  37. #include "scene/resources/scene_format_text.h"
  38. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  39. #include "api_generator.h"
  40. #endif
  41. #ifdef TOOLS_ENABLED
  42. #include "editor/editor_node.h"
  43. #endif
  44. Error NativeLibrary::initialize(NativeLibrary *&p_native_lib, const StringName p_path) {
  45. if (DLScriptLanguage::get_singleton()->initialized_libraries.has(p_path)) {
  46. p_native_lib = DLScriptLanguage::get_singleton()->initialized_libraries[p_path];
  47. return OK;
  48. }
  49. NativeLibrary *lib = memnew(NativeLibrary);
  50. lib->path = p_path;
  51. p_native_lib = lib;
  52. // Open the file
  53. Error error;
  54. error = OS::get_singleton()->open_dynamic_library(p_path, lib->handle);
  55. if (error) return error;
  56. ERR_FAIL_COND_V(!lib->handle, ERR_BUG);
  57. // Get the method
  58. void *library_init;
  59. error = OS::get_singleton()->get_dynamic_library_symbol_handle(lib->handle, DLScriptLanguage::get_init_symbol_name(), library_init);
  60. if (error) return error;
  61. ERR_FAIL_COND_V(!library_init, ERR_BUG);
  62. void (*library_init_fpointer)(godot_dlscript_init_options *) = (void (*)(godot_dlscript_init_options *))library_init;
  63. godot_dlscript_init_options options;
  64. options.in_editor = SceneTree::get_singleton()->is_editor_hint();
  65. /*
  66. options.core_api_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  67. options.editor_api_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  68. options.no_api_hash = ClassDB::get_api_hash(ClassDB::API_NONE);
  69. */
  70. library_init_fpointer(&options); // Catch errors?
  71. DLScriptLanguage::get_singleton()->initialized_libraries[p_path] = lib;
  72. return OK;
  73. }
  74. Error NativeLibrary::terminate(NativeLibrary *&p_native_lib) {
  75. if (!DLScriptLanguage::get_singleton()->initialized_libraries.has(p_native_lib->path)) {
  76. OS::get_singleton()->close_dynamic_library(p_native_lib->handle);
  77. p_native_lib->handle = 0;
  78. return OK;
  79. }
  80. Error error = OK;
  81. void *library_terminate;
  82. error = OS::get_singleton()->get_dynamic_library_symbol_handle(p_native_lib->handle, DLScriptLanguage::get_terminate_symbol_name(), library_terminate);
  83. if (error)
  84. return OK; // no terminate? okay, not that important lol
  85. void (*library_terminate_pointer)(godot_dlscript_terminate_options *) = (void (*)(godot_dlscript_terminate_options *))library_terminate;
  86. godot_dlscript_terminate_options options;
  87. options.in_editor = SceneTree::get_singleton()->is_editor_hint();
  88. library_terminate_pointer(&options);
  89. DLScriptLanguage::get_singleton()->initialized_libraries.erase(p_native_lib->path);
  90. OS::get_singleton()->close_dynamic_library(p_native_lib->handle);
  91. p_native_lib->handle = 0;
  92. return OK;
  93. }
  94. // Script
  95. #ifdef TOOLS_ENABLED
  96. void DLScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  97. List<PropertyInfo> pinfo;
  98. Map<StringName, Variant> values;
  99. for (Map<StringName, DLScriptData::Property>::Element *E = script_data->properties.front(); E; E = E->next()) {
  100. PropertyInfo p = E->get().info;
  101. p.name = String(E->key());
  102. pinfo.push_back(p);
  103. values[p.name] = E->get().default_value;
  104. }
  105. p_placeholder->update(pinfo, values);
  106. }
  107. void DLScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  108. placeholders.erase(p_placeholder);
  109. }
  110. #endif
  111. bool DLScript::can_instance() const {
  112. #ifdef DLSCRIPT_EDITOR_FEATURES
  113. return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled());
  114. #else
  115. // allow defaultlibrary without editor features
  116. if (!library.is_valid()) {
  117. String path = GLOBAL_GET("dlscript/default_dllibrary");
  118. RES lib = ResourceLoader::load(path);
  119. if (lib.is_valid() && lib->cast_to<DLLibrary>()) {
  120. return true;
  121. }
  122. }
  123. return script_data;
  124. #endif
  125. //return script_data || (!tool && !ScriptServer::is_scripting_enabled());
  126. // change to true enable in editor stuff.
  127. }
  128. Ref<Script> DLScript::get_base_script() const {
  129. Ref<DLScript> base_script;
  130. base_script->library = library;
  131. base_script->script_data = script_data;
  132. base_script->script_name = script_data->base;
  133. return base_script;
  134. }
  135. StringName DLScript::get_instance_base_type() const {
  136. return script_data->base_native_type;
  137. }
  138. ScriptInstance *DLScript::instance_create(Object *p_this) {
  139. #ifdef TOOLS_ENABLED
  140. // find a good way to initialize stuff in the editor
  141. #ifdef DLSCRIPT_EDITOR_FEATURES
  142. if (!ScriptServer::is_scripting_enabled() && !is_tool()) {
  143. // placeholder, for nodes. But for tools we want the real thing
  144. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(DLScriptLanguage::singleton, Ref<Script>((Script *)this), p_this));
  145. placeholders.insert(sins);
  146. if (!library.is_valid())
  147. return sins;
  148. if (!library->native_library) {
  149. Error err = library->_initialize();
  150. if (err != OK) {
  151. return sins;
  152. }
  153. }
  154. if (!script_data) {
  155. script_data = library->get_script_data(script_name);
  156. }
  157. if (script_data && script_data->create_func.create_func) {
  158. script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  159. }
  160. _update_placeholder(sins);
  161. return sins;
  162. }
  163. #endif
  164. #endif
  165. if (!library.is_valid()) {
  166. String path = GLOBAL_GET("dlscript/default_dllibrary");
  167. RES lib = ResourceLoader::load(path);
  168. if (lib.is_valid() && lib->cast_to<DLLibrary>()) {
  169. set_library(lib);
  170. }
  171. }
  172. DLInstance *new_instance = memnew(DLInstance);
  173. new_instance->owner = p_this;
  174. new_instance->script = Ref<DLScript>(this);
  175. #ifndef DLSCRIPT_EDITOR_FEATURES
  176. if (!ScriptServer::is_scripting_enabled()) {
  177. new_instance->userdata = 0;
  178. } else {
  179. new_instance->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  180. }
  181. #else
  182. new_instance->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  183. #endif
  184. instances.insert(p_this);
  185. return new_instance;
  186. }
  187. bool DLScript::instance_has(const Object *p_this) const {
  188. return instances.has((Object *)p_this); // TODO
  189. }
  190. bool DLScript::has_source_code() const {
  191. return false;
  192. }
  193. String DLScript::get_source_code() const {
  194. return "";
  195. }
  196. Error DLScript::reload(bool p_keep_state) {
  197. return FAILED;
  198. }
  199. bool DLScript::has_method(const StringName &p_method) const {
  200. if (!script_data)
  201. return false;
  202. DLScriptData *data = script_data;
  203. while (data) {
  204. if (data->methods.has(p_method))
  205. return true;
  206. data = data->base_data;
  207. }
  208. return false;
  209. }
  210. MethodInfo DLScript::get_method_info(const StringName &p_method) const {
  211. if (!script_data)
  212. return MethodInfo();
  213. DLScriptData *data = script_data;
  214. while (data) {
  215. if (data->methods.has(p_method))
  216. return data->methods[p_method].info;
  217. data = data->base_data;
  218. }
  219. ERR_FAIL_COND_V(!script_data->methods.has(p_method), MethodInfo());
  220. return MethodInfo();
  221. }
  222. void DLScript::get_script_method_list(List<MethodInfo> *p_list) const {
  223. if (!script_data) return;
  224. Set<MethodInfo> methods;
  225. DLScriptData *data = script_data;
  226. while (data) {
  227. for (Map<StringName, DLScriptData::Method>::Element *E = data->methods.front(); E; E = E->next()) {
  228. methods.insert(E->get().info);
  229. }
  230. data = data->base_data;
  231. }
  232. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  233. p_list->push_back(E->get());
  234. }
  235. }
  236. void DLScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  237. if (!script_data) return;
  238. Set<PropertyInfo> properties;
  239. DLScriptData *data = script_data;
  240. while (data) {
  241. for (Map<StringName, DLScriptData::Property>::Element *E = data->properties.front(); E; E = E->next()) {
  242. properties.insert(E->get().info);
  243. }
  244. data = data->base_data;
  245. }
  246. for (Set<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  247. p_list->push_back(E->get());
  248. }
  249. }
  250. bool DLScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  251. if (!script_data) return false;
  252. DLScriptData *data = script_data;
  253. while (data) {
  254. if (data->properties.has(p_property)) {
  255. r_value = data->properties[p_property].default_value;
  256. return true;
  257. }
  258. data = data->base_data;
  259. }
  260. return false;
  261. }
  262. bool DLScript::is_tool() const {
  263. ERR_FAIL_COND_V(!script_data, false);
  264. return script_data->is_tool;
  265. }
  266. String DLScript::get_node_type() const {
  267. return ""; // ?
  268. }
  269. ScriptLanguage *DLScript::get_language() const {
  270. return DLScriptLanguage::singleton;
  271. }
  272. bool DLScript::has_script_signal(const StringName &p_signal) const {
  273. if (!script_data)
  274. return false;
  275. DLScriptData *data = script_data;
  276. while (data) {
  277. if (data->signals_.has(p_signal)) {
  278. return true;
  279. }
  280. data = data->base_data;
  281. }
  282. return false;
  283. }
  284. void DLScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  285. if (!script_data)
  286. return;
  287. Set<MethodInfo> signals_;
  288. DLScriptData *data = script_data;
  289. while (data) {
  290. for (Map<StringName, DLScriptData::Signal>::Element *S = data->signals_.front(); S; S = S->next()) {
  291. signals_.insert(S->get().signal);
  292. }
  293. data = data->base_data;
  294. }
  295. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  296. r_signals->push_back(E->get());
  297. }
  298. }
  299. Ref<DLLibrary> DLScript::get_library() const {
  300. return library;
  301. }
  302. void DLScript::set_library(Ref<DLLibrary> p_library) {
  303. library = p_library;
  304. #ifdef TOOLS_ENABLED
  305. if (!ScriptServer::is_scripting_enabled())
  306. return;
  307. #endif
  308. if (library.is_valid()) {
  309. Error initalize_status = library->_initialize();
  310. ERR_FAIL_COND(initalize_status != OK);
  311. if (script_name) {
  312. script_data = library->native_library->scripts[script_name];
  313. ERR_FAIL_COND(!script_data);
  314. }
  315. }
  316. }
  317. StringName DLScript::get_script_name() const {
  318. return script_name;
  319. }
  320. void DLScript::set_script_name(StringName p_script_name) {
  321. script_name = p_script_name;
  322. if (library.is_valid()) {
  323. #ifdef DLSCRIPT_EDITOR_FEATURES
  324. if (!library->native_library) {
  325. library->_initialize();
  326. }
  327. #endif
  328. if (library->native_library) {
  329. script_data = library->get_script_data(script_name);
  330. ERR_FAIL_COND(!script_data);
  331. }
  332. }
  333. }
  334. void DLScript::_bind_methods() {
  335. ClassDB::bind_method(D_METHOD("get_library"), &DLScript::get_library);
  336. ClassDB::bind_method(D_METHOD("set_library", "library"), &DLScript::set_library);
  337. ClassDB::bind_method(D_METHOD("get_script_name"), &DLScript::get_script_name);
  338. ClassDB::bind_method(D_METHOD("set_script_name", "script_name"), &DLScript::set_script_name);
  339. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "DLLibrary"), "set_library", "get_library");
  340. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "script_name"), "set_script_name", "get_script_name");
  341. }
  342. DLScript::DLScript() {
  343. script_data = NULL;
  344. DLScriptLanguage::get_singleton()->script_list.insert(this);
  345. }
  346. DLScript::~DLScript() {
  347. //hmm
  348. DLScriptLanguage::get_singleton()->script_list.erase(this);
  349. }
  350. // Library
  351. DLLibrary *DLLibrary::currently_initialized_library = NULL;
  352. DLLibrary *DLLibrary::get_currently_initialized_library() {
  353. return currently_initialized_library;
  354. }
  355. static const char *_dl_platforms_info[] = {
  356. "|unix|so|Unix",
  357. "unix|x11|so|X11",
  358. "unix|server|so|Server",
  359. "unix|android|so|Android",
  360. "unix|blackberry|so|Blackberry 10",
  361. "unix|haiku|so|Haiku", // Right?
  362. "|mac|dynlib|Mac",
  363. "mac|ios|dynlib|iOS",
  364. "mac|osx|dynlib|OSX",
  365. "|html5|js|HTML5",
  366. "|windows|dll|Windows",
  367. "windows|uwp|dll|UWP",
  368. NULL // Finishing condition
  369. };
  370. void DLLibrary::set_platform_file(StringName p_platform, String p_file) {
  371. if (p_file.empty()) {
  372. platform_files.erase(p_platform);
  373. } else {
  374. platform_files[p_platform] = p_file;
  375. }
  376. }
  377. String DLLibrary::get_platform_file(StringName p_platform) const {
  378. if (platform_files.has(p_platform)) {
  379. return platform_files[p_platform];
  380. } else {
  381. return "";
  382. }
  383. }
  384. Error DLLibrary::_initialize() {
  385. _THREAD_SAFE_METHOD_
  386. // Get the file
  387. const String platform_name = OS::get_singleton()->get_name();
  388. String platform_file("");
  389. char **platform_info = (char **)_dl_platforms_info;
  390. if (platform_files.has(platform_name.to_lower())) {
  391. platform_file = platform_files[platform_name.to_lower()];
  392. }
  393. while (*platform_info) {
  394. String platform_info_string(*platform_info);
  395. if (platform_name == platform_info_string.get_slicec('|', 3)) {
  396. String platform_key = platform_info_string.get_slicec('|', 1);
  397. String fallback_platform_key = platform_info_string.get_slicec('|', 0);
  398. if (platform_files.has(platform_key)) {
  399. platform_file = platform_files[platform_key];
  400. } else if (!fallback_platform_key.empty() && platform_files.has(fallback_platform_key)) {
  401. platform_file = platform_files[fallback_platform_key];
  402. } else {
  403. return ERR_UNAVAILABLE;
  404. }
  405. }
  406. platform_info++;
  407. }
  408. ERR_FAIL_COND_V(platform_file == "", ERR_DOES_NOT_EXIST);
  409. StringName path = GlobalConfig::get_singleton()->globalize_path(platform_file);
  410. DLLibrary::currently_initialized_library = this;
  411. Error ret = NativeLibrary::initialize(native_library, path);
  412. native_library->dllib = this;
  413. DLLibrary::currently_initialized_library = NULL;
  414. return ret;
  415. }
  416. Error DLLibrary::_terminate() {
  417. ERR_FAIL_COND_V(!native_library, ERR_BUG);
  418. ERR_FAIL_COND_V(!native_library->handle, ERR_BUG);
  419. // de-init stuff
  420. for (Map<StringName, DLScriptData *>::Element *E = native_library->scripts.front(); E; E = E->next()) {
  421. for (Map<StringName, DLScriptData::Method>::Element *M = E->get()->methods.front(); M; M = M->next()) {
  422. if (M->get().method.free_func) {
  423. M->get().method.free_func(M->get().method.method_data);
  424. }
  425. }
  426. if (E->get()->create_func.free_func) {
  427. E->get()->create_func.free_func(E->get()->create_func.method_data);
  428. }
  429. if (E->get()->destroy_func.free_func) {
  430. E->get()->destroy_func.free_func(E->get()->destroy_func.method_data);
  431. }
  432. for (Set<DLScript *>::Element *S = DLScriptLanguage::get_singleton()->script_list.front(); S; S = S->next()) {
  433. if (S->get()->script_data == E->get()) {
  434. S->get()->script_data = NULL;
  435. }
  436. }
  437. memdelete(E->get());
  438. }
  439. Error ret = NativeLibrary::terminate(native_library);
  440. native_library->scripts.clear();
  441. return ret;
  442. }
  443. void DLLibrary::_register_script(const StringName p_name, const StringName p_base, godot_instance_create_func p_instance_func, godot_instance_destroy_func p_destroy_func) {
  444. ERR_FAIL_COND(!native_library);
  445. ERR_FAIL_COND(native_library->scripts.has(p_name));
  446. DLScriptData *s = memnew(DLScriptData);
  447. s->base = p_base;
  448. s->create_func = p_instance_func;
  449. s->destroy_func = p_destroy_func;
  450. Map<StringName, DLScriptData *>::Element *E = native_library->scripts.find(p_base);
  451. if (E) {
  452. s->base_data = E->get();
  453. s->base_native_type = s->base_data->base_native_type;
  454. } else {
  455. if (!ClassDB::class_exists(p_base)) {
  456. memdelete(s);
  457. ERR_EXPLAIN("Invalid base for registered type '" + p_name + "'");
  458. ERR_FAIL();
  459. }
  460. s->base_native_type = p_base;
  461. }
  462. native_library->scripts.insert(p_name, s);
  463. }
  464. void DLLibrary::_register_tool_script(const StringName p_name, const StringName p_base, godot_instance_create_func p_instance_func, godot_instance_destroy_func p_destroy_func) {
  465. ERR_FAIL_COND(!native_library);
  466. ERR_FAIL_COND(native_library->scripts.has(p_name));
  467. DLScriptData *s = memnew(DLScriptData);
  468. s->base = p_base;
  469. s->create_func = p_instance_func;
  470. s->destroy_func = p_destroy_func;
  471. s->is_tool = true;
  472. Map<StringName, DLScriptData *>::Element *E = native_library->scripts.find(p_base);
  473. if (E) {
  474. s->base_data = E->get();
  475. s->base_native_type = s->base_data->base_native_type;
  476. } else {
  477. if (!ClassDB::class_exists(p_base)) {
  478. memdelete(s);
  479. ERR_EXPLAIN("Invalid base for registered type '" + p_name + "'");
  480. ERR_FAIL();
  481. }
  482. s->base_native_type = p_base;
  483. }
  484. native_library->scripts.insert(p_name, s);
  485. }
  486. void DLLibrary::_register_script_method(const StringName p_name, const StringName p_method, godot_method_attributes p_attr, godot_instance_method p_func, MethodInfo p_info) {
  487. ERR_FAIL_COND(!native_library);
  488. ERR_FAIL_COND(!native_library->scripts.has(p_name));
  489. p_info.name = p_method;
  490. DLScriptData::Method method;
  491. method = DLScriptData::Method(p_func, p_info, p_attr.rpc_type);
  492. native_library->scripts[p_name]->methods.insert(p_method, method);
  493. }
  494. void DLLibrary::_register_script_property(const StringName p_name, const String p_path, godot_property_attributes *p_attr, godot_property_set_func p_setter, godot_property_get_func p_getter) {
  495. ERR_FAIL_COND(!native_library);
  496. ERR_FAIL_COND(!native_library->scripts.has(p_name));
  497. DLScriptData::Property p;
  498. PropertyInfo pi;
  499. pi.name = p_path;
  500. if (p_attr != NULL) {
  501. pi = PropertyInfo((Variant::Type)p_attr->type, p_path, (PropertyHint)p_attr->hint, *(String *)&p_attr->hint_string, p_attr->usage);
  502. p = DLScriptData::Property(p_setter, p_getter, pi, *(Variant *)&p_attr->default_value, p_attr->rset_type);
  503. }
  504. native_library->scripts[p_name]->properties.insert(p_path, p);
  505. }
  506. void DLLibrary::_register_script_signal(const StringName p_name, const godot_signal *p_signal) {
  507. ERR_FAIL_COND(!native_library);
  508. ERR_FAIL_COND(!native_library->scripts.has(p_name));
  509. ERR_FAIL_COND(!p_signal);
  510. DLScriptData::Signal signal;
  511. signal.signal.name = *(String *)&p_signal->name;
  512. {
  513. List<PropertyInfo> arguments;
  514. for (int i = 0; i < p_signal->num_args; i++) {
  515. PropertyInfo info;
  516. godot_signal_argument attrib = p_signal->args[i];
  517. String *name = (String *)&attrib.name;
  518. info.name = *name;
  519. info.type = (Variant::Type)attrib.type;
  520. info.hint = (PropertyHint)attrib.hint;
  521. info.hint_string = *(String *)&attrib.hint_string;
  522. info.usage = attrib.usage;
  523. arguments.push_back(info);
  524. }
  525. signal.signal.arguments = arguments;
  526. }
  527. {
  528. Vector<Variant> default_arguments;
  529. for (int i = 0; i < p_signal->num_default_args; i++) {
  530. Variant *v;
  531. godot_signal_argument attrib = p_signal->args[i];
  532. v = (Variant *)&attrib.default_value;
  533. default_arguments.push_back(*v);
  534. }
  535. signal.signal.default_arguments = default_arguments;
  536. }
  537. native_library->scripts[p_name]->signals_.insert(*(String *)&p_signal->name, signal);
  538. }
  539. DLScriptData *DLLibrary::get_script_data(const StringName p_name) {
  540. ERR_FAIL_COND_V(!native_library, NULL);
  541. ERR_FAIL_COND_V(!native_library->scripts.has(p_name), NULL);
  542. return native_library->scripts[p_name];
  543. }
  544. bool DLLibrary::_set(const StringName &p_name, const Variant &p_value) {
  545. String name = p_name;
  546. if (name.begins_with("platform/")) {
  547. set_platform_file(name.get_slice("/", 1), p_value);
  548. return true;
  549. }
  550. return false;
  551. }
  552. bool DLLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  553. String name = p_name;
  554. if (name.begins_with("platform/")) {
  555. r_ret = get_platform_file(name.get_slice("/", 1));
  556. return true;
  557. }
  558. return false;
  559. }
  560. void DLLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  561. char **platform_info = (char **)_dl_platforms_info;
  562. Set<String> registered_platform_names;
  563. {
  564. List<StringName> ep;
  565. // ep.push_back("X11");
  566. // EditorImportExport::get_singleton()->get_export_platforms(&ep);
  567. // @Todo
  568. // get export platforms with the new export system somehow.
  569. for (List<StringName>::Element *E = ep.front(); E; E = E->next()) {
  570. registered_platform_names.insert(String(E->get()).to_lower());
  571. }
  572. }
  573. while (*platform_info) {
  574. String platform_info_string(*platform_info);
  575. String fallback_platform_key = platform_info_string.get_slicec('|', 0);
  576. String platform_key = platform_info_string.get_slicec('|', 1);
  577. String platform_extension = platform_info_string.get_slicec('|', 2);
  578. String platform_name = platform_info_string.get_slicec('|', 3);
  579. registered_platform_names.erase(platform_name);
  580. if (fallback_platform_key.empty()) {
  581. p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension));
  582. } else {
  583. if (platform_files.has(platform_key)) {
  584. p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED));
  585. } else {
  586. p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_key, PROPERTY_HINT_FILE, "*." + platform_extension, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CHECKABLE));
  587. }
  588. }
  589. platform_info++;
  590. }
  591. while (registered_platform_names.size()) {
  592. const String platform_name = registered_platform_names.front()->get();
  593. registered_platform_names.erase(platform_name);
  594. p_list->push_back(PropertyInfo(Variant::STRING, "platform/" + platform_name.to_lower(), PROPERTY_HINT_FILE, "*"));
  595. }
  596. }
  597. void DLLibrary::_notification(int what) {
  598. // TODO
  599. }
  600. void DLLibrary::_bind_methods() {
  601. ClassDB::bind_method(D_METHOD("set_platform_file", "platform", "file"), &DLLibrary::set_platform_file);
  602. ClassDB::bind_method(D_METHOD("get_platform_file", "platform"), &DLLibrary::get_platform_file);
  603. }
  604. DLLibrary::DLLibrary() {
  605. native_library = NULL;
  606. }
  607. DLLibrary::~DLLibrary() {
  608. if (!native_library) {
  609. return;
  610. }
  611. if (native_library->handle) {
  612. _terminate();
  613. }
  614. }
  615. // Instance
  616. bool DLInstance::set(const StringName &p_name, const Variant &p_value) {
  617. if (!script->script_data)
  618. return false;
  619. if (script->script_data->properties.has(p_name)) {
  620. script->script_data->properties[p_name].setter.set_func((godot_object *)owner, script->script_data->properties[p_name].setter.method_data, userdata, *(godot_variant *)&p_value);
  621. return true;
  622. }
  623. return false;
  624. }
  625. bool DLInstance::get(const StringName &p_name, Variant &r_ret) const {
  626. if (!script->script_data)
  627. return false;
  628. if (script->script_data->properties.has(p_name)) {
  629. godot_variant value = script->script_data->properties[p_name].getter.get_func((godot_object *)owner, script->script_data->properties[p_name].getter.method_data, userdata);
  630. r_ret = *(Variant *)&value;
  631. return true;
  632. }
  633. return false;
  634. }
  635. void DLInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  636. script->get_script_property_list(p_properties);
  637. // TODO: dynamic properties
  638. }
  639. Variant::Type DLInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  640. if (script->script_data->properties.has(p_name)) {
  641. *r_is_valid = true;
  642. return script->script_data->properties[p_name].info.type;
  643. }
  644. *r_is_valid = false;
  645. return Variant::NIL;
  646. }
  647. void DLInstance::get_method_list(List<MethodInfo> *p_list) const {
  648. script->get_script_method_list(p_list);
  649. }
  650. bool DLInstance::has_method(const StringName &p_method) const {
  651. return script->has_method(p_method);
  652. }
  653. Variant DLInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  654. // TODO: validated methods & errors
  655. DLScriptData *data_ptr = script->script_data;
  656. while (data_ptr) {
  657. Map<StringName, DLScriptData::Method>::Element *E = data_ptr->methods.find(p_method);
  658. if (E) {
  659. godot_variant result = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  660. return *(Variant *)&result;
  661. }
  662. data_ptr = data_ptr->base_data;
  663. }
  664. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  665. return Variant();
  666. }
  667. void DLInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  668. // TODO: validated methods & errors
  669. DLScriptData *data_ptr = script->script_data;
  670. while (data_ptr) {
  671. Map<StringName, DLScriptData::Method>::Element *E = data_ptr->methods.find(p_method);
  672. if (E) {
  673. E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  674. }
  675. data_ptr = data_ptr->base_data;
  676. }
  677. }
  678. void DLInstance::_ml_call_reversed(DLScriptData *data_ptr, const StringName &p_method, const Variant **p_args, int p_argcount) {
  679. // TODO: validated methods & errors
  680. if (data_ptr->base_data)
  681. _ml_call_reversed(data_ptr->base_data, p_method, p_args, p_argcount);
  682. // Variant::CallError ce;
  683. Map<StringName, DLScriptData::Method>::Element *E = data_ptr->methods.find(p_method);
  684. if (E) {
  685. E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  686. }
  687. }
  688. void DLInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  689. if (script.ptr() && script->script_data) {
  690. _ml_call_reversed(script->script_data, p_method, p_args, p_argcount);
  691. }
  692. }
  693. void DLInstance::notification(int p_notification) {
  694. Variant value = p_notification;
  695. const Variant *args[1] = { &value };
  696. call_multilevel(DLScriptLanguage::singleton->strings._notification, args, 1);
  697. }
  698. Ref<Script> DLInstance::get_script() const {
  699. return script;
  700. }
  701. ScriptLanguage *DLInstance::get_language() {
  702. return DLScriptLanguage::singleton;
  703. }
  704. ScriptInstance::RPCMode DLInstance::get_rpc_mode(const StringName &p_method) const {
  705. DLScriptData::Method m = script->script_data->methods[p_method];
  706. switch (m.rpc_mode) {
  707. case GODOT_METHOD_RPC_MODE_DISABLED:
  708. return RPC_MODE_DISABLED;
  709. case GODOT_METHOD_RPC_MODE_REMOTE:
  710. return RPC_MODE_REMOTE;
  711. case GODOT_METHOD_RPC_MODE_SYNC:
  712. return RPC_MODE_SYNC;
  713. case GODOT_METHOD_RPC_MODE_MASTER:
  714. return RPC_MODE_MASTER;
  715. case GODOT_METHOD_RPC_MODE_SLAVE:
  716. return RPC_MODE_SLAVE;
  717. default:
  718. return RPC_MODE_DISABLED;
  719. }
  720. }
  721. ScriptInstance::RPCMode DLInstance::get_rset_mode(const StringName &p_variable) const {
  722. DLScriptData::Property p = script->script_data->properties[p_variable];
  723. switch (p.rset_mode) {
  724. case GODOT_METHOD_RPC_MODE_DISABLED:
  725. return RPC_MODE_DISABLED;
  726. case GODOT_METHOD_RPC_MODE_REMOTE:
  727. return RPC_MODE_REMOTE;
  728. case GODOT_METHOD_RPC_MODE_SYNC:
  729. return RPC_MODE_SYNC;
  730. case GODOT_METHOD_RPC_MODE_MASTER:
  731. return RPC_MODE_MASTER;
  732. case GODOT_METHOD_RPC_MODE_SLAVE:
  733. return RPC_MODE_SLAVE;
  734. default:
  735. return RPC_MODE_DISABLED;
  736. }
  737. }
  738. DLInstance::DLInstance() {
  739. owner = NULL;
  740. userdata = NULL;
  741. }
  742. DLInstance::~DLInstance() {
  743. if (script.is_valid()) {
  744. if (owner) {
  745. script->instances.erase(owner);
  746. }
  747. if (!script->script_data)
  748. return;
  749. script->script_data->destroy_func.destroy_func((godot_object *)owner, script->script_data->destroy_func.method_data, userdata);
  750. if (script->script_data->destroy_func.free_func)
  751. script->script_data->destroy_func.free_func(script->script_data->destroy_func.method_data);
  752. if (script->script_data->create_func.free_func)
  753. script->script_data->create_func.free_func(script->script_data->create_func.method_data);
  754. }
  755. }
  756. // Language
  757. DLScriptLanguage *DLScriptLanguage::singleton = NULL;
  758. String DLScriptLanguage::get_name() const {
  759. return "DLScript";
  760. }
  761. void _add_reload_node() {
  762. #ifdef TOOLS_ENABLED
  763. DLReloadNode *rn = memnew(DLReloadNode);
  764. EditorNode::get_singleton()->add_child(rn);
  765. #endif
  766. }
  767. void DLScriptLanguage::init() {
  768. // TODO: Expose globals
  769. GLOBAL_DEF("dlscript/default_dllibrary", "");
  770. PropertyInfo prop_info(Variant::STRING, "dlscript/default_dllibrary", PROPERTY_HINT_FILE, "tres,res,dllib");
  771. GlobalConfig::get_singleton()->set_custom_property_info("dlscript/default_dllibrary", prop_info);
  772. // generate bindings
  773. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  774. List<String> args = OS::get_singleton()->get_cmdline_args();
  775. List<String>::Element *E = args.find("--dlscript-generate-json-api");
  776. if (E && E->next()) {
  777. if (generate_c_api(E->next()->get()) != OK) {
  778. ERR_PRINT("Failed to generate C API\n");
  779. }
  780. }
  781. #endif
  782. #ifdef TOOLS_ENABLED
  783. // if (SceneTree::get_singleton()->is_editor_hint()) {
  784. EditorNode::add_init_callback(&_add_reload_node);
  785. // }
  786. #endif
  787. }
  788. String DLScriptLanguage::get_type() const {
  789. return "DLScript";
  790. }
  791. String DLScriptLanguage::get_extension() const {
  792. return "dl";
  793. }
  794. Error DLScriptLanguage::execute_file(const String &p_path) {
  795. return OK; // ??
  796. }
  797. void DLScriptLanguage::finish() {
  798. // cleanup is for noobs
  799. }
  800. // scons doesn't want to link in the api source so we need to call a dummy function to cause it to link
  801. extern "C" void _api_anchor();
  802. void DLScriptLanguage::_compile_dummy_for_the_api() {
  803. _api_anchor();
  804. }
  805. Ref<Script> DLScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  806. DLScript *src = memnew(DLScript);
  807. src->set_script_name(p_class_name);
  808. return Ref<DLScript>(src);
  809. }
  810. bool DLScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const {
  811. return false; // TODO
  812. }
  813. Script *DLScriptLanguage::create_script() const {
  814. DLScript *scr = memnew(DLScript);
  815. return scr;
  816. }
  817. bool DLScriptLanguage::has_named_classes() const {
  818. return true;
  819. }
  820. int DLScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  821. return -1; // No source code!
  822. }
  823. String DLScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  824. return ""; // No source code!
  825. }
  826. void DLScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  827. // TODO TODO TODO
  828. }
  829. // TODO: Any debugging? (research)
  830. String DLScriptLanguage::debug_get_error() const {
  831. return "";
  832. }
  833. int DLScriptLanguage::debug_get_stack_level_count() const {
  834. return 1; // ?
  835. }
  836. int DLScriptLanguage::debug_get_stack_level_line(int p_level) const {
  837. return -1;
  838. }
  839. String DLScriptLanguage::debug_get_stack_level_function(int p_level) const {
  840. return "[native code]"; // ?
  841. }
  842. String DLScriptLanguage::debug_get_stack_level_source(int p_level) const {
  843. return "";
  844. }
  845. void DLScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {}
  846. void DLScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {}
  847. String DLScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  848. return ""; // ??
  849. }
  850. void DLScriptLanguage::reload_all_scripts() {
  851. // @Todo
  852. }
  853. void DLScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  854. // @Todo
  855. OS::get_singleton()->print("reload tool scripts\n");
  856. }
  857. void DLScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  858. p_extensions->push_back("dl"); // Container file format
  859. }
  860. void DLScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  861. }
  862. void DLScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  863. }
  864. // TODO: all profilling
  865. void DLScriptLanguage::profiling_start() {
  866. }
  867. void DLScriptLanguage::profiling_stop() {
  868. }
  869. int DLScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  870. return 0;
  871. }
  872. int DLScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  873. return 0;
  874. }
  875. void DLScriptLanguage::frame() {
  876. }
  877. String DLScriptLanguage::get_init_symbol_name() {
  878. return "godot_dlscript_init"; // TODO: Maybe make some internal function which would do the actual stuff
  879. }
  880. String DLScriptLanguage::get_terminate_symbol_name() {
  881. return "godot_dlscript_terminate";
  882. }
  883. DLScriptLanguage::DLScriptLanguage() {
  884. ERR_FAIL_COND(singleton);
  885. strings._notification = StringName("_notification");
  886. singleton = this;
  887. initialized_libraries = Map<StringName, NativeLibrary *>();
  888. }
  889. DLScriptLanguage::~DLScriptLanguage() {
  890. singleton = NULL;
  891. }
  892. // DLReloadNode
  893. void DLReloadNode::_bind_methods() {
  894. ClassDB::bind_method("_notification", &DLReloadNode::_notification);
  895. }
  896. void DLReloadNode::_notification(int p_what) {
  897. #ifdef TOOLS_ENABLED
  898. switch (p_what) {
  899. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  900. // break; // For now.
  901. Set<NativeLibrary *> libs_to_reload;
  902. for (Map<StringName, NativeLibrary *>::Element *L = DLScriptLanguage::get_singleton()->initialized_libraries.front(); L; L = L->next()) {
  903. // check if file got modified at all
  904. // @Todo
  905. libs_to_reload.insert(L->get());
  906. }
  907. for (Set<NativeLibrary *>::Element *L = libs_to_reload.front(); L; L = L->next()) {
  908. DLLibrary *lib = L->get()->dllib;
  909. lib->_terminate();
  910. lib->_initialize();
  911. // update placeholders (if any)
  912. DLScript *script = NULL;
  913. for (Set<DLScript *>::Element *S = DLScriptLanguage::get_singleton()->script_list.front(); S; S = S->next()) {
  914. if (lib->native_library->scripts.has(S->get()->get_script_name())) {
  915. script = S->get();
  916. script->script_data = lib->get_script_data(script->get_script_name());
  917. break;
  918. }
  919. }
  920. if (script == NULL) {
  921. // new class, cool. Nothing to do here
  922. continue;
  923. }
  924. if (script->placeholders.size() == 0)
  925. continue;
  926. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  927. PlaceHolderScriptInstance *p = P->get();
  928. script->_update_placeholder(p);
  929. }
  930. }
  931. } break;
  932. default: {
  933. };
  934. }
  935. #endif
  936. }
  937. // Resource loader/saver
  938. RES ResourceFormatLoaderDLScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  939. ResourceFormatLoaderText rsflt;
  940. return rsflt.load(p_path, p_original_path, r_error);
  941. }
  942. void ResourceFormatLoaderDLScript::get_recognized_extensions(List<String> *p_extensions) const {
  943. p_extensions->push_back("dl");
  944. }
  945. bool ResourceFormatLoaderDLScript::handles_type(const String &p_type) const {
  946. return (p_type == "Script" || p_type == "DLScript");
  947. }
  948. String ResourceFormatLoaderDLScript::get_resource_type(const String &p_path) const {
  949. String el = p_path.get_extension().to_lower();
  950. if (el == "dl")
  951. return "DLScript";
  952. return "";
  953. }
  954. Error ResourceFormatSaverDLScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  955. ResourceFormatSaverText rfst;
  956. return rfst.save(p_path, p_resource, p_flags);
  957. }
  958. bool ResourceFormatSaverDLScript::recognize(const RES &p_resource) const {
  959. return p_resource->cast_to<DLScript>() != NULL;
  960. }
  961. void ResourceFormatSaverDLScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  962. if (p_resource->cast_to<DLScript>()) {
  963. p_extensions->push_back("dl");
  964. }
  965. }