nativescript.cpp 55 KB

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