Command.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/IO/Log.h>
  23. #include "../ToolSystem.h"
  24. #include "../Project/Project.h"
  25. #include "Command.h"
  26. namespace ToolCore
  27. {
  28. Command::Command(Context* context) : Object(context),
  29. timeOut_(0.0f)
  30. {
  31. }
  32. Command::~Command()
  33. {
  34. }
  35. bool Command::LoadProject()
  36. {
  37. if (!projectPath_.Length())
  38. return false;
  39. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  40. // default to current directly if command doesn't provide the path
  41. if (!projectPath_.Length())
  42. projectPath_ = fileSystem->GetCurrentDir();
  43. String projectFile;
  44. if (projectPath_.EndsWith(".atomic", false))
  45. {
  46. projectFile = projectPath_;
  47. projectPath_ = GetPath(projectPath_);
  48. }
  49. else
  50. {
  51. Vector<String> projectFiles;
  52. fileSystem->ScanDir(projectFiles, projectPath_, "*.atomic", SCAN_FILES, false);
  53. if (!projectFiles.Size())
  54. {
  55. ATOMIC_LOGERRORF("No .atomic project file in %s", projectPath_.CString());
  56. return false;
  57. }
  58. else if (projectFiles.Size() > 1)
  59. {
  60. ATOMIC_LOGERRORF("Multiple .atomic project files found in %s", projectPath_.CString());
  61. return false;
  62. }
  63. projectFile = projectPath_ + "/" + projectFiles[0];
  64. }
  65. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  66. if (!tsystem->LoadProject(projectFile))
  67. {
  68. ATOMIC_LOGERRORF("Failed to load project: %s", projectFile.CString());
  69. return false;
  70. }
  71. return true;
  72. }
  73. void Command::ParseCommonArguments(const Vector<String> &arguments, String &errorMsg)
  74. {
  75. for (unsigned i = 0; i < arguments.Size(); ++i)
  76. {
  77. if (arguments[i].Length() > 1 && arguments[i][0] == '-')
  78. {
  79. String argument = arguments[i].Substring(1).ToLower();
  80. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  81. // eat additonal argument '-'
  82. while (argument.StartsWith("-"))
  83. {
  84. argument.Erase(0);
  85. }
  86. if (argument == "project" && value.Length())
  87. {
  88. projectPath_ = value;
  89. }
  90. }
  91. }
  92. }
  93. bool Command::Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  94. {
  95. errorMsg = String::EMPTY;
  96. ParseCommonArguments(arguments, errorMsg);
  97. if (errorMsg.Length())
  98. {
  99. return false;
  100. }
  101. return ParseInternal(arguments, startIndex, errorMsg);
  102. }
  103. void Command::Error(const String& errorMsg)
  104. {
  105. VariantMap eventData;
  106. eventData[CommandError::P_MESSAGE] = errorMsg;
  107. SendEvent(E_COMMANDERROR, eventData);
  108. }
  109. void Command::Finished()
  110. {
  111. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  112. Project* project = tsystem->GetProject();
  113. if (project && project->IsDirty())
  114. project->Save(project->GetProjectFilePath());
  115. SendEvent(E_COMMANDFINISHED);
  116. }
  117. }