nativescript.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*************************************************************************/
  2. /* nativescript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "modules/gdnative/godot/gdnative.h"
  32. #include "global_constants.h"
  33. #include "io/file_access_encrypted.h"
  34. #include "os/file_access.h"
  35. #include "os/os.h"
  36. #include "project_settings.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "scene/resources/scene_format_text.h"
  39. #ifndef NO_THREADS
  40. #include "os/thread.h"
  41. #endif
  42. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  43. #include "api_generator.h"
  44. #endif
  45. #ifdef TOOLS_ENABLED
  46. #include "editor/editor_node.h"
  47. #endif
  48. ////// Script stuff
  49. void NativeScript::_bind_methods() {
  50. ClassDB::bind_method(D_METHOD("set_class_name", "class_name:String"), &NativeScript::set_class_name);
  51. ClassDB::bind_method(D_METHOD("get_class_name:String"), &NativeScript::get_class_name);
  52. ClassDB::bind_method(D_METHOD("set_library", "library:GDNativeLibrary"), &NativeScript::set_library);
  53. ClassDB::bind_method(D_METHOD("get_library:GDNativeLibrary"), &NativeScript::get_library);
  54. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "class_name"), "set_class_name", "get_class_name");
  55. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library");
  56. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new"));
  57. }
  58. #define NSL NativeScriptLanguage::get_singleton()
  59. #ifdef TOOLS_ENABLED
  60. void NativeScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
  61. NativeScriptDesc *script_data = get_script_desc();
  62. ERR_FAIL_COND(!script_data);
  63. List<PropertyInfo> info;
  64. Map<StringName, Variant> values;
  65. for (Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.front(); E; E = E->next()) {
  66. PropertyInfo p = E->get().info;
  67. p.name = String(E->key());
  68. info.push_back(p);
  69. values[p.name] = E->get().default_value;
  70. }
  71. p_placeholder->update(info, values);
  72. }
  73. void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  74. placeholders.erase(p_placeholder);
  75. }
  76. #endif
  77. void NativeScript::set_class_name(String p_class_name) {
  78. class_name = p_class_name;
  79. }
  80. String NativeScript::get_class_name() const {
  81. return class_name;
  82. }
  83. void NativeScript::set_library(Ref<GDNativeLibrary> p_library) {
  84. if (!library.is_null()) {
  85. WARN_PRINT("library on NativeScript already set. Do nothing.");
  86. return;
  87. }
  88. library = p_library;
  89. lib_path = library->get_active_library_path();
  90. #ifndef NO_THREADS
  91. if (Thread::get_caller_ID() != Thread::get_main_ID()) {
  92. NSL->defer_init_library(p_library, this);
  93. } else
  94. #endif
  95. {
  96. NSL->init_library(p_library);
  97. NSL->register_script(this);
  98. }
  99. }
  100. Ref<GDNativeLibrary> NativeScript::get_library() const {
  101. return library;
  102. }
  103. bool NativeScript::can_instance() const {
  104. NativeScriptDesc *script_data = get_script_desc();
  105. #ifdef TOOLS_ENABLED
  106. return script_data || (!is_tool() && !ScriptServer::is_scripting_enabled());
  107. #else
  108. return script_data;
  109. #endif
  110. }
  111. // TODO(karroffel): implement this
  112. Ref<Script> NativeScript::get_base_script() const {
  113. NativeScriptDesc *script_data = get_script_desc();
  114. if (!script_data)
  115. return Ref<Script>();
  116. Ref<NativeScript> ns = Ref<NativeScript>(NSL->create_script());
  117. ns->set_class_name(script_data->base);
  118. ns->set_library(get_library());
  119. return ns;
  120. }
  121. StringName NativeScript::get_instance_base_type() const {
  122. NativeScriptDesc *script_data = get_script_desc();
  123. if (!script_data)
  124. return "";
  125. return script_data->base_native_type;
  126. }
  127. ScriptInstance *NativeScript::instance_create(Object *p_this) {
  128. NativeScriptDesc *script_data = get_script_desc();
  129. if (!script_data) {
  130. return NULL;
  131. }
  132. #ifdef TOOLS_ENABLED
  133. if (!ScriptServer::is_scripting_enabled() && !is_tool()) {
  134. // placeholder for nodes. For tools we want the rool thing.
  135. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(NSL, Ref<Script>(this), p_this));
  136. placeholders.insert(sins);
  137. if (script_data->create_func.create_func) {
  138. script_data->create_func.create_func(
  139. (godot_object *)p_this,
  140. script_data->create_func.method_data);
  141. }
  142. _update_placeholder(sins);
  143. return sins;
  144. }
  145. #endif
  146. NativeScriptInstance *nsi = memnew(NativeScriptInstance);
  147. nsi->owner = p_this;
  148. nsi->script = Ref<NativeScript>(this);
  149. #ifndef TOOLS_ENABLED
  150. if (!ScriptServer::is_scripting_enabled()) {
  151. nsi->userdata = NULL;
  152. } else {
  153. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  154. }
  155. #else
  156. nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data);
  157. #endif
  158. instance_owners.insert(p_this);
  159. return nsi;
  160. }
  161. bool NativeScript::instance_has(const Object *p_this) const {
  162. return instance_owners.has((Object *)p_this);
  163. }
  164. bool NativeScript::has_source_code() const {
  165. return false;
  166. }
  167. String NativeScript::get_source_code() const {
  168. return "";
  169. }
  170. void NativeScript::set_source_code(const String &p_code) {
  171. }
  172. Error NativeScript::reload(bool p_keep_state) {
  173. return FAILED;
  174. }
  175. bool NativeScript::has_method(const StringName &p_method) const {
  176. NativeScriptDesc *script_data = get_script_desc();
  177. while (script_data) {
  178. if (script_data->methods.has(p_method))
  179. return true;
  180. script_data = script_data->base_data;
  181. }
  182. return false;
  183. }
  184. MethodInfo NativeScript::get_method_info(const StringName &p_method) const {
  185. NativeScriptDesc *script_data = get_script_desc();
  186. if (!script_data)
  187. return MethodInfo();
  188. while (script_data) {
  189. Map<StringName, NativeScriptDesc::Method>::Element *M = script_data->methods.find(p_method);
  190. if (M)
  191. return M->get().info;
  192. script_data = script_data->base_data;
  193. }
  194. return MethodInfo();
  195. }
  196. bool NativeScript::is_tool() const {
  197. NativeScriptDesc *script_data = get_script_desc();
  198. if (script_data)
  199. return script_data->is_tool;
  200. return false;
  201. }
  202. String NativeScript::get_node_type() const {
  203. return ""; // NOTE(karroffel): uhm?
  204. }
  205. ScriptLanguage *NativeScript::get_language() const {
  206. return NativeScriptLanguage::get_singleton();
  207. }
  208. bool NativeScript::has_script_signal(const StringName &p_signal) const {
  209. NativeScriptDesc *script_data = get_script_desc();
  210. if (!script_data)
  211. return false;
  212. return script_data->signals_.has(p_signal);
  213. }
  214. void NativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  215. NativeScriptDesc *script_data = get_script_desc();
  216. if (!script_data)
  217. return;
  218. Set<MethodInfo> signals_;
  219. while (script_data) {
  220. for (Map<StringName, NativeScriptDesc::Signal>::Element *S = script_data->signals_.front(); S; S = S->next()) {
  221. signals_.insert(S->get().signal);
  222. }
  223. script_data = script_data->base_data;
  224. }
  225. for (Set<MethodInfo>::Element *E = signals_.front(); E; E = E->next()) {
  226. r_signals->push_back(E->get());
  227. }
  228. }
  229. bool NativeScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  230. NativeScriptDesc *script_data = get_script_desc();
  231. if (!script_data)
  232. return false;
  233. Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_property);
  234. if (!P)
  235. return false;
  236. r_value = P->get().default_value;
  237. return true;
  238. }
  239. void NativeScript::update_exports() {
  240. }
  241. void NativeScript::get_script_method_list(List<MethodInfo> *p_list) const {
  242. NativeScriptDesc *script_data = get_script_desc();
  243. if (!script_data)
  244. return;
  245. Set<MethodInfo> methods;
  246. while (script_data) {
  247. for (Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.front(); E; E = E->next()) {
  248. methods.insert(E->get().info);
  249. }
  250. script_data = script_data->base_data;
  251. }
  252. for (Set<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  253. p_list->push_back(E->get());
  254. }
  255. }
  256. void NativeScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  257. NativeScriptDesc *script_data = get_script_desc();
  258. if (!script_data)
  259. return;
  260. Set<PropertyInfo> properties;
  261. while (script_data) {
  262. for (Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.front(); E; E = E->next()) {
  263. properties.insert(E->get().info);
  264. }
  265. script_data = script_data->base_data;
  266. }
  267. for (Set<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  268. p_list->push_back(E->get());
  269. }
  270. }
  271. Variant NativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  272. if (lib_path.empty() || class_name.empty() || library.is_null()) {
  273. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  274. return Variant();
  275. }
  276. NativeScriptDesc *script_data = get_script_desc();
  277. if (!script_data) {
  278. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  279. return Variant();
  280. }
  281. r_error.error = Variant::CallError::CALL_OK;
  282. REF ref;
  283. Object *owner = NULL;
  284. if (!(script_data->base_native_type == "")) {
  285. owner = ClassDB::instance(script_data->base_native_type);
  286. } else {
  287. owner = memnew(Reference);
  288. }
  289. Reference *r = owner->cast_to<Reference>();
  290. if (r) {
  291. ref = REF(r);
  292. }
  293. // GDScript does it like this: _create_instance(p_args, p_argcount, owner, r != NULL, r_error);
  294. // TODO(karroffel): support varargs for constructors.
  295. NativeScriptInstance *instance = (NativeScriptInstance *)instance_create(owner);
  296. owner->set_script_instance(instance);
  297. if (!instance) {
  298. if (ref.is_null()) {
  299. memdelete(owner); //no owner, sorry
  300. }
  301. return Variant();
  302. }
  303. if (ref.is_valid()) {
  304. return ref;
  305. } else {
  306. return owner;
  307. }
  308. }
  309. // TODO(karroffel): implement this
  310. NativeScript::NativeScript() {
  311. library = Ref<GDNative>();
  312. lib_path = "";
  313. class_name = "";
  314. }
  315. // TODO(karroffel): implement this
  316. NativeScript::~NativeScript() {
  317. NSL->unregister_script(this);
  318. }
  319. ////// ScriptInstance stuff
  320. #define GET_SCRIPT_DESC() script->get_script_desc()
  321. void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
  322. if (script_data->base_data) {
  323. _ml_call_reversed(script_data->base_data, p_method, p_args, p_argcount);
  324. }
  325. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  326. if (E) {
  327. godot_variant res = E->get().method.method((godot_object *)owner, E->get().method.method_data, userdata, p_argcount, (godot_variant **)p_args);
  328. godot_variant_destroy(&res);
  329. }
  330. }
  331. bool NativeScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  332. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  333. while (script_data) {
  334. Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name);
  335. if (P) {
  336. P->get().setter.set_func((godot_object *)owner,
  337. P->get().setter.method_data,
  338. userdata,
  339. (godot_variant *)&p_value);
  340. return true;
  341. }
  342. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_set");
  343. if (E) {
  344. Variant name = p_name;
  345. const Variant *args[2] = { &name, &p_value };
  346. E->get().method.method((godot_object *)owner,
  347. E->get().method.method_data,
  348. userdata,
  349. 2,
  350. (godot_variant **)args);
  351. return true;
  352. }
  353. script_data = script_data->base_data;
  354. }
  355. return false;
  356. }
  357. bool NativeScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  358. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  359. while (script_data) {
  360. Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name);
  361. if (P) {
  362. godot_variant value;
  363. value = P->get().getter.get_func((godot_object *)owner,
  364. P->get().getter.method_data,
  365. userdata);
  366. r_ret = *(Variant *)&value;
  367. godot_variant_destroy(&value);
  368. return true;
  369. }
  370. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get");
  371. if (E) {
  372. Variant name = p_name;
  373. const Variant *args[1] = { &name };
  374. godot_variant result;
  375. result = E->get().method.method((godot_object *)owner,
  376. E->get().method.method_data,
  377. userdata,
  378. 1,
  379. (godot_variant **)args);
  380. r_ret = *(Variant *)&result;
  381. godot_variant_destroy(&result);
  382. if (r_ret.get_type() == Variant::NIL) {
  383. return false;
  384. }
  385. return true;
  386. }
  387. script_data = script_data->base_data;
  388. }
  389. return false;
  390. }
  391. void NativeScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  392. script->get_script_property_list(p_properties);
  393. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  394. while (script_data) {
  395. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find("_get_property_list");
  396. if (E) {
  397. godot_variant result;
  398. result = E->get().method.method((godot_object *)owner,
  399. E->get().method.method_data,
  400. userdata,
  401. 0,
  402. NULL);
  403. Variant res = *(Variant *)&result;
  404. godot_variant_destroy(&result);
  405. if (res.get_type() != Variant::ARRAY) {
  406. ERR_EXPLAIN("_get_property_list must return an array of dictionaries");
  407. ERR_FAIL();
  408. }
  409. Array arr = res;
  410. for (int i = 0; i < arr.size(); i++) {
  411. Dictionary d = arr[i];
  412. ERR_CONTINUE(!d.has("name"));
  413. ERR_CONTINUE(!d.has("type"));
  414. PropertyInfo info;
  415. info.type = Variant::Type(d["type"].operator int64_t());
  416. ERR_CONTINUE(info.type < 0 || info.type >= Variant::VARIANT_MAX);
  417. info.name = d["name"];
  418. ERR_CONTINUE(info.name == "");
  419. if (d.has("hint")) {
  420. info.hint = PropertyHint(d["hint"].operator int64_t());
  421. }
  422. if (d.has("hint_string")) {
  423. info.hint_string = d["hint_string"];
  424. }
  425. if (d.has("usage")) {
  426. info.usage = d["usage"];
  427. }
  428. p_properties->push_back(info);
  429. }
  430. }
  431. script_data = script_data->base_data;
  432. }
  433. return;
  434. }
  435. Variant::Type NativeScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  436. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  437. while (script_data) {
  438. Map<StringName, NativeScriptDesc::Property>::Element *P = script_data->properties.find(p_name);
  439. if (P) {
  440. *r_is_valid = true;
  441. return P->get().info.type;
  442. }
  443. script_data = script_data->base_data;
  444. }
  445. return Variant::NIL;
  446. }
  447. void NativeScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  448. script->get_method_list(p_list);
  449. }
  450. bool NativeScriptInstance::has_method(const StringName &p_method) const {
  451. return script->has_method(p_method);
  452. }
  453. Variant NativeScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  454. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  455. while (script_data) {
  456. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  457. if (E) {
  458. godot_variant result;
  459. result = E->get().method.method((godot_object *)owner,
  460. E->get().method.method_data,
  461. userdata,
  462. p_argcount,
  463. (godot_variant **)p_args);
  464. Variant res = *(Variant *)&result;
  465. godot_variant_destroy(&result);
  466. r_error.error = Variant::CallError::CALL_OK;
  467. return res;
  468. }
  469. script_data = script_data->base_data;
  470. }
  471. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  472. return Variant();
  473. }
  474. void NativeScriptInstance::notification(int p_notification) {
  475. Variant value = p_notification;
  476. const Variant *args[1] = { &value };
  477. call_multilevel("_notification", args, 1);
  478. }
  479. Ref<Script> NativeScriptInstance::get_script() const {
  480. return script;
  481. }
  482. NativeScriptInstance::RPCMode NativeScriptInstance::get_rpc_mode(const StringName &p_method) const {
  483. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  484. while (script_data) {
  485. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  486. if (E) {
  487. switch (E->get().rpc_mode) {
  488. case GODOT_METHOD_RPC_MODE_DISABLED:
  489. return RPC_MODE_DISABLED;
  490. case GODOT_METHOD_RPC_MODE_REMOTE:
  491. return RPC_MODE_REMOTE;
  492. case GODOT_METHOD_RPC_MODE_SYNC:
  493. return RPC_MODE_SYNC;
  494. case GODOT_METHOD_RPC_MODE_MASTER:
  495. return RPC_MODE_MASTER;
  496. case GODOT_METHOD_RPC_MODE_SLAVE:
  497. return RPC_MODE_SLAVE;
  498. default:
  499. return RPC_MODE_DISABLED;
  500. }
  501. }
  502. script_data = script_data->base_data;
  503. }
  504. return RPC_MODE_DISABLED;
  505. }
  506. // TODO(karroffel): implement this
  507. NativeScriptInstance::RPCMode NativeScriptInstance::get_rset_mode(const StringName &p_variable) const {
  508. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  509. while (script_data) {
  510. Map<StringName, NativeScriptDesc::Property>::Element *E = script_data->properties.find(p_variable);
  511. if (E) {
  512. switch (E->get().rset_mode) {
  513. case GODOT_METHOD_RPC_MODE_DISABLED:
  514. return RPC_MODE_DISABLED;
  515. case GODOT_METHOD_RPC_MODE_REMOTE:
  516. return RPC_MODE_REMOTE;
  517. case GODOT_METHOD_RPC_MODE_SYNC:
  518. return RPC_MODE_SYNC;
  519. case GODOT_METHOD_RPC_MODE_MASTER:
  520. return RPC_MODE_MASTER;
  521. case GODOT_METHOD_RPC_MODE_SLAVE:
  522. return RPC_MODE_SLAVE;
  523. default:
  524. return RPC_MODE_DISABLED;
  525. }
  526. }
  527. script_data = script_data->base_data;
  528. }
  529. return RPC_MODE_DISABLED;
  530. }
  531. ScriptLanguage *NativeScriptInstance::get_language() {
  532. return NativeScriptLanguage::get_singleton();
  533. }
  534. void NativeScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  535. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  536. while (script_data) {
  537. Map<StringName, NativeScriptDesc::Method>::Element *E = script_data->methods.find(p_method);
  538. if (E) {
  539. godot_variant res = E->get().method.method((godot_object *)owner,
  540. E->get().method.method_data,
  541. userdata,
  542. p_argcount,
  543. (godot_variant **)p_args);
  544. godot_variant_destroy(&res);
  545. }
  546. script_data = script_data->base_data;
  547. }
  548. }
  549. void NativeScriptInstance::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  550. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  551. if (script_data) {
  552. _ml_call_reversed(script_data, p_method, p_args, p_argcount);
  553. }
  554. }
  555. NativeScriptInstance::~NativeScriptInstance() {
  556. NativeScriptDesc *script_data = GET_SCRIPT_DESC();
  557. if (!script_data)
  558. return;
  559. script_data->destroy_func.destroy_func((godot_object *)owner, script_data->destroy_func.method_data, userdata);
  560. if (owner) {
  561. script->instance_owners.erase(owner);
  562. }
  563. }
  564. ////// ScriptingLanguage stuff
  565. NativeScriptLanguage *NativeScriptLanguage::singleton;
  566. extern "C" void _native_script_hook();
  567. void NativeScriptLanguage::_hacky_api_anchor() {
  568. _native_script_hook();
  569. }
  570. void NativeScriptLanguage::_unload_stuff() {
  571. for (Map<String, Map<StringName, NativeScriptDesc> >::Element *L = library_classes.front(); L; L = L->next()) {
  572. for (Map<StringName, NativeScriptDesc>::Element *C = L->get().front(); C; C = C->next()) {
  573. // free property stuff first
  574. for (Map<StringName, NativeScriptDesc::Property>::Element *P = C->get().properties.front(); P; P = P->next()) {
  575. if (P->get().getter.free_func)
  576. P->get().getter.free_func(P->get().getter.method_data);
  577. if (P->get().setter.free_func)
  578. P->get().setter.free_func(P->get().setter.method_data);
  579. }
  580. // free method stuff
  581. for (Map<StringName, NativeScriptDesc::Method>::Element *M = C->get().methods.front(); M; M = M->next()) {
  582. if (M->get().method.free_func)
  583. M->get().method.free_func(M->get().method.method_data);
  584. }
  585. // free constructor/destructor
  586. if (C->get().create_func.free_func)
  587. C->get().create_func.free_func(C->get().create_func.method_data);
  588. if (C->get().destroy_func.free_func)
  589. C->get().destroy_func.free_func(C->get().destroy_func.method_data);
  590. }
  591. }
  592. }
  593. NativeScriptLanguage::NativeScriptLanguage() {
  594. NativeScriptLanguage::singleton = this;
  595. #ifndef NO_THREADS
  596. mutex = Mutex::create();
  597. #endif
  598. }
  599. // TODO(karroffel): implement this
  600. NativeScriptLanguage::~NativeScriptLanguage() {
  601. // _unload_stuff(); // NOTE(karroffel): This gets called in ::finish()
  602. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  603. L->get()->terminate();
  604. NSL->library_classes.clear();
  605. NSL->library_gdnatives.clear();
  606. NSL->library_script_users.clear();
  607. }
  608. #ifndef NO_THREADS
  609. memdelete(mutex);
  610. #endif
  611. }
  612. String NativeScriptLanguage::get_name() const {
  613. return "NativeScript";
  614. }
  615. void _add_reload_node() {
  616. #ifdef TOOLS_ENABLED
  617. NativeReloadNode *rn = memnew(NativeReloadNode);
  618. EditorNode::get_singleton()->add_child(rn);
  619. #endif
  620. }
  621. // TODO(karroffel): implement this
  622. void NativeScriptLanguage::init() {
  623. #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED)
  624. List<String> args = OS::get_singleton()->get_cmdline_args();
  625. List<String>::Element *E = args.find("--gdnative-generate-json-api");
  626. if (E && E->next()) {
  627. if (generate_c_api(E->next()->get()) != OK) {
  628. ERR_PRINT("Failed to generate C API\n");
  629. }
  630. }
  631. #endif
  632. #ifdef TOOLS_ENABLED
  633. EditorNode::add_init_callback(&_add_reload_node);
  634. #endif
  635. }
  636. String NativeScriptLanguage::get_type() const {
  637. return "NativeScript";
  638. }
  639. String NativeScriptLanguage::get_extension() const {
  640. return "gdns";
  641. }
  642. Error NativeScriptLanguage::execute_file(const String &p_path) {
  643. return OK; // Qué?
  644. }
  645. void NativeScriptLanguage::finish() {
  646. _unload_stuff();
  647. }
  648. void NativeScriptLanguage::get_reserved_words(List<String> *p_words) const {
  649. }
  650. void NativeScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  651. }
  652. void NativeScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  653. }
  654. // TODO(karroffel): implement this
  655. Ref<Script> NativeScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  656. NativeScript *s = memnew(NativeScript);
  657. s->set_class_name(p_class_name);
  658. // TODO(karroffel): use p_base_class_name
  659. return Ref<NativeScript>(s);
  660. }
  661. 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) const {
  662. return false;
  663. }
  664. Script *NativeScriptLanguage::create_script() const {
  665. NativeScript *script = memnew(NativeScript);
  666. return script;
  667. }
  668. bool NativeScriptLanguage::has_named_classes() const {
  669. return true;
  670. }
  671. int NativeScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  672. return -1;
  673. }
  674. String NativeScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  675. return "";
  676. }
  677. void NativeScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  678. }
  679. void NativeScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  680. }
  681. // Debugging stuff here. Not used for now.
  682. String NativeScriptLanguage::debug_get_error() const {
  683. return "";
  684. }
  685. int NativeScriptLanguage::debug_get_stack_level_count() const {
  686. return -1;
  687. }
  688. int NativeScriptLanguage::debug_get_stack_level_line(int p_level) const {
  689. return -1;
  690. }
  691. String NativeScriptLanguage::debug_get_stack_level_function(int p_level) const {
  692. return "";
  693. }
  694. String NativeScriptLanguage::debug_get_stack_level_source(int p_level) const {
  695. return "";
  696. }
  697. 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) {
  698. }
  699. 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) {
  700. }
  701. void NativeScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  702. }
  703. String NativeScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  704. return "";
  705. }
  706. // Debugging stuff end.
  707. void NativeScriptLanguage::reload_all_scripts() {
  708. }
  709. void NativeScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  710. }
  711. void NativeScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  712. p_extensions->push_back("gdns");
  713. }
  714. void NativeScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  715. }
  716. void NativeScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  717. }
  718. void NativeScriptLanguage::profiling_start() {
  719. }
  720. void NativeScriptLanguage::profiling_stop() {
  721. }
  722. int NativeScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  723. return -1;
  724. }
  725. int NativeScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  726. return -1;
  727. }
  728. #ifndef NO_THREADS
  729. void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
  730. MutexLock lock(mutex);
  731. libs_to_init.insert(lib);
  732. scripts_to_register.insert(script);
  733. has_objects_to_register = true;
  734. }
  735. #endif
  736. void NativeScriptLanguage::init_library(const Ref<GDNativeLibrary> &lib) {
  737. #ifndef NO_THREADS
  738. MutexLock lock(mutex);
  739. #endif
  740. // See if this library was "registered" already.
  741. const String &lib_path = lib->get_active_library_path();
  742. Map<String, Ref<GDNative> >::Element *E = library_gdnatives.find(lib_path);
  743. if (!E) {
  744. Ref<GDNative> gdn;
  745. gdn.instance();
  746. gdn->set_library(lib);
  747. // TODO(karroffel): check the return value?
  748. gdn->initialize();
  749. library_gdnatives.insert(lib_path, gdn);
  750. library_classes.insert(lib_path, Map<StringName, NativeScriptDesc>());
  751. if (!library_script_users.has(lib_path))
  752. library_script_users.insert(lib_path, Set<NativeScript *>());
  753. void *args[1] = {
  754. (void *)&lib_path
  755. };
  756. // here the library registers all the classes and stuff.
  757. gdn->call_native_raw(_init_call_type,
  758. _init_call_name,
  759. NULL,
  760. 1,
  761. args,
  762. NULL);
  763. } else {
  764. // already initialized. Nice.
  765. }
  766. }
  767. void NativeScriptLanguage::register_script(NativeScript *script) {
  768. #ifndef NO_THREADS
  769. MutexLock lock(mutex);
  770. #endif
  771. library_script_users[script->lib_path].insert(script);
  772. }
  773. void NativeScriptLanguage::unregister_script(NativeScript *script) {
  774. #ifndef NO_THREADS
  775. MutexLock lock(mutex);
  776. #endif
  777. Map<String, Set<NativeScript *> >::Element *S = library_script_users.find(script->lib_path);
  778. if (S) {
  779. S->get().erase(script);
  780. if (S->get().size() == 0) {
  781. library_script_users.erase(S);
  782. }
  783. }
  784. #ifndef NO_THREADS
  785. scripts_to_register.erase(script);
  786. #endif
  787. }
  788. #ifndef NO_THREADS
  789. void NativeScriptLanguage::frame() {
  790. if (has_objects_to_register) {
  791. MutexLock lock(mutex);
  792. for (Set<Ref<GDNativeLibrary> >::Element *L = libs_to_init.front(); L; L = L->next()) {
  793. init_library(L->get());
  794. }
  795. libs_to_init.clear();
  796. for (Set<NativeScript *>::Element *S = scripts_to_register.front(); S; S = S->next()) {
  797. register_script(S->get());
  798. }
  799. scripts_to_register.clear();
  800. has_objects_to_register = false;
  801. }
  802. }
  803. void NativeScriptLanguage::thread_enter() {
  804. Vector<Ref<GDNative> > libs;
  805. {
  806. MutexLock lock(mutex);
  807. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  808. libs.push_back(L->get());
  809. }
  810. }
  811. for (int i = 0; i < libs.size(); ++i) {
  812. libs[i]->call_native_raw(
  813. _thread_cb_call_type,
  814. _thread_enter_call_name,
  815. NULL,
  816. 0,
  817. NULL,
  818. NULL);
  819. }
  820. }
  821. void NativeScriptLanguage::thread_exit() {
  822. Vector<Ref<GDNative> > libs;
  823. {
  824. MutexLock lock(mutex);
  825. for (Map<String, Ref<GDNative> >::Element *L = library_gdnatives.front(); L; L = L->next()) {
  826. libs.push_back(L->get());
  827. }
  828. }
  829. for (int i = 0; i < libs.size(); ++i) {
  830. libs[i]->call_native_raw(
  831. _thread_cb_call_type,
  832. _thread_exit_call_name,
  833. NULL,
  834. 0,
  835. NULL,
  836. NULL);
  837. }
  838. }
  839. #endif // NO_THREADS
  840. void NativeReloadNode::_bind_methods() {
  841. ClassDB::bind_method(D_METHOD("_notification"), &NativeReloadNode::_notification);
  842. }
  843. void NativeReloadNode::_notification(int p_what) {
  844. #ifdef TOOLS_ENABLED
  845. switch (p_what) {
  846. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  847. if (unloaded)
  848. break;
  849. #ifndef NO_THREADS
  850. MutexLock lock(NSL->mutex);
  851. #endif
  852. NSL->_unload_stuff();
  853. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  854. L->get()->terminate();
  855. NSL->library_classes.erase(L->key());
  856. }
  857. unloaded = true;
  858. } break;
  859. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  860. if (!unloaded)
  861. break;
  862. #ifndef NO_THREADS
  863. MutexLock lock(NSL->mutex);
  864. #endif
  865. Set<StringName> libs_to_remove;
  866. for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
  867. if (!L->get()->initialize()) {
  868. libs_to_remove.insert(L->key());
  869. continue;
  870. }
  871. NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
  872. void *args[1] = {
  873. (void *)&L->key()
  874. };
  875. // here the library registers all the classes and stuff.
  876. L->get()->call_native_raw(NSL->_init_call_type,
  877. NSL->_init_call_name,
  878. NULL,
  879. 1,
  880. args,
  881. NULL);
  882. for (Map<String, Set<NativeScript *> >::Element *U = NSL->library_script_users.front(); U; U = U->next()) {
  883. for (Set<NativeScript *>::Element *S = U->get().front(); S; S = S->next()) {
  884. NativeScript *script = S->get();
  885. if (script->placeholders.size() == 0)
  886. continue;
  887. for (Set<PlaceHolderScriptInstance *>::Element *P = script->placeholders.front(); P; P = P->next()) {
  888. script->_update_placeholder(P->get());
  889. }
  890. }
  891. }
  892. }
  893. unloaded = false;
  894. for (Set<StringName>::Element *R = libs_to_remove.front(); R; R = R->next()) {
  895. NSL->library_gdnatives.erase(R->get());
  896. }
  897. } break;
  898. default: {
  899. };
  900. }
  901. #endif
  902. }
  903. RES ResourceFormatLoaderNativeScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  904. ResourceFormatLoaderText rsflt;
  905. return rsflt.load(p_path, p_original_path, r_error);
  906. }
  907. void ResourceFormatLoaderNativeScript::get_recognized_extensions(List<String> *p_extensions) const {
  908. p_extensions->push_back("gdns");
  909. }
  910. bool ResourceFormatLoaderNativeScript::handles_type(const String &p_type) const {
  911. return (p_type == "Script" || p_type == "NativeScript");
  912. }
  913. String ResourceFormatLoaderNativeScript::get_resource_type(const String &p_path) const {
  914. String el = p_path.get_extension().to_lower();
  915. if (el == "gdns")
  916. return "NativeScript";
  917. return "";
  918. }
  919. Error ResourceFormatSaverNativeScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  920. ResourceFormatSaverText rfst;
  921. return rfst.save(p_path, p_resource, p_flags);
  922. }
  923. bool ResourceFormatSaverNativeScript::recognize(const RES &p_resource) const {
  924. return p_resource->cast_to<NativeScript>() != NULL;
  925. }
  926. void ResourceFormatSaverNativeScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  927. if (p_resource->cast_to<NativeScript>()) {
  928. p_extensions->push_back("gdns");
  929. }
  930. }