Browse Source

Adding play command to AtomicTool

Josh Engebretson 10 years ago
parent
commit
3023ac71b8

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

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

+ 92 - 0
Source/ToolCore/Command/PlayCmd.cpp

@@ -0,0 +1,92 @@
+#include <Poco/Process.h>
+
+#include <Atomic/Core/StringUtils.h>
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/File.h>
+
+#include "../ToolSystem.h"
+#include "../ToolEnvironment.h"
+#include "../Project/Project.h"
+#include "../Build/BuildEvents.h"
+#include "../Build/BuildSystem.h"
+
+#include "PlayCmd.h"
+
+namespace ToolCore
+{
+
+PlayCmd::PlayCmd(Context* context) : Command(context)
+{
+
+}
+
+PlayCmd::~PlayCmd()
+{
+
+}
+
+bool PlayCmd::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 != "play")
+    {
+        errorMsg = "Unable to parse build command";
+        return false;
+    }
+
+    return true;
+}
+
+bool PlayCmd::LaunchPlayerProcess(const String& command, const Vector<String>& args, const String& initialDirectory)
+{
+    Poco::Process::Args pargs;
+
+    for (unsigned i = 0; i < args.Size(); i++)
+        pargs.push_back(args[i].CString());
+
+    std::string pcommand = command.CString();
+    std::string pinitialDirectory = initialDirectory.CString();
+
+    // this can take an ENV as well, may come in useful
+    Poco::ProcessHandle handle(Poco::Process::launch(pcommand, pargs, pinitialDirectory));
+
+    if (!Poco::Process::isRunning(handle))
+        return false;
+
+    return true;
+
+}
+
+
+void PlayCmd::Run()
+{
+    LOGINFOF("Playing project");
+
+    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
+    ToolEnvironment* env = GetSubsystem<ToolCore::ToolEnvironment>();
+    Project* project = tsystem->GetProject();
+    const String& editorBinary = env->GetEditorBinary();
+
+    Vector<String> paths;
+    paths.Push(env->GetCoreDataDir());
+    paths.Push(env->GetPlayerDataDir());
+    paths.Push(project->GetResourcePath());
+
+    String resourcePaths;
+    resourcePaths.Join(paths, "!");
+
+    Vector<String> vargs;
+
+    String args = ToString("--editor-resource-paths \"%s\"", resourcePaths.CString());
+
+    vargs = args.Split(' ');
+    vargs.Insert(0, "--player");
+
+    // TODO: use IPC (maybe before this set log location/access the log and output it, we need access to errors)
+    LaunchPlayerProcess(editorBinary, vargs, "");
+
+}
+
+}

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

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