pluginscript_language.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*************************************************************************/
  2. /* pluginscript_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/io/file_access.h"
  33. #include "core/os/os.h"
  34. // PluginScript imports
  35. #include "pluginscript_language.h"
  36. #include "pluginscript_script.h"
  37. String PluginScriptLanguage::get_name() const {
  38. return String(_desc.name);
  39. }
  40. void PluginScriptLanguage::init() {
  41. _data = _desc.init();
  42. }
  43. String PluginScriptLanguage::get_type() const {
  44. // We should use _desc.type here, however the returned type is used to
  45. // query ClassDB which would complain given the type is not registered
  46. // from his point of view...
  47. // To solve this we just use a more generic (but present in ClassDB) type.
  48. return String("PluginScript");
  49. }
  50. String PluginScriptLanguage::get_extension() const {
  51. return String(_desc.extension);
  52. }
  53. Error PluginScriptLanguage::execute_file(const String &p_path) {
  54. // TODO: pretty sure this method is totally deprecated and should be removed...
  55. return OK;
  56. }
  57. void PluginScriptLanguage::finish() {
  58. _desc.finish(_data);
  59. }
  60. /* EDITOR FUNCTIONS */
  61. void PluginScriptLanguage::get_reserved_words(List<String> *p_words) const {
  62. if (_desc.reserved_words) {
  63. const char **w = _desc.reserved_words;
  64. while (*w) {
  65. p_words->push_back(*w);
  66. w++;
  67. }
  68. }
  69. }
  70. bool PluginScriptLanguage::is_control_flow_keyword(String p_keyword) const {
  71. return false;
  72. }
  73. void PluginScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  74. if (_desc.comment_delimiters) {
  75. const char **w = _desc.comment_delimiters;
  76. while (*w) {
  77. p_delimiters->push_back(*w);
  78. w++;
  79. }
  80. }
  81. }
  82. void PluginScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  83. if (_desc.string_delimiters) {
  84. const char **w = _desc.string_delimiters;
  85. while (*w) {
  86. p_delimiters->push_back(*w);
  87. w++;
  88. }
  89. }
  90. }
  91. Ref<Script> PluginScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  92. Script *ns = create_script();
  93. Ref<Script> script = Ref<Script>(ns);
  94. if (_desc.get_template_source_code) {
  95. godot_string src = _desc.get_template_source_code(_data, (godot_string *)&p_class_name, (godot_string *)&p_base_class_name);
  96. script->set_source_code(*(String *)&src);
  97. godot_string_destroy(&src);
  98. }
  99. return script;
  100. }
  101. bool PluginScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  102. PackedStringArray functions;
  103. if (_desc.validate) {
  104. bool ret = _desc.validate(
  105. _data,
  106. (godot_string *)&p_script,
  107. &r_line_error,
  108. &r_col_error,
  109. (godot_string *)&r_test_error,
  110. (godot_string *)&p_path,
  111. (godot_packed_string_array *)&functions);
  112. for (int i = 0; i < functions.size(); i++) {
  113. r_functions->push_back(functions[i]);
  114. }
  115. return ret;
  116. }
  117. return true;
  118. }
  119. Script *PluginScriptLanguage::create_script() const {
  120. PluginScript *script = memnew(PluginScript());
  121. // I'm hurting kittens doing this I guess...
  122. script->init(const_cast<PluginScriptLanguage *>(this));
  123. return script;
  124. }
  125. bool PluginScriptLanguage::has_named_classes() const {
  126. return _desc.has_named_classes;
  127. }
  128. bool PluginScriptLanguage::supports_builtin_mode() const {
  129. return _desc.supports_builtin_mode;
  130. }
  131. bool PluginScriptLanguage::can_inherit_from_file() const {
  132. return _desc.can_inherit_from_file;
  133. }
  134. int PluginScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  135. if (_desc.find_function) {
  136. return _desc.find_function(_data, (godot_string *)&p_function, (godot_string *)&p_code);
  137. }
  138. return -1;
  139. }
  140. String PluginScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  141. if (_desc.make_function) {
  142. godot_string tmp = _desc.make_function(_data, (godot_string *)&p_class, (godot_string *)&p_name, (godot_packed_string_array *)&p_args);
  143. String ret = *(String *)&tmp;
  144. godot_string_destroy(&tmp);
  145. return ret;
  146. }
  147. return String();
  148. }
  149. Error PluginScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_force, String &r_call_hint) {
  150. if (_desc.complete_code) {
  151. Array options;
  152. godot_error tmp = _desc.complete_code(
  153. _data,
  154. (godot_string *)&p_code,
  155. (godot_string *)&p_path,
  156. (godot_object *)p_owner,
  157. (godot_array *)&options,
  158. &r_force,
  159. (godot_string *)&r_call_hint);
  160. for (int i = 0; i < options.size(); i++) {
  161. ScriptCodeCompletionOption option(options[i], ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  162. r_options->push_back(option);
  163. }
  164. return (Error)tmp;
  165. }
  166. return ERR_UNAVAILABLE;
  167. }
  168. void PluginScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  169. if (_desc.auto_indent_code) {
  170. _desc.auto_indent_code(_data, (godot_string *)&p_code, p_from_line, p_to_line);
  171. }
  172. return;
  173. }
  174. void PluginScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  175. _desc.add_global_constant(_data, (godot_string_name *)&p_variable, (godot_variant *)&p_value);
  176. }
  177. /* LOADER FUNCTIONS */
  178. void PluginScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  179. for (int i = 0; _desc.recognized_extensions[i]; ++i) {
  180. p_extensions->push_back(String(_desc.recognized_extensions[i]));
  181. }
  182. }
  183. void PluginScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  184. // TODO: provide this statically in `godot_pluginscript_language_desc` ?
  185. if (_desc.get_public_functions) {
  186. Array functions;
  187. _desc.get_public_functions(_data, (godot_array *)&functions);
  188. for (int i = 0; i < functions.size(); i++) {
  189. MethodInfo mi = MethodInfo::from_dict(functions[i]);
  190. p_functions->push_back(mi);
  191. }
  192. }
  193. }
  194. void PluginScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  195. // TODO: provide this statically in `godot_pluginscript_language_desc` ?
  196. if (_desc.get_public_constants) {
  197. Dictionary constants;
  198. _desc.get_public_constants(_data, (godot_dictionary *)&constants);
  199. for (const Variant *key = constants.next(); key; key = constants.next(key)) {
  200. Variant value = constants[*key];
  201. p_constants->push_back(Pair<String, Variant>(*key, value));
  202. }
  203. }
  204. }
  205. void PluginScriptLanguage::profiling_start() {
  206. #ifdef DEBUG_ENABLED
  207. if (_desc.profiling_start) {
  208. lock();
  209. _desc.profiling_start(_data);
  210. unlock();
  211. }
  212. #endif
  213. }
  214. void PluginScriptLanguage::profiling_stop() {
  215. #ifdef DEBUG_ENABLED
  216. if (_desc.profiling_stop) {
  217. lock();
  218. _desc.profiling_stop(_data);
  219. unlock();
  220. }
  221. #endif
  222. }
  223. int PluginScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  224. int info_count = 0;
  225. #ifdef DEBUG_ENABLED
  226. if (_desc.profiling_get_accumulated_data) {
  227. godot_pluginscript_profiling_data *info = (godot_pluginscript_profiling_data *)memalloc(
  228. sizeof(godot_pluginscript_profiling_data) * p_info_max);
  229. info_count = _desc.profiling_get_accumulated_data(_data, info, p_info_max);
  230. for (int i = 0; i < info_count; ++i) {
  231. p_info_arr[i].signature = *(StringName *)&info[i].signature;
  232. p_info_arr[i].call_count = info[i].call_count;
  233. p_info_arr[i].total_time = info[i].total_time;
  234. p_info_arr[i].self_time = info[i].self_time;
  235. godot_string_name_destroy(&info[i].signature);
  236. }
  237. }
  238. #endif
  239. return info_count;
  240. }
  241. int PluginScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  242. int info_count = 0;
  243. #ifdef DEBUG_ENABLED
  244. if (_desc.profiling_get_frame_data) {
  245. godot_pluginscript_profiling_data *info = (godot_pluginscript_profiling_data *)memalloc(
  246. sizeof(godot_pluginscript_profiling_data) * p_info_max);
  247. info_count = _desc.profiling_get_frame_data(_data, info, p_info_max);
  248. for (int i = 0; i < info_count; ++i) {
  249. p_info_arr[i].signature = *(StringName *)&info[i].signature;
  250. p_info_arr[i].call_count = info[i].call_count;
  251. p_info_arr[i].total_time = info[i].total_time;
  252. p_info_arr[i].self_time = info[i].self_time;
  253. godot_string_name_destroy(&info[i].signature);
  254. }
  255. }
  256. #endif
  257. return info_count;
  258. }
  259. void PluginScriptLanguage::frame() {
  260. #ifdef DEBUG_ENABLED
  261. if (_desc.profiling_frame) {
  262. _desc.profiling_frame(_data);
  263. }
  264. #endif
  265. }
  266. /* DEBUGGER FUNCTIONS */
  267. String PluginScriptLanguage::debug_get_error() const {
  268. if (_desc.debug_get_error) {
  269. godot_string tmp = _desc.debug_get_error(_data);
  270. String ret = *(String *)&tmp;
  271. godot_string_destroy(&tmp);
  272. return ret;
  273. }
  274. return String("Nothing");
  275. }
  276. int PluginScriptLanguage::debug_get_stack_level_count() const {
  277. if (_desc.debug_get_stack_level_count) {
  278. return _desc.debug_get_stack_level_count(_data);
  279. }
  280. return 1;
  281. }
  282. int PluginScriptLanguage::debug_get_stack_level_line(int p_level) const {
  283. if (_desc.debug_get_stack_level_line) {
  284. return _desc.debug_get_stack_level_line(_data, p_level);
  285. }
  286. return 1;
  287. }
  288. String PluginScriptLanguage::debug_get_stack_level_function(int p_level) const {
  289. if (_desc.debug_get_stack_level_function) {
  290. godot_string tmp = _desc.debug_get_stack_level_function(_data, p_level);
  291. String ret = *(String *)&tmp;
  292. godot_string_destroy(&tmp);
  293. return ret;
  294. }
  295. return String("Nothing");
  296. }
  297. String PluginScriptLanguage::debug_get_stack_level_source(int p_level) const {
  298. if (_desc.debug_get_stack_level_source) {
  299. godot_string tmp = _desc.debug_get_stack_level_source(_data, p_level);
  300. String ret = *(String *)&tmp;
  301. godot_string_destroy(&tmp);
  302. return ret;
  303. }
  304. return String("Nothing");
  305. }
  306. void PluginScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  307. if (_desc.debug_get_stack_level_locals) {
  308. PackedStringArray locals;
  309. Array values;
  310. _desc.debug_get_stack_level_locals(_data, p_level, (godot_packed_string_array *)&locals, (godot_array *)&values, p_max_subitems, p_max_depth);
  311. for (int i = 0; i < locals.size(); i++) {
  312. p_locals->push_back(locals[i]);
  313. }
  314. for (int i = 0; i < values.size(); i++) {
  315. p_values->push_back(values[i]);
  316. }
  317. }
  318. }
  319. void PluginScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  320. if (_desc.debug_get_stack_level_members) {
  321. PackedStringArray members;
  322. Array values;
  323. _desc.debug_get_stack_level_members(_data, p_level, (godot_packed_string_array *)&members, (godot_array *)&values, p_max_subitems, p_max_depth);
  324. for (int i = 0; i < members.size(); i++) {
  325. p_members->push_back(members[i]);
  326. }
  327. for (int i = 0; i < values.size(); i++) {
  328. p_values->push_back(values[i]);
  329. }
  330. }
  331. }
  332. void PluginScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  333. if (_desc.debug_get_globals) {
  334. PackedStringArray locals;
  335. Array values;
  336. _desc.debug_get_globals(_data, (godot_packed_string_array *)&locals, (godot_array *)&values, p_max_subitems, p_max_depth);
  337. for (int i = 0; i < locals.size(); i++) {
  338. p_locals->push_back(locals[i]);
  339. }
  340. for (int i = 0; i < values.size(); i++) {
  341. p_values->push_back(values[i]);
  342. }
  343. }
  344. }
  345. String PluginScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  346. if (_desc.debug_parse_stack_level_expression) {
  347. godot_string tmp = _desc.debug_parse_stack_level_expression(_data, p_level, (godot_string *)&p_expression, p_max_subitems, p_max_depth);
  348. String ret = *(String *)&tmp;
  349. godot_string_destroy(&tmp);
  350. return ret;
  351. }
  352. return String("Nothing");
  353. }
  354. void PluginScriptLanguage::reload_all_scripts() {
  355. // TODO
  356. }
  357. void PluginScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  358. #ifdef DEBUG_ENABLED
  359. lock();
  360. // TODO
  361. unlock();
  362. #endif
  363. }
  364. bool PluginScriptLanguage::handles_global_class_type(const String &p_type) const {
  365. return p_type == "PluginScript";
  366. }
  367. String PluginScriptLanguage::get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path) const {
  368. if (!p_path.is_empty()) {
  369. Ref<PluginScript> script = ResourceLoader::load(p_path, "PluginScript");
  370. if (script.is_valid()) {
  371. if (r_base_type) {
  372. *r_base_type = script->get_instance_base_type();
  373. }
  374. if (r_icon_path) {
  375. *r_icon_path = script->get_script_class_icon_path();
  376. }
  377. return script->get_script_class_name();
  378. }
  379. if (r_base_type) {
  380. *r_base_type = String();
  381. }
  382. if (r_icon_path) {
  383. *r_icon_path = String();
  384. }
  385. }
  386. return String();
  387. }
  388. void PluginScriptLanguage::lock() {
  389. _lock.lock();
  390. }
  391. void PluginScriptLanguage::unlock() {
  392. _lock.unlock();
  393. }
  394. PluginScriptLanguage::PluginScriptLanguage(const godot_pluginscript_language_desc *desc) :
  395. _desc(*desc) {
  396. _resource_loader = Ref<ResourceFormatLoaderPluginScript>(memnew(ResourceFormatLoaderPluginScript(this)));
  397. _resource_saver = Ref<ResourceFormatSaverPluginScript>(memnew(ResourceFormatSaverPluginScript(this)));
  398. }
  399. PluginScriptLanguage::~PluginScriptLanguage() {
  400. }