godotsharp_export.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*************************************************************************/
  2. /* godotsharp_export.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 "godotsharp_export.h"
  31. #include "core/version.h"
  32. #include "../csharp_script.h"
  33. #include "../godotsharp_defs.h"
  34. #include "../godotsharp_dirs.h"
  35. #include "../mono_gd/gd_mono_class.h"
  36. #include "../mono_gd/gd_mono_marshal.h"
  37. #include "csharp_project.h"
  38. #include "godotsharp_builds.h"
  39. static MonoString *godot_icall_GodotSharpExport_GetTemplatesDir() {
  40. String current_version = VERSION_FULL_CONFIG;
  41. String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(current_version);
  42. return GDMonoMarshal::mono_string_from_godot(ProjectSettings::get_singleton()->globalize_path(templates_dir));
  43. }
  44. static MonoString *godot_icall_GodotSharpExport_GetDataDirName() {
  45. String appname = ProjectSettings::get_singleton()->get("application/config/name");
  46. String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
  47. return GDMonoMarshal::mono_string_from_godot("data_" + appname_safe);
  48. }
  49. void GodotSharpExport::register_internal_calls() {
  50. static bool registered = false;
  51. ERR_FAIL_COND(registered);
  52. registered = true;
  53. mono_add_internal_call("GodotSharpTools.Editor.GodotSharpExport::GetTemplatesDir", (void *)godot_icall_GodotSharpExport_GetTemplatesDir);
  54. mono_add_internal_call("GodotSharpTools.Editor.GodotSharpExport::GetDataDirName", (void *)godot_icall_GodotSharpExport_GetDataDirName);
  55. }
  56. void GodotSharpExport::_export_file(const String &p_path, const String &p_type, const Set<String> &) {
  57. if (p_type != CSharpLanguage::get_singleton()->get_type())
  58. return;
  59. ERR_FAIL_COND(p_path.get_extension() != CSharpLanguage::get_singleton()->get_extension());
  60. // TODO what if the source file is not part of the game's C# project
  61. if (!GLOBAL_GET("mono/export/include_scripts_content")) {
  62. // We don't want to include the source code on exported games
  63. add_file(p_path, Vector<uint8_t>(), false);
  64. skip();
  65. }
  66. }
  67. void GodotSharpExport::_export_begin(const Set<String> &p_features, bool p_debug, const String &p_path, int p_flags) {
  68. // TODO right now there is no way to stop the export process with an error
  69. ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
  70. ERR_FAIL_NULL(TOOLS_DOMAIN);
  71. ERR_FAIL_NULL(GDMono::get_singleton()->get_editor_tools_assembly());
  72. String build_config = p_debug ? "Debug" : "Release";
  73. String scripts_metadata_path = GodotSharpDirs::get_res_metadata_dir().plus_file("scripts_metadata." + String(p_debug ? "debug" : "release"));
  74. Error metadata_err = CSharpProject::generate_scripts_metadata(GodotSharpDirs::get_project_csproj_path(), scripts_metadata_path);
  75. ERR_FAIL_COND(metadata_err != OK);
  76. ERR_FAIL_COND(!_add_file(scripts_metadata_path, scripts_metadata_path));
  77. ERR_FAIL_COND(!GodotSharpBuilds::build_project_blocking(build_config));
  78. // Add dependency assemblies
  79. Map<String, String> dependencies;
  80. String project_dll_name = ProjectSettings::get_singleton()->get("application/config/name");
  81. if (project_dll_name.empty()) {
  82. project_dll_name = "UnnamedProject";
  83. }
  84. String project_dll_src_dir = GodotSharpDirs::get_res_temp_assemblies_base_dir().plus_file(build_config);
  85. String project_dll_src_path = project_dll_src_dir.plus_file(project_dll_name + ".dll");
  86. dependencies.insert(project_dll_name, project_dll_src_path);
  87. {
  88. MonoDomain *export_domain = GDMonoUtils::create_domain("GodotEngine.ProjectExportDomain");
  89. ERR_FAIL_NULL(export_domain);
  90. _GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(export_domain);
  91. _GDMONO_SCOPE_DOMAIN_(export_domain);
  92. GDMonoAssembly *scripts_assembly = NULL;
  93. bool load_success = GDMono::get_singleton()->load_assembly_from(project_dll_name,
  94. project_dll_src_path, &scripts_assembly, /* refonly: */ true);
  95. ERR_EXPLAIN("Cannot load refonly assembly: " + project_dll_name);
  96. ERR_FAIL_COND(!load_success);
  97. Vector<String> search_dirs;
  98. GDMonoAssembly::fill_search_dirs(search_dirs);
  99. Error depend_error = _get_assembly_dependencies(scripts_assembly, search_dirs, dependencies);
  100. ERR_FAIL_COND(depend_error != OK);
  101. }
  102. for (Map<String, String>::Element *E = dependencies.front(); E; E = E->next()) {
  103. String depend_src_path = E->value();
  104. String depend_dst_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(depend_src_path.get_file());
  105. ERR_FAIL_COND(!_add_file(depend_src_path, depend_dst_path));
  106. }
  107. // Mono specific export template extras (data dir)
  108. GDMonoClass *export_class = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Editor", "GodotSharpExport");
  109. ERR_FAIL_NULL(export_class);
  110. GDMonoMethod *export_begin_method = export_class->get_method("_ExportBegin", 4);
  111. ERR_FAIL_NULL(export_begin_method);
  112. MonoArray *features = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), p_features.size());
  113. int i = 0;
  114. for (const Set<String>::Element *E = p_features.front(); E; E = E->next()) {
  115. MonoString *boxed = GDMonoMarshal::mono_string_from_godot(E->get());
  116. mono_array_set(features, MonoString *, i, boxed);
  117. i++;
  118. }
  119. MonoBoolean debug = p_debug;
  120. MonoString *path = GDMonoMarshal::mono_string_from_godot(p_path);
  121. uint32_t flags = p_flags;
  122. void *args[4] = { features, &debug, path, &flags };
  123. MonoException *exc = NULL;
  124. export_begin_method->invoke_raw(NULL, args, &exc);
  125. if (exc) {
  126. GDMonoUtils::debug_print_unhandled_exception(exc);
  127. ERR_FAIL();
  128. }
  129. }
  130. bool GodotSharpExport::_add_file(const String &p_src_path, const String &p_dst_path, bool p_remap) {
  131. FileAccessRef f = FileAccess::open(p_src_path, FileAccess::READ);
  132. ERR_FAIL_COND_V(!f, false);
  133. Vector<uint8_t> data;
  134. data.resize(f->get_len());
  135. f->get_buffer(data.ptrw(), data.size());
  136. add_file(p_dst_path, data, p_remap);
  137. return true;
  138. }
  139. Error GodotSharpExport::_get_assembly_dependencies(GDMonoAssembly *p_assembly, const Vector<String> &p_search_dirs, Map<String, String> &r_dependencies) {
  140. MonoImage *image = p_assembly->get_image();
  141. for (int i = 0; i < mono_image_get_table_rows(image, MONO_TABLE_ASSEMBLYREF); i++) {
  142. MonoAssemblyName *ref_aname = aname_prealloc;
  143. mono_assembly_get_assemblyref(image, i, ref_aname);
  144. String ref_name = mono_assembly_name_get_name(ref_aname);
  145. if (r_dependencies.find(ref_name))
  146. continue;
  147. GDMonoAssembly *ref_assembly = NULL;
  148. String path;
  149. bool has_extension = ref_name.ends_with(".dll") || ref_name.ends_with(".exe");
  150. for (int i = 0; i < p_search_dirs.size(); i++) {
  151. const String &search_dir = p_search_dirs[i];
  152. if (has_extension) {
  153. path = search_dir.plus_file(ref_name);
  154. if (FileAccess::exists(path)) {
  155. GDMono::get_singleton()->load_assembly_from(ref_name.get_basename(), path, &ref_assembly, true);
  156. if (ref_assembly != NULL)
  157. break;
  158. }
  159. } else {
  160. path = search_dir.plus_file(ref_name + ".dll");
  161. if (FileAccess::exists(path)) {
  162. GDMono::get_singleton()->load_assembly_from(ref_name, path, &ref_assembly, true);
  163. if (ref_assembly != NULL)
  164. break;
  165. }
  166. path = search_dir.plus_file(ref_name + ".exe");
  167. if (FileAccess::exists(path)) {
  168. GDMono::get_singleton()->load_assembly_from(ref_name, path, &ref_assembly, true);
  169. if (ref_assembly != NULL)
  170. break;
  171. }
  172. }
  173. }
  174. if (!ref_assembly) {
  175. ERR_EXPLAIN("Cannot load assembly (refonly): " + ref_name);
  176. ERR_FAIL_V(ERR_CANT_RESOLVE);
  177. }
  178. r_dependencies.insert(ref_name, ref_assembly->get_path());
  179. Error err = _get_assembly_dependencies(ref_assembly, p_search_dirs, r_dependencies);
  180. if (err != OK)
  181. return err;
  182. }
  183. return OK;
  184. }
  185. GodotSharpExport::GodotSharpExport() {
  186. // MonoAssemblyName is an incomplete type (internal to mono), so we can't allocate it ourselves.
  187. // There isn't any api to allocate an empty one either, so we need to do it this way.
  188. aname_prealloc = mono_assembly_name_new("whatever");
  189. mono_assembly_name_free(aname_prealloc); // "it does not frees the object itself, only the name members" (typo included)
  190. }
  191. GodotSharpExport::~GodotSharpExport() {
  192. if (aname_prealloc)
  193. mono_free(aname_prealloc);
  194. }