int main() { /* TODO: using abs paths just to test this shit. */ // Set path where managed assemblies live. mono_set_assemblies_path("C:\\Users\\Niko\\Source\\Repos\\SaNi\\vs2013\\sanimono\\SaNi\\bin\\Debug"); // Set Mono library and configuration paths. mono_set_dirs("C:\\Program Files (x86)\\Mono\\lib", "C:\\Program Files (x86)\\Mono\\etc"); // Our application assembly that contains // the code we are going to run. domain = mono_jit_init_version("SaNi", "v4.0.30319"); // Our domain in which the code will be // executed. MonoAssembly* assembly = mono_domain_assembly_open(domain, "C:\\Users\\Niko\\Source\\Repos\\SaNi\\vs2013\\sanimono\\SaNi\\bin\\Debug\\SaNi.exe"); if (!assembly) throw std::runtime_error("could not load the given Mono assembly"); mono_add_internal_call("SaNi.Utils::Print(string)", (void*)print); mono_add_internal_call("SaNi.Utils::GetString()", (void*)getString); const auto argc = 6; char* argv[argc] = { "HELLO", "WORLD", "FROM", "THE", "F*****G", "MONO" }; mono_jit_exec(domain, assembly, argc, argv); // Clean mono. mono_jit_cleanup(domain); return 0; }
CScriptDomain::CScriptDomain(ERuntimeVersion runtimeVersion) : m_bRootDomain(true) , m_bDestroying(false) , m_name("root") { const char *version = "v2.0.50727"; switch(runtimeVersion) { case eRV_2_50215: version = "v2.0.50215"; break; case eRV_2_50727: break; case eRV_4_20506: version = "v4.0.20506"; break; case eRV_4_30128: version = "v4.0.30128"; break; case eRV_4_30319: version = "v4.0.30319"; break; } // Crashing on this line is an indicator of mono being incorrectly configured, Make sure Bin(32/64)/mono.exe, Bin(32/64)/mono-2.0.dll & Engine/Mono are up-to-date. m_pDomain = mono_jit_init_version("CryMono", version); if(!m_pDomain) CryFatalError("Failed to initialize root domain with runtime version %s!", version); }
static HRESULT RuntimeHost_AddDefaultDomain(RuntimeHost *This, MonoDomain **result) { struct DomainEntry *entry; HRESULT res=S_OK; EnterCriticalSection(&This->lock); entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry)); if (!entry) { res = E_OUTOFMEMORY; goto end; } /* FIXME: Use exe filename to name the domain? */ entry->domain = mono_jit_init_version("mscorlib.dll", "v4.0.30319"); if (!entry->domain) { HeapFree(GetProcessHeap(), 0, entry); res = E_FAIL; goto end; } is_mono_started = TRUE; list_add_tail(&This->domains, &entry->entry); *result = entry->domain; end: LeaveCriticalSection(&This->lock); return res; }
/** * Starts the mono runtime, and loads the encapsulated assembly, unless * it's already been loaded. */ void sp_ensure_runtime() { /* Check that the runtime hasn't already been started. */ if (runtime_started) { return; } /* Initialise the JIT. */ m_domain = mono_jit_init_version(__assembly_info.name, __assembly_info.version); if (!m_domain) { printf("error: unable to create mono domain\n"); _exit(0); } /* Open the assembly image, from the data inside the object * file. This data is accessed via the special symbols created * when the PE is linked in as a binary object. */ m_image = mono_image_open_from_data(&_binary_test_dll_start, &_binary_test_dll_end - &_binary_test_dll_start, 0, NULL); if (!m_image) { printf("error: unable to open assembly image\n"); _exit(0); } /* Now, load the assembly. */ m_assembly = mono_assembly_load_from_full(m_image, __assembly_info.name, NULL, 0); if (!m_assembly) { printf("error: unable to load assembly\n"); _exit(0); } /* Mark the runtime as started, just in case we neglected to * rewrite some function stubs. */ runtime_started = 1; }
int main() { #if LOADDYNAMIC SetupMono(); #endif #if WIN32 SetEnvironmentVariable(L"MONO_PATH",L"..\\..\\..\\builds\\monodistribution\\lib\\mono\\2.0"); #else setenv("MONO_PATH","../../../builds/monodistribution/lib/mono/2.0",1); #endif MonoDomain* domain = mono_jit_init_version ("Unity Root Domain","v2.0.50727"); //create and set child domain MonoDomain* child = mono_domain_create_appdomain("Unity Child Domain",NULL); mono_domain_set(child,0); //load assembly and call entrypoint MonoAssembly* ass = mono_domain_assembly_open (mono_domain_get (),"lucas.exe"); MonoImage* img = mono_assembly_get_image(ass); printf("image %p\n",img); MonoMethodDesc* desc = mono_method_desc_new("Main2:Main",1); MonoMethod* m = mono_method_desc_search_in_image(desc,img); printf("method %p\n",m); MonoObject* exc; MonoObject* newinst = mono_object_new(mono_domain_get(),mono_method_get_class(m)); MonoObject* ret = mono_runtime_invoke(m,newinst,0,&exc); printf ("Exception: %p\n",exc); if (exc) { MonoException* exc2 = (MonoException*) exc; printf ("exc msg:%s\n",mono_class_get_name(mono_object_get_class((MonoObject*)exc))); } printf ("ret: %p\n",ret); Sleep(0); //switch back to root domain mono_domain_set(domain,0); mono_domain_unload(child); mono_runtime_set_shutting_down (); mono_threads_set_shutting_down (); mono_thread_pool_cleanup (); mono_domain_finalize(mono_get_root_domain(),2000); mono_runtime_cleanup(mono_get_root_domain()); printf("Unloading mono\n"); #if LOADDYNAMIC CleanupMono(); #endif while(1){} //sleep so stale monothreads have a chance to crash return 0; }
//##################################################################### // Function Mono //##################################################################### MonoSystem::MonoSystem() : mDomain(NULL) { mono_install_assembly_load_hook(assembly_loaded, &mAssemblies); mWorkDir = "/home/daniel/sirikata/";//Sirikata::getCWD(); mDomain = Domain(mono_jit_init_version("Root Domain", "v2.0.50727")); assert(!mDomain.null()); Domain::setRoot(mDomain); //mono_trace_set_level(G_LOG_LEVEL_INFO); //mono_trace_set_level(G_LOG_LEVEL_DEBUG); }
// initialize Mono and PythonNet PyNet_Args* PyNet_Init(int ext) { PyNet_Args *pn_args; pn_args = (PyNet_Args *)malloc(sizeof(PyNet_Args)); pn_args->pr_file = PR_ASSEMBLY; pn_args->error = NULL; pn_args->shutdown = NULL; pn_args->module = NULL; if (ext == 0) { pn_args->init_name = "Python.Runtime:Initialize()"; } else { pn_args->init_name = "Python.Runtime:InitExt()"; } pn_args->shutdown_name = "Python.Runtime:Shutdown()"; pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION); /* * Load the default Mono configuration file, this is needed * if you are planning on using the dllmaps defined on the * system configuration */ mono_config_parse(NULL); /* I can't use this call to run the main_thread_handler. The function * runs it in another thread but *this* thread holds the Python * import lock -> DEAD LOCK. * * mono_runtime_exec_managed_code(pn_args->domain, main_thread_handler, * pn_args); */ main_thread_handler(pn_args); if (pn_args->error != NULL) { PyErr_SetString(PyExc_ImportError, pn_args->error); } return pn_args; }
void Application::InitializeMono() { //this will override the internal assembly search logic. //do it in case you package mono in a different structure //mono_set_assemblies_path(dllDir.c_str()); mono_config_parse(NULL); // initialize the root domain which will hold corlib and will always be alive domain = mono_jit_init_version("CCubed Root Domain", "v4.0.30319"); // soft debugger needs this mono_thread_set_main(mono_thread_current()); // add icalls mono_add_internal_call("EmbedThings.EntryPoint::reload", reload); // run the c# bits Run(); // we're exiting the whole thing, cleanup mono_jit_cleanup(mono_domain_get()); }
int main (void) { int res = 0; MonoDomain *domain = NULL; MonoAssembly *assembly = NULL; MonoImage *prog_image = NULL; MonoImage *corlib = NULL; MonoClass *prog_klass, *console_klass; MonoMethod *meth; MonoImageOpenStatus status; //FIXME This is a fugly hack due to embedding simply not working from the tree mono_set_assemblies_path ("../../mcs/class/lib/net_4_x"); test_methods = g_array_new (FALSE, TRUE, sizeof (MonoMethod *)); if (!test_methods) { res = 1; printf ("FAILED INITIALIZING METHODS ARRAY\n"); goto out; } domain = mono_jit_init_version ("TEST RUNNER", "mobile"); assembly = mono_assembly_open (TESTPROG, &status); if (!domain || !assembly) { res = 1; printf("FAILED LOADING TEST PROGRAM\n"); goto out; } mono_callspec_set_assembly(assembly); prog_image = mono_assembly_get_image (assembly); prog_klass = test_mono_class_from_name (prog_image, "Baz", "Foo"); if (!prog_klass) { res = 1; printf ("FAILED FINDING Baz.Foo\n"); goto out; } meth = mono_class_get_method_from_name (prog_klass, "Bar", 0); if (!meth) { res = 1; printf ("FAILED FINDING Baz.Foo:Bar ()\n"); goto out; } g_array_append_val (test_methods, meth); meth = mono_class_get_method_from_name (prog_klass, "Bar", 1); if (!meth) { res = 1; printf ("FAILED FINDING Baz.Foo:Bar (string)\n"); goto out; } g_array_append_val (test_methods, meth); prog_klass = test_mono_class_from_name (prog_image, "Baz", "Goo"); if (!prog_klass) { res = 1; printf ("FAILED FINDING Baz.Goo\n"); goto out; } meth = mono_class_get_method_from_name (prog_klass, "Bar", 1); if (!meth) { res = 1; printf ("FAILED FINDING Baz.Goo:Bar (string)\n"); goto out; } g_array_append_val (test_methods, meth); prog_klass = test_mono_class_from_name (prog_image, "Baz", "Foo2"); if (!prog_klass) { res = 1; printf ("FAILED FINDING Baz.Foo2\n"); goto out; } meth = mono_class_get_method_from_name (prog_klass, "Bar", 1); if (!meth) { res = 1; printf ("FAILED FINDING Baz.Foo2:Bar (string)\n"); goto out; } g_array_append_val (test_methods, meth); corlib = mono_get_corlib (); console_klass = test_mono_class_from_name (corlib, "System", "Console"); if (!console_klass) { res = 1; printf ("FAILED FINDING System.Console\n"); goto out; } meth = mono_class_get_method_from_name (console_klass, "WriteLine", 1); if (!meth) { res = 1; printf ("FAILED FINDING System.Console:WriteLine\n"); goto out; } g_array_append_val (test_methods, meth); res = test_all_callspecs (); out: return res; }
void GDMono::initialize() { ERR_FAIL_NULL(Engine::get_singleton()); print_verbose("Mono: Initializing module..."); #ifdef DEBUG_METHODS_ENABLED _initialize_and_check_api_hashes(); #endif GDMonoLog::get_singleton()->initialize(); #ifdef MONO_PRINT_HANDLER_ENABLED mono_trace_set_print_handler(gdmono_MonoPrintCallback); mono_trace_set_printerr_handler(gdmono_MonoPrintCallback); #endif #ifdef WINDOWS_ENABLED mono_reg_info = MonoRegUtils::find_mono(); CharString assembly_dir; CharString config_dir; if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) { assembly_dir = mono_reg_info.assembly_dir.utf8(); } if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) { config_dir = mono_reg_info.config_dir.utf8(); } mono_set_dirs(assembly_dir.length() ? assembly_dir.get_data() : NULL, config_dir.length() ? config_dir.get_data() : NULL); #elif OSX_ENABLED mono_set_dirs(NULL, NULL); { const char *assembly_rootdir = mono_assembly_getrootdir(); const char *config_dir = mono_get_config_dir(); if (!assembly_rootdir || !config_dir || !DirAccess::exists(assembly_rootdir) || !DirAccess::exists(config_dir)) { Vector<const char *> locations; locations.push_back("/Library/Frameworks/Mono.framework/Versions/Current/"); locations.push_back("/usr/local/var/homebrew/linked/mono/"); for (int i = 0; i < locations.size(); i++) { String hint_assembly_rootdir = path_join(locations[i], "lib"); String hint_mscorlib_path = path_join(hint_assembly_rootdir, "mono", "4.5", "mscorlib.dll"); String hint_config_dir = path_join(locations[i], "etc"); if (FileAccess::exists(hint_mscorlib_path) && DirAccess::exists(hint_config_dir)) { mono_set_dirs(hint_assembly_rootdir.utf8().get_data(), hint_config_dir.utf8().get_data()); break; } } } } #else mono_set_dirs(NULL, NULL); #endif GDMonoAssembly::initialize(); #ifdef DEBUG_ENABLED gdmono_debug_init(); #endif mono_config_parse(NULL); mono_install_unhandled_exception_hook(&unhandled_exception_hook, NULL); root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319"); ERR_EXPLAIN("Mono: Failed to initialize runtime"); ERR_FAIL_NULL(root_domain); GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread()); setup_runtime_main_args(); // Required for System.Environment.GetCommandLineArgs runtime_initialized = true; print_verbose("Mono: Runtime initialized"); // mscorlib assembly MUST be present at initialization ERR_EXPLAIN("Mono: Failed to load mscorlib assembly"); ERR_FAIL_COND(!_load_corlib_assembly()); #ifdef TOOLS_ENABLED // The tools domain must be loaded here, before the scripts domain. // Otherwise domain unload on the scripts domain will hang indefinitely. ERR_EXPLAIN("Mono: Failed to load tools domain"); ERR_FAIL_COND(_load_tools_domain() != OK); // TODO move to editor init callback, and do it lazily when required before editor init (e.g.: bindings generation) ERR_EXPLAIN("Mono: Failed to load Editor Tools assembly"); ERR_FAIL_COND(!_load_editor_tools_assembly()); #endif ERR_EXPLAIN("Mono: Failed to load scripts domain"); ERR_FAIL_COND(_load_scripts_domain() != OK); #ifdef DEBUG_ENABLED bool debugger_attached = _wait_for_debugger_msecs(500); if (!debugger_attached && OS::get_singleton()->is_stdout_verbose()) print_error("Mono: Debugger wait timeout"); #endif _register_internal_calls(); // The following assemblies are not required at initialization #ifdef MONO_GLUE_ENABLED if (_load_api_assemblies()) { if (!core_api_assembly_out_of_sync && !editor_api_assembly_out_of_sync && GDMonoUtils::mono_cache.godot_api_cache_updated) { // Everything is fine with the api assemblies, load the project assembly _load_project_assembly(); } else { #ifdef TOOLS_ENABLED // The assembly was successfully loaded, but the full api could not be cached. // This is most likely an outdated assembly loaded because of an invalid version in the metadata, // so we invalidate the version in the metadata and unload the script domain. if (core_api_assembly_out_of_sync) { ERR_PRINT("The loaded Core API assembly is out of sync"); metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true); } else if (!GDMonoUtils::mono_cache.godot_api_cache_updated) { ERR_PRINT("The loaded Core API assembly is in sync, but the cache update failed"); metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true); } if (editor_api_assembly_out_of_sync) { ERR_PRINT("The loaded Editor API assembly is out of sync"); metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true); } print_line("Mono: Proceeding to unload scripts domain because of invalid API assemblies."); Error err = _unload_scripts_domain(); if (err != OK) { WARN_PRINT("Mono: Failed to unload scripts domain"); } #else ERR_PRINT("The loaded API assembly is invalid"); CRASH_NOW(); #endif } } #else print_verbose("Mono: Glue disabled, ignoring script assemblies."); #endif print_verbose("Mono: INITIALIZED"); }
static void InitMono() { std::string citizenClrPath = MakeRelativeNarrowPath("citizen/clr2/lib/"); std::string citizenCfgPath = MakeRelativeNarrowPath("citizen/clr2/cfg/"); mono_set_dirs(citizenClrPath.c_str(), citizenCfgPath.c_str()); #ifdef _WIN32 std::wstring citizenClrLibPath = MakeRelativeCitPath(L"citizen/clr2/lib/mono/4.5/"); SetEnvironmentVariable(L"MONO_PATH", citizenClrLibPath.c_str()); mono_set_crash_chaining(true); #else std::string citizenClrLibPath = MakeRelativeNarrowPath("citizen/clr2/lib/mono/4.5/"); putenv(const_cast<char*>(va("MONO_PATH=%s", citizenClrLibPath))); #endif mono_assembly_setrootdir(citizenClrPath.c_str()); putenv("MONO_DEBUG=casts"); #ifndef IS_FXSERVER mono_security_enable_core_clr(); mono_security_core_clr_set_options((MonoSecurityCoreCLROptions)(MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_DELEGATE | MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION)); mono_security_set_core_clr_platform_callback(CoreClrCallback); #endif char* args[1]; #ifdef _WIN32 args[0] = "--soft-breakpoints"; #else args[0] = "--use-fallback-tls"; #endif mono_jit_parse_options(1, args); mono_debug_init(MONO_DEBUG_FORMAT_MONO); g_rootDomain = mono_jit_init_version("Citizen", "v4.0.30319"); mono_domain_set_config(g_rootDomain, ".", "cfx.config"); mono_install_unhandled_exception_hook([] (MonoObject* exc, void*) { OutputExceptionDetails(exc); }, nullptr); mono_set_crash_chaining(true); mono_add_internal_call("CitizenFX.Core.GameInterface::PrintLog", reinterpret_cast<void*>(GI_PrintLogCall)); mono_add_internal_call("CitizenFX.Core.GameInterface::fwFree", reinterpret_cast<void*>(fwFree)); std::string platformPath = MakeRelativeNarrowPath("citizen/clr2/lib/mono/4.5/CitizenFX.Core.dll"); auto scriptManagerAssembly = mono_domain_assembly_open(g_rootDomain, platformPath.c_str()); if (!scriptManagerAssembly) { FatalError("Could not load CitizenFX.Core.dll.\n"); } auto scriptManagerImage = mono_assembly_get_image(scriptManagerAssembly); bool methodSearchSuccess = true; MonoMethodDesc* description; #define method_search(name, method) description = mono_method_desc_new(name, 1); \ method = mono_method_desc_search_in_image(description, scriptManagerImage); \ mono_method_desc_free(description); \ methodSearchSuccess = methodSearchSuccess && method != NULL MonoMethod* rtInitMethod; method_search("CitizenFX.Core.RuntimeManager:Initialize", rtInitMethod); method_search("CitizenFX.Core.RuntimeManager:GetImplementedClasses", g_getImplementsMethod); method_search("CitizenFX.Core.RuntimeManager:CreateObjectInstance", g_createObjectMethod); if (!methodSearchSuccess) { FatalError("Couldn't find one or more CitizenFX.Core methods.\n"); } MonoObject* exc = nullptr; mono_runtime_invoke(rtInitMethod, nullptr, nullptr, &exc); if (exc) { OutputExceptionDetails(exc); return; } }
void GDMono::initialize() { ERR_FAIL_NULL(Engine::get_singleton()); OS::get_singleton()->print("Mono: Initializing module...\n"); #ifdef DEBUG_METHODS_ENABLED _initialize_and_check_api_hashes(); #endif GDMonoLog::get_singleton()->initialize(); #ifdef MONO_PRINT_HANDLER_ENABLED mono_trace_set_print_handler(gdmono_MonoPrintCallback); mono_trace_set_printerr_handler(gdmono_MonoPrintCallback); #endif #ifdef WINDOWS_ENABLED mono_reg_info = MonoRegUtils::find_mono(); CharString assembly_dir; CharString config_dir; if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) { assembly_dir = mono_reg_info.assembly_dir.utf8(); } if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) { config_dir = mono_reg_info.config_dir.utf8(); } mono_set_dirs(assembly_dir.length() ? assembly_dir.get_data() : NULL, config_dir.length() ? config_dir.get_data() : NULL); #else mono_set_dirs(NULL, NULL); #endif GDMonoAssembly::initialize(); #ifdef DEBUG_ENABLED gdmono_debug_init(); #endif mono_config_parse(NULL); root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319"); ERR_EXPLAIN("Mono: Failed to initialize runtime"); ERR_FAIL_NULL(root_domain); GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread()); runtime_initialized = true; OS::get_singleton()->print("Mono: Runtime initialized\n"); // mscorlib assembly MUST be present at initialization ERR_EXPLAIN("Mono: Failed to load mscorlib assembly"); ERR_FAIL_COND(!_load_corlib_assembly()); #ifdef TOOLS_ENABLED // The tools domain must be loaded here, before the scripts domain. // Otherwise domain unload on the scripts domain will hang indefinitely. ERR_EXPLAIN("Mono: Failed to load tools domain"); ERR_FAIL_COND(_load_tools_domain() != OK); // TODO move to editor init callback, and do it lazily when required before editor init (e.g.: bindings generation) ERR_EXPLAIN("Mono: Failed to load Editor Tools assembly"); ERR_FAIL_COND(!_load_editor_tools_assembly()); #endif ERR_EXPLAIN("Mono: Failed to load scripts domain"); ERR_FAIL_COND(_load_scripts_domain() != OK); #ifdef DEBUG_ENABLED bool debugger_attached = _wait_for_debugger_msecs(500); if (!debugger_attached && OS::get_singleton()->is_stdout_verbose()) OS::get_singleton()->printerr("Mono: Debugger wait timeout\n"); #endif _register_internal_calls(); // The following assemblies are not required at initialization _load_all_script_assemblies(); mono_install_unhandled_exception_hook(gdmono_unhandled_exception_hook, NULL); OS::get_singleton()->print("Mono: INITIALIZED\n"); }