2
0

godotsharp_export.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* godotsharp_export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 <mono/metadata/image.h>
  32. #include "core/config/project_settings.h"
  33. #include "core/io/file_access_pack.h"
  34. #include "core/os/os.h"
  35. #include "../mono_gd/gd_mono.h"
  36. #include "../mono_gd/gd_mono_assembly.h"
  37. #include "../mono_gd/gd_mono_cache.h"
  38. #include "../utils/macros.h"
  39. namespace GodotSharpExport {
  40. MonoAssemblyName *new_mono_assembly_name() {
  41. // Mono has no public API to create an empty MonoAssemblyName and the struct is private.
  42. // As such the only way to create it is with a stub name and then clear it.
  43. MonoAssemblyName *aname = mono_assembly_name_new("stub");
  44. CRASH_COND(aname == nullptr);
  45. mono_assembly_name_free(aname); // Frees the string fields, not the struct
  46. return aname;
  47. }
  48. struct AssemblyRefInfo {
  49. String name;
  50. uint16_t major = 0;
  51. uint16_t minor = 0;
  52. uint16_t build = 0;
  53. uint16_t revision = 0;
  54. };
  55. AssemblyRefInfo get_assemblyref_name(MonoImage *p_image, int index) {
  56. const MonoTableInfo *table_info = mono_image_get_table_info(p_image, MONO_TABLE_ASSEMBLYREF);
  57. uint32_t cols[MONO_ASSEMBLYREF_SIZE];
  58. mono_metadata_decode_row(table_info, index, cols, MONO_ASSEMBLYREF_SIZE);
  59. return {
  60. String::utf8(mono_metadata_string_heap(p_image, cols[MONO_ASSEMBLYREF_NAME])),
  61. (uint16_t)cols[MONO_ASSEMBLYREF_MAJOR_VERSION],
  62. (uint16_t)cols[MONO_ASSEMBLYREF_MINOR_VERSION],
  63. (uint16_t)cols[MONO_ASSEMBLYREF_BUILD_NUMBER],
  64. (uint16_t)cols[MONO_ASSEMBLYREF_REV_NUMBER]
  65. };
  66. }
  67. Error get_assembly_dependencies(GDMonoAssembly *p_assembly, MonoAssemblyName *reusable_aname, const Vector<String> &p_search_dirs, Dictionary &r_assembly_dependencies) {
  68. MonoImage *image = p_assembly->get_image();
  69. for (int i = 0; i < mono_image_get_table_rows(image, MONO_TABLE_ASSEMBLYREF); i++) {
  70. AssemblyRefInfo ref_info = get_assemblyref_name(image, i);
  71. const String &ref_name = ref_info.name;
  72. if (r_assembly_dependencies.has(ref_name)) {
  73. continue;
  74. }
  75. mono_assembly_get_assemblyref(image, i, reusable_aname);
  76. GDMonoAssembly *ref_assembly = NULL;
  77. if (!GDMono::get_singleton()->load_assembly(ref_name, reusable_aname, &ref_assembly, /* refonly: */ true, p_search_dirs)) {
  78. ERR_FAIL_V_MSG(ERR_CANT_RESOLVE, "Cannot load assembly (refonly): '" + ref_name + "'.");
  79. }
  80. r_assembly_dependencies[ref_name] = ref_assembly->get_path();
  81. Error err = get_assembly_dependencies(ref_assembly, reusable_aname, p_search_dirs, r_assembly_dependencies);
  82. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot load one of the dependencies for the assembly: '" + ref_name + "'.");
  83. }
  84. return OK;
  85. }
  86. Error get_exported_assembly_dependencies(const Dictionary &p_initial_assemblies,
  87. const String &p_build_config, const String &p_custom_bcl_dir, Dictionary &r_assembly_dependencies) {
  88. MonoDomain *export_domain = GDMonoUtils::create_domain("GodotEngine.Domain.ProjectExport");
  89. ERR_FAIL_NULL_V(export_domain, FAILED);
  90. _GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(export_domain);
  91. _GDMONO_SCOPE_DOMAIN_(export_domain);
  92. Vector<String> search_dirs;
  93. GDMonoAssembly::fill_search_dirs(search_dirs, p_build_config, p_custom_bcl_dir);
  94. if (p_custom_bcl_dir.length()) {
  95. // Only one mscorlib can be loaded. We need this workaround to make sure we get it from the right BCL directory.
  96. r_assembly_dependencies["mscorlib"] = p_custom_bcl_dir.plus_file("mscorlib.dll").simplify_path();
  97. }
  98. for (const Variant *key = p_initial_assemblies.next(); key; key = p_initial_assemblies.next(key)) {
  99. String assembly_name = *key;
  100. String assembly_path = p_initial_assemblies[*key];
  101. GDMonoAssembly *assembly = nullptr;
  102. bool load_success = GDMono::get_singleton()->load_assembly_from(assembly_name, assembly_path, &assembly, /* refonly: */ true);
  103. ERR_FAIL_COND_V_MSG(!load_success, ERR_CANT_RESOLVE, "Cannot load assembly (refonly): '" + assembly_name + "'.");
  104. MonoAssemblyName *reusable_aname = new_mono_assembly_name();
  105. SCOPE_EXIT { mono_free(reusable_aname); };
  106. Error err = get_assembly_dependencies(assembly, reusable_aname, search_dirs, r_assembly_dependencies);
  107. if (err != OK) {
  108. return err;
  109. }
  110. }
  111. return OK;
  112. }
  113. } // namespace GodotSharpExport