pluginscript_script.cpp 16 KB

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