pluginscript_script.cpp 13 KB

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