AtomicTool.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include <Atomic/Core/ProcessUtils.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/IO/FileSystem.h>
  4. #include <Atomic/Engine/Engine.h>
  5. #include <Atomic/Resource/ResourceCache.h>
  6. #include <ToolCore/ToolSystem.h>
  7. #include <ToolCore/ToolEnvironment.h>
  8. #include <ToolCore/Build/BuildSystem.h>
  9. #include <ToolCore/License/LicenseEvents.h>
  10. #include <ToolCore/License/LicenseSystem.h>
  11. #include <ToolCore/Command/Command.h>
  12. #include <ToolCore/Command/CommandParser.h>
  13. #include "AtomicTool.h"
  14. DEFINE_APPLICATION_MAIN(AtomicTool::AtomicTool);
  15. using namespace ToolCore;
  16. namespace AtomicTool
  17. {
  18. AtomicTool::AtomicTool(Context* context) :
  19. Application(context),
  20. deactivate_(false)
  21. {
  22. }
  23. AtomicTool::~AtomicTool()
  24. {
  25. }
  26. void AtomicTool::Setup()
  27. {
  28. const Vector<String>& arguments = GetArguments();
  29. for (unsigned i = 0; i < arguments.Size(); ++i)
  30. {
  31. if (arguments[i].Length() > 1)
  32. {
  33. String argument = arguments[i].ToLower();
  34. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  35. if (argument == "--cli-data-path")
  36. {
  37. if (!value.Length())
  38. ErrorExit("Unable to parse --cli-data-path");
  39. cliDataPath_ = AddTrailingSlash(value);
  40. }
  41. else if (argument == "--activate")
  42. {
  43. if (!value.Length())
  44. ErrorExit("Unable to parse --activation product key");
  45. activationKey_ = value;
  46. }
  47. else if (argument == "--deactivate")
  48. {
  49. deactivate_ = true;
  50. }
  51. }
  52. }
  53. engineParameters_["Headless"] = true;
  54. engineParameters_["LogLevel"] = LOG_INFO;
  55. // no default resources (will be initialized later)
  56. engineParameters_["ResourcePaths"] = "";
  57. }
  58. void AtomicTool::HandleCommandFinished(StringHash eventType, VariantMap& eventData)
  59. {
  60. GetSubsystem<Engine>()->Exit();
  61. }
  62. void AtomicTool::HandleCommandError(StringHash eventType, VariantMap& eventData)
  63. {
  64. String error = "Command Error";
  65. const String& message = eventData[CommandError::P_MESSAGE].ToString();
  66. if (message.Length())
  67. error = message;
  68. ErrorExit(error);
  69. }
  70. void AtomicTool::HandleLicenseEulaRequired(StringHash eventType, VariantMap& eventData)
  71. {
  72. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  73. }
  74. void AtomicTool::HandleLicenseActivationRequired(StringHash eventType, VariantMap& eventData)
  75. {
  76. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  77. }
  78. void AtomicTool::HandleLicenseSuccess(StringHash eventType, VariantMap& eventData)
  79. {
  80. if (command_.Null())
  81. {
  82. GetSubsystem<Engine>()->Exit();
  83. return;
  84. }
  85. command_->Run();
  86. }
  87. void AtomicTool::HandleLicenseError(StringHash eventType, VariantMap& eventData)
  88. {
  89. ErrorExit("\nActivation Required: Please run: atomic-cli activate\n");
  90. }
  91. void AtomicTool::HandleLicenseActivationError(StringHash eventType, VariantMap& eventData)
  92. {
  93. String message = eventData[LicenseActivationError::P_MESSAGE].ToString();
  94. ErrorExit(message);
  95. }
  96. void AtomicTool::HandleLicenseActivationSuccess(StringHash eventType, VariantMap& eventData)
  97. {
  98. LOGRAW("\nActivation successful, thank you!\n\n");
  99. GetSubsystem<Engine>()->Exit();
  100. }
  101. void AtomicTool::DoActivation()
  102. {
  103. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  104. if (!licenseSystem->ValidateKey(activationKey_))
  105. {
  106. ErrorExit(ToString("\nProduct key \"%s\" is invalid, keys are in the form ATOMIC-XXXX-XXXX-XXXX-XXXX\n", activationKey_.CString()));
  107. return;
  108. }
  109. licenseSystem->LicenseAgreementConfirmed();
  110. SubscribeToEvent(E_LICENSE_ACTIVATIONERROR, HANDLER(AtomicTool, HandleLicenseActivationError));
  111. SubscribeToEvent(E_LICENSE_ACTIVATIONSUCCESS, HANDLER(AtomicTool, HandleLicenseActivationSuccess));
  112. licenseSystem->RequestServerActivation(activationKey_);
  113. }
  114. void AtomicTool::HandleLicenseDeactivationError(StringHash eventType, VariantMap& eventData)
  115. {
  116. String message = eventData[LicenseDeactivationError::P_MESSAGE].ToString();
  117. ErrorExit(message);
  118. }
  119. void AtomicTool::HandleLicenseDeactivationSuccess(StringHash eventType, VariantMap& eventData)
  120. {
  121. LOGRAW("\nDeactivation successful\n\n");
  122. GetSubsystem<Engine>()->Exit();
  123. }
  124. void AtomicTool::DoDeactivation()
  125. {
  126. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  127. if (!licenseSystem->LoadLicense())
  128. {
  129. ErrorExit("\nNot activated");
  130. return;
  131. }
  132. if (!licenseSystem->Deactivate())
  133. {
  134. ErrorExit("\nNot activated\n");
  135. return;
  136. }
  137. SubscribeToEvent(E_LICENSE_DEACTIVATIONERROR, HANDLER(AtomicTool, HandleLicenseDeactivationError));
  138. SubscribeToEvent(E_LICENSE_DEACTIVATIONSUCCESS, HANDLER(AtomicTool, HandleLicenseDeactivationSuccess));
  139. }
  140. void AtomicTool::Start()
  141. {
  142. // Subscribe to events
  143. SubscribeToEvent(E_COMMANDERROR, HANDLER(AtomicTool, HandleCommandError));
  144. SubscribeToEvent(E_COMMANDFINISHED, HANDLER(AtomicTool, HandleCommandFinished));
  145. SubscribeToEvent(E_LICENSE_EULAREQUIRED, HANDLER(AtomicTool, HandleLicenseEulaRequired));
  146. SubscribeToEvent(E_LICENSE_ACTIVATIONREQUIRED, HANDLER(AtomicTool, HandleLicenseActivationRequired));
  147. SubscribeToEvent(E_LICENSE_ERROR, HANDLER(AtomicTool, HandleLicenseError));
  148. SubscribeToEvent(E_LICENSE_SUCCESS, HANDLER(AtomicTool, HandleLicenseSuccess));
  149. const Vector<String>& arguments = GetArguments();
  150. ToolSystem* tsystem = new ToolSystem(context_);
  151. context_->RegisterSubsystem(tsystem);
  152. ToolEnvironment* env = new ToolEnvironment(context_);
  153. context_->RegisterSubsystem(env);
  154. //#ifdef ATOMIC_DEV_BUILD
  155. if (!env->InitFromJSON())
  156. {
  157. ErrorExit(ToString("Unable to initialize tool environment from %s", env->GetDevConfigFilename().CString()));
  158. return;
  159. }
  160. if (!cliDataPath_.Length())
  161. {
  162. cliDataPath_ = env->GetRootSourceDir() + "/Resources/";
  163. }
  164. //#endif
  165. ResourceCache* cache = GetSubsystem<ResourceCache>();
  166. cache->AddResourceDir(env->GetCoreDataDir());
  167. cache->AddResourceDir(env->GetPlayerDataDir());
  168. tsystem->SetCLI();
  169. tsystem->SetDataPath(cliDataPath_);
  170. if (activationKey_.Length())
  171. {
  172. DoActivation();
  173. return;
  174. } else if (deactivate_)
  175. {
  176. DoDeactivation();
  177. return;
  178. }
  179. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  180. SharedPtr<CommandParser> parser(new CommandParser(context_));
  181. SharedPtr<Command> cmd(parser->Parse(arguments));
  182. if (!cmd)
  183. {
  184. String error = "No command found";
  185. if (parser->GetErrorMessage().Length())
  186. error = parser->GetErrorMessage();
  187. ErrorExit(error);
  188. return;
  189. }
  190. if (cmd->RequiresProjectLoad())
  191. {
  192. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  193. String projectDirectory = fileSystem->GetCurrentDir();
  194. Vector<String> projectFiles;
  195. fileSystem->ScanDir(projectFiles, projectDirectory, "*.atomic", SCAN_FILES, false);
  196. if (!projectFiles.Size())
  197. {
  198. ErrorExit(ToString("No .atomic project file in %s", projectDirectory.CString()));
  199. return;
  200. }
  201. else if (projectFiles.Size() > 1)
  202. {
  203. ErrorExit(ToString("Multiple .atomic project files found in %s", projectDirectory.CString()));
  204. return;
  205. }
  206. String projectFile = projectDirectory + "/" + projectFiles[0];
  207. if (!tsystem->LoadProject(projectFile))
  208. {
  209. //ErrorExit(ToString("Failed to load project: %s", projectFile.CString()));
  210. //return;
  211. }
  212. // Set the build path
  213. String buildFolder = projectDirectory + "/" + "Build";
  214. buildSystem->SetBuildPath(buildFolder);
  215. if (!fileSystem->DirExists(buildFolder))
  216. {
  217. fileSystem->CreateDir(buildFolder);
  218. if (!fileSystem->DirExists(buildFolder))
  219. {
  220. ErrorExit(ToString("Failed to create build folder: %s", buildFolder.CString()));
  221. return;
  222. }
  223. }
  224. }
  225. command_ = cmd;
  226. // BEGIN LICENSE MANAGEMENT
  227. if (cmd->RequiresLicenseValidation())
  228. {
  229. GetSubsystem<LicenseSystem>()->Initialize();
  230. }
  231. else
  232. {
  233. if (command_.Null())
  234. {
  235. GetSubsystem<Engine>()->Exit();
  236. return;
  237. }
  238. command_->Run();
  239. }
  240. // END LICENSE MANAGEMENT
  241. }
  242. void AtomicTool::Stop()
  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. }