NETRunApp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Core/ProcessUtils.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Engine/Engine.h>
  11. #include <Atomic/Resource/ResourceCache.h>
  12. #include <AtomicNET/NETCore/NETHost.h>
  13. #include <AtomicNET/NETCore/NETCore.h>
  14. #include "NETRunApp.h"
  15. DEFINE_APPLICATION_MAIN(Atomic::NETRunApp)
  16. namespace Atomic
  17. {
  18. NETRunApp::NETRunApp(Context* context) :
  19. Application(context)
  20. {
  21. }
  22. NETRunApp::~NETRunApp()
  23. {
  24. }
  25. void NETRunApp::Setup()
  26. {
  27. String coreCLRAbsPath;
  28. String execAssemblyPath;
  29. String tpaPaths;
  30. const Vector<String>& arguments = GetArguments();
  31. for (unsigned i = 0; i < arguments.Size(); ++i)
  32. {
  33. if (arguments[i].Length() > 1)
  34. {
  35. String argument = arguments[i].ToLower();
  36. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  37. if (argument == "--coreclr-abspath")
  38. {
  39. if (!value.Length())
  40. ErrorExit("Unable to parse --coreclr-abspath");
  41. coreCLRAbsPath = AddTrailingSlash(value);
  42. }
  43. else if (argument == "--exec-assembly")
  44. {
  45. if (!value.Length())
  46. ErrorExit("Unable to parse --exec-assembly");
  47. execAssemblyPath = value;
  48. }
  49. else if (argument == "--tpa-paths")
  50. {
  51. if (!value.Length())
  52. ErrorExit("Unable to parse --tpa-paths");
  53. tpaPaths = AddTrailingSlash(value);
  54. }
  55. }
  56. }
  57. if (!coreCLRAbsPath.Length())
  58. ErrorExit("Unable to parse --coreclr-abspath");
  59. if (!execAssemblyPath.Length())
  60. ErrorExit("Unable to parse --exec-assembly");
  61. if (!tpaPaths.Length())
  62. ErrorExit("Unable to parse --tpa-paths");
  63. // Parse assembly
  64. String pathName, fileName, ext;
  65. SplitPath(execAssemblyPath, pathName, fileName, ext);
  66. // this needs to be full path to assembly
  67. assemblyToExecute_ = execAssemblyPath;
  68. NETHost::SetCoreCLRFilesAbsPath(coreCLRAbsPath);
  69. NETHost::SetCoreCLRTPAPaths(tpaPaths);
  70. NETHost::SetCoreCLRAssemblyLoadPaths(pathName);
  71. // Instantiate and register the AtomicNET subsystem
  72. SharedPtr<NETCore> netCore (new NETCore(context_));
  73. context_->RegisterSubsystem(netCore);
  74. String netCoreErrorMsg;
  75. if (!netCore->Initialize(netCoreErrorMsg))
  76. {
  77. ErrorExit(ToString("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString()));
  78. }
  79. engineParameters_["Headless"] = true;
  80. engineParameters_["LogLevel"] = LOG_INFO;
  81. // no default resources (will be initialized later)
  82. engineParameters_["ResourcePaths"] = "";
  83. }
  84. void NETRunApp::Start()
  85. {
  86. Application::Start();
  87. NETCore* netCore = GetSubsystem<NETCore>();
  88. assemblyArgs_.Push(assemblyToExecute_);
  89. assemblyArgs_.Push("--help");
  90. netCore->ExecAssembly(assemblyToExecute_, assemblyArgs_);
  91. GetSubsystem<Engine>()->Exit();
  92. }
  93. void NETRunApp::Stop()
  94. {
  95. Application::Stop();
  96. }
  97. void NETRunApp::ErrorExit(const String& message)
  98. {
  99. engine_->Exit(); // Close the rendering window
  100. exitCode_ = EXIT_FAILURE;
  101. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  102. if (!message.Length())
  103. {
  104. #ifdef WIN32
  105. Atomic::ErrorExit(startupErrors_.Length() ? startupErrors_ :
  106. "Application has been terminated due to unexpected error.", exitCode_);
  107. #endif
  108. }
  109. else
  110. Atomic::ErrorExit(message, exitCode_);
  111. }
  112. }