nativescript.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /*************************************************************************/
  2. /* nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "nativescript.h"
  31. #include <stdint.h>
  32. #include "gdnative/gdnative.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/core_constants.h"
  35. #include "core/core_string_names.h"
  36. #include "core/io/file_access.h"
  37. #include "core/io/file_access_encrypted.h"
  38. #include "core/os/os.h"
  39. #include "main/main.h"
  40. #include "scene/main/scene_tree.h"
  41. #include "scene/resources/resource_format_text.h"
  42. #include <stdlib.h>
  43. #ifndef NO_THREADS
  44. #include "core/os/thread.h"
  45. #endif
  46. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  47. #include "api_generator.h"
  48. #endif
  49. #ifdef TOOLS_ENABLED
  50. #include "editor/editor_node.h"
  51. #endif
  52. void NativeScript::_bind_methods() {
  53. ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
  54. ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
  55. ClassDB::bind_method(D_METHOD("set_library", "library"), &NativeScript::set_library);
  56. ClassDB::bind_method(D_METHOD("get_library"), &NativeScript::get_library);
  57. ClassDB::bind_method(D_METHOD("set_script_class_name", "class_name"), &NativeScript::set_script_class_name);
  58. ClassDB::bind_method(D_METHOD("get_script_class_name"), &NativeScript::get_script_class_name);
  59. ClassDB::bind_method(D_METHOD("set_script_class_icon_path", "icon_path"), &NativeScript::set_script_class_icon_path);
  60. ClassDB::bind_method(D_METHOD("get_script_class_icon_path"), &NativeScript::get_script_class_icon_path);
  61. ClassDB::bind_method(D_METHOD("get_class_documentation"), &NativeScript::get_class_documentation);
  62. ClassDB::bind_method(D_METHOD("get_method_documentation", "method"), &NativeScript::get_method_documentation);
  63. ClassDB::bind_method(D_METHOD("get_signal_documentation", "signal_name"), &NativeScript::get_signal_documentation);
  64. ClassDB::bind_method(D_METHOD("get_property_documentation", "path"), &NativeScript::get_property_documentation);
  65. ADD_PROPERTY(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  66. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  67. ADD_GROUP("Script Class", "script_class_");
  68. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_name"), "set_script_class_name", "get_script_class_name");
  69. ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_icon_path", PROPERTY_HINT_FILE), "set_script_class_icon_path", "get_script_class_icon_path");
  70. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo("new"));
  71. }
  72. #define NSL NativeScriptLanguage::get_singleton()
  73. #ifdef TOOLS_ENABLED
  74. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  75. NativeScriptDesc *script_data = get_script_desc();
  76. ERR_FAIL_COND(!script_data);
  77. List<PropertyInfo> info;
  78. get_script_property_list(&info);
  79. Map<StringName, Variant> values;
  80. for (const PropertyInfo &E : info) {
  81. Variant value;
  82. get_property_default_value(E.name, value);
  83. values[E.name] = value;
  84. }
  85. p_placeholder->update(info, values);
  86. }
  87. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  88. placeholders.erase(p_placeholder);
  89. }
  90. #endif
  91. bool NativeScript::inherits_script(const Ref<Script> &p_script) const {
  92. Ref<NativeScript> ns = p_script;
  93. if (ns.is_null()) {
  94. return false;
  95. }
  96. const NativeScriptDesc *other_s = ns->get_script_desc();
  97. if (!other_s) {
  98. return false;
  99. }
  100. const NativeScriptDesc *s = get_script_desc();
  101. while (s) {
  102. if (s == other_s) {
  103. return true;
  104. }
  105. s = s->base_data;
  106. }
  107. return false;
  108. }
  109. void NativeScript::set_class_name(String p_class_name) {
  110. class_name = p_class_name;
  111. }
  112. String NativeScript::get_class_name() const {
  113. return class_name;
  114. }
  115. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  116. if (!library.is_null()) {
  117. WARN_PRINT("Library in NativeScript already set. Do nothing.");
  118. return;
  119. }
  120. if (p_library.is_null()) {
  121. return;
  122. }
  123. library = p_library;
  124. lib_path = library->get_current_library_path();
  125. #ifndef NO_THREADS
  126. if (Thread::get_caller_id() != Thread::get_main_id()) {
  127. NSL->defer_init_library(p_library, this);
  128. } else
  129. #endif
  130. {
  131. NSL->init_library(p_library);
  132. NSL->register_script(this);
  133. }
  134. }
  135. Ref<GDNativeLibrary> NativeScript::get_library() const {
  136. return library;
  137. }
  138. void NativeScript::set_script_class_name(String p_type) {
  139. script_class_name = p_type;
  140. }
  141. String NativeScript::get_script_class_name() const {
  142. return script_class_name;
  143. }
  144. void NativeScript::set_script_class_icon_path(String p_icon_path) {
  145. script_class_icon_path = p_icon_path;
  146. }
  147. String NativeScript::get_script_class_icon_path() const {
  148. return script_class_icon_path;
  149. }
  150. bool NativeScript::can_instantiate() const {
  151. NativeScriptDesc *script_data = get_script_desc();
  152. #ifdef TOOLS_ENABLED
  153. // Only valid if this is either a tool script or a "regular" script.
  154. // (so, an environment where scripting is disabled (and not the editor) would not
  155. // create objects).
  156. return script_data && (is_tool() || ScriptServer::is_scripting_enabled());
  157. #else
  158. return script_data;
  159. #endif
  160. }
  161. Ref<Script> NativeScript::get_base_script() const {
  162. NativeScriptDesc *script_data = get_script_desc();
  163. if (!script_data) {
  164. return Ref<Script>();
  165. }
  166. NativeScript *script = (NativeScript *)NSL->create_script();
  167. Ref<NativeScript> ns = Ref<NativeScript>(script);
  168. ERR_FAIL_COND_V(!ns.is_valid(), Ref<Script>());
  169. ns->set_class_name(script_data->base);
  170. ns->set_library(get_library());
  171. return ns;
  172. }
  173. StringName NativeScript::get_instance_base_type() const {
  174. NativeScriptDesc *script_data = get_script_desc();
  175. if (!script_data) {
  176. return "";
  177. }
  178. return script_data->base_native_type;
  179. }
  180. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  181. NativeScriptDesc *script_data = get_script_desc();
  182. if (!script_data) {
  183. return nullptr;
  184. }
  185. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  186. nsi->owner = p_this;
  187. nsi->script = Ref<NativeScript>(this);
  188. #ifndef TOOLS_ENABLED
  189. if (!ScriptServer::is_scripting_enabled()) {
  190. nsi->userdata = nullptr;
  191. } else {
  192. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  193. }
  194. #else
  195. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  196. #endif
  197. {
  198. MutexLock lock(owners_lock);
  199. instance_owners.insert(p_this);
  200. }
  201. return nsi;
  202. }
  203. PlaceHolderScriptInstance *NativeScript::placeholder_instance_create(Object *p_this) {
  204. #ifdef TOOLS_ENABLED
  205. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  206. placeholders.insert(sins);
  207. _update_placeholder(sins);
  208. return sins;
  209. #else
  210. return nullptr;
  211. #endif
  212. }
  213. bool NativeScript::instance_has(const Object *p_this) const {
  214. return instance_owners.has((Object *)p_this);
  215. }
  216. bool NativeScript::has_source_code() const {
  217. return false;
  218. }
  219. String NativeScript::get_source_code() const {
  220. return "";
  221. }
  222. void NativeScript::set_source_code(const String &p_code) {
  223. }
  224. Error NativeScript::reload(bool p_keep_state) {
  225. return FAILED;
  226. }
  227. bool NativeScript::has_method(const StringName &p_method) const {
  228. NativeScriptDesc *script_data = get_script_desc();
  229. while (script_data) {
  230. if (script_data->methods.has(p_method)) {
  231. return true;
  232. }
  233. script_data = script_data->base_data;
  234. }
  235. return false;
  236. }
  237. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  238. NativeScriptDesc *script_data = get_script_desc();
  239. if (!script_data) {
  240. return MethodInfo();
  241. }
  242. while (script_data) {
  243. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  244. if (M) {
  245. return M->get().info;
  246. }
  247. script_data = script_data->base_data;
  248. }
  249. return MethodInfo();
  250. }
  251. bool NativeScript::is_valid() const {
  252. return true;
  253. }
  254. bool NativeScript::is_tool() const {
  255. NativeScriptDesc *script_data = get_script_desc();
  256. if (script_data) {
  257. return script_data->is_tool;
  258. }
  259. return false;
  260. }
  261. ScriptLanguage *NativeScript::get_language() const {
  262. return NativeScriptLanguage::get_singleton();
  263. }
  264. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  265. NativeScriptDesc *script_data = get_script_desc();
  266. while (script_data) {
  267. if (script_data->signals_.has(p_signal)) {
  268. return true;
  269. }
  270. script_data = script_data->base_data;
  271. }
  272. return false;
  273. }
  274. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  275. NativeScriptDesc *script_data = get_script_desc();
  276. if (!script_data) {
  277. return;
  278. }
  279. Set<MethodInfo> signals_;
  280. while (script_data) {
  281. for (const KeyValue<StringName, NativeScriptDesc::Signal> &S : script_data->signals_) {
  282. signals_.insert(S.value.signal);
  283. }
  284. script_data = script_data->base_data;
  285. }
  286. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  287. r_signals->push_back(E->get());
  288. }
  289. }
  290. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  291. NativeScriptDesc *script_data = get_script_desc();
  292. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P;
  293. while (!P && script_data) {
  294. P = script_data->properties.find(p_property);
  295. script_data = script_data->base_data;
  296. }
  297. if (!P) {
  298. return false;
  299. }
  300. r_value = P.get().default_value;
  301. return true;
  302. }
  303. void NativeScript::update_exports() {
  304. }
  305. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  306. NativeScriptDesc *script_data = get_script_desc();
  307. if (!script_data) {
  308. return;
  309. }
  310. Set<MethodInfo> methods;
  311. while (script_data) {
  312. for (const KeyValue<StringName, NativeScriptDesc::Method> &E : script_data->methods) {
  313. methods.insert(E.value.info);
  314. }
  315. script_data = script_data->base_data;
  316. }
  317. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  318. p_list->push_back(E->get());
  319. }
  320. }
  321. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  322. NativeScriptDesc *script_data = get_script_desc();
  323. Set<StringName> existing_properties;
  324. List<PropertyInfo>::Element *original_back = p_list->back();
  325. while (script_data) {
  326. List<PropertyInfo>::Element *insert_position = original_back;
  327. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element E = script_data->properties.front(); E; E = E.next()) {
  328. if (!existing_properties.has(E.key())) {
  329. insert_position = p_list->insert_after(insert_position, E.get().info);
  330. existing_properties.insert(E.key());
  331. }
  332. }
  333. script_data = script_data->base_data;
  334. }
  335. }
  336. const Vector<Multiplayer::RPCConfig> NativeScript::get_rpc_methods() const {
  337. NativeScriptDesc *script_data = get_script_desc();
  338. ERR_FAIL_COND_V(!script_data, Vector<Multiplayer::RPCConfig>());
  339. return script_data->rpc_methods;
  340. }
  341. String NativeScript::get_class_documentation() const {
  342. NativeScriptDesc *script_data = get_script_desc();
  343. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get class documentation on invalid NativeScript.");
  344. return script_data->documentation;
  345. }
  346. String NativeScript::get_method_documentation(const StringName &p_method) const {
  347. NativeScriptDesc *script_data = get_script_desc();
  348. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get method documentation on invalid NativeScript.");
  349. while (script_data) {
  350. Map<StringName, NativeScriptDesc::Method>::Element *method = script_data->methods.find(p_method);
  351. if (method) {
  352. return method->get().documentation;
  353. }
  354. script_data = script_data->base_data;
  355. }
  356. ERR_FAIL_V_MSG("", "Attempt to get method documentation for non-existent method.");
  357. }
  358. String NativeScript::get_signal_documentation(const StringName &p_signal_name) const {
  359. NativeScriptDesc *script_data = get_script_desc();
  360. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get signal documentation on invalid NativeScript.");
  361. while (script_data) {
  362. Map<StringName, NativeScriptDesc::Signal>::Element *signal = script_data->signals_.find(p_signal_name);
  363. if (signal) {
  364. return signal->get().documentation;
  365. }
  366. script_data = script_data->base_data;
  367. }
  368. ERR_FAIL_V_MSG("", "Attempt to get signal documentation for non-existent signal.");
  369. }
  370. String NativeScript::get_property_documentation(const StringName &p_path) const {
  371. NativeScriptDesc *script_data = get_script_desc();
  372. ERR_FAIL_COND_V_MSG(!script_data, "", "Attempt to get property documentation on invalid NativeScript.");
  373. while (script_data) {
  374. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element property = script_data->properties.find(p_path);
  375. if (property) {
  376. return property.get().documentation;
  377. }
  378. script_data = script_data->base_data;
  379. }
  380. ERR_FAIL_V_MSG("", "Attempt to get property documentation for non-existent signal.");
  381. }
  382. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  383. if (lib_path.is_empty() || class_name.is_empty() || library.is_null()) {
  384. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  385. return Variant();
  386. }
  387. NativeScriptDesc *script_data = get_script_desc();
  388. if (!script_data) {
  389. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  390. return Variant();
  391. }
  392. r_error.error = Callable::CallError::CALL_OK;
  393. REF ref;
  394. Object *owner = nullptr;
  395. if (!(script_data->base_native_type == "")) {
  396. owner = ClassDB::instantiate(script_data->base_native_type);
  397. } else {
  398. owner = memnew(RefCounted);
  399. }
  400. if (!owner) {
  401. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  402. return Variant();
  403. }
  404. RefCounted *r = Object::cast_to<RefCounted>(owner);
  405. if (r) {
  406. ref = REF(r);
  407. }
  408. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  409. owner->set_script_instance(instance);
  410. if (!instance) {
  411. if (ref.is_null()) {
  412. memdelete(owner); //no owner, sorry
  413. }
  414. return Variant();
  415. }
  416. if (ref.is_valid()) {
  417. return ref;
  418. } else {
  419. return owner;
  420. }
  421. }
  422. NativeScript::NativeScript() {
  423. library = Ref<GDNative>();
  424. lib_path = "";
  425. class_name = "";
  426. }
  427. NativeScript::~NativeScript() {
  428. NSL->unregister_script(this);
  429. }
  430. #define GET_SCRIPT_DESC() script->get_script_desc()
  431. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  432. if (script_data->base_data) {
  433. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  434. }
  435. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  436. if (E) {
  437. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  438. godot_variant_destroy(&res);
  439. }
  440. }
  441. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  442. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  443. while (script_data) {
  444. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  445. if (P) {
  446. P.get().setter.set_func((godot_object *)owner,
  447. P.get().setter.method_data,
  448. userdata,
  449. (godot_variant *)&p_value);
  450. return true;
  451. }
  452. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  453. if (E) {
  454. Variant name = p_name;
  455. const Variant *args[2] = { &name, &p_value };
  456. godot_variant result;
  457. result = E->get().method.method((godot_object *)owner,
  458. E->get().method.method_data,
  459. userdata,
  460. 2,
  461. (godot_variant **)args);
  462. bool handled = *(Variant *)&result;
  463. godot_variant_destroy(&result);
  464. if (handled) {
  465. return true;
  466. }
  467. }
  468. script_data = script_data->base_data;
  469. }
  470. return false;
  471. }
  472. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  473. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  474. while (script_data) {
  475. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  476. if (P) {
  477. godot_variant value;
  478. value = P.get().getter.get_func((godot_object *)owner,
  479. P.get().getter.method_data,
  480. userdata);
  481. r_ret = *(Variant *)&value;
  482. godot_variant_destroy(&value);
  483. return true;
  484. }
  485. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  486. if (E) {
  487. Variant name = p_name;
  488. const Variant *args[1] = { &name };
  489. godot_variant result;
  490. result = E->get().method.method((godot_object *)owner,
  491. E->get().method.method_data,
  492. userdata,
  493. 1,
  494. (godot_variant **)args);
  495. r_ret = *(Variant *)&result;
  496. godot_variant_destroy(&result);
  497. if (r_ret.get_type() != Variant::NIL) {
  498. return true;
  499. }
  500. }
  501. script_data = script_data->base_data;
  502. }
  503. return false;
  504. }
  505. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  506. script->get_script_property_list(p_properties);
  507. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  508. while (script_data) {
  509. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  510. if (E) {
  511. godot_variant result;
  512. result = E->get().method.method((godot_object *)owner,
  513. E->get().method.method_data,
  514. userdata,
  515. 0,
  516. nullptr);
  517. Variant res = *(Variant *)&result;
  518. godot_variant_destroy(&result);
  519. ERR_FAIL_COND_MSG(res.get_type() != Variant::ARRAY, "_get_property_list must return an array of dictionaries.");
  520. Array arr = res;
  521. for (int i = 0; i < arr.size(); i++) {
  522. Dictionary d = arr[i];
  523. ERR_CONTINUE(!d.has("name"));
  524. ERR_CONTINUE(!d.has("type"));
  525. PropertyInfo info;
  526. info.type = Variant::Type(d["type"].operator int64_t());
  527. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  528. info.name = d["name"];
  529. ERR_CONTINUE(info.name.is_empty());
  530. if (d.has("hint")) {
  531. info.hint = PropertyHint(d["hint"].operator int64_t());
  532. }
  533. if (d.has("hint_string")) {
  534. info.hint_string = d["hint_string"];
  535. }
  536. if (d.has("usage")) {
  537. info.usage = d["usage"];
  538. }
  539. p_properties->push_back(info);
  540. }
  541. }
  542. script_data = script_data->base_data;
  543. }
  544. return;
  545. }
  546. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  547. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  548. while (script_data) {
  549. OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = script_data->properties.find(p_name);
  550. if (P) {
  551. *r_is_valid = true;
  552. return P.get().info.type;
  553. }
  554. script_data = script_data->base_data;
  555. }
  556. return Variant::NIL;
  557. }
  558. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  559. script->get_script_method_list(p_list);
  560. }
  561. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  562. return script->has_method(p_method);
  563. }
  564. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  565. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  566. while (script_data) {
  567. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  568. if (E) {
  569. godot_variant result;
  570. #ifdef DEBUG_ENABLED
  571. current_method_call = p_method;
  572. #endif
  573. result = E->get().method.method((godot_object *)owner,
  574. E->get().method.method_data,
  575. userdata,
  576. p_argcount,
  577. (godot_variant **)p_args);
  578. #ifdef DEBUG_ENABLED
  579. current_method_call = "";
  580. #endif
  581. Variant res = *(Variant *)&result;
  582. godot_variant_destroy(&result);
  583. r_error.error = Callable::CallError::CALL_OK;
  584. return res;
  585. }
  586. script_data = script_data->base_data;
  587. }
  588. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  589. return Variant();
  590. }
  591. void NativeScriptInstance::notification(int p_what) {
  592. #ifdef DEBUG_ENABLED
  593. switch (p_what) {
  594. case MainLoop::NOTIFICATION_CRASH: {
  595. if (current_method_call != StringName()) {
  596. ERR_PRINT("NativeScriptInstance detected crash on method: " + current_method_call);
  597. current_method_call = "";
  598. }
  599. } break;
  600. }
  601. #endif
  602. Variant value = p_what;
  603. const Variant *args[1] = { &value };
  604. Callable::CallError error;
  605. call("_notification", args, 1, error);
  606. }
  607. String NativeScriptInstance::to_string(bool *r_valid) {
  608. if (has_method(CoreStringNames::get_singleton()->_to_string)) {
  609. Callable::CallError ce;
  610. Variant ret = call(CoreStringNames::get_singleton()->_to_string, nullptr, 0, ce);
  611. if (ce.error == Callable::CallError::CALL_OK) {
  612. if (ret.get_type() != Variant::STRING) {
  613. if (r_valid) {
  614. *r_valid = false;
  615. }
  616. ERR_FAIL_V_MSG(String(), "Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String.");
  617. }
  618. if (r_valid) {
  619. *r_valid = true;
  620. }
  621. return ret.operator String();
  622. }
  623. }
  624. if (r_valid) {
  625. *r_valid = false;
  626. }
  627. return String();
  628. }
  629. void NativeScriptInstance::refcount_incremented() {
  630. Callable::CallError err;
  631. call("_refcount_incremented", nullptr, 0, err);
  632. if (err.error != Callable::CallError::CALL_OK && err.error != Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  633. ERR_PRINT("Failed to invoke _refcount_incremented - should not happen");
  634. }
  635. }
  636. bool NativeScriptInstance::refcount_decremented() {
  637. Callable::CallError err;
  638. Variant ret = call("_refcount_decremented", nullptr, 0, err);
  639. if (err.error != Callable::CallError::CALL_OK && err.error != Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  640. ERR_PRINT("Failed to invoke _refcount_decremented - should not happen");
  641. return true; // assume we can destroy the object
  642. }
  643. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  644. // the method does not exist, default is true
  645. return true;
  646. }
  647. return ret;
  648. }
  649. Ref<Script> NativeScriptInstance::get_script() const {
  650. return script;
  651. }
  652. const Vector<Multiplayer::RPCConfig> NativeScriptInstance::get_rpc_methods() const {
  653. return script->get_rpc_methods();
  654. }
  655. ScriptLanguage *NativeScriptInstance::get_language() {
  656. return NativeScriptLanguage::get_singleton();
  657. }
  658. NativeScriptInstance::~NativeScriptInstance() {
  659. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  660. if (!script_data) {
  661. return;
  662. }
  663. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  664. if (owner) {
  665. MutexLock lock(script->owners_lock);
  666. script->instance_owners.erase(owner);
  667. }
  668. }
  669. NativeScriptLanguage *NativeScriptLanguage::singleton;
  670. void NativeScriptLanguage::_unload_stuff(bool p_reload) {
  671. Map<String, Ref<GDNative>> erase_and_unload;
  672. for (KeyValue<String, Map<StringName, NativeScriptDesc>> &L : library_classes) {
  673. String lib_path = L.key;
  674. Map<StringName, NativeScriptDesc> classes = L.value;
  675. if (p_reload) {
  676. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  677. Ref<GDNative> gdn;
  678. if (E) {
  679. gdn = E->get();
  680. }
  681. bool should_reload = false;
  682. if (gdn.is_valid()) {
  683. Ref<GDNativeLibrary> lib = gdn->get_library();
  684. if (lib.is_valid()) {
  685. should_reload = lib->is_reloadable();
  686. }
  687. }
  688. if (!should_reload) {
  689. continue;
  690. }
  691. }
  692. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  693. Ref<GDNative> gdn;
  694. if (E) {
  695. gdn = E->get();
  696. }
  697. for (KeyValue<StringName, NativeScriptDesc> &C : classes) {
  698. // free property stuff first
  699. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C.value.properties.front(); P; P = P.next()) {
  700. if (P.get().getter.free_func) {
  701. P.get().getter.free_func(P.get().getter.method_data);
  702. }
  703. if (P.get().setter.free_func) {
  704. P.get().setter.free_func(P.get().setter.method_data);
  705. }
  706. }
  707. // free method stuff
  708. for (const KeyValue<StringName, NativeScriptDesc::Method> &M : C.value.methods) {
  709. if (M.value.method.free_func) {
  710. M.value.method.free_func(M.value.method.method_data);
  711. }
  712. }
  713. // free constructor/destructor
  714. if (C.value.create_func.free_func) {
  715. C.value.create_func.free_func(C.value.create_func.method_data);
  716. }
  717. if (C.value.destroy_func.free_func) {
  718. C.value.destroy_func.free_func(C.value.destroy_func.method_data);
  719. }
  720. }
  721. erase_and_unload.insert(lib_path, gdn);
  722. }
  723. for (KeyValue<String, Ref<GDNative>> &E : erase_and_unload) {
  724. String lib_path = E.key;
  725. Ref<GDNative> gdn = E.value;
  726. library_classes.erase(lib_path);
  727. if (gdn.is_valid() && gdn->get_library().is_valid()) {
  728. Ref<GDNativeLibrary> lib = gdn->get_library();
  729. void *terminate_fn;
  730. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _terminate_call_name, terminate_fn, true);
  731. if (err == OK) {
  732. void (*terminate)(void *) = (void (*)(void *))terminate_fn;
  733. terminate((void *)&lib_path);
  734. }
  735. }
  736. }
  737. }
  738. NativeScriptLanguage::NativeScriptLanguage() {
  739. NativeScriptLanguage::singleton = this;
  740. _init_call_type = "nativescript_init";
  741. _init_call_name = "nativescript_init";
  742. _terminate_call_name = "nativescript_terminate";
  743. _noarg_call_type = "nativescript_no_arg";
  744. _frame_call_name = "nativescript_frame";
  745. #ifndef NO_THREADS
  746. _thread_enter_call_name = "nativescript_thread_enter";
  747. _thread_exit_call_name = "nativescript_thread_exit";
  748. #endif
  749. }
  750. NativeScriptLanguage::~NativeScriptLanguage() {
  751. for (KeyValue<String, Ref<GDNative>> &L : NSL->library_gdnatives) {
  752. Ref<GDNative> lib = L.value;
  753. // only shut down valid libs, duh!
  754. if (lib.is_valid()) {
  755. // If it's a singleton-library then the gdnative module
  756. // manages the destruction at engine shutdown, not NativeScript.
  757. if (!lib->get_library()->is_singleton()) {
  758. lib->terminate();
  759. }
  760. }
  761. }
  762. NSL->library_classes.clear();
  763. NSL->library_gdnatives.clear();
  764. NSL->library_script_users.clear();
  765. }
  766. String NativeScriptLanguage::get_name() const {
  767. return "NativeScript";
  768. }
  769. void _add_reload_node() {
  770. #ifdef TOOLS_ENABLED
  771. NativeReloadNode *rn = memnew(NativeReloadNode);
  772. EditorNode::get_singleton()->add_child(rn);
  773. #endif
  774. }
  775. void NativeScriptLanguage::init() {
  776. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  777. List<String> args = OS::get_singleton()->get_cmdline_args();
  778. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  779. if (E && E->next()) {
  780. if (generate_c_api(E->next()->get()) != OK) {
  781. ERR_PRINT("Failed to generate C API\n");
  782. }
  783. Main::cleanup(true);
  784. exit(0);
  785. }
  786. E = args.find("--gdnative-generate-json-builtin-api");
  787. if (E && E->next()) {
  788. if (generate_c_builtin_api(E->next()->get()) != OK) {
  789. ERR_PRINT("Failed to generate C builtin API\n");
  790. }
  791. Main::cleanup(true);
  792. exit(0);
  793. }
  794. #endif
  795. #ifdef TOOLS_ENABLED
  796. EditorNode::add_init_callback(&_add_reload_node);
  797. #endif
  798. }
  799. String NativeScriptLanguage::get_type() const {
  800. return "NativeScript";
  801. }
  802. String NativeScriptLanguage::get_extension() const {
  803. return "gdns";
  804. }
  805. Error NativeScriptLanguage::execute_file(const String &p_path) {
  806. return OK; // Qué?
  807. }
  808. void NativeScriptLanguage::finish() {
  809. _unload_stuff();
  810. }
  811. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  812. }
  813. bool NativeScriptLanguage::is_control_flow_keyword(String p_keyword) const {
  814. return false;
  815. }
  816. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  817. }
  818. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  819. }
  820. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  821. NativeScript *s = memnew(NativeScript);
  822. s->set_class_name(p_class_name);
  823. return Ref<NativeScript>(s);
  824. }
  825. bool NativeScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  826. return true;
  827. }
  828. Script *NativeScriptLanguage::create_script() const {
  829. NativeScript *script = memnew(NativeScript);
  830. return script;
  831. }
  832. bool NativeScriptLanguage::has_named_classes() const {
  833. return true;
  834. }
  835. bool NativeScriptLanguage::supports_builtin_mode() const {
  836. return true;
  837. }
  838. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  839. return -1;
  840. }
  841. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  842. return "";
  843. }
  844. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  845. }
  846. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  847. }
  848. // Debugging stuff here. Not used for now.
  849. String NativeScriptLanguage::debug_get_error() const {
  850. return "";
  851. }
  852. int NativeScriptLanguage::debug_get_stack_level_count() const {
  853. return -1;
  854. }
  855. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  856. return -1;
  857. }
  858. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  859. return "";
  860. }
  861. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  862. return "";
  863. }
  864. void NativeScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  865. }
  866. void NativeScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  867. }
  868. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  869. }
  870. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  871. return "";
  872. }
  873. // Debugging stuff end.
  874. void NativeScriptLanguage::reload_all_scripts() {
  875. }
  876. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  877. }
  878. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  879. p_extensions->push_back("gdns");
  880. }
  881. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  882. }
  883. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  884. }
  885. void NativeScriptLanguage::profiling_start() {
  886. #ifdef DEBUG_ENABLED
  887. MutexLock lock(mutex);
  888. profile_data.clear();
  889. #endif
  890. }
  891. void NativeScriptLanguage::profiling_stop() {
  892. #ifdef DEBUG_ENABLED
  893. MutexLock lock(mutex);
  894. #endif
  895. }
  896. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  897. #ifdef DEBUG_ENABLED
  898. MutexLock lock(mutex);
  899. int current = 0;
  900. for (const KeyValue<StringName, ProfileData> &d : profile_data) {
  901. if (current >= p_info_max) {
  902. break;
  903. }
  904. p_info_arr[current].call_count = d.value.call_count;
  905. p_info_arr[current].self_time = d.value.self_time;
  906. p_info_arr[current].total_time = d.value.total_time;
  907. p_info_arr[current].signature = d.value.signature;
  908. current++;
  909. }
  910. return current;
  911. #else
  912. return 0;
  913. #endif
  914. }
  915. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  916. #ifdef DEBUG_ENABLED
  917. MutexLock lock(mutex);
  918. int current = 0;
  919. for (const KeyValue<StringName, ProfileData> &d : profile_data) {
  920. if (current >= p_info_max) {
  921. break;
  922. }
  923. if (d.value.last_frame_call_count) {
  924. p_info_arr[current].call_count = d.value.last_frame_call_count;
  925. p_info_arr[current].self_time = d.value.last_frame_self_time;
  926. p_info_arr[current].total_time = d.value.last_frame_total_time;
  927. p_info_arr[current].signature = d.value.signature;
  928. current++;
  929. }
  930. }
  931. return current;
  932. #else
  933. return 0;
  934. #endif
  935. }
  936. void NativeScriptLanguage::profiling_add_data(StringName p_signature, uint64_t p_time) {
  937. #ifdef DEBUG_ENABLED
  938. MutexLock lock(mutex);
  939. Map<StringName, ProfileData>::Element *d = profile_data.find(p_signature);
  940. if (d) {
  941. d->get().call_count += 1;
  942. d->get().total_time += p_time;
  943. d->get().frame_call_count += 1;
  944. d->get().frame_total_time += p_time;
  945. } else {
  946. ProfileData data;
  947. data.signature = p_signature;
  948. data.call_count = 1;
  949. data.self_time = 0;
  950. data.total_time = p_time;
  951. data.frame_call_count = 1;
  952. data.frame_self_time = 0;
  953. data.frame_total_time = p_time;
  954. data.last_frame_call_count = 0;
  955. data.last_frame_self_time = 0;
  956. data.last_frame_total_time = 0;
  957. profile_data.insert(p_signature, data);
  958. }
  959. #endif
  960. }
  961. int NativeScriptLanguage::register_binding_functions(godot_nativescript_instance_binding_functions p_binding_functions) {
  962. // find index
  963. int idx = -1;
  964. for (int i = 0; i < binding_functions.size(); i++) {
  965. if (!binding_functions[i].first) {
  966. // free, we'll take it
  967. idx = i;
  968. break;
  969. }
  970. }
  971. if (idx == -1) {
  972. idx = binding_functions.size();
  973. binding_functions.resize(idx + 1);
  974. }
  975. // set the functions
  976. binding_functions.write[idx].first = true;
  977. binding_functions.write[idx].second = p_binding_functions;
  978. return idx;
  979. }
  980. void NativeScriptLanguage::unregister_binding_functions(int p_idx) {
  981. ERR_FAIL_INDEX(p_idx, binding_functions.size());
  982. for (Set<Vector<void *> *>::Element *E = binding_instances.front(); E; E = E->next()) {
  983. Vector<void *> &binding_data = *E->get();
  984. if (p_idx < binding_data.size() && binding_data[p_idx] && binding_functions[p_idx].second.free_instance_binding_data) {
  985. binding_functions[p_idx].second.free_instance_binding_data(binding_functions[p_idx].second.data, binding_data[p_idx]);
  986. }
  987. }
  988. binding_functions.write[p_idx].first = false;
  989. if (binding_functions[p_idx].second.free_func) {
  990. binding_functions[p_idx].second.free_func(binding_functions[p_idx].second.data);
  991. }
  992. }
  993. void *NativeScriptLanguage::get_instance_binding_data(int p_idx, Object *p_object) {
  994. return nullptr;
  995. #if 0
  996. ERR_FAIL_INDEX_V(p_idx, binding_functions.size(), nullptr);
  997. ERR_FAIL_COND_V_MSG(!binding_functions[p_idx].first, nullptr, "Tried to get binding data for a nativescript binding that does not exist.");
  998. Vector<void *> *binding_data = (Vector<void *> *)p_object->get_script_instance_binding(lang_idx);
  999. if (!binding_data) {
  1000. return nullptr; // should never happen.
  1001. }
  1002. if (binding_data->size() <= p_idx) {
  1003. // okay, add new elements here.
  1004. int old_size = binding_data->size();
  1005. binding_data->resize(p_idx + 1);
  1006. for (int i = old_size; i <= p_idx; i++) {
  1007. (*binding_data).write[i] = nullptr;
  1008. }
  1009. }
  1010. if (!(*binding_data)[p_idx]) {
  1011. const void *global_type_tag = get_global_type_tag(p_idx, p_object->get_class_name());
  1012. // no binding data yet, soooooo alloc new one \o/
  1013. (*binding_data).write[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, global_type_tag, (godot_object *)p_object);
  1014. }
  1015. return (*binding_data)[p_idx];
  1016. #endif
  1017. }
  1018. void *NativeScriptLanguage::alloc_instance_binding_data(Object *p_object) {
  1019. return nullptr;
  1020. #if 0
  1021. Vector<void *> *binding_data = new Vector<void *>;
  1022. binding_data->resize(binding_functions.size());
  1023. for (int i = 0; i < binding_functions.size(); i++) {
  1024. (*binding_data).write[i] = nullptr;
  1025. }
  1026. binding_instances.insert(binding_data);
  1027. return (void *)binding_data;
  1028. #endif
  1029. }
  1030. void NativeScriptLanguage::free_instance_binding_data(void *p_data) {
  1031. #if 0
  1032. if (!p_data) {
  1033. return;
  1034. }
  1035. Vector<void *> &binding_data = *(Vector<void *> *)p_data;
  1036. for (int i = 0; i < binding_data.size(); i++) {
  1037. if (!binding_data[i]) {
  1038. continue;
  1039. }
  1040. if (binding_functions[i].first && binding_functions[i].second.free_instance_binding_data) {
  1041. binding_functions[i].second.free_instance_binding_data(binding_functions[i].second.data, binding_data[i]);
  1042. }
  1043. }
  1044. binding_instances.erase(&binding_data);
  1045. delete &binding_data;
  1046. #endif
  1047. }
  1048. void NativeScriptLanguage::refcount_incremented_instance_binding(Object *p_object) {
  1049. #if 0
  1050. void *data = p_object->get_script_instance_binding(lang_idx);
  1051. if (!data) {
  1052. return;
  1053. }
  1054. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1055. for (int i = 0; i < binding_data.size(); i++) {
  1056. if (!binding_data[i]) {
  1057. continue;
  1058. }
  1059. if (!binding_functions[i].first) {
  1060. continue;
  1061. }
  1062. if (binding_functions[i].second.refcount_incremented_instance_binding) {
  1063. binding_functions[i].second.refcount_incremented_instance_binding(binding_data[i], p_object);
  1064. }
  1065. }
  1066. #endif
  1067. }
  1068. bool NativeScriptLanguage::refcount_decremented_instance_binding(Object *p_object) {
  1069. #if 0
  1070. void *data = p_object->get_script_instance_binding(lang_idx);
  1071. if (!data) {
  1072. return true;
  1073. }
  1074. Vector<void *> &binding_data = *(Vector<void *> *)data;
  1075. bool can_die = true;
  1076. for (int i = 0; i < binding_data.size(); i++) {
  1077. if (!binding_data[i]) {
  1078. continue;
  1079. }
  1080. if (!binding_functions[i].first) {
  1081. continue;
  1082. }
  1083. if (binding_functions[i].second.refcount_decremented_instance_binding) {
  1084. can_die = can_die && binding_functions[i].second.refcount_decremented_instance_binding(binding_data[i], p_object);
  1085. }
  1086. }
  1087. return can_die;
  1088. #endif
  1089. return false;
  1090. }
  1091. void NativeScriptLanguage::set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag) {
  1092. if (!global_type_tags.has(p_idx)) {
  1093. global_type_tags.insert(p_idx, HashMap<StringName, const void *>());
  1094. }
  1095. HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1096. tags.set(p_class_name, p_type_tag);
  1097. }
  1098. const void *NativeScriptLanguage::get_global_type_tag(int p_idx, StringName p_class_name) const {
  1099. if (!global_type_tags.has(p_idx)) {
  1100. return nullptr;
  1101. }
  1102. const HashMap<StringName, const void *> &tags = global_type_tags[p_idx];
  1103. if (!tags.has(p_class_name)) {
  1104. return nullptr;
  1105. }
  1106. const void *tag = tags.get(p_class_name);
  1107. return tag;
  1108. }
  1109. #ifndef NO_THREADS
  1110. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  1111. MutexLock lock(mutex);
  1112. libs_to_init.insert(lib);
  1113. scripts_to_register.insert(script);
  1114. has_objects_to_register.set();
  1115. }
  1116. #endif
  1117. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  1118. MutexLock lock(mutex);
  1119. // See if this library was "registered" already.
  1120. const String &lib_path = lib->get_current_library_path();
  1121. ERR_FAIL_COND_MSG(lib_path.length() == 0, lib->get_name() + " does not have a library for the current platform.");
  1122. Map<String, Ref<GDNative>>::Element *E = library_gdnatives.find(lib_path);
  1123. if (!E) {
  1124. Ref<GDNative> gdn;
  1125. gdn.instantiate();
  1126. gdn->set_library(lib);
  1127. // TODO check the return value?
  1128. gdn->initialize();
  1129. library_gdnatives.insert(lib_path, gdn);
  1130. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  1131. if (!library_script_users.has(lib_path)) {
  1132. library_script_users.insert(lib_path, Set<NativeScript *>());
  1133. }
  1134. void *proc_ptr;
  1135. Error err = gdn->get_symbol(lib->get_symbol_prefix() + _init_call_name, proc_ptr);
  1136. if (err != OK) {
  1137. ERR_PRINT(String("No " + _init_call_name + " in \"" + lib_path + "\" found").utf8().get_data());
  1138. } else {
  1139. ((void (*)(godot_string *))proc_ptr)((godot_string *)&lib_path);
  1140. }
  1141. } else {
  1142. // already initialized. Nice.
  1143. }
  1144. }
  1145. void NativeScriptLanguage::register_script(NativeScript *script) {
  1146. MutexLock lock(mutex);
  1147. library_script_users[script->lib_path].insert(script);
  1148. }
  1149. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  1150. MutexLock lock(mutex);
  1151. Map<String, Set<NativeScript *>>::Element *S = library_script_users.find(script->lib_path);
  1152. if (S) {
  1153. S->get().erase(script);
  1154. if (S->get().size() == 0) {
  1155. library_script_users.erase(S);
  1156. Map<String, Ref<GDNative>>::Element *G = library_gdnatives.find(script->lib_path);
  1157. if (G && G->get()->get_library()->is_reloadable()) {
  1158. // ONLY if the library is marked as reloadable, and no more instances of its scripts exist do we unload the library
  1159. // First remove meta data related to the library
  1160. Map<String, Map<StringName, NativeScriptDesc>>::Element *L = library_classes.find(script->lib_path);
  1161. if (L) {
  1162. Map<StringName, NativeScriptDesc> classes = L->get();
  1163. for (KeyValue<StringName, NativeScriptDesc> &C : classes) {
  1164. // free property stuff first
  1165. for (OrderedHashMap<StringName, NativeScriptDesc::Property>::Element P = C.value.properties.front(); P; P = P.next()) {
  1166. if (P.get().getter.free_func) {
  1167. P.get().getter.free_func(P.get().getter.method_data);
  1168. }
  1169. if (P.get().setter.free_func) {
  1170. P.get().setter.free_func(P.get().setter.method_data);
  1171. }
  1172. }
  1173. // free method stuff
  1174. for (const KeyValue<StringName, NativeScriptDesc::Method> &M : C.value.methods) {
  1175. if (M.value.method.free_func) {
  1176. M.value.method.free_func(M.value.method.method_data);
  1177. }
  1178. }
  1179. // free constructor/destructor
  1180. if (C.value.create_func.free_func) {
  1181. C.value.create_func.free_func(C.value.create_func.method_data);
  1182. }
  1183. if (C.value.destroy_func.free_func) {
  1184. C.value.destroy_func.free_func(C.value.destroy_func.method_data);
  1185. }
  1186. }
  1187. library_classes.erase(script->lib_path);
  1188. }
  1189. // now unload the library
  1190. G->get()->terminate();
  1191. library_gdnatives.erase(G);
  1192. }
  1193. }
  1194. }
  1195. #ifndef NO_THREADS
  1196. scripts_to_register.erase(script);
  1197. #endif
  1198. }
  1199. void NativeScriptLanguage::call_libraries_cb(const StringName &name) {
  1200. // library_gdnatives is modified only from the main thread, so it's safe not to use mutex here
  1201. for (KeyValue<String, Ref<GDNative>> &L : library_gdnatives) {
  1202. if (L.value.is_null()) {
  1203. continue;
  1204. }
  1205. if (L.value->is_initialized()) {
  1206. void *proc_ptr;
  1207. Error err = L.value->get_symbol(L.value->get_library()->get_symbol_prefix() + name, proc_ptr);
  1208. if (!err) {
  1209. ((void (*)())proc_ptr)();
  1210. }
  1211. }
  1212. }
  1213. }
  1214. void NativeScriptLanguage::frame() {
  1215. #ifndef NO_THREADS
  1216. if (has_objects_to_register.is_set()) {
  1217. MutexLock lock(mutex);
  1218. for (Set<Ref<GDNativeLibrary>>::Element *L = libs_to_init.front(); L; L = L->next()) {
  1219. init_library(L->get());
  1220. }
  1221. libs_to_init.clear();
  1222. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  1223. register_script(S->get());
  1224. }
  1225. scripts_to_register.clear();
  1226. has_objects_to_register.clear();
  1227. }
  1228. #endif
  1229. #ifdef DEBUG_ENABLED
  1230. {
  1231. MutexLock lock(mutex);
  1232. for (KeyValue<StringName, ProfileData> &d : profile_data) {
  1233. d.value.last_frame_call_count = d.value.frame_call_count;
  1234. d.value.last_frame_self_time = d.value.frame_self_time;
  1235. d.value.last_frame_total_time = d.value.frame_total_time;
  1236. d.value.frame_call_count = 0;
  1237. d.value.frame_self_time = 0;
  1238. d.value.frame_total_time = 0;
  1239. }
  1240. }
  1241. #endif
  1242. call_libraries_cb(_frame_call_name);
  1243. }
  1244. #ifndef NO_THREADS
  1245. void NativeScriptLanguage::thread_enter() {
  1246. call_libraries_cb(_thread_enter_call_name);
  1247. }
  1248. void NativeScriptLanguage::thread_exit() {
  1249. call_libraries_cb(_thread_exit_call_name);
  1250. }
  1251. #endif // NO_THREADS
  1252. bool NativeScriptLanguage::handles_global_class_type(const String &p_type) const {
  1253. return p_type == "NativeScript";
  1254. }
  1255. String NativeScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const {
  1256. if (!p_path.is_empty()) {
  1257. Ref<NativeScript> script = ResourceLoader::load(p_path, "NativeScript");
  1258. if (script.is_valid()) {
  1259. if (r_base_type) {
  1260. *r_base_type = script->get_instance_base_type();
  1261. }
  1262. if (r_icon_path) {
  1263. *r_icon_path = script->get_script_class_icon_path();
  1264. }
  1265. return script->get_script_class_name();
  1266. }
  1267. if (r_base_type) {
  1268. *r_base_type = String();
  1269. }
  1270. if (r_icon_path) {
  1271. *r_icon_path = String();
  1272. }
  1273. }
  1274. return String();
  1275. }
  1276. void NativeReloadNode::_bind_methods() {
  1277. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  1278. }
  1279. void NativeReloadNode::_notification(int p_what) {
  1280. #ifdef TOOLS_ENABLED
  1281. switch (p_what) {
  1282. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  1283. if (unloaded) {
  1284. break;
  1285. }
  1286. MutexLock lock(NSL->mutex);
  1287. NSL->_unload_stuff(true);
  1288. for (KeyValue<String, Ref<GDNative>> &L : NSL->library_gdnatives) {
  1289. Ref<GDNative> gdn = L.value;
  1290. if (gdn.is_null()) {
  1291. continue;
  1292. }
  1293. // Don't unload what should not be reloaded!
  1294. if (!gdn->get_library()->is_reloadable()) {
  1295. continue;
  1296. }
  1297. // singleton libraries might have alive pointers living inside the
  1298. // editor. Also reloading a singleton library would mean that
  1299. // the singleton entry will not be called again, as this only
  1300. // happens at engine startup.
  1301. if (gdn->get_library()->is_singleton()) {
  1302. continue;
  1303. }
  1304. gdn->terminate();
  1305. }
  1306. unloaded = true;
  1307. } break;
  1308. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  1309. if (!unloaded) {
  1310. break;
  1311. }
  1312. MutexLock lock(NSL->mutex);
  1313. Set<StringName> libs_to_remove;
  1314. for (KeyValue<String, Ref<GDNative>> &L : NSL->library_gdnatives) {
  1315. Ref<GDNative> gdn = L.value;
  1316. if (gdn.is_null()) {
  1317. continue;
  1318. }
  1319. if (!gdn->get_library()->is_reloadable()) {
  1320. continue;
  1321. }
  1322. // since singleton libraries are not unloaded there is no point
  1323. // in loading them again.
  1324. if (gdn->get_library()->is_singleton()) {
  1325. continue;
  1326. }
  1327. if (!gdn->initialize()) {
  1328. libs_to_remove.insert(L.key);
  1329. continue;
  1330. }
  1331. NSL->library_classes.insert(L.key, Map<StringName, NativeScriptDesc>());
  1332. // here the library registers all the classes and stuff.
  1333. void *proc_ptr;
  1334. Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
  1335. if (err != OK) {
  1336. ERR_PRINT(String("No godot_nativescript_init in \"" + L.key + "\" found").utf8().get_data());
  1337. } else {
  1338. ((void (*)(void *))proc_ptr)((void *)&L.key);
  1339. }
  1340. for (KeyValue<String, Set<NativeScript *>> &U : NSL->library_script_users) {
  1341. for (Set<NativeScript *>::Element *S = U.value.front(); S; S = S->next()) {
  1342. NativeScript *script = S->get();
  1343. if (script->placeholders.size() == 0) {
  1344. continue;
  1345. }
  1346. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  1347. script->_update_placeholder(P->get());
  1348. }
  1349. }
  1350. }
  1351. }
  1352. unloaded = false;
  1353. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  1354. NSL->library_gdnatives.erase(R->get());
  1355. }
  1356. } break;
  1357. }
  1358. #endif
  1359. }
  1360. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_no_cache) {
  1361. return ResourceFormatLoaderText::singleton->load(p_path, p_original_path, r_error);
  1362. }
  1363. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  1364. p_extensions->push_back("gdns");
  1365. }
  1366. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  1367. return (p_type == "Script" || p_type == "NativeScript");
  1368. }
  1369. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  1370. String el = p_path.get_extension().to_lower();
  1371. if (el == "gdns") {
  1372. return "NativeScript";
  1373. }
  1374. return "";
  1375. }
  1376. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  1377. ResourceFormatSaverText rfst;
  1378. return rfst.save(p_path, p_resource, p_flags);
  1379. }
  1380. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  1381. return Object::cast_to<NativeScript>(*p_resource) != nullptr;
  1382. }
  1383. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  1384. if (Object::cast_to<NativeScript>(*p_resource)) {
  1385. p_extensions->push_back("gdns");
  1386. }
  1387. }