Mono: Editor and export template dependencies and fixes

- Bundle editor dependencies:
    - 'GodotSharp': Root data directory for the editor
        - 'Tools': Editor dependencies. Only GodotSharp.dll for now.
        - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies.
        - 'Mono': Mono files to bundle with the editor.
            - 'bin': (Optional, not used for now) Mono bin directory.
            - 'etc': Mono configuration files.
            - 'lib': Mono dependency shared libraries.
            - 'lib/mono/4.5': Framework assemblies.
    - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default.

- Export template dependencies:
    - 'data_AppName'/'data_Godot':
        - 'Mono': Mono files to bundle with the game.
            - 'etc': Mono configuration files.
            - 'lib': Mono dependency shared libraries.
    - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template.
    - In OSX, alternative location for directories (needed for app bundles) are:
        - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc'
        - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib'

- The editor can bundle prebuilt API assemblies.
    - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution)
    - Build the solutions and copy the output files to '#bin/GodotSharp/Api'.
- Fixed API assembly being added twice during the export process.
This commit is contained in:
Ignacio Etcheverry 2018-10-03 19:01:57 +02:00
parent 2e87703136
commit d7ece43b74
15 changed files with 651 additions and 239 deletions

View file

@ -30,12 +30,37 @@
#include "godotsharp_export.h"
#include "core/version.h"
#include "../csharp_script.h"
#include "../godotsharp_defs.h"
#include "../godotsharp_dirs.h"
#include "../mono_gd/gd_mono_class.h"
#include "../mono_gd/gd_mono_marshal.h"
#include "godotsharp_builds.h"
void GodotSharpExport::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
static MonoString *godot_icall_GodotSharpExport_GetTemplatesDir() {
String current_version = VERSION_FULL_CONFIG;
String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(current_version);
return GDMonoMarshal::mono_string_from_godot(ProjectSettings::get_singleton()->globalize_path(templates_dir));
}
static MonoString *godot_icall_GodotSharpExport_GetDataDirName() {
String appname = ProjectSettings::get_singleton()->get("application/config/name");
String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
return GDMonoMarshal::mono_string_from_godot("data_" + appname_safe);
}
void GodotSharpExport::register_internal_calls() {
static bool registered = false;
ERR_FAIL_COND(registered);
registered = true;
mono_add_internal_call("GodotSharpTools.Editor.GodotSharpExport::GetTemplatesDir", (void *)godot_icall_GodotSharpExport_GetTemplatesDir);
mono_add_internal_call("GodotSharpTools.Editor.GodotSharpExport::GetDataDirName", (void *)godot_icall_GodotSharpExport_GetDataDirName);
}
void GodotSharpExport::_export_file(const String &p_path, const String &p_type, const Set<String> &) {
if (p_type != CSharpLanguage::get_singleton()->get_type())
return;
@ -56,59 +81,78 @@ void GodotSharpExport::_export_begin(const Set<String> &p_features, bool p_debug
// TODO right now there is no way to stop the export process with an error
ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
ERR_FAIL_NULL(GDMono::get_singleton()->get_tools_domain());
ERR_FAIL_NULL(TOOLS_DOMAIN);
ERR_FAIL_NULL(GDMono::get_singleton()->get_editor_tools_assembly());
String build_config = p_debug ? "Debug" : "Release";
ERR_FAIL_COND(!GodotSharpBuilds::build_project_blocking(build_config));
// Add API assemblies
// Add dependency assemblies
String core_api_dll_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(API_ASSEMBLY_NAME ".dll");
ERR_FAIL_COND(!_add_assembly(core_api_dll_path, core_api_dll_path));
String editor_api_dll_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
ERR_FAIL_COND(!_add_assembly(editor_api_dll_path, editor_api_dll_path));
// Add project assembly
Map<String, String> dependencies;
String project_dll_name = ProjectSettings::get_singleton()->get("application/config/name");
if (project_dll_name.empty()) {
project_dll_name = "UnnamedProject";
}
String project_dll_src_path = GodotSharpDirs::get_res_temp_assemblies_base_dir().plus_file(build_config).plus_file(project_dll_name + ".dll");
String project_dll_dst_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(project_dll_name + ".dll");
ERR_FAIL_COND(!_add_assembly(project_dll_src_path, project_dll_dst_path));
String project_dll_src_dir = GodotSharpDirs::get_res_temp_assemblies_base_dir().plus_file(build_config);
String project_dll_src_path = project_dll_src_dir.plus_file(project_dll_name + ".dll");
dependencies.insert(project_dll_name, project_dll_src_path);
// Add dependencies
{
MonoDomain *export_domain = GDMonoUtils::create_domain("GodotEngine.ProjectExportDomain");
ERR_FAIL_NULL(export_domain);
_GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(export_domain);
MonoDomain *prev_domain = mono_domain_get();
MonoDomain *export_domain = GDMonoUtils::create_domain("GodotEngine.ProjectExportDomain");
_GDMONO_SCOPE_DOMAIN_(export_domain);
ERR_FAIL_COND(!export_domain);
ERR_FAIL_COND(!mono_domain_set(export_domain, false));
GDMonoAssembly *scripts_assembly = NULL;
bool load_success = GDMono::get_singleton()->load_assembly_from(project_dll_name,
project_dll_src_dir, &scripts_assembly, /* refonly: */ true);
Map<String, String> dependencies;
dependencies.insert("mscorlib", GDMono::get_singleton()->get_corlib_assembly()->get_path());
ERR_EXPLAIN("Cannot load refonly assembly: " + project_dll_name);
ERR_FAIL_COND(!load_success);
GDMonoAssembly *scripts_assembly = GDMonoAssembly::load_from(project_dll_name, project_dll_src_path, /* refonly: */ true);
ERR_EXPLAIN("Cannot load refonly assembly: " + project_dll_name);
ERR_FAIL_COND(!scripts_assembly);
Error depend_error = _get_assembly_dependencies(scripts_assembly, dependencies);
GDMono::get_singleton()->finalize_and_unload_domain(export_domain);
mono_domain_set(prev_domain, false);
ERR_FAIL_COND(depend_error != OK);
Vector<String> search_dirs;
GDMonoAssembly::fill_search_dirs(search_dirs);
Error depend_error = _get_assembly_dependencies(scripts_assembly, search_dirs, dependencies);
ERR_FAIL_COND(depend_error != OK);
}
for (Map<String, String>::Element *E = dependencies.front(); E; E = E->next()) {
String depend_src_path = E->value();
String depend_dst_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(depend_src_path.get_file());
ERR_FAIL_COND(!_add_assembly(depend_src_path, depend_dst_path));
}
// Mono specific export template extras (data dir)
GDMonoClass *export_class = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Editor", "GodotSharpExport");
ERR_FAIL_NULL(export_class);
GDMonoMethod *export_begin_method = export_class->get_method("_ExportBegin", 4);
ERR_FAIL_NULL(export_begin_method);
MonoArray *features = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), p_features.size());
int i = 0;
for (const Set<String>::Element *E = p_features.front(); E; E = E->next()) {
MonoString *boxed = GDMonoMarshal::mono_string_from_godot(E->get());
mono_array_set(features, MonoString *, i, boxed);
i++;
}
MonoBoolean debug = p_debug;
MonoString *path = GDMonoMarshal::mono_string_from_godot(p_path);
uint32_t flags = p_flags;
void *args[4] = { features, &debug, path, &flags };
MonoException *exc = NULL;
export_begin_method->invoke_raw(NULL, args, &exc);
if (exc) {
GDMonoUtils::debug_print_unhandled_exception(exc);
ERR_FAIL();
}
}
bool GodotSharpExport::_add_assembly(const String &p_src_path, const String &p_dst_path) {
@ -125,7 +169,7 @@ bool GodotSharpExport::_add_assembly(const String &p_src_path, const String &p_d
return true;
}
Error GodotSharpExport::_get_assembly_dependencies(GDMonoAssembly *p_assembly, Map<String, String> &r_dependencies) {
Error GodotSharpExport::_get_assembly_dependencies(GDMonoAssembly *p_assembly, const Vector<String> &p_search_dirs, Map<String, String> &r_dependencies) {
MonoImage *image = p_assembly->get_image();
@ -134,18 +178,48 @@ Error GodotSharpExport::_get_assembly_dependencies(GDMonoAssembly *p_assembly, M
mono_assembly_get_assemblyref(image, i, ref_aname);
String ref_name = mono_assembly_name_get_name(ref_aname);
if (ref_name == "mscorlib" || r_dependencies.find(ref_name))
if (r_dependencies.find(ref_name))
continue;
GDMonoAssembly *ref_assembly = NULL;
if (!GDMono::get_singleton()->load_assembly(ref_name, ref_aname, &ref_assembly, /* refonly: */ true)) {
ERR_EXPLAIN("Cannot load refonly assembly: " + ref_name);
String path;
bool has_extension = ref_name.ends_with(".dll") || ref_name.ends_with(".exe");
for (int i = 0; i < p_search_dirs.size(); i++) {
const String &search_dir = p_search_dirs[i];
if (has_extension) {
path = search_dir.plus_file(ref_name);
if (FileAccess::exists(path)) {
GDMono::get_singleton()->load_assembly_from(ref_name.get_basename(), search_dir, ref_aname, &ref_assembly, true);
if (ref_assembly != NULL)
break;
}
} else {
path = search_dir.plus_file(ref_name + ".dll");
if (FileAccess::exists(path)) {
GDMono::get_singleton()->load_assembly_from(ref_name, search_dir, ref_aname, &ref_assembly, true);
if (ref_assembly != NULL)
break;
}
path = search_dir.plus_file(ref_name + ".exe");
if (FileAccess::exists(path)) {
GDMono::get_singleton()->load_assembly_from(ref_name, search_dir, ref_aname, &ref_assembly, true);
if (ref_assembly != NULL)
break;
}
}
}
if (!ref_assembly) {
ERR_EXPLAIN("Cannot load assembly (refonly): " + ref_name);
ERR_FAIL_V(ERR_CANT_RESOLVE);
}
r_dependencies.insert(ref_name, ref_assembly->get_path());
Error err = _get_assembly_dependencies(ref_assembly, r_dependencies);
Error err = _get_assembly_dependencies(ref_assembly, p_search_dirs, r_dependencies);
if (err != OK)
return err;
}