nativescript.cpp 55 KB

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