Browse Source

Adding Import command to ToolCore

Josh Engebretson 10 years ago
parent
commit
8d382b3368

+ 3 - 2
Source/AtomicTool/AtomicTool.cpp

@@ -70,8 +70,9 @@ void AtomicTool::Setup()
         ErrorExit("Unable to parse --data-path");
 
     engineParameters_["Headless"] = true;
-    engineParameters_["LogLevel"] = LOG_WARNING;
-    engineParameters_["ResourcePaths"] = "";
+    engineParameters_["LogLevel"] = LOG_INFO;
+    // FIXME
+    engineParameters_["ResourcePaths"] = "/Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicPlayer/Resources/CoreData";
 }
 
 void AtomicTool::HandleCommandFinished(StringHash eventType, VariantMap& eventData)

+ 5 - 0
Source/ToolCore/Command/CommandParser.cpp

@@ -4,6 +4,7 @@
 #include "NewProjectCmd.h"
 #include "PlatformAddCmd.h"
 #include "BuildCmd.h"
+#include "ImportCmd.h"
 
 namespace ToolCore
 {
@@ -40,6 +41,10 @@ Command* CommandParser::Parse(const Vector<String>& arguments)
             {
                 cmd = new PlatformAddCmd(context_);
             }
+            else if (argument == "import")
+            {
+                cmd = new ImportCmd(context_);
+            }
 
         }
 

+ 81 - 0
Source/ToolCore/Command/ImportCmd.cpp

@@ -0,0 +1,81 @@
+
+#include <Atomic/Core/StringUtils.h>
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/File.h>
+
+#include "../ToolSystem.h"
+#include "../Project/Project.h"
+
+#include "../Import/JSONSceneImporter.h"
+#include "../Import/JSONSceneProcess.h"
+
+#include "ImportCmd.h"
+
+#include <Poco/File.h>
+
+namespace ToolCore
+{
+
+ImportCmd::ImportCmd(Context* context) : Command(context)
+{
+
+}
+
+ImportCmd::~ImportCmd()
+{
+
+}
+
+bool ImportCmd::Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg)
+{
+    String argument = arguments[startIndex].ToLower();
+    String value = startIndex + 1 < arguments.Size() ? arguments[startIndex + 1] : String::EMPTY;
+
+    if (argument != "import")
+    {
+        errorMsg = "Unable to parse import command";
+        return false;
+    }
+
+    if (!value.Length())
+    {
+        errorMsg = "Unable to parse source JSON filename";
+        return false;
+    }
+
+    sourceJSONFilename_ = value;
+
+    return true;
+}
+
+void ImportCmd::Run()
+{
+
+    Poco::File file(sourceJSONFilename_.CString());
+
+    if (!file.exists())
+    {
+        Error(ToString("JSON source scene does not exist: %s", sourceJSONFilename_.CString()));
+        return;
+    }
+
+    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
+    Project* project = tsystem->GetProject();
+
+    String resourcePath = project->GetResourcePath();
+
+    LOGRAWF("Importing: %s", sourceJSONFilename_.CString());
+
+    SharedPtr<JSONSceneImporter> jimporter;
+    jimporter = new JSONSceneImporter(context_);
+    jimporter->Import(sourceJSONFilename_);
+
+    SharedPtr<JSONSceneProcess> sceneProcess;
+    sceneProcess = new JSONSceneProcess(context_, jimporter);
+    sceneProcess->Process(resourcePath);
+    sceneProcess->Write();
+
+    Finished();
+}
+
+}

+ 30 - 0
Source/ToolCore/Command/ImportCmd.h

@@ -0,0 +1,30 @@
+
+#pragma once
+
+#include "Command.h"
+
+using namespace Atomic;
+
+namespace ToolCore
+{
+
+class ImportCmd: public Command
+{
+    OBJECT(ImportCmd);
+
+public:
+
+    ImportCmd(Context* context);
+    virtual ~ImportCmd();
+
+    bool Parse(const Vector<String>& arguments, unsigned startIndex, String& errorMsg);
+
+    void Run();
+
+private:
+
+    String sourceJSONFilename_;
+
+};
+
+}

+ 1 - 1
Source/ToolCore/Import/JSONSceneImporter.cpp

@@ -13,7 +13,7 @@
 
 using namespace rapidjson;
 
-namespace AtomicEditor
+namespace ToolCore
 {
 
 static unsigned char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

+ 1 - 1
Source/ToolCore/Import/JSONSceneImporter.h

@@ -21,7 +21,7 @@ typedef GenericDocument<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Document
 }
 
 
-namespace AtomicEditor
+namespace ToolCore
 {
 
 class JSONSceneImporter;

+ 27 - 18
Source/ToolCore/Import/JSONSceneProcess.cpp

@@ -7,6 +7,7 @@
 #include "AtomicEditor.h"
 #include <Atomic/Core/ProcessUtils.h>
 #include <Atomic/IO/Log.h>
+#include <Atomic/IO/FileSystem.h>
 
 #include <Atomic/Resource/Image.h>
 
@@ -30,9 +31,7 @@
 #include "JSONSceneProcess.h"
 #include "JSONSceneImporter.h"
 
-static String __rootFolder = "/Users/josh/Desktop/Test/Resources";
-
-namespace AtomicEditor
+namespace ToolCore
 {
 
 bool JSONSceneProcess::ProcessTextures()
@@ -46,7 +45,7 @@ bool JSONSceneProcess::ProcessTextures()
         unsigned length;
         const unsigned char* pixels = jtexture->GetPNGPixels(length);
 
-        String filename = __rootFolder + "/Textures/" + jtexture->GetName() + ".png";
+        String filename = resourcePath_ + "Textures/" + jtexture->GetName() + ".png";
 
         SharedPtr<File> file (new File(context_, filename, FILE_WRITE));
 
@@ -70,7 +69,7 @@ bool JSONSceneProcess::ProcessLightmaps()
         unsigned length;
         const unsigned char* pixels = jlightmap->GetPNGPixels(length);
 
-        String filename = __rootFolder + "/Textures/" + jlightmap->GetName() + ".png";
+        String filename = resourcePath_ + "Textures/" + jlightmap->GetName() + ".png";
 
         SharedPtr<File> file (new File(context_, filename, FILE_WRITE));
 
@@ -160,7 +159,7 @@ bool JSONSceneProcess::WriteMaterials()
     {
         Material* material = itr->second_;
         SharedPtr<File> file;
-        file = new File(context_, __rootFolder + "/" + material->GetName(), FILE_WRITE);
+        file = new File(context_, resourcePath_ + material->GetName(), FILE_WRITE);
         material->Save(*file);
         itr++;
     }
@@ -175,7 +174,7 @@ bool JSONSceneProcess::WriteModels()
     {
         Model* model = itr->second_;
         SharedPtr<File> file;
-        file = new File(context_, __rootFolder + "/" + model->GetName(), FILE_WRITE);
+        file = new File(context_, resourcePath_ + model->GetName(), FILE_WRITE);
         model->Save(*file);
         itr++;
     }
@@ -202,12 +201,12 @@ bool JSONSceneProcess::WriteHierarchy(Scene* scene)
 
     filename.AppendWithFormat("%s", importer_->GetSceneName().CString());
 
-    if (!useXML)
+    //if (!useXML)
         filename += ".scene";
-    else
-        filename += ".xml";
+    //else
+        //filename += ".xml";
 
-    filename = __rootFolder + "/Scenes/" + filename;
+    filename = resourcePath_ + "Scenes/" + filename;
 
     if (!file.Open(filename, FILE_WRITE))
         ErrorExit("Could not open output file: Scenes/Test.bin");
@@ -621,7 +620,7 @@ bool JSONSceneProcess::ProcessComponent(Node* node, const JSONTerrain* jterrain)
     image->SetSize(heightmapWidth, heightmapHeight, 4);
     image->SetData(&bytes[0]);
 
-    String heightMapPath = __rootFolder + "/Textures/TerrainHeightMap.png";
+    String heightMapPath = resourcePath_ + "Textures/TerrainHeightMap.png";
     image->SavePNG(heightMapPath);
 
     int alphamapWidth = jterrain->GetAlphaMapHeight();
@@ -658,7 +657,7 @@ bool JSONSceneProcess::ProcessComponent(Node* node, const JSONTerrain* jterrain)
     alphaWeights->SetSize(alphamapWidth, alphamapHeight, 4);
     alphaWeights->SetData(&bytes[0]);
 
-    String alphaWeightsPath = __rootFolder + "/Textures/TerrainWeights.png";
+    String alphaWeightsPath = resourcePath_ + "Textures/TerrainWeights.png";
     alphaWeights->SavePNG(alphaWeightsPath);
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -760,16 +759,15 @@ bool JSONSceneProcess::ProcessComponent(Node* node, const JSONAnimation* janim )
 
         outAnim->SetTracks(tracks);
 
-        String filename = __rootFolder;
+        String filename = resourcePath_;
 
-        filename.AppendWithFormat("/Models/AS_FatZombie_FBX_FatZombie_LOD0_%s.ani", clip->name_.CString());
+        filename.AppendWithFormat("Models/FIXME_ANIMATION_PROCESSNAME_%s.ani", clip->name_.CString());
 
         File outFile(context_);
         if (!outFile.Open(filename, FILE_WRITE))
             ErrorExit("Could not open output file for animation");
         outAnim->Save(outFile);
 
-
     }
 
 
@@ -952,10 +950,11 @@ bool JSONSceneProcess::ProcessHierarchy(Scene* scene)
     return true;
 }
 
-bool JSONSceneProcess::Process()
+bool JSONSceneProcess::Process(const String &resourcePath)
 {
+    resourcePath_ = resourcePath;
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    cache->AddResourceDir(__rootFolder);
+    cache->AddResourceDir(resourcePath_);
     scene_ = new Scene(context_);
     scene_->CreateComponent<PhysicsWorld>();
     scene_->CreateComponent<Octree>();
@@ -977,6 +976,16 @@ bool JSONSceneProcess::Process()
 
 bool JSONSceneProcess::Write()
 {
+    FileSystem* fs = GetSubsystem<FileSystem>();
+
+    if (!fs->DirExists(resourcePath_ + "Scenes"))
+        fs->CreateDir(resourcePath_ + "Scenes");
+    if (!fs->DirExists(resourcePath_ + "Models"))
+        fs->CreateDir(resourcePath_ + "Models");
+    if (!fs->DirExists(resourcePath_ + "Materials"))
+        fs->CreateDir(resourcePath_ + "Materials");
+    if (!fs->DirExists(resourcePath_ + "Textures"))
+        fs->CreateDir(resourcePath_ + "Textures");
 
     WriteMaterials();
     WriteModels();

+ 4 - 2
Source/ToolCore/Import/JSONSceneProcess.h

@@ -16,7 +16,7 @@
 
 using namespace Atomic;
 
-namespace AtomicEditor
+namespace ToolCore
 {
 
 class JSONSceneImporter;
@@ -44,11 +44,13 @@ public:
         importer_ = importer;
     }
 
-    bool Process();
+    bool Process(const String& resourcePath);
     bool Write();
 
 private:
 
+    String resourcePath_;
+
     HashMap<String, SharedPtr<Material> > materials_;
     HashMap<String, SharedPtr<Model> > models_;
 

+ 1 - 1
Source/ToolCore/Project/Project.h

@@ -27,7 +27,7 @@ public:
     void Save(const String& fullpath = "");
 
     /// Paths
-    const String GetResourcePath() { return resourcePath_; }
+    const String& GetResourcePath() { return resourcePath_; }
     void SetResourcePath(const String& resourcePath)
     {
         resourcePath_ = AddTrailingSlash(resourcePath);