ImportCmd.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/StringUtils.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include "../ToolSystem.h"
  26. #include "../Project/Project.h"
  27. #include "../Import/JSONSceneImporter.h"
  28. #include "../Import/JSONSceneProcess.h"
  29. #include "../Import/OpenAssetImporter.h"
  30. #include "ImportCmd.h"
  31. #include <Poco/File.h>
  32. namespace ToolCore
  33. {
  34. ImportCmd::ImportCmd(Context* context) : Command(context)
  35. {
  36. }
  37. ImportCmd::~ImportCmd()
  38. {
  39. }
  40. bool ImportCmd::ParseInternal(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
  41. {
  42. String argument = arguments[startIndex].ToLower();
  43. String value = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1] : String::EMPTY;
  44. if (argument != "import")
  45. {
  46. errorMsg = "Unable to parse import command";
  47. return false;
  48. }
  49. if (!value.Length())
  50. {
  51. errorMsg = "Unable to parse source JSON filename";
  52. return false;
  53. }
  54. assetFilename_ = value;
  55. return true;
  56. }
  57. void ImportCmd::Run()
  58. {
  59. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  60. Project* project = tsystem->GetProject();
  61. String resourcePath = project->GetResourcePath();
  62. String ext = GetExtension(assetFilename_);
  63. if (ext == ".json")
  64. {
  65. Poco::File file(assetFilename_.CString());
  66. if (!file.exists())
  67. {
  68. Error(ToString("JSON source scene does not exist: %s", assetFilename_.CString()));
  69. return;
  70. }
  71. ATOMIC_LOGRAWF("Importing JSON: %s", assetFilename_.CString());
  72. SharedPtr<JSONSceneImporter> jimporter;
  73. jimporter = new JSONSceneImporter(context_);
  74. jimporter->Import(assetFilename_);
  75. SharedPtr<JSONSceneProcess> sceneProcess;
  76. sceneProcess = new JSONSceneProcess(context_, jimporter);
  77. sceneProcess->Process(resourcePath);
  78. sceneProcess->Write();
  79. }
  80. else
  81. {
  82. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  83. if (importer->Load(assetFilename_))
  84. {
  85. importer->ExportModel("/Users/josh/Desktop/ExportedModel.mdl");
  86. }
  87. }
  88. Finished();
  89. }
  90. }