nativescript.cpp 49 KB

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