NETRunApp.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/ProcessUtils.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Engine/Engine.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #include <AtomicNET/NETCore/NETHost.h>
  28. #include <AtomicNET/NETCore/NETCore.h>
  29. #include "NETRunApp.h"
  30. DEFINE_APPLICATION_MAIN(Atomic::NETRunApp)
  31. namespace Atomic
  32. {
  33. NETRunApp::NETRunApp(Context* context) :
  34. Application(context)
  35. {
  36. }
  37. NETRunApp::~NETRunApp()
  38. {
  39. }
  40. void NETRunApp::Setup()
  41. {
  42. String coreCLRAbsPath;
  43. String execAssemblyPath;
  44. String tpaPaths;
  45. const Vector<String>& arguments = GetArguments();
  46. for (unsigned i = 0; i < arguments.Size(); ++i)
  47. {
  48. if (arguments[i].Length() > 1)
  49. {
  50. String argument = arguments[i].ToLower();
  51. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  52. if (argument == "--coreclr-abspath")
  53. {
  54. if (!value.Length())
  55. ErrorExit("Unable to parse --coreclr-abspath");
  56. coreCLRAbsPath = AddTrailingSlash(value);
  57. }
  58. else if (argument == "--exec-assembly")
  59. {
  60. if (!value.Length())
  61. ErrorExit("Unable to parse --exec-assembly");
  62. execAssemblyPath = value;
  63. }
  64. else if (argument == "--tpa-paths")
  65. {
  66. if (!value.Length())
  67. ErrorExit("Unable to parse --tpa-paths");
  68. tpaPaths = AddTrailingSlash(value);
  69. }
  70. }
  71. }
  72. if (!coreCLRAbsPath.Length())
  73. ErrorExit("Unable to parse --coreclr-abspath");
  74. if (!execAssemblyPath.Length())
  75. ErrorExit("Unable to parse --exec-assembly");
  76. if (!tpaPaths.Length())
  77. ErrorExit("Unable to parse --tpa-paths");
  78. // Parse assembly
  79. String pathName, fileName, ext;
  80. SplitPath(execAssemblyPath, pathName, fileName, ext);
  81. // this needs to be full path to assembly
  82. assemblyToExecute_ = execAssemblyPath;
  83. NETHost::SetCoreCLRFilesAbsPath(coreCLRAbsPath);
  84. NETHost::SetCoreCLRTPAPaths(tpaPaths);
  85. NETHost::SetCoreCLRAssemblyLoadPaths(pathName);
  86. // Instantiate and register the AtomicNET subsystem
  87. SharedPtr<NETCore> netCore (new NETCore(context_));
  88. context_->RegisterSubsystem(netCore);
  89. String netCoreErrorMsg;
  90. if (!netCore->Initialize(netCoreErrorMsg))
  91. {
  92. ErrorExit(ToString("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString()));
  93. }
  94. engineParameters_["Headless"] = true;
  95. engineParameters_["LogLevel"] = LOG_INFO;
  96. // no default resources (will be initialized later)
  97. engineParameters_["ResourcePaths"] = "";
  98. }
  99. void NETRunApp::Start()
  100. {
  101. Application::Start();
  102. NETCore* netCore = GetSubsystem<NETCore>();
  103. assemblyArgs_.Push(assemblyToExecute_);
  104. assemblyArgs_.Push("--help");
  105. netCore->ExecAssembly(assemblyToExecute_, assemblyArgs_);
  106. GetSubsystem<Engine>()->Exit();
  107. }
  108. void NETRunApp::Stop()
  109. {
  110. Application::Stop();
  111. }
  112. void NETRunApp::ErrorExit(const String& message)
  113. {
  114. engine_->Exit(); // Close the rendering window
  115. exitCode_ = EXIT_FAILURE;
  116. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  117. if (!message.Length())
  118. {
  119. #ifdef WIN32
  120. Atomic::ErrorExit(startupErrors_.Length() ? startupErrors_ :
  121. "Application has been terminated due to unexpected error.", exitCode_);
  122. #endif
  123. }
  124. else
  125. Atomic::ErrorExit(message, exitCode_);
  126. }
  127. }