AtomicTool.cpp 3.4 KB

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