pluginscript_script.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*************************************************************************/
  2. /* pluginscript_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. // Godot imports
  31. #include "core/os/file_access.h"
  32. // PluginScript imports
  33. #include "pluginscript_instance.h"
  34. #include "pluginscript_script.h"
  35. #if DEBUG_ENABLED
  36. #define __ASSERT_SCRIPT_REASON "Cannot retrieve pluginscript class for this script, is you code correct ?"
  37. #define ASSERT_SCRIPT_VALID() \
  38. { \
  39. ERR_EXPLAIN(__ASSERT_SCRIPT_REASON); \
  40. ERR_FAIL_COND(!can_instance()) \
  41. }
  42. #define ASSERT_SCRIPT_VALID_V(ret) \
  43. { \
  44. ERR_EXPLAIN(__ASSERT_SCRIPT_REASON); \
  45. ERR_FAIL_COND_V(!can_instance(), ret) \
  46. }
  47. #else
  48. #define ASSERT_SCRIPT_VALID()
  49. #define ASSERT_SCRIPT_VALID_V(ret)
  50. #endif
  51. void PluginScript::_bind_methods() {
  52. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &PluginScript::_new, MethodInfo(Variant::OBJECT, "new"));
  53. }
  54. PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Variant::CallError &r_error) {
  55. r_error.error = Variant::CallError::CALL_OK;
  56. // Create instance
  57. PluginScriptInstance *instance = memnew(PluginScriptInstance());
  58. if (instance->init(this, p_owner)) {
  59. _language->lock();
  60. _instances.insert(instance->get_owner());
  61. _language->unlock();
  62. } else {
  63. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  64. memdelete(instance);
  65. ERR_FAIL_V(NULL);
  66. }
  67. // Construct
  68. // TODO: Support arguments in the constructor?
  69. // There is currently no way to get the constructor function name of the script.
  70. // instance->call("__init__", p_args, p_argcount, r_error);
  71. if (p_argcount > 0) {
  72. WARN_PRINT("PluginScript doesn't support arguments in the constructor")
  73. }
  74. return instance;
  75. }
  76. Variant PluginScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  77. r_error.error = Variant::CallError::CALL_OK;
  78. if (!_valid) {
  79. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  80. return Variant();
  81. }
  82. REF ref;
  83. Object *owner = NULL;
  84. if (get_instance_base_type() == "") {
  85. owner = memnew(Reference);
  86. } else {
  87. owner = ClassDB::instance(get_instance_base_type());
  88. }
  89. if (!owner) {
  90. r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  91. return Variant();
  92. }
  93. Reference *r = Object::cast_to<Reference>(owner);
  94. if (r) {
  95. ref = REF(r);
  96. }
  97. PluginScriptInstance *instance = _create_instance(p_args, p_argcount, owner, r_error);
  98. if (!instance) {
  99. if (ref.is_null()) {
  100. memdelete(owner); //no owner, sorry
  101. }
  102. return Variant();
  103. }
  104. if (ref.is_valid()) {
  105. return ref;
  106. } else {
  107. return owner;
  108. }
  109. }
  110. #ifdef TOOLS_ENABLED
  111. void PluginScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  112. placeholders.erase(p_placeholder);
  113. }
  114. #endif
  115. bool PluginScript::can_instance() const {
  116. bool can = _valid || (!_tool && !ScriptServer::is_scripting_enabled());
  117. return can;
  118. }
  119. Ref<Script> PluginScript::get_base_script() const {
  120. if (_ref_base_parent.is_valid()) {
  121. return Ref<PluginScript>(_ref_base_parent);
  122. } else {
  123. return Ref<Script>();
  124. }
  125. }
  126. StringName PluginScript::get_instance_base_type() const {
  127. if (_native_parent)
  128. return _native_parent;
  129. if (_ref_base_parent.is_valid())
  130. return _ref_base_parent->get_instance_base_type();
  131. return StringName();
  132. }
  133. void PluginScript::update_exports() {
  134. #ifdef TOOLS_ENABLED
  135. ASSERT_SCRIPT_VALID();
  136. if (placeholders.size()) {
  137. //update placeholders if any
  138. Map<StringName, Variant> propdefvalues;
  139. List<PropertyInfo> propinfos;
  140. get_script_property_list(&propinfos);
  141. for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
  142. E->get()->update(propinfos, _properties_default_values);
  143. }
  144. }
  145. #endif
  146. }
  147. // TODO: rename p_this "p_owner" ?
  148. ScriptInstance *PluginScript::instance_create(Object *p_this) {
  149. ASSERT_SCRIPT_VALID_V(NULL);
  150. // TODO check script validity ?
  151. if (!_tool && !ScriptServer::is_scripting_enabled()) {
  152. #ifdef TOOLS_ENABLED
  153. // Instance a fake script for editing the values
  154. PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(get_language(), Ref<Script>(this), p_this));
  155. placeholders.insert(si);
  156. update_exports();
  157. return si;
  158. #else
  159. return NULL;
  160. #endif
  161. }
  162. StringName base_type = get_instance_base_type();
  163. if (base_type) {
  164. if (!ClassDB::is_parent_class(p_this->get_class_name(), base_type)) {
  165. String msg = "Script inherits from native type '" + String(base_type) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'";
  166. // TODO: implement PluginscriptLanguage::debug_break_parse
  167. // if (ScriptDebugger::get_singleton()) {
  168. // _language->debug_break_parse(get_path(), 0, msg);
  169. // }
  170. ERR_EXPLAIN(msg);
  171. ERR_FAIL_V(NULL);
  172. }
  173. }
  174. Variant::CallError unchecked_error;
  175. return _create_instance(NULL, 0, p_this, unchecked_error);
  176. }
  177. bool PluginScript::instance_has(const Object *p_this) const {
  178. _language->lock();
  179. bool hasit = _instances.has((Object *)p_this);
  180. _language->unlock();
  181. return hasit;
  182. }
  183. bool PluginScript::has_source_code() const {
  184. bool has = _source != "";
  185. return has;
  186. }
  187. String PluginScript::get_source_code() const {
  188. return _source;
  189. }
  190. void PluginScript::set_source_code(const String &p_code) {
  191. if (_source == p_code)
  192. return;
  193. _source = p_code;
  194. }
  195. Error PluginScript::reload(bool p_keep_state) {
  196. _language->lock();
  197. ERR_FAIL_COND_V(!p_keep_state && _instances.size(), ERR_ALREADY_IN_USE);
  198. _language->unlock();
  199. _valid = false;
  200. String basedir = _path;
  201. if (basedir == "")
  202. basedir = get_path();
  203. if (basedir != "")
  204. basedir = basedir.get_base_dir();
  205. if (_data) {
  206. _desc->finish(_data);
  207. }
  208. Error err;
  209. godot_pluginscript_script_manifest manifest = _desc->init(
  210. _language->_data,
  211. (godot_string *)&_path,
  212. (godot_string *)&_source,
  213. (godot_error *)&err);
  214. if (err) {
  215. // TODO: GDscript uses `ScriptDebugger` here to jump into the parsing error
  216. return err;
  217. }
  218. // Script's parent is passed as base_name which can make reference to a
  219. // ClassDB name (i.e. `Node2D`) or a resource path (i.e. `res://foo/bar.gd`)
  220. StringName *base_name = (StringName *)&manifest.base;
  221. if (*base_name) {
  222. if (ClassDB::class_exists(*base_name)) {
  223. _native_parent = *base_name;
  224. } else {
  225. Ref<Script> res = ResourceLoader::load(*base_name);
  226. if (res.is_valid()) {
  227. _ref_base_parent = res;
  228. } else {
  229. String name = *(StringName *)&manifest.name;
  230. ERR_EXPLAIN(_path + ": Script '" + name + "' has an invalid parent '" + *base_name + "'.");
  231. ERR_FAIL_V(ERR_PARSE_ERROR);
  232. }
  233. }
  234. }
  235. _valid = true;
  236. // Use the manifest to configure this script object
  237. _data = manifest.data;
  238. _name = *(StringName *)&manifest.name;
  239. _tool = manifest.is_tool;
  240. Dictionary *members = (Dictionary *)&manifest.member_lines;
  241. for (const Variant *key = members->next(); key != NULL; key = members->next(key)) {
  242. _member_lines[*key] = (*members)[key];
  243. }
  244. Array *methods = (Array *)&manifest.methods;
  245. for (int i = 0; i < methods->size(); ++i) {
  246. Dictionary v = (*methods)[i];
  247. MethodInfo mi = MethodInfo::from_dict(v);
  248. _methods_info[mi.name] = mi;
  249. // rpc_mode is passed as an optional field and is not part of MethodInfo
  250. Variant var = v["rpc_mode"];
  251. if (var == Variant()) {
  252. _methods_rpc_mode[mi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
  253. } else {
  254. _methods_rpc_mode[mi.name] = MultiplayerAPI::RPCMode(int(var));
  255. }
  256. }
  257. Array *signals = (Array *)&manifest.signals;
  258. for (int i = 0; i < signals->size(); ++i) {
  259. Variant v = (*signals)[i];
  260. MethodInfo mi = MethodInfo::from_dict(v);
  261. _signals_info[mi.name] = mi;
  262. }
  263. Array *properties = (Array *)&manifest.properties;
  264. for (int i = 0; i < properties->size(); ++i) {
  265. Dictionary v = (*properties)[i];
  266. PropertyInfo pi = PropertyInfo::from_dict(v);
  267. _properties_info[pi.name] = pi;
  268. _properties_default_values[pi.name] = v["default_value"];
  269. // rset_mode is passed as an optional field and is not part of PropertyInfo
  270. Variant var = v["rset_mode"];
  271. if (var == Variant()) {
  272. _methods_rpc_mode[pi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
  273. } else {
  274. _methods_rpc_mode[pi.name] = MultiplayerAPI::RPCMode(int(var));
  275. }
  276. }
  277. // Manifest's attributes must be explicitly freed
  278. godot_string_name_destroy(&manifest.name);
  279. godot_string_name_destroy(&manifest.base);
  280. godot_dictionary_destroy(&manifest.member_lines);
  281. godot_array_destroy(&manifest.methods);
  282. godot_array_destroy(&manifest.signals);
  283. godot_array_destroy(&manifest.properties);
  284. #ifdef TOOLS_ENABLED
  285. /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  286. _update_placeholder(E->get());
  287. }*/
  288. #endif
  289. return OK;
  290. }
  291. void PluginScript::get_script_method_list(List<MethodInfo> *r_methods) const {
  292. ASSERT_SCRIPT_VALID();
  293. for (Map<StringName, MethodInfo>::Element *e = _methods_info.front(); e != NULL; e = e->next()) {
  294. r_methods->push_back(e->get());
  295. }
  296. }
  297. void PluginScript::get_script_property_list(List<PropertyInfo> *r_properties) const {
  298. ASSERT_SCRIPT_VALID();
  299. for (Map<StringName, PropertyInfo>::Element *e = _properties_info.front(); e != NULL; e = e->next()) {
  300. r_properties->push_back(e->get());
  301. }
  302. }
  303. bool PluginScript::has_method(const StringName &p_method) const {
  304. ASSERT_SCRIPT_VALID_V(false);
  305. return _methods_info.has(p_method);
  306. }
  307. MethodInfo PluginScript::get_method_info(const StringName &p_method) const {
  308. ASSERT_SCRIPT_VALID_V(MethodInfo());
  309. const Map<StringName, MethodInfo>::Element *e = _methods_info.find(p_method);
  310. if (e != NULL) {
  311. return e->get();
  312. } else {
  313. return MethodInfo();
  314. }
  315. }
  316. bool PluginScript::has_property(const StringName &p_method) const {
  317. ASSERT_SCRIPT_VALID_V(false);
  318. return _properties_info.has(p_method);
  319. }
  320. PropertyInfo PluginScript::get_property_info(const StringName &p_property) const {
  321. ASSERT_SCRIPT_VALID_V(PropertyInfo());
  322. const Map<StringName, PropertyInfo>::Element *e = _properties_info.find(p_property);
  323. if (e != NULL) {
  324. return e->get();
  325. } else {
  326. return PropertyInfo();
  327. }
  328. }
  329. bool PluginScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  330. ASSERT_SCRIPT_VALID_V(false);
  331. #ifdef TOOLS_ENABLED
  332. const Map<StringName, Variant>::Element *e = _properties_default_values.find(p_property);
  333. if (e != NULL) {
  334. r_value = e->get();
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. #endif
  340. return false;
  341. }
  342. ScriptLanguage *PluginScript::get_language() const {
  343. return _language;
  344. }
  345. Error PluginScript::load_source_code(const String &p_path) {
  346. PoolVector<uint8_t> sourcef;
  347. Error err;
  348. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  349. if (err) {
  350. ERR_FAIL_COND_V(err, err);
  351. }
  352. int len = f->get_len();
  353. sourcef.resize(len + 1);
  354. PoolVector<uint8_t>::Write w = sourcef.write();
  355. int r = f->get_buffer(w.ptr(), len);
  356. f->close();
  357. memdelete(f);
  358. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  359. w[len] = 0;
  360. String s;
  361. if (s.parse_utf8((const char *)w.ptr())) {
  362. ERR_EXPLAIN("Script '" + p_path + "' contains invalid unicode (utf-8), so it was not loaded. Please ensure that scripts are saved in valid utf-8 unicode.");
  363. ERR_FAIL_V(ERR_INVALID_DATA);
  364. }
  365. _source = s;
  366. #ifdef TOOLS_ENABLED
  367. // source_changed_cache=true;
  368. #endif
  369. _path = p_path;
  370. return OK;
  371. }
  372. bool PluginScript::has_script_signal(const StringName &p_signal) const {
  373. ASSERT_SCRIPT_VALID_V(false);
  374. return _signals_info.has(p_signal);
  375. }
  376. void PluginScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  377. ASSERT_SCRIPT_VALID();
  378. for (Map<StringName, MethodInfo>::Element *e = _signals_info.front(); e != NULL; e = e->next()) {
  379. r_signals->push_back(e->get());
  380. }
  381. }
  382. int PluginScript::get_member_line(const StringName &p_member) const {
  383. #ifdef TOOLS_ENABLED
  384. if (_member_lines.has(p_member))
  385. return _member_lines[p_member];
  386. else
  387. #endif
  388. return -1;
  389. }
  390. MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const {
  391. ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
  392. const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _methods_rpc_mode.find(p_method);
  393. if (e != NULL) {
  394. return e->get();
  395. } else {
  396. return MultiplayerAPI::RPC_MODE_DISABLED;
  397. }
  398. }
  399. MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const {
  400. ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
  401. const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _variables_rset_mode.find(p_variable);
  402. if (e != NULL) {
  403. return e->get();
  404. } else {
  405. return MultiplayerAPI::RPC_MODE_DISABLED;
  406. }
  407. }
  408. PluginScript::PluginScript() :
  409. _data(NULL),
  410. _tool(false),
  411. _valid(false),
  412. _script_list(this) {
  413. }
  414. void PluginScript::init(PluginScriptLanguage *language) {
  415. _desc = &language->_desc.script_desc;
  416. _language = language;
  417. #ifdef DEBUG_ENABLED
  418. _language->lock();
  419. _language->_script_list.add(&_script_list);
  420. _language->unlock();
  421. #endif
  422. }
  423. PluginScript::~PluginScript() {
  424. _desc->finish(_data);
  425. #ifdef DEBUG_ENABLED
  426. _language->lock();
  427. _language->_script_list.remove(&_script_list);
  428. _language->unlock();
  429. #endif
  430. }