BsMonoExec.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #ifdef _WIN32
  4. #define _CRT_SECURE_NO_WARNINGS 1
  5. #include <direct.h>
  6. #define getcwd _getcwd
  7. #else
  8. #include <unistd.h>
  9. #endif
  10. #include <mono/jit/jit.h>
  11. #include <mono/metadata/assembly.h>
  12. #include <mono/metadata/mono-config.h>
  13. #include <cstring>
  14. const char* LIB_DIR = "lib/";
  15. const char* ETC_DIR = "etc/";
  16. const char* ASSEMBLIES_DIR = "lib/mono/4.5/";
  17. // Starts up the Mono runtime, runs the managed assembly provided as the first parameter using the
  18. // runtime and passes the rest of the arguments to it. When done shuts down the Mono runtime.
  19. int main(int argc, char* argv[])
  20. {
  21. // No assembly to run, or Mono directory not provided
  22. if(argc < 3)
  23. return 0;
  24. char* monoDir = argv[2];
  25. char* libDir = new char[strlen(monoDir) + strlen(LIB_DIR) + 1];
  26. strcpy(libDir, monoDir);
  27. strcat(libDir, LIB_DIR);
  28. char* etcDir = new char[strlen(monoDir) + strlen(ETC_DIR) + 1];
  29. strcpy(etcDir, monoDir);
  30. strcat(etcDir, ETC_DIR);
  31. char* assembliesDir = new char[strlen(monoDir) + strlen(ASSEMBLIES_DIR) + 1];
  32. strcpy(assembliesDir, monoDir);
  33. strcat(assembliesDir, ASSEMBLIES_DIR);
  34. mono_set_dirs(libDir, etcDir);
  35. mono_set_assemblies_path(assembliesDir);
  36. delete[] assembliesDir;
  37. delete[] etcDir;
  38. delete[] libDir;
  39. mono_config_parse(nullptr);
  40. MonoDomain* domain = mono_jit_init(argv[1]);
  41. if(domain == nullptr)
  42. return 1;
  43. MonoAssembly* assembly = mono_domain_assembly_open(domain, argv[1]);
  44. if(assembly == nullptr)
  45. return 1;
  46. int returnVal = mono_jit_exec(domain, assembly, argc - 3, argv + 3);
  47. mono_jit_cleanup(domain);
  48. return returnVal;
  49. }