AtomicTool.cpp 9.4 KB

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