pluginscript_language.cpp 14 KB

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