Browse Source

Adding NETRun

Josh Engebretson 10 years ago
parent
commit
631c4e65b9

+ 1 - 0
Source/AtomicNET/CMakeLists.txt

@@ -2,3 +2,4 @@
 add_subdirectory(NETCore)
 add_subdirectory(NETScript)
 add_subdirectory(NETJS)
+add_subdirectory(NETRun)

+ 5 - 0
Source/AtomicNET/NETCore/NETCore.cpp

@@ -107,6 +107,11 @@ void NETCore::AddAssemblyLoadPath(const String& assemblyPath)
 
 }
 
+int NETCore::ExecAssembly(const String& assemblyName, const Vector<String>& args)
+{
+    return netHost_->ExecAssembly(assemblyName, args);
+}
+
 bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
 {
     return netHost_->CreateDelegate(assemblyName, qualifiedClassName, methodName, funcOut);

+ 2 - 0
Source/AtomicNET/NETCore/NETCore.h

@@ -46,6 +46,8 @@ public:
 
     void AddAssemblyLoadPath(const String& assemblyPath);
 
+    int ExecAssembly(const String& assemblyName, const Vector<String>& args);
+
     bool CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut);
     void WaitForDebuggerConnect();
 

+ 1 - 0
Source/AtomicNET/NETCore/NETHost.h

@@ -30,6 +30,7 @@ public:
     virtual bool Initialize() = 0;
     virtual bool CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut) = 0;
     virtual void WaitForDebuggerConnect() = 0;
+    virtual int ExecAssembly(const String& assemblyName, const Vector<String>& args) = 0;
     virtual void Shutdown() = 0;
 
 protected:

+ 22 - 0
Source/AtomicNET/NETCore/Platforms/Unix/NETHostUnix.cpp

@@ -67,6 +67,28 @@ bool NETHostUnix::Initialize()
     return true;
 }
 
+int NETHostUnix::ExecAssembly(const String& assemblyName, const Vector<String>& args)
+{
+    //void* hostHandle,
+    //unsigned int domainId,
+    //int argc,
+    //const char** argv,
+    //const char* managedAssemblyPath,
+    //unsigned int* exitCode);
+
+    const char** _argv { 0 };
+
+    PODVector<const char*> argv;
+    for (unsigned i = 0; i < args.Size(); i++)
+        argv.Push(args[i].CString());
+
+    unsigned int exitCode = 0;
+
+    ExecuteAssembly_(hostHandle_, domainId_, (int) argv.Size(), argv.Size() ? &argv[0] : _argv, assemblyName.CString(), &exitCode);
+
+    return (int) exitCode;
+}
+
 bool NETHostUnix::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
 {
 

+ 2 - 0
Source/AtomicNET/NETCore/Platforms/Unix/NETHostUnix.h

@@ -56,6 +56,8 @@ public:
     void WaitForDebuggerConnect();
     void Shutdown();
 
+    int ExecAssembly(const String& assemblyName, const Vector<String>& args);
+
 private:
 
     bool LoadCLRDLL(String& errorMsg);

+ 8 - 0
Source/AtomicNET/NETRun/CMakeLists.txt

@@ -0,0 +1,8 @@
+include_directories (${CMAKE_SOURCE_DIR}/Source/ThirdParty/rapidjson/include
+                     ${CMAKE_SOURCE_DIR}/Source/ThirdParty )
+
+file (GLOB SOURCE_FILES *.cpp *.h)
+
+add_executable(NETRun ${SOURCE_FILES})
+
+target_link_libraries(NETRun NETCore NETScript Poco ${ATOMIC_LINK_LIBRARIES})

+ 157 - 0
Source/AtomicNET/NETRun/NETRunApp.cpp

@@ -0,0 +1,157 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// LICENSE: Atomic Game Engine Editor and Tools EULA
+// Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
+// license information: https://github.com/AtomicGameEngine/AtomicGameEngine
+//
+
+#include <Atomic/Core/ProcessUtils.h>
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/FileSystem.h>
+#include <Atomic/Engine/Engine.h>
+#include <Atomic/Resource/ResourceCache.h>
+
+#include <AtomicNET/NETCore/NETHost.h>
+#include <AtomicNET/NETCore/NETCore.h>
+
+#include "NETRunApp.h"
+
+DEFINE_APPLICATION_MAIN(Atomic::NETRunApp)
+
+namespace Atomic
+{
+
+NETRunApp::NETRunApp(Context* context) :
+    Application(context)
+{
+
+}
+
+NETRunApp::~NETRunApp()
+{
+
+}
+
+void NETRunApp::Setup()
+{
+
+    String coreCLRAbsPath;
+    String execAssemblyPath;
+    String tpaPaths;
+
+    const Vector<String>& arguments = GetArguments();
+
+    for (unsigned i = 0; i < arguments.Size(); ++i)
+    {
+        if (arguments[i].Length() > 1)
+        {
+            String argument = arguments[i].ToLower();
+            String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
+
+            if (argument == "--coreclr-abspath")
+            {
+                if (!value.Length())
+                    ErrorExit("Unable to parse --coreclr-abspath");
+
+                coreCLRAbsPath = AddTrailingSlash(value);
+            }
+            else if (argument == "--exec-assembly")
+            {
+                if (!value.Length())
+                    ErrorExit("Unable to parse --exec-assembly");
+
+                execAssemblyPath = value;
+            }
+            else if (argument == "--tpa-paths")
+            {
+                if (!value.Length())
+                    ErrorExit("Unable to parse --tpa-paths");
+
+                tpaPaths = AddTrailingSlash(value);
+            }
+
+        }
+
+    }
+
+    if (!coreCLRAbsPath.Length())
+        ErrorExit("Unable to parse --coreclr-abspath");
+
+    if (!execAssemblyPath.Length())
+        ErrorExit("Unable to parse --exec-assembly");
+
+    if (!tpaPaths.Length())
+        ErrorExit("Unable to parse --tpa-paths");
+
+    // Parse assembly
+    String pathName, fileName, ext;
+    SplitPath(execAssemblyPath, pathName, fileName, ext);
+
+    // this needs to be full path to assembly
+    assemblyToExecute_ = execAssemblyPath;
+
+    NETHost::SetCoreCLRFilesAbsPath(coreCLRAbsPath);
+    NETHost::SetCoreCLRTPAPaths(tpaPaths);
+    NETHost::SetCoreCLRAssemblyLoadPaths(pathName);
+
+    // Instantiate and register the AtomicNET subsystem
+    SharedPtr<NETCore> netCore (new NETCore(context_));
+    context_->RegisterSubsystem(netCore);
+    String netCoreErrorMsg;
+
+    if (!netCore->Initialize(netCoreErrorMsg))
+    {
+        ErrorExit(ToString("NetCore: Unable to initialize! %s", netCoreErrorMsg.CString()));
+    }
+
+    engineParameters_["Headless"] = true;
+    engineParameters_["LogLevel"] = LOG_INFO;
+
+    // no default resources (will be initialized later)
+    engineParameters_["ResourcePaths"] = "";
+}
+
+
+
+void NETRunApp::Start()
+{
+    Application::Start();
+
+    NETCore* netCore = GetSubsystem<NETCore>();
+
+    assemblyArgs_.Push(assemblyToExecute_);
+    assemblyArgs_.Push("--help");
+
+    netCore->ExecAssembly(assemblyToExecute_, assemblyArgs_);
+
+    GetSubsystem<Engine>()->Exit();
+}
+
+void NETRunApp::Stop()
+{
+    Application::Stop();
+
+}
+
+void NETRunApp::ErrorExit(const String& message)
+{
+    engine_->Exit(); // Close the rendering window
+    exitCode_ = EXIT_FAILURE;
+
+    // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
+    if (!message.Length())
+    {
+        #ifdef WIN32
+        Atomic::ErrorExit(startupErrors_.Length() ? startupErrors_ :
+            "Application has been terminated due to unexpected error.", exitCode_);
+        #endif
+    }
+    else
+        Atomic::ErrorExit(message, exitCode_);
+}
+
+
+
+}
+
+

+ 40 - 0
Source/AtomicNET/NETRun/NETRunApp.h

@@ -0,0 +1,40 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// LICENSE: Atomic Game Engine Editor and Tools EULA
+// Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
+// license information: https://github.com/AtomicGameEngine/AtomicGameEngine
+//
+
+#pragma once
+
+#include <Atomic/Engine/Application.h>
+
+namespace Atomic
+{
+
+class NETRunApp : public Application
+{
+    OBJECT(NETRunApp);
+
+public:
+
+    NETRunApp(Context* context);
+    virtual ~NETRunApp();
+
+    /// Setup before engine initialization. Verify that a script file has been specified.
+    virtual void Setup();
+    /// Setup after engine initialization. Load the script and execute its start function.
+    virtual void Start();
+    /// Cleanup after the main loop. Run the script's stop function if it exists.
+    virtual void Stop();
+
+    void ErrorExit(const String& message = String::EMPTY);
+
+private:
+
+    String assemblyToExecute_;
+    Vector<String> assemblyArgs_;
+
+};
+
+}