nativescript.cpp 49 KB

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