nativescript.cpp 47 KB

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