csharp_project.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*************************************************************************/
  2. /* csharp_project.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. #include "csharp_project.h"
  31. #include "core/io/json.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "../csharp_script.h"
  35. #include "../mono_gd/gd_mono_class.h"
  36. #include "../mono_gd/gd_mono_marshal.h"
  37. #include "../utils/string_utils.h"
  38. #include "script_class_parser.h"
  39. namespace CSharpProject {
  40. String generate_core_api_project(const String &p_dir, const Vector<String> &p_files) {
  41. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  42. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator");
  43. Variant dir = p_dir;
  44. Variant compile_items = p_files;
  45. const Variant *args[2] = { &dir, &compile_items };
  46. MonoException *exc = NULL;
  47. MonoObject *ret = klass->get_method("GenCoreApiProject", 2)->invoke(NULL, args, &exc);
  48. if (exc) {
  49. GDMonoUtils::debug_print_unhandled_exception(exc);
  50. ERR_FAIL_V(String());
  51. }
  52. return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : String();
  53. }
  54. String generate_editor_api_project(const String &p_dir, const String &p_core_dll_path, const Vector<String> &p_files) {
  55. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  56. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator");
  57. Variant dir = p_dir;
  58. Variant core_dll_path = p_core_dll_path;
  59. Variant compile_items = p_files;
  60. const Variant *args[3] = { &dir, &core_dll_path, &compile_items };
  61. MonoException *exc = NULL;
  62. MonoObject *ret = klass->get_method("GenEditorApiProject", 3)->invoke(NULL, args, &exc);
  63. if (exc) {
  64. GDMonoUtils::debug_print_unhandled_exception(exc);
  65. ERR_FAIL_V(String());
  66. }
  67. return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : String();
  68. }
  69. String generate_game_project(const String &p_dir, const String &p_name, const Vector<String> &p_files) {
  70. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  71. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator");
  72. Variant dir = p_dir;
  73. Variant name = p_name;
  74. Variant compile_items = p_files;
  75. const Variant *args[3] = { &dir, &name, &compile_items };
  76. MonoException *exc = NULL;
  77. MonoObject *ret = klass->get_method("GenGameProject", 3)->invoke(NULL, args, &exc);
  78. if (exc) {
  79. GDMonoUtils::debug_print_unhandled_exception(exc);
  80. ERR_FAIL_V(String());
  81. }
  82. return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : String();
  83. }
  84. void add_item(const String &p_project_path, const String &p_item_type, const String &p_include) {
  85. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  86. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectUtils");
  87. Variant project_path = p_project_path;
  88. Variant item_type = p_item_type;
  89. Variant include = p_include;
  90. const Variant *args[3] = { &project_path, &item_type, &include };
  91. MonoException *exc = NULL;
  92. klass->get_method("AddItemToProjectChecked", 3)->invoke(NULL, args, &exc);
  93. if (exc) {
  94. GDMonoUtils::debug_print_unhandled_exception(exc);
  95. ERR_FAIL();
  96. }
  97. }
  98. Error generate_scripts_metadata(const String &p_project_path, const String &p_output_path) {
  99. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  100. GDMonoClass *project_utils = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectUtils");
  101. void *args[2] = {
  102. GDMonoMarshal::mono_string_from_godot(p_project_path),
  103. GDMonoMarshal::mono_string_from_godot("Compile")
  104. };
  105. MonoException *exc = NULL;
  106. MonoArray *ret = (MonoArray *)project_utils->get_method("GetIncludeFiles", 2)->invoke_raw(NULL, args, &exc);
  107. if (exc) {
  108. GDMonoUtils::debug_print_unhandled_exception(exc);
  109. ERR_FAIL_V(FAILED);
  110. }
  111. PoolStringArray project_files = GDMonoMarshal::mono_array_to_PoolStringArray(ret);
  112. PoolStringArray::Read r = project_files.read();
  113. Dictionary old_dict = CSharpLanguage::get_singleton()->get_scripts_metadata();
  114. Dictionary new_dict;
  115. for (int i = 0; i < project_files.size(); i++) {
  116. const String &project_file = ("res://" + r[i]).simplify_path();
  117. uint64_t modified_time = FileAccess::get_modified_time(project_file);
  118. const Variant *old_file_var = old_dict.getptr(project_file);
  119. if (old_file_var) {
  120. Dictionary old_file_dict = old_file_var->operator Dictionary();
  121. if (old_file_dict["modified_time"].operator uint64_t() == modified_time) {
  122. // No changes so no need to parse again
  123. new_dict[project_file] = old_file_dict;
  124. continue;
  125. }
  126. }
  127. ScriptClassParser scp;
  128. Error err = scp.parse_file(project_file);
  129. if (err != OK) {
  130. ERR_PRINTS("Parse error: " + scp.get_error());
  131. ERR_EXPLAIN("Failed to determine namespace and class for script: " + project_file);
  132. ERR_FAIL_V(err);
  133. }
  134. Vector<ScriptClassParser::ClassDecl> classes = scp.get_classes();
  135. bool found = false;
  136. Dictionary class_dict;
  137. String search_name = project_file.get_file().get_basename();
  138. for (int j = 0; j < classes.size(); j++) {
  139. const ScriptClassParser::ClassDecl &class_decl = classes[j];
  140. if (class_decl.base.size() == 0)
  141. continue; // Does not inherit nor implement anything, so it can't be a script class
  142. String class_cmp;
  143. if (class_decl.nested) {
  144. class_cmp = class_decl.name.get_slice(".", class_decl.name.get_slice_count(".") - 1);
  145. } else {
  146. class_cmp = class_decl.name;
  147. }
  148. if (class_cmp != search_name)
  149. continue;
  150. class_dict["namespace"] = class_decl.namespace_;
  151. class_dict["class_name"] = class_decl.name;
  152. class_dict["nested"] = class_decl.nested;
  153. found = true;
  154. break;
  155. }
  156. if (found) {
  157. Dictionary file_dict;
  158. file_dict["modified_time"] = modified_time;
  159. file_dict["class"] = class_dict;
  160. new_dict[project_file] = file_dict;
  161. }
  162. }
  163. if (new_dict.size()) {
  164. String json = JSON::print(new_dict, "", false);
  165. Error ferr;
  166. FileAccess *f = FileAccess::open(p_output_path, FileAccess::WRITE, &ferr);
  167. ERR_EXPLAIN("Cannot open file for writing: " + p_output_path);
  168. ERR_FAIL_COND_V(ferr != OK, ferr);
  169. f->store_string(json);
  170. f->flush();
  171. f->close();
  172. memdelete(f);
  173. }
  174. return OK;
  175. }
  176. } // namespace CSharpProject