AtomicTool.cpp 4.0 KB

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