csharp_project.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/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. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  88. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectUtils");
  89. Variant project_path = p_project_path;
  90. Variant item_type = p_item_type;
  91. Variant include = p_include;
  92. const Variant *args[3] = { &project_path, &item_type, &include };
  93. MonoException *exc = NULL;
  94. klass->get_method("AddItemToProjectChecked", 3)->invoke(NULL, args, &exc);
  95. if (exc) {
  96. GDMonoUtils::debug_print_unhandled_exception(exc);
  97. ERR_FAIL();
  98. }
  99. }
  100. Error generate_scripts_metadata(const String &p_project_path, const String &p_output_path) {
  101. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  102. if (FileAccess::exists(p_output_path)) {
  103. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  104. Error rm_err = da->remove(p_output_path);
  105. ERR_EXPLAIN("Failed to remove old scripts metadata file");
  106. ERR_FAIL_COND_V(rm_err != OK, rm_err);
  107. }
  108. GDMonoClass *project_utils = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectUtils");
  109. void *args[2] = {
  110. GDMonoMarshal::mono_string_from_godot(p_project_path),
  111. GDMonoMarshal::mono_string_from_godot("Compile")
  112. };
  113. MonoException *exc = NULL;
  114. MonoArray *ret = (MonoArray *)project_utils->get_method("GetIncludeFiles", 2)->invoke_raw(NULL, args, &exc);
  115. if (exc) {
  116. GDMonoUtils::debug_print_unhandled_exception(exc);
  117. ERR_FAIL_V(FAILED);
  118. }
  119. PoolStringArray project_files = GDMonoMarshal::mono_array_to_PoolStringArray(ret);
  120. PoolStringArray::Read r = project_files.read();
  121. Dictionary old_dict = CSharpLanguage::get_singleton()->get_scripts_metadata();
  122. Dictionary new_dict;
  123. for (int i = 0; i < project_files.size(); i++) {
  124. const String &project_file = ("res://" + r[i]).simplify_path();
  125. uint64_t modified_time = FileAccess::get_modified_time(project_file);
  126. const Variant *old_file_var = old_dict.getptr(project_file);
  127. if (old_file_var) {
  128. Dictionary old_file_dict = old_file_var->operator Dictionary();
  129. if (old_file_dict["modified_time"].operator uint64_t() == modified_time) {
  130. // No changes so no need to parse again
  131. new_dict[project_file] = old_file_dict;
  132. continue;
  133. }
  134. }
  135. ScriptClassParser scp;
  136. Error err = scp.parse_file(project_file);
  137. if (err != OK) {
  138. ERR_PRINTS("Parse error: " + scp.get_error());
  139. ERR_EXPLAIN("Failed to determine namespace and class for script: " + project_file);
  140. ERR_FAIL_V(err);
  141. }
  142. Vector<ScriptClassParser::ClassDecl> classes = scp.get_classes();
  143. bool found = false;
  144. Dictionary class_dict;
  145. String search_name = project_file.get_file().get_basename();
  146. for (int j = 0; j < classes.size(); j++) {
  147. const ScriptClassParser::ClassDecl &class_decl = classes[j];
  148. if (class_decl.base.size() == 0)
  149. continue; // Does not inherit nor implement anything, so it can't be a script class
  150. String class_cmp;
  151. if (class_decl.nested) {
  152. class_cmp = class_decl.name.get_slice(".", class_decl.name.get_slice_count(".") - 1);
  153. } else {
  154. class_cmp = class_decl.name;
  155. }
  156. if (class_cmp != search_name)
  157. continue;
  158. class_dict["namespace"] = class_decl.namespace_;
  159. class_dict["class_name"] = class_decl.name;
  160. class_dict["nested"] = class_decl.nested;
  161. found = true;
  162. break;
  163. }
  164. if (found) {
  165. Dictionary file_dict;
  166. file_dict["modified_time"] = modified_time;
  167. file_dict["class"] = class_dict;
  168. new_dict[project_file] = file_dict;
  169. }
  170. }
  171. if (new_dict.size()) {
  172. String json = JSON::print(new_dict, "", false);
  173. Error ferr;
  174. FileAccess *f = FileAccess::open(p_output_path, FileAccess::WRITE, &ferr);
  175. ERR_EXPLAIN("Cannot open file for writing: " + p_output_path);
  176. ERR_FAIL_COND_V(ferr != OK, ferr);
  177. f->store_string(json);
  178. f->flush();
  179. f->close();
  180. memdelete(f);
  181. }
  182. return OK;
  183. }
  184. } // namespace CSharpProject