pluginscript_language.cpp 14 KB

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