pluginscript_script.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*************************************************************************/
  2. /* pluginscript_script.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. // 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. StringName base_type = get_instance_base_type();
  122. if (base_type) {
  123. if (!ClassDB::is_parent_class(p_this->get_class_name(), base_type)) {
  124. String msg = "Script inherits from native type '" + String(base_type) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'";
  125. // TODO: implement PluginscriptLanguage::debug_break_parse
  126. // if (ScriptDebugger::get_singleton()) {
  127. // _language->debug_break_parse(get_path(), 0, msg);
  128. // }
  129. ERR_EXPLAIN(msg);
  130. ERR_FAIL_V(NULL);
  131. }
  132. }
  133. PluginScriptInstance *instance = memnew(PluginScriptInstance());
  134. const bool success = instance->init(this, p_this);
  135. if (success) {
  136. _language->lock();
  137. _instances.insert(instance->get_owner());
  138. _language->unlock();
  139. return instance;
  140. } else {
  141. memdelete(instance);
  142. ERR_FAIL_V(NULL);
  143. }
  144. }
  145. bool PluginScript::instance_has(const Object *p_this) const {
  146. _language->lock();
  147. bool hasit = _instances.has((Object *)p_this);
  148. _language->unlock();
  149. return hasit;
  150. }
  151. bool PluginScript::has_source_code() const {
  152. bool has = _source != "";
  153. return has;
  154. }
  155. String PluginScript::get_source_code() const {
  156. return _source;
  157. }
  158. void PluginScript::set_source_code(const String &p_code) {
  159. if (_source == p_code)
  160. return;
  161. _source = p_code;
  162. }
  163. Error PluginScript::reload(bool p_keep_state) {
  164. _language->lock();
  165. ERR_FAIL_COND_V(!p_keep_state && _instances.size(), ERR_ALREADY_IN_USE);
  166. _language->unlock();
  167. _valid = false;
  168. String basedir = _path;
  169. if (basedir == "")
  170. basedir = get_path();
  171. if (basedir != "")
  172. basedir = basedir.get_base_dir();
  173. if (_data) {
  174. _desc->finish(_data);
  175. }
  176. Error err;
  177. godot_pluginscript_script_manifest manifest = _desc->init(
  178. _language->_data,
  179. (godot_string *)&_path,
  180. (godot_string *)&_source,
  181. (godot_error *)&err);
  182. if (err) {
  183. // TODO: GDscript uses `ScriptDebugger` here to jump into the parsing error
  184. return err;
  185. }
  186. // Script's parent is passed as base_name which can make reference to a
  187. // ClassDB name (i.e. `Node2D`) or a resource path (i.e. `res://foo/bar.gd`)
  188. StringName *base_name = (StringName *)&manifest.base;
  189. if (*base_name) {
  190. if (ClassDB::class_exists(*base_name)) {
  191. _native_parent = *base_name;
  192. } else {
  193. Ref<Script> res = ResourceLoader::load(*base_name);
  194. if (res.is_valid()) {
  195. _ref_base_parent = res;
  196. } else {
  197. String name = *(StringName *)&manifest.name;
  198. ERR_EXPLAIN(_path + ": Script '" + name + "' has an invalid parent '" + *base_name + "'.");
  199. ERR_FAIL_V(ERR_PARSE_ERROR);
  200. }
  201. }
  202. }
  203. _valid = true;
  204. // Use the manifest to configure this script object
  205. _data = manifest.data;
  206. _name = *(StringName *)&manifest.name;
  207. _tool = manifest.is_tool;
  208. Dictionary *members = (Dictionary *)&manifest.member_lines;
  209. for (const Variant *key = members->next(); key != NULL; key = members->next(key)) {
  210. _member_lines[*key] = (*members)[key];
  211. }
  212. Array *methods = (Array *)&manifest.methods;
  213. for (int i = 0; i < methods->size(); ++i) {
  214. Dictionary v = (*methods)[i];
  215. MethodInfo mi = MethodInfo::from_dict(v);
  216. _methods_info[mi.name] = mi;
  217. // rpc_mode is passed as an optional field and is not part of MethodInfo
  218. Variant var = v["rpc_mode"];
  219. if (var == Variant()) {
  220. _methods_rpc_mode[mi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
  221. } else {
  222. _methods_rpc_mode[mi.name] = MultiplayerAPI::RPCMode(int(var));
  223. }
  224. }
  225. Array *signals = (Array *)&manifest.signals;
  226. for (int i = 0; i < signals->size(); ++i) {
  227. Variant v = (*signals)[i];
  228. MethodInfo mi = MethodInfo::from_dict(v);
  229. _signals_info[mi.name] = mi;
  230. }
  231. Array *properties = (Array *)&manifest.properties;
  232. for (int i = 0; i < properties->size(); ++i) {
  233. Dictionary v = (*properties)[i];
  234. PropertyInfo pi = PropertyInfo::from_dict(v);
  235. _properties_info[pi.name] = pi;
  236. _properties_default_values[pi.name] = v["default_value"];
  237. // rset_mode is passed as an optional field and is not part of PropertyInfo
  238. Variant var = v["rset_mode"];
  239. if (var == Variant()) {
  240. _methods_rpc_mode[pi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
  241. } else {
  242. _methods_rpc_mode[pi.name] = MultiplayerAPI::RPCMode(int(var));
  243. }
  244. }
  245. // Manifest's attributes must be explicitly freed
  246. godot_string_name_destroy(&manifest.name);
  247. godot_string_name_destroy(&manifest.base);
  248. godot_dictionary_destroy(&manifest.member_lines);
  249. godot_array_destroy(&manifest.methods);
  250. godot_array_destroy(&manifest.signals);
  251. godot_array_destroy(&manifest.properties);
  252. #ifdef TOOLS_ENABLED
  253. /*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
  254. _update_placeholder(E->get());
  255. }*/
  256. #endif
  257. return OK;
  258. }
  259. void PluginScript::get_script_method_list(List<MethodInfo> *r_methods) const {
  260. ASSERT_SCRIPT_VALID();
  261. for (Map<StringName, MethodInfo>::Element *e = _methods_info.front(); e != NULL; e = e->next()) {
  262. r_methods->push_back(e->get());
  263. }
  264. }
  265. void PluginScript::get_script_property_list(List<PropertyInfo> *r_properties) const {
  266. ASSERT_SCRIPT_VALID();
  267. for (Map<StringName, PropertyInfo>::Element *e = _properties_info.front(); e != NULL; e = e->next()) {
  268. r_properties->push_back(e->get());
  269. }
  270. }
  271. bool PluginScript::has_method(const StringName &p_method) const {
  272. ASSERT_SCRIPT_VALID_V(false);
  273. return _methods_info.has(p_method);
  274. }
  275. MethodInfo PluginScript::get_method_info(const StringName &p_method) const {
  276. ASSERT_SCRIPT_VALID_V(MethodInfo());
  277. const Map<StringName, MethodInfo>::Element *e = _methods_info.find(p_method);
  278. if (e != NULL) {
  279. return e->get();
  280. } else {
  281. return MethodInfo();
  282. }
  283. }
  284. bool PluginScript::has_property(const StringName &p_method) const {
  285. ASSERT_SCRIPT_VALID_V(false);
  286. return _properties_info.has(p_method);
  287. }
  288. PropertyInfo PluginScript::get_property_info(const StringName &p_property) const {
  289. ASSERT_SCRIPT_VALID_V(PropertyInfo());
  290. const Map<StringName, PropertyInfo>::Element *e = _properties_info.find(p_property);
  291. if (e != NULL) {
  292. return e->get();
  293. } else {
  294. return PropertyInfo();
  295. }
  296. }
  297. bool PluginScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  298. ASSERT_SCRIPT_VALID_V(false);
  299. #ifdef TOOLS_ENABLED
  300. const Map<StringName, Variant>::Element *e = _properties_default_values.find(p_property);
  301. if (e != NULL) {
  302. r_value = e->get();
  303. return true;
  304. } else {
  305. return false;
  306. }
  307. #endif
  308. return false;
  309. }
  310. ScriptLanguage *PluginScript::get_language() const {
  311. return _language;
  312. }
  313. Error PluginScript::load_source_code(const String &p_path) {
  314. PoolVector<uint8_t> sourcef;
  315. Error err;
  316. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  317. if (err) {
  318. ERR_FAIL_COND_V(err, err);
  319. }
  320. int len = f->get_len();
  321. sourcef.resize(len + 1);
  322. PoolVector<uint8_t>::Write w = sourcef.write();
  323. int r = f->get_buffer(w.ptr(), len);
  324. f->close();
  325. memdelete(f);
  326. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  327. w[len] = 0;
  328. String s;
  329. if (s.parse_utf8((const char *)w.ptr())) {
  330. 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.");
  331. ERR_FAIL_V(ERR_INVALID_DATA);
  332. }
  333. _source = s;
  334. #ifdef TOOLS_ENABLED
  335. // source_changed_cache=true;
  336. #endif
  337. _path = p_path;
  338. return OK;
  339. }
  340. bool PluginScript::has_script_signal(const StringName &p_signal) const {
  341. ASSERT_SCRIPT_VALID_V(false);
  342. return _signals_info.has(p_signal);
  343. }
  344. void PluginScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  345. ASSERT_SCRIPT_VALID();
  346. for (Map<StringName, MethodInfo>::Element *e = _signals_info.front(); e != NULL; e = e->next()) {
  347. r_signals->push_back(e->get());
  348. }
  349. }
  350. int PluginScript::get_member_line(const StringName &p_member) const {
  351. #ifdef TOOLS_ENABLED
  352. if (_member_lines.has(p_member))
  353. return _member_lines[p_member];
  354. else
  355. #endif
  356. return -1;
  357. }
  358. MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const {
  359. ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
  360. const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _methods_rpc_mode.find(p_method);
  361. if (e != NULL) {
  362. return e->get();
  363. } else {
  364. return MultiplayerAPI::RPC_MODE_DISABLED;
  365. }
  366. }
  367. MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const {
  368. ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
  369. const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _variables_rset_mode.find(p_variable);
  370. if (e != NULL) {
  371. return e->get();
  372. } else {
  373. return MultiplayerAPI::RPC_MODE_DISABLED;
  374. }
  375. }
  376. PluginScript::PluginScript() :
  377. _data(NULL),
  378. _tool(false),
  379. _valid(false),
  380. _script_list(this) {
  381. }
  382. void PluginScript::init(PluginScriptLanguage *language) {
  383. _desc = &language->_desc.script_desc;
  384. _language = language;
  385. #ifdef DEBUG_ENABLED
  386. _language->lock();
  387. _language->_script_list.add(&_script_list);
  388. _language->unlock();
  389. #endif
  390. }
  391. PluginScript::~PluginScript() {
  392. _desc->finish(_data);
  393. #ifdef DEBUG_ENABLED
  394. _language->lock();
  395. _language->_script_list.remove(&_script_list);
  396. _language->unlock();
  397. #endif
  398. }