AtomicTool.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <ToolCore/ToolSystem.h>
  6. #include <ToolCore/Build/BuildSystem.h>
  7. #include <ToolCore/License/LicenseEvents.h>
  8. #include <ToolCore/License/LicenseSystem.h>
  9. #include <ToolCore/Command/Command.h>
  10. #include <ToolCore/Command/CommandParser.h>
  11. #include "AtomicTool.h"
  12. DEFINE_APPLICATION_MAIN(AtomicTool::AtomicTool);
  13. using namespace ToolCore;
  14. namespace AtomicTool
  15. {
  16. AtomicTool::AtomicTool(Context* context) :
  17. Application(context)
  18. {
  19. }
  20. AtomicTool::~AtomicTool()
  21. {
  22. }
  23. void AtomicTool::Setup()
  24. {
  25. const Vector<String>& arguments = GetArguments();
  26. for (unsigned i = 0; i < arguments.Size(); ++i)
  27. {
  28. if (arguments[i].Length() > 1)
  29. {
  30. String argument = arguments[i].ToLower();
  31. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  32. if (argument == "--cli-data-path")
  33. {
  34. if (!value.Length())
  35. ErrorExit("Unable to parse --cli-data-path");
  36. cliDataPath_ = AddTrailingSlash(value);
  37. }
  38. }
  39. }
  40. if (!cliDataPath_.Length())
  41. ErrorExit("Unable to parse --data-path");
  42. engineParameters_["Headless"] = true;
  43. engineParameters_["LogLevel"] = LOG_INFO;
  44. engineParameters_["ResourcePaths"] = "";
  45. }
  46. void AtomicTool::HandleCommandFinished(StringHash eventType, VariantMap& eventData)
  47. {
  48. GetSubsystem<Engine>()->Exit();
  49. }
  50. void AtomicTool::HandleCommandError(StringHash eventType, VariantMap& eventData)
  51. {
  52. String error = "Command Error";
  53. const String& message = eventData[CommandError::P_MESSAGE].ToString();
  54. if (message.Length())
  55. error = message;
  56. ErrorExit(error);
  57. }
  58. void AtomicTool::HandleLicenseEulaRequired(StringHash eventType, VariantMap& eventData)
  59. {
  60. }
  61. void AtomicTool::HandleLicenseActivationRequired(StringHash eventType, VariantMap& eventData)
  62. {
  63. }
  64. void AtomicTool::HandleLicenseSuccess(StringHash eventType, VariantMap& eventData)
  65. {
  66. command_->Run();
  67. }
  68. void AtomicTool::HandleLicenseError(StringHash eventType, VariantMap& eventData)
  69. {
  70. }
  71. void AtomicTool::Start()
  72. {
  73. // Subscribe to events
  74. SubscribeToEvent(E_COMMANDERROR, HANDLER(AtomicTool, HandleCommandError));
  75. SubscribeToEvent(E_COMMANDFINISHED, HANDLER(AtomicTool, HandleCommandFinished));
  76. SubscribeToEvent(E_LICENSE_EULAREQUIRED, HANDLER(AtomicTool, HandleLicenseEulaRequired));
  77. SubscribeToEvent(E_LICENSE_ACTIVATIONREQUIRED, HANDLER(AtomicTool, HandleLicenseActivationRequired));
  78. SubscribeToEvent(E_LICENSE_ERROR, HANDLER(AtomicTool, HandleLicenseError));
  79. SubscribeToEvent(E_LICENSE_SUCCESS, HANDLER(AtomicTool, HandleLicenseSuccess));
  80. const Vector<String>& arguments = GetArguments();
  81. ToolSystem* tsystem = new ToolSystem(context_);
  82. context_->RegisterSubsystem(tsystem);
  83. tsystem->SetCLI();
  84. tsystem->SetDataPath(cliDataPath_);
  85. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  86. SharedPtr<CommandParser> parser(new CommandParser(context_));
  87. SharedPtr<Command> cmd(parser->Parse(arguments));
  88. if (!cmd)
  89. {
  90. String error = "No command found";
  91. if (parser->GetErrorMessage().Length())
  92. error = parser->GetErrorMessage();
  93. ErrorExit(error);
  94. return;
  95. }
  96. if (cmd->RequiresProjectLoad())
  97. {
  98. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  99. String projectDirectory = fileSystem->GetCurrentDir();
  100. Vector<String> projectFiles;
  101. fileSystem->ScanDir(projectFiles, projectDirectory, "*.atomic", SCAN_FILES, false);
  102. if (!projectFiles.Size())
  103. {
  104. ErrorExit(ToString("No .atomic project file in %s", projectDirectory.CString()));
  105. return;
  106. }
  107. else if (projectFiles.Size() > 1)
  108. {
  109. ErrorExit(ToString("Multiple .atomic project files found in %s", projectDirectory.CString()));
  110. return;
  111. }
  112. String projectFile = projectDirectory + "/" + projectFiles[0];
  113. if (!tsystem->LoadProject(projectFile))
  114. {
  115. ErrorExit(ToString("Failed to load project: %s", projectFile.CString()));
  116. return;
  117. }
  118. // Set the build path
  119. String buildFolder = projectDirectory + "/" + "Build";
  120. buildSystem->SetBuildPath(buildFolder);
  121. if (!fileSystem->DirExists(buildFolder))
  122. {
  123. fileSystem->CreateDir(buildFolder);
  124. if (!fileSystem->DirExists(buildFolder))
  125. {
  126. ErrorExit(ToString("Failed to create build folder: %s", buildFolder.CString()));
  127. return;
  128. }
  129. }
  130. }
  131. command_ = cmd;
  132. // BEGIN LICENSE MANAGEMENT
  133. GetSubsystem<LicenseSystem>()->Initialize();
  134. // END LICENSE MANAGEMENT
  135. }
  136. void AtomicTool::Stop()
  137. {
  138. }
  139. }