nativescript.cpp 48 KB

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