pluginscript_script.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*************************************************************************/
  2. /* pluginscript_script.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. // 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. }
  53. #ifdef TOOLS_ENABLED
  54. void PluginScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  55. placeholders.erase(p_placeholder);
  56. }
  57. #endif
  58. bool PluginScript::can_instance() const {
  59. bool can = _valid || (!_tool && !ScriptServer::is_scripting_enabled());
  60. return can;
  61. }
  62. Ref<Script> PluginScript::get_base_script() const {
  63. if (_ref_base_parent.is_valid()) {
  64. return Ref<PluginScript>(_ref_base_parent);
  65. } else {
  66. return Ref<Script>();
  67. }
  68. }
  69. StringName PluginScript::get_instance_base_type() const {
  70. if (_native_parent)
  71. return _native_parent;
  72. if (_ref_base_parent.is_valid())
  73. return _ref_base_parent->get_instance_base_type();
  74. return StringName();
  75. }
  76. void PluginScript::update_exports() {
  77. // TODO
  78. #ifdef TOOLS_ENABLED
  79. #if 0
  80. ASSERT_SCRIPT_VALID();
  81. if (/*changed &&*/ placeholders.size()) { //hm :(
  82. //update placeholders if any
  83. Map<StringName, Variant> propdefvalues;
  84. List<PropertyInfo> propinfos;
  85. const String *props = (const String *)pybind_get_prop_list(_py_exposed_class);
  86. for (int i = 0; props[i] != ""; ++i) {
  87. const String propname = props[i];
  88. pybind_get_prop_default_value(_py_exposed_class, propname.c_str(), (godot_variant *)&propdefvalues[propname]);
  89. pybind_prop_info raw_info;
  90. pybind_get_prop_info(_py_exposed_class, propname.c_str(), &raw_info);
  91. PropertyInfo info;
  92. info.type = (Variant::Type)raw_info.type;
  93. info.name = propname;
  94. info.hint = (PropertyHint)raw_info.hint;
  95. info.hint_string = *(String *)&raw_info.hint_string;
  96. info.usage = raw_info.usage;
  97. propinfos.push_back(info);
  98. }
  99. for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
  100. E->get()->update(propinfos, propdefvalues);
  101. }
  102. }
  103. #endif
  104. #endif
  105. }
  106. // TODO: rename p_this "p_owner" ?
  107. ScriptInstance *PluginScript::instance_create(Object *p_this) {
  108. ASSERT_SCRIPT_VALID_V(NULL);
  109. // TODO check script validity ?
  110. if (!_tool && !ScriptServer::is_scripting_enabled()) {
  111. #ifdef TOOLS_ENABLED
  112. // Instance a fake script for editing the values
  113. PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(get_language(), Ref<Script>(this), p_this));
  114. placeholders.insert(si);
  115. update_exports();
  116. return si;
  117. #else
  118. return NULL;
  119. #endif
  120. }
  121. PluginScript *top = this;
  122. // TODO: can be optimized by storing a PluginScript::_base_parent direct pointer
  123. while (top->_ref_base_parent.is_valid())
  124. top = top->_ref_base_parent.ptr();
  125. if (top->_native_parent) {
  126. if (!ClassDB::is_parent_class(p_this->get_class_name(), top->_native_parent)) {
  127. String msg = "Script inherits from native type '" + String(top->_native_parent) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'";
  128. // TODO: implement PluginscriptLanguage::debug_break_parse
  129. // if (ScriptDebugger::get_singleton()) {
  130. // _language->debug_break_parse(get_path(), 0, msg);
  131. // }
  132. ERR_EXPLAIN(msg);
  133. ERR_FAIL_V(NULL);
  134. }
  135. }
  136. PluginScriptInstance *instance = memnew(PluginScriptInstance());
  137. const bool success = instance->init(this, p_this);
  138. if (success) {
  139. _language->lock();
  140. _instances.insert(instance->get_owner());
  141. _language->unlock();
  142. return instance;
  143. } else {
  144. memdelete(instance);
  145. ERR_FAIL_V(NULL);
  146. }
  147. }
  148. bool PluginScript::instance_has(const Object *p_this) const {
  149. _language->lock();
  150. bool hasit = _instances.has((Object *)p_this);
  151. _language->unlock();
  152. return hasit;
  153. }
  154. bool PluginScript::has_source_code() const {
  155. bool has = _source != "";
  156. return has;
  157. }
  158. String PluginScript::get_source_code() const {
  159. return _source;
  160. }
  161. void PluginScript::set_source_code(const String &p_code) {
  162. if (_source == p_code)
  163. return;
  164. _source = p_code;
  165. }
  166. Error PluginScript::reload(bool p_keep_state) {
  167. _language->lock();
  168. ERR_FAIL_COND_V(!p_keep_state && _instances.size(), ERR_ALREADY_IN_USE);
  169. _language->unlock();
  170. _valid = false;
  171. String basedir = _path;
  172. if (basedir == "")
  173. basedir = get_path();
  174. if (basedir != "")
  175. basedir = basedir.get_base_dir();
  176. if (_data) {
  177. _desc->finish(_data);
  178. }
  179. Error err;
  180. godot_pluginscript_script_manifest manifest = _desc->init(
  181. _language->_data,
  182. (godot_string *)&_path,
  183. (godot_string *)&_source,
  184. (godot_error *)&err);
  185. if (err) {
  186. // TODO: GDscript uses `ScriptDebugger` here to jump into the parsing error
  187. return err;
  188. }
  189. _valid = true;
  190. // Use the manifest to configure this script object
  191. _data = manifest.data;
  192. _name = *(StringName *)&manifest.name;
  193. _tool = manifest.is_tool;
  194. // Base name is either another PluginScript or a regular class accessible
  195. // through ClassDB
  196. StringName *base_name = (StringName *)&manifest.base;
  197. for (SelfList<PluginScript> *e = _language->_script_list.first(); e != NULL; e = e->next()) {
  198. if (e->self()->_name == *base_name) {
  199. // Found you, base is a PluginScript !
  200. _ref_base_parent = Ref<PluginScript>(e->self());
  201. break;
  202. }
  203. }
  204. if (!_ref_base_parent.is_valid()) {
  205. // Base is a native ClassDB
  206. if (!ClassDB::class_exists(*base_name)) {
  207. ERR_EXPLAIN("Unknown script '" + String(_name) + "' parent '" + String(*base_name) + "'.");
  208. ERR_FAIL_V(ERR_PARSE_ERROR);
  209. }
  210. _native_parent = *base_name;
  211. }
  212. Dictionary *members = (Dictionary *)&manifest.member_lines;
  213. for (const Variant *key = members->next(); key != NULL; key = members->next(key)) {
  214. _member_lines[*key] = (*members)[key];
  215. }
  216. Array *methods = (Array *)&manifest.methods;
  217. for (int i = 0; i < methods->size(); ++i) {
  218. Dictionary v = (*methods)[i];
  219. MethodInfo mi = MethodInfo::from_dict(v);
  220. _methods_info[mi.name] = mi;
  221. // rpc_mode is passed as an optional field and is not part of MethodInfo
  222. Variant var = v["rpc_mode"];
  223. if (var == Variant()) {
  224. _methods_rpc_mode[mi.name] = ScriptInstance::RPC_MODE_DISABLED;
  225. } else {
  226. _methods_rpc_mode[mi.name] = ScriptInstance::RPCMode(int(var));
  227. }
  228. }
  229. Array *signals = (Array *)&manifest.signals;
  230. for (int i = 0; i < signals->size(); ++i) {
  231. Variant v = (*signals)[i];
  232. MethodInfo mi = MethodInfo::from_dict(v);
  233. _signals_info[mi.name] = mi;
  234. }
  235. Array *properties = (Array *)&manifest.properties;
  236. for (int i = 0; i < properties->size(); ++i) {
  237. Dictionary v = (*properties)[i];
  238. PropertyInfo pi = PropertyInfo::from_dict(v);
  239. _properties_info[pi.name] = pi;
  240. _properties_default_values[pi.name] = v["default_value"];
  241. // rset_mode is passed as an optional field and is not part of PropertyInfo
  242. Variant var = v["rset_mode"];
  243. if (var == Variant()) {
  244. _methods_rpc_mode[pi.name] = ScriptInstance::RPC_MODE_DISABLED;
  245. } else {
  246. _methods_rpc_mode[pi.name] = ScriptInstance::RPCMode(int(var));
  247. }
  248. }
  249. // Manifest's attributes must be explicitly freed
  250. godot_string_name_destroy(&manifest.name);
  251. godot_string_name_destroy(&manifest.base);
  252. godot_dictionary_destroy(&manifest.member_lines);
  253. godot_array_destroy(&manifest.methods);
  254. godot_array_destroy(&manifest.signals);
  255. godot_array_destroy(&manifest.properties);
  256. #ifdef TOOLS_ENABLED
  257. /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  258. _update_placeholder(E->get());
  259. }*/
  260. #endif
  261. return OK;
  262. }
  263. void PluginScript::get_script_method_list(List<MethodInfo> *r_methods) const {
  264. ASSERT_SCRIPT_VALID();
  265. for (Map<StringName, MethodInfo>::Element *e = _methods_info.front(); e != NULL; e = e->next()) {
  266. r_methods->push_back(e->get());
  267. }
  268. }
  269. void PluginScript::get_script_property_list(List<PropertyInfo> *r_properties) const {
  270. ASSERT_SCRIPT_VALID();
  271. for (Map<StringName, PropertyInfo>::Element *e = _properties_info.front(); e != NULL; e = e->next()) {
  272. r_properties->push_back(e->get());
  273. }
  274. }
  275. bool PluginScript::has_method(const StringName &p_method) const {
  276. ASSERT_SCRIPT_VALID_V(false);
  277. return _methods_info.has(p_method);
  278. }
  279. MethodInfo PluginScript::get_method_info(const StringName &p_method) const {
  280. ASSERT_SCRIPT_VALID_V(MethodInfo());
  281. const Map<StringName, MethodInfo>::Element *e = _methods_info.find(p_method);
  282. if (e != NULL) {
  283. return e->get();
  284. } else {
  285. return MethodInfo();
  286. }
  287. }
  288. bool PluginScript::has_property(const StringName &p_method) const {
  289. ASSERT_SCRIPT_VALID_V(false);
  290. return _properties_info.has(p_method);
  291. }
  292. PropertyInfo PluginScript::get_property_info(const StringName &p_property) const {
  293. ASSERT_SCRIPT_VALID_V(PropertyInfo());
  294. const Map<StringName, PropertyInfo>::Element *e = _properties_info.find(p_property);
  295. if (e != NULL) {
  296. return e->get();
  297. } else {
  298. return PropertyInfo();
  299. }
  300. }
  301. bool PluginScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  302. ASSERT_SCRIPT_VALID_V(false);
  303. #ifdef TOOLS_ENABLED
  304. const Map<StringName, Variant>::Element *e = _properties_default_values.find(p_property);
  305. if (e != NULL) {
  306. r_value = e->get();
  307. return true;
  308. } else {
  309. return false;
  310. }
  311. #endif
  312. return false;
  313. }
  314. String PluginScript::get_node_type() const {
  315. // Even GDscript doesn't know what to put here !
  316. return ""; // ?
  317. }
  318. ScriptLanguage *PluginScript::get_language() const {
  319. return _language;
  320. }
  321. Error PluginScript::load_source_code(const String &p_path) {
  322. PoolVector<uint8_t> sourcef;
  323. Error err;
  324. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  325. if (err) {
  326. ERR_FAIL_COND_V(err, err);
  327. }
  328. int len = f->get_len();
  329. sourcef.resize(len + 1);
  330. PoolVector<uint8_t>::Write w = sourcef.write();
  331. int r = f->get_buffer(w.ptr(), len);
  332. f->close();
  333. memdelete(f);
  334. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  335. w[len] = 0;
  336. String s;
  337. if (s.parse_utf8((const char *)w.ptr())) {
  338. 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.");
  339. ERR_FAIL_V(ERR_INVALID_DATA);
  340. }
  341. _source = s;
  342. #ifdef TOOLS_ENABLED
  343. // source_changed_cache=true;
  344. #endif
  345. _path = p_path;
  346. return OK;
  347. }
  348. bool PluginScript::has_script_signal(const StringName &p_signal) const {
  349. ASSERT_SCRIPT_VALID_V(false);
  350. return _signals_info.has(p_signal);
  351. }
  352. void PluginScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  353. ASSERT_SCRIPT_VALID();
  354. for (Map<StringName, MethodInfo>::Element *e = _signals_info.front(); e != NULL; e = e->next()) {
  355. r_signals->push_back(e->get());
  356. }
  357. }
  358. int PluginScript::get_member_line(const StringName &p_member) const {
  359. #ifdef TOOLS_ENABLED
  360. if (_member_lines.has(p_member))
  361. return _member_lines[p_member];
  362. else
  363. #endif
  364. return -1;
  365. }
  366. ScriptInstance::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const {
  367. ASSERT_SCRIPT_VALID_V(ScriptInstance::RPC_MODE_DISABLED);
  368. const Map<StringName, ScriptInstance::RPCMode>::Element *e = _methods_rpc_mode.find(p_method);
  369. if (e != NULL) {
  370. return e->get();
  371. } else {
  372. return ScriptInstance::RPC_MODE_DISABLED;
  373. }
  374. }
  375. ScriptInstance::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const {
  376. ASSERT_SCRIPT_VALID_V(ScriptInstance::RPC_MODE_DISABLED);
  377. const Map<StringName, ScriptInstance::RPCMode>::Element *e = _variables_rset_mode.find(p_variable);
  378. if (e != NULL) {
  379. return e->get();
  380. } else {
  381. return ScriptInstance::RPC_MODE_DISABLED;
  382. }
  383. }
  384. PluginScript::PluginScript()
  385. : _data(NULL), _tool(false), _valid(false), _script_list(this) {
  386. }
  387. void PluginScript::init(PluginScriptLanguage *language) {
  388. _desc = &language->_desc.script_desc;
  389. _language = language;
  390. #ifdef DEBUG_ENABLED
  391. _language->lock();
  392. _language->_script_list.add(&_script_list);
  393. _language->unlock();
  394. #endif
  395. }
  396. PluginScript::~PluginScript() {
  397. _desc->finish(_data);
  398. #ifdef DEBUG_ENABLED
  399. _language->lock();
  400. _language->_script_list.remove(&_script_list);
  401. _language->unlock();
  402. #endif
  403. }