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