csharp_project.cpp 8.5 KB

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