AtomicTool.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. for (unsigned i = 0; i < arguments.Size(); ++i)
  51. {
  52. if (arguments[i].Length() > 1)
  53. {
  54. String argument = arguments[i].ToLower();
  55. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  56. if (argument == "--cli-data-path")
  57. {
  58. if (!value.Length())
  59. ErrorExit("Unable to parse --cli-data-path");
  60. cliDataPath_ = AddTrailingSlash(value);
  61. }
  62. else if (argument == "--activate")
  63. {
  64. if (!value.Length())
  65. ErrorExit("Unable to parse --activation product key");
  66. activationKey_ = value;
  67. }
  68. else if (argument == "--deactivate")
  69. {
  70. deactivate_ = true;
  71. }
  72. }
  73. }
  74. engineParameters_["Headless"] = true;
  75. engineParameters_["LogLevel"] = LOG_INFO;
  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. if (!licenseSystem->ValidateKey(activationKey_))
  126. {
  127. ErrorExit(ToString("\nProduct key \"%s\" is invalid, keys are in the form ATOMIC-XXXX-XXXX-XXXX-XXXX\n", activationKey_.CString()));
  128. return;
  129. }
  130. licenseSystem->LicenseAgreementConfirmed();
  131. SubscribeToEvent(E_LICENSE_ACTIVATIONERROR, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationError));
  132. SubscribeToEvent(E_LICENSE_ACTIVATIONSUCCESS, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationSuccess));
  133. licenseSystem->RequestServerActivation(activationKey_);
  134. }
  135. void AtomicTool::HandleLicenseDeactivationError(StringHash eventType, VariantMap& eventData)
  136. {
  137. String message = eventData[LicenseDeactivationError::P_MESSAGE].ToString();
  138. ErrorExit(message);
  139. }
  140. void AtomicTool::HandleLicenseDeactivationSuccess(StringHash eventType, VariantMap& eventData)
  141. {
  142. ATOMIC_LOGRAW("\nDeactivation successful\n\n");
  143. GetSubsystem<Engine>()->Exit();
  144. }
  145. void AtomicTool::DoDeactivation()
  146. {
  147. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  148. if (!licenseSystem->LoadLicense())
  149. {
  150. ErrorExit("\nNot activated");
  151. return;
  152. }
  153. if (!licenseSystem->Deactivate())
  154. {
  155. ErrorExit("\nNot activated\n");
  156. return;
  157. }
  158. SubscribeToEvent(E_LICENSE_DEACTIVATIONERROR, ATOMIC_HANDLER(AtomicTool, HandleLicenseDeactivationError));
  159. SubscribeToEvent(E_LICENSE_DEACTIVATIONSUCCESS, ATOMIC_HANDLER(AtomicTool, HandleLicenseDeactivationSuccess));
  160. }
  161. void AtomicTool::Start()
  162. {
  163. // Subscribe to events
  164. SubscribeToEvent(E_COMMANDERROR, ATOMIC_HANDLER(AtomicTool, HandleCommandError));
  165. SubscribeToEvent(E_COMMANDFINISHED, ATOMIC_HANDLER(AtomicTool, HandleCommandFinished));
  166. SubscribeToEvent(E_LICENSE_EULAREQUIRED, ATOMIC_HANDLER(AtomicTool, HandleLicenseEulaRequired));
  167. SubscribeToEvent(E_LICENSE_ACTIVATIONREQUIRED, ATOMIC_HANDLER(AtomicTool, HandleLicenseActivationRequired));
  168. SubscribeToEvent(E_LICENSE_ERROR, ATOMIC_HANDLER(AtomicTool, HandleLicenseError));
  169. SubscribeToEvent(E_LICENSE_SUCCESS, ATOMIC_HANDLER(AtomicTool, HandleLicenseSuccess));
  170. const Vector<String>& arguments = GetArguments();
  171. ToolSystem* tsystem = new ToolSystem(context_);
  172. context_->RegisterSubsystem(tsystem);
  173. ToolEnvironment* env = new ToolEnvironment(context_);
  174. context_->RegisterSubsystem(env);
  175. //#ifdef ATOMIC_DEV_BUILD
  176. if (!env->InitFromJSON())
  177. {
  178. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  179. return;
  180. }
  181. if (!cliDataPath_.Length())
  182. {
  183. cliDataPath_ = env->GetRootSourceDir() + "/Resources/";
  184. }
  185. //#endif
  186. tsystem->SetCLI();
  187. tsystem->SetDataPath(cliDataPath_);
  188. if (activationKey_.Length())
  189. {
  190. DoActivation();
  191. return;
  192. } else if (deactivate_)
  193. {
  194. DoDeactivation();
  195. return;
  196. }
  197. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  198. SharedPtr<CommandParser> parser(new CommandParser(context_));
  199. SharedPtr<Command> cmd(parser->Parse(arguments));
  200. if (!cmd)
  201. {
  202. String error = "No command found";
  203. if (parser->GetErrorMessage().Length())
  204. error = parser->GetErrorMessage();
  205. ErrorExit(error);
  206. return;
  207. }
  208. if (cmd->RequiresProjectLoad())
  209. {
  210. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  211. String projectDirectory = cmd->GetProjectPath();
  212. // default to current directly if command doesn't provide the path
  213. if (!projectDirectory.Length())
  214. projectDirectory = fileSystem->GetCurrentDir();
  215. Vector<String> projectFiles;
  216. fileSystem->ScanDir(projectFiles, projectDirectory, "*.atomic", SCAN_FILES, false);
  217. if (!projectFiles.Size())
  218. {
  219. ErrorExit(ToString("No .atomic project file in %s", projectDirectory.CString()));
  220. return;
  221. }
  222. else if (projectFiles.Size() > 1)
  223. {
  224. ErrorExit(ToString("Multiple .atomic project files found in %s", projectDirectory.CString()));
  225. return;
  226. }
  227. String projectFile = projectDirectory + "/" + projectFiles[0];
  228. if (!tsystem->LoadProject(projectFile))
  229. {
  230. //ErrorExit(ToString("Failed to load project: %s", projectFile.CString()));
  231. //return;
  232. }
  233. // Set the build path
  234. String buildFolder = projectDirectory + "/" + "Build";
  235. buildSystem->SetBuildPath(buildFolder);
  236. if (!fileSystem->DirExists(buildFolder))
  237. {
  238. fileSystem->CreateDir(buildFolder);
  239. if (!fileSystem->DirExists(buildFolder))
  240. {
  241. ErrorExit(ToString("Failed to create build folder: %s", buildFolder.CString()));
  242. return;
  243. }
  244. }
  245. }
  246. command_ = cmd;
  247. // BEGIN LICENSE MANAGEMENT
  248. if (cmd->RequiresLicenseValidation())
  249. {
  250. GetSubsystem<LicenseSystem>()->Initialize();
  251. }
  252. else
  253. {
  254. if (command_.Null())
  255. {
  256. GetSubsystem<Engine>()->Exit();
  257. return;
  258. }
  259. command_->Run();
  260. }
  261. // END LICENSE MANAGEMENT
  262. }
  263. void AtomicTool::Stop()
  264. {
  265. }
  266. void AtomicTool::ErrorExit(const String& message)
  267. {
  268. engine_->Exit(); // Close the rendering window
  269. exitCode_ = EXIT_FAILURE;
  270. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  271. if (!message.Length())
  272. {
  273. #ifdef WIN32
  274. Atomic::ErrorExit(startupErrors_.Length() ? startupErrors_ :
  275. "Application has been terminated due to unexpected error.", exitCode_);
  276. #endif
  277. }
  278. else
  279. Atomic::ErrorExit(message, exitCode_);
  280. }
  281. }