AtomicTool.cpp 8.4 KB

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