pluginscript_language.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*************************************************************************/
  2. /* pluginscript_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "core/os/os.h"
  33. #include "core/project_settings.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. void PluginScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  71. if (_desc.comment_delimiters) {
  72. const char **w = _desc.comment_delimiters;
  73. while (*w) {
  74. p_delimiters->push_back(*w);
  75. w++;
  76. }
  77. }
  78. }
  79. void PluginScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  80. if (_desc.string_delimiters) {
  81. const char **w = _desc.string_delimiters;
  82. while (*w) {
  83. p_delimiters->push_back(*w);
  84. w++;
  85. }
  86. }
  87. }
  88. Ref<Script> PluginScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  89. Script *ns = create_script();
  90. Ref<Script> script = Ref<Script>(ns);
  91. if (_desc.get_template_source_code) {
  92. godot_string src = _desc.get_template_source_code(_data, (godot_string *)&p_class_name, (godot_string *)&p_base_class_name);
  93. script->set_source_code(*(String *)&src);
  94. godot_string_destroy(&src);
  95. }
  96. return script;
  97. }
  98. 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 {
  99. PoolStringArray functions;
  100. if (_desc.validate) {
  101. bool ret = _desc.validate(
  102. _data,
  103. (godot_string *)&p_script,
  104. &r_line_error,
  105. &r_col_error,
  106. (godot_string *)&r_test_error,
  107. (godot_string *)&p_path,
  108. (godot_pool_string_array *)&functions);
  109. for (int i = 0; i < functions.size(); i++) {
  110. r_functions->push_back(functions[i]);
  111. }
  112. return ret;
  113. }
  114. return true;
  115. }
  116. Script *PluginScriptLanguage::create_script() const {
  117. PluginScript *script = memnew(PluginScript());
  118. // I'm hurting kittens doing this I guess...
  119. script->init(const_cast<PluginScriptLanguage *>(this));
  120. return script;
  121. }
  122. bool PluginScriptLanguage::has_named_classes() const {
  123. return _desc.has_named_classes;
  124. }
  125. bool PluginScriptLanguage::supports_builtin_mode() const {
  126. return _desc.supports_builtin_mode;
  127. }
  128. int PluginScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  129. if (_desc.find_function) {
  130. return _desc.find_function(_data, (godot_string *)&p_function, (godot_string *)&p_code);
  131. }
  132. return -1;
  133. }
  134. String PluginScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  135. if (_desc.make_function) {
  136. godot_string tmp = _desc.make_function(_data, (godot_string *)&p_class, (godot_string *)&p_name, (godot_pool_string_array *)&p_args);
  137. String ret = *(String *)&tmp;
  138. godot_string_destroy(&tmp);
  139. return ret;
  140. }
  141. return String();
  142. }
  143. Error PluginScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_force, String &r_call_hint) {
  144. if (_desc.complete_code) {
  145. Array options;
  146. godot_error tmp = _desc.complete_code(
  147. _data,
  148. (godot_string *)&p_code,
  149. (godot_string *)&p_base_path,
  150. (godot_object *)p_owner,
  151. (godot_array *)&options,
  152. &r_force,
  153. (godot_string *)&r_call_hint);
  154. for (int i = 0; i < options.size(); i++) {
  155. r_options->push_back(String(options[i]));
  156. }
  157. Error err = *(Error *)&tmp;
  158. return err;
  159. }
  160. return ERR_UNAVAILABLE;
  161. }
  162. void PluginScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  163. if (_desc.auto_indent_code) {
  164. _desc.auto_indent_code(_data, (godot_string *)&p_code, p_from_line, p_to_line);
  165. }
  166. return;
  167. }
  168. void PluginScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  169. const String variable = String(p_variable);
  170. _desc.add_global_constant(_data, (godot_string *)&variable, (godot_variant *)&p_value);
  171. }
  172. /* LOADER FUNCTIONS */
  173. void PluginScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  174. for (int i = 0; _desc.recognized_extensions[i]; ++i) {
  175. p_extensions->push_back(String(_desc.recognized_extensions[i]));
  176. }
  177. }
  178. void PluginScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  179. // TODO: provid this statically in `godot_pluginscript_language_desc` ?
  180. if (_desc.get_public_functions) {
  181. Array functions;
  182. _desc.get_public_functions(_data, (godot_array *)&functions);
  183. for (int i = 0; i < functions.size(); i++) {
  184. MethodInfo mi = MethodInfo::from_dict(functions[i]);
  185. p_functions->push_back(mi);
  186. }
  187. }
  188. }
  189. void PluginScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  190. // TODO: provid this statically in `godot_pluginscript_language_desc` ?
  191. if (_desc.get_public_constants) {
  192. Dictionary constants;
  193. _desc.get_public_constants(_data, (godot_dictionary *)&constants);
  194. for (const Variant *key = constants.next(); key; key = constants.next(key)) {
  195. Variant value = constants[key];
  196. p_constants->push_back(Pair<String, Variant>(*key, value));
  197. }
  198. }
  199. }
  200. void PluginScriptLanguage::profiling_start() {
  201. #ifdef DEBUG_ENABLED
  202. if (_desc.profiling_start) {
  203. lock();
  204. _desc.profiling_start(_data);
  205. unlock();
  206. }
  207. #endif
  208. }
  209. void PluginScriptLanguage::profiling_stop() {
  210. #ifdef DEBUG_ENABLED
  211. if (_desc.profiling_stop) {
  212. lock();
  213. _desc.profiling_stop(_data);
  214. unlock();
  215. }
  216. #endif
  217. }
  218. int PluginScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  219. int info_count = 0;
  220. #ifdef DEBUG_ENABLED
  221. if (_desc.profiling_get_accumulated_data) {
  222. godot_pluginscript_profiling_data *info = (godot_pluginscript_profiling_data *)memalloc(
  223. sizeof(godot_pluginscript_profiling_data) * p_info_max);
  224. info_count = _desc.profiling_get_accumulated_data(_data, info, p_info_max);
  225. for (int i = 0; i < info_count; ++i) {
  226. p_info_arr[i].signature = *(StringName *)&info[i].signature;
  227. p_info_arr[i].call_count = info[i].call_count;
  228. p_info_arr[i].total_time = info[i].total_time;
  229. p_info_arr[i].self_time = info[i].self_time;
  230. godot_string_name_destroy(&info[i].signature);
  231. }
  232. }
  233. #endif
  234. return info_count;
  235. }
  236. int PluginScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  237. int info_count = 0;
  238. #ifdef DEBUG_ENABLED
  239. if (_desc.profiling_get_frame_data) {
  240. godot_pluginscript_profiling_data *info = (godot_pluginscript_profiling_data *)memalloc(
  241. sizeof(godot_pluginscript_profiling_data) * p_info_max);
  242. info_count = _desc.profiling_get_frame_data(_data, info, p_info_max);
  243. for (int i = 0; i < info_count; ++i) {
  244. p_info_arr[i].signature = *(StringName *)&info[i].signature;
  245. p_info_arr[i].call_count = info[i].call_count;
  246. p_info_arr[i].total_time = info[i].total_time;
  247. p_info_arr[i].self_time = info[i].self_time;
  248. godot_string_name_destroy(&info[i].signature);
  249. }
  250. }
  251. #endif
  252. return info_count;
  253. }
  254. void PluginScriptLanguage::frame() {
  255. #ifdef DEBUG_ENABLED
  256. if (_desc.profiling_frame) {
  257. _desc.profiling_frame(_data);
  258. }
  259. #endif
  260. }
  261. /* DEBUGGER FUNCTIONS */
  262. String PluginScriptLanguage::debug_get_error() const {
  263. if (_desc.debug_get_error) {
  264. godot_string tmp = _desc.debug_get_error(_data);
  265. String ret = *(String *)&tmp;
  266. godot_string_destroy(&tmp);
  267. return ret;
  268. }
  269. return String("Nothing");
  270. }
  271. int PluginScriptLanguage::debug_get_stack_level_count() const {
  272. if (_desc.debug_get_stack_level_count) {
  273. return _desc.debug_get_stack_level_count(_data);
  274. }
  275. return 1;
  276. }
  277. int PluginScriptLanguage::debug_get_stack_level_line(int p_level) const {
  278. if (_desc.debug_get_stack_level_line) {
  279. return _desc.debug_get_stack_level_line(_data, p_level);
  280. }
  281. return 1;
  282. }
  283. String PluginScriptLanguage::debug_get_stack_level_function(int p_level) const {
  284. if (_desc.debug_get_stack_level_function) {
  285. godot_string tmp = _desc.debug_get_stack_level_function(_data, p_level);
  286. String ret = *(String *)&tmp;
  287. godot_string_destroy(&tmp);
  288. return ret;
  289. }
  290. return String("Nothing");
  291. }
  292. String PluginScriptLanguage::debug_get_stack_level_source(int p_level) const {
  293. if (_desc.debug_get_stack_level_source) {
  294. godot_string tmp = _desc.debug_get_stack_level_source(_data, p_level);
  295. String ret = *(String *)&tmp;
  296. godot_string_destroy(&tmp);
  297. return ret;
  298. }
  299. return String("Nothing");
  300. }
  301. 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) {
  302. if (_desc.debug_get_stack_level_locals) {
  303. PoolStringArray locals;
  304. Array values;
  305. _desc.debug_get_stack_level_locals(_data, p_level, (godot_pool_string_array *)&locals, (godot_array *)&values, p_max_subitems, p_max_depth);
  306. for (int i = 0; i < locals.size(); i++) {
  307. p_locals->push_back(locals[i]);
  308. }
  309. for (int i = 0; i < values.size(); i++) {
  310. p_values->push_back(values[i]);
  311. }
  312. }
  313. }
  314. 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) {
  315. if (_desc.debug_get_stack_level_members) {
  316. PoolStringArray members;
  317. Array values;
  318. _desc.debug_get_stack_level_members(_data, p_level, (godot_pool_string_array *)&members, (godot_array *)&values, p_max_subitems, p_max_depth);
  319. for (int i = 0; i < members.size(); i++) {
  320. p_members->push_back(members[i]);
  321. }
  322. for (int i = 0; i < values.size(); i++) {
  323. p_values->push_back(values[i]);
  324. }
  325. }
  326. }
  327. void PluginScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  328. if (_desc.debug_get_globals) {
  329. PoolStringArray locals;
  330. Array values;
  331. _desc.debug_get_globals(_data, (godot_pool_string_array *)&locals, (godot_array *)&values, p_max_subitems, p_max_depth);
  332. for (int i = 0; i < locals.size(); i++) {
  333. p_locals->push_back(locals[i]);
  334. }
  335. for (int i = 0; i < values.size(); i++) {
  336. p_values->push_back(values[i]);
  337. }
  338. }
  339. }
  340. String PluginScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  341. if (_desc.debug_parse_stack_level_expression) {
  342. godot_string tmp = _desc.debug_parse_stack_level_expression(_data, p_level, (godot_string *)&p_expression, p_max_subitems, p_max_depth);
  343. String ret = *(String *)&tmp;
  344. godot_string_destroy(&tmp);
  345. return ret;
  346. }
  347. return String("Nothing");
  348. }
  349. void PluginScriptLanguage::reload_all_scripts() {
  350. // TODO
  351. }
  352. void PluginScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  353. #ifdef DEBUG_ENABLED
  354. lock();
  355. // TODO
  356. unlock();
  357. #endif
  358. }
  359. void PluginScriptLanguage::lock() {
  360. #ifndef NO_THREADS
  361. if (_lock) {
  362. _lock->lock();
  363. }
  364. #endif
  365. }
  366. void PluginScriptLanguage::unlock() {
  367. #ifndef NO_THREADS
  368. if (_lock) {
  369. _lock->unlock();
  370. }
  371. #endif
  372. }
  373. PluginScriptLanguage::PluginScriptLanguage(const godot_pluginscript_language_desc *desc) :
  374. _desc(*desc) {
  375. _resource_loader = Ref<ResourceFormatLoaderPluginScript>(memnew(ResourceFormatLoaderPluginScript(this)));
  376. _resource_saver = Ref<ResourceFormatSaverPluginScript>(memnew(ResourceFormatSaverPluginScript(this)));
  377. // TODO: totally remove _lock attribute if NO_THREADS is set
  378. #ifdef NO_THREADS
  379. _lock = NULL;
  380. #else
  381. _lock = Mutex::create();
  382. #endif
  383. }
  384. PluginScriptLanguage::~PluginScriptLanguage() {
  385. #ifndef NO_THREADS
  386. if (_lock) {
  387. memdelete(_lock);
  388. _lock = NULL;
  389. }
  390. #endif
  391. }