AtomicTool.cpp 9.1 KB

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