nativescript.cpp 56 KB

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