AtomicTool.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <ToolCore/ToolSystem.h>
  28. #include <ToolCore/ToolEnvironment.h>
  29. #include <ToolCore/Build/BuildSystem.h>
  30. #include <ToolCore/License/LicenseEvents.h>
  31. #include <ToolCore/License/LicenseSystem.h>
  32. #include <ToolCore/Command/Command.h>
  33. #include <ToolCore/Command/CommandParser.h>
  34. #include "AtomicTool.h"
  35. ATOMIC_DEFINE_APPLICATION_MAIN(AtomicTool::AtomicTool);
  36. using namespace ToolCore;
  37. namespace AtomicTool
  38. {
  39. AtomicTool::AtomicTool(Context* context) :
  40. Application(context),
  41. deactivate_(false)
  42. {
  43. }
  44. AtomicTool::~AtomicTool()
  45. {
  46. }
  47. void AtomicTool::Setup()
  48. {
  49. const Vector<String>& arguments = GetArguments();
  50. if (arguments.Contains("-toolbootstrap"))
  51. ToolEnvironment::SetBootstrapping();
  52. engineParameters_["Headless"] = true;
  53. engineParameters_["LogLevel"] = LOG_INFO;
  54. for (unsigned i = 0; i < arguments.Size(); i++)
  55. {
  56. if (arguments[i].Length() > 1 && arguments[i][0] == '-')
  57. {
  58. String argument = arguments[i].Substring(1).ToLower();
  59. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  60. if (argument == "toolbootstrap")
  61. {
  62. ToolEnvironment::SetBootstrapping();
  63. }
  64. else if (argument == "loglevel")
  65. {
  66. engineParameters_["LogLevel"] = Variant(VariantType::VAR_INT, value);
  67. i++;
  68. }
  69. }
  70. }
  71. // no default resources, AtomicTool may be run outside of source tree
  72. engineParameters_["ResourcePaths"] = "";
  73. }
  74. void AtomicTool::HandleCommandFinished(StringHash eventType, VariantMap& eventData)
  75. {
  76. GetSubsystem<Engine>()->Exit();
  77. }
  78. void AtomicTool::HandleCommandError(StringHash eventType, VariantMap& eventData)
  79. {
  80. String error = "Command Error";
  81. const String& message = eventData[CommandError::P_MESSAGE].ToString();
  82. if (message.Length())
  83. error = message;
  84. ErrorExit(error);
  85. }
  86. void AtomicTool::HandleLicenseEulaRequired(StringHash eventType, VariantMap& eventData)
  87. {
  88. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  89. }
  90. void AtomicTool::HandleLicenseActivationRequired(StringHash eventType, VariantMap& eventData)
  91. {
  92. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  93. }
  94. void AtomicTool::HandleLicenseSuccess(StringHash eventType, VariantMap& eventData)
  95. {
  96. if (command_.Null())
  97. {
  98. GetSubsystem<Engine>()->Exit();
  99. return;
  100. }
  101. command_->Run();
  102. }
  103. void AtomicTool::HandleLicenseError(StringHash eventType, VariantMap& eventData)
  104. {
  105. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  106. }
  107. void AtomicTool::HandleLicenseActivationError(StringHash eventType, VariantMap& eventData)
  108. {
  109. String message = eventData[LicenseActivationError::P_MESSAGE].ToString();
  110. ErrorExit(message);
  111. }
  112. void AtomicTool::HandleLicenseActivationSuccess(StringHash eventType, VariantMap& eventData)
  113. {
  114. ATOMIC_LOGRAW("\nActivation successful, thank you!\n\n");
  115. GetSubsystem<Engine>()->Exit();
  116. }
  117. void AtomicTool::DoActivation()
  118. {
  119. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  120. licenseSystem->LicenseAgreementConfirmed();
  121. SubscribeToEvent(E_LICENSE_ACTIVATIONERROR, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationError));
  122. SubscribeToEvent(E_LICENSE_ACTIVATIONSUCCESS, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationSuccess));
  123. }
  124. void AtomicTool::HandleLicenseDeactivationError(StringHash eventType, VariantMap& eventData)
  125. {
  126. String message = eventData[LicenseDeactivationError::P_MESSAGE].ToString();
  127. ErrorExit(message);
  128. }
  129. void AtomicTool::HandleLicenseDeactivationSuccess(StringHash eventType, VariantMap& eventData)
  130. {
  131. ATOMIC_LOGRAW("\nDeactivation successful\n\n");
  132. GetSubsystem<Engine>()->Exit();
  133. }
  134. void AtomicTool::DoDeactivation()
  135. {
  136. }
  137. void AtomicTool::Start()
  138. {
  139. // Subscribe to events
  140. SubscribeToEvent(E_COMMANDERROR, ATOMIC_HANDLER(AtomicTool, HandleCommandError));
  141. SubscribeToEvent(E_COMMANDFINISHED, ATOMIC_HANDLER(AtomicTool, HandleCommandFinished));
  142. SubscribeToEvent(E_LICENSE_EULAREQUIRED, ATOMIC_HANDLER(AtomicTool, HandleLicenseEulaRequired));
  143. SubscribeToEvent(E_LICENSE_ACTIVATIONREQUIRED, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationRequired));
  144. SubscribeToEvent(E_LICENSE_ERROR, ATOMIC_HANDLER(AtomicTool, HandleLicenseError));
  145. SubscribeToEvent(E_LICENSE_SUCCESS, ATOMIC_HANDLER(AtomicTool, HandleLicenseSuccess));
  146. const Vector<String>& arguments = GetArguments();
  147. ToolSystem* tsystem = new ToolSystem(context_);
  148. context_->RegisterSubsystem(tsystem);
  149. ToolEnvironment* env = new ToolEnvironment(context_);
  150. context_->RegisterSubsystem(env);
  151. // Initialize the ToolEnvironment
  152. if (!env->Initialize(true))
  153. {
  154. ErrorExit("Unable to initialize tool environment");
  155. return;
  156. }
  157. if (activationKey_.Length())
  158. {
  159. DoActivation();
  160. return;
  161. } else if (deactivate_)
  162. {
  163. DoDeactivation();
  164. return;
  165. }
  166. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  167. SharedPtr<CommandParser> parser(new CommandParser(context_));
  168. SharedPtr<Command> cmd(parser->Parse(arguments));
  169. if (!cmd)
  170. {
  171. String error = "No command found";
  172. if (parser->GetErrorMessage().Length())
  173. error = parser->GetErrorMessage();
  174. ErrorExit(error);
  175. return;
  176. }
  177. if (cmd->RequiresProjectLoad())
  178. {
  179. if (!cmd->LoadProject())
  180. {
  181. ErrorExit(ToString("Failed to load project: %s", cmd->GetProjectPath().CString()));
  182. return;
  183. }
  184. String projectPath = cmd->GetProjectPath();
  185. // Set the build path
  186. String buildFolder = projectPath + "/" + "Build";
  187. buildSystem->SetBuildPath(buildFolder);
  188. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  189. if (!fileSystem->DirExists(buildFolder))
  190. {
  191. fileSystem->CreateDir(buildFolder);
  192. if (!fileSystem->DirExists(buildFolder))
  193. {
  194. ErrorExit(ToString("Failed to create build folder: %s", buildFolder.CString()));
  195. return;
  196. }
  197. }
  198. }
  199. command_ = cmd;
  200. // BEGIN LICENSE MANAGEMENT
  201. if (cmd->RequiresLicenseValidation())
  202. {
  203. GetSubsystem<LicenseSystem>()->Initialize();
  204. }
  205. else
  206. {
  207. if (command_.Null())
  208. {
  209. GetSubsystem<Engine>()->Exit();
  210. return;
  211. }
  212. command_->Run();
  213. }
  214. // END LICENSE MANAGEMENT
  215. }
  216. void AtomicTool::Stop()
  217. {
  218. }
  219. void AtomicTool::ErrorExit(const String& message)
  220. {
  221. engine_->Exit(); // Close the rendering window
  222. exitCode_ = EXIT_FAILURE;
  223. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  224. if (!message.Length())
  225. {
  226. #ifdef WIN32
  227. Atomic::ErrorExit(startupErrors_.Length() ? startupErrors_ :
  228. "Application has been terminated due to unexpected error.", exitCode_);
  229. #endif
  230. }
  231. else
  232. Atomic::ErrorExit(message, exitCode_);
  233. }
  234. }