nativescript.cpp 48 KB

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