AtomicTool.cpp 10.0 KB

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