dl_script.cpp 32 KB

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