Browse Source

Assembly loading from the editor is working with system meta data inspector fields, playing example works with CSComponent via --player switch

JoshEngebretson 10 years ago
parent
commit
4371781c91

+ 19 - 4
Build/AtomicNET/AtomicNETBootstrap/Bootstrap.cs

@@ -16,20 +16,35 @@ public class AtomicLoadContext : LoadContext
       Console.WriteLine("Bootstrap Startup");
       Console.WriteLine("Bootstrap Startup");
     }
     }
 
 
+    [DllImport("kernel32.dll")]
+    public static extern IntPtr GetModuleHandle(string lpModuleName);
 
 
     [DllImport("dl")]
     [DllImport("dl")]
     protected static extern IntPtr dlopen(string filename, int flags);
     protected static extern IntPtr dlopen(string filename, int flags);
 
 
     protected override IntPtr LoadUnmanagedDll(String unmanagedDllName)
     protected override IntPtr LoadUnmanagedDll(String unmanagedDllName)
     {
     {
+
+      if (unmanagedDllName == "__Internal")
+      {
+        IntPtr result = GetModuleHandle(null);
+        return result;
+      }
+
+      // do we need to walk paths here?
+      Console.WriteLine("LoadUnmanagedDll: " + unmanagedDllName);
+      return GetModuleHandle(unmanagedDllName); //RTLD_LAZY
+
+      /*
       if (unmanagedDllName == "__Internal")
       if (unmanagedDllName == "__Internal")
       {
       {
-        return dlopen(null, 1 /*RTLD_LAZY*/);
+        return dlopen(null, 1); //RTLD_LAZY
       }
       }
 
 
       // do we need to walk paths here?
       // do we need to walk paths here?
       Console.WriteLine("LoadUnmanagedDll: " + unmanagedDllName);
       Console.WriteLine("LoadUnmanagedDll: " + unmanagedDllName);
-      return dlopen(unmanagedDllName, 1 /*RTLD_LAZY*/);
+      return dlopen(unmanagedDllName, 1 ); //RTLD_LAZY
+      */
     }
     }
 
 
     public override Assembly LoadAssembly(AssemblyName assemblyName)
     public override Assembly LoadAssembly(AssemblyName assemblyName)
@@ -38,14 +53,14 @@ public class AtomicLoadContext : LoadContext
       Console.WriteLine(assemblyName.Name);
       Console.WriteLine(assemblyName.Name);
       Assembly assembly = null;
       Assembly assembly = null;
       try {
       try {
-            assembly = LoadFromAssemblyPath("/Users/josh/Desktop/" + assemblyName.Name + ".dll");
+            assembly = LoadFromAssemblyPath("C:\\Users\\Josh/Desktop\\" + assemblyName.Name + ".dll");
       } catch (Exception e)
       } catch (Exception e)
       {
       {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.Message);
       }
       }
 
 
       if (assembly == null)
       if (assembly == null)
-        assembly = LoadFromAssemblyPath("/Users/josh/Desktop/OSX.x64.Debug/" + assemblyName.Name + ".dll");
+        assembly = LoadFromAssemblyPath("C:\\Dev\\coreclr\\x64\\" + assemblyName.Name + ".dll");
 
 
       Console.WriteLine("Assembly: " + assembly);
       Console.WriteLine("Assembly: " + assembly);
       return assembly;
       return assembly;

+ 27 - 0
Build/AtomicNET/build.cmd

@@ -0,0 +1,27 @@
+
+csc /out:C:/Dev/coreclr/x64/AtomicNETBootstrap.dll /nostdlib+ /noconfig /t:library /lib:C:/Dev/coreclr/x64/ ^
+/r:C:/Dev/coreclr/x64/System.Collections.Concurrent.dll /r:C:/Dev/coreclr/x64/System.Runtime.dll ^
+/r:C:/Dev/coreclr/x64/mscorlib.dll ^
+C:/Dev/atomic/AtomicGameEngine/Build/AtomicNET/AtomicNETBootstrap/*.cs
+
+csc /out:C:/Users/Josh/Desktop/AtomicNETEngine.dll /nostdlib+ /noconfig /t:library /w:0 /lib:C:/Dev/coreclr/x64/ ^
+/r:C:/Dev/coreclr/x64/System.Runtime.dll /r:C:/Dev/coreclr/x64/System.IO.dll /r:C:/Dev/coreclr/x64/System.IO.FileSystem.dll ^
+/r:C:/Dev/coreclr/x64/mscorlib.dll ^
+/r:C:/Dev/coreclr/x64/System.Linq.dll ^
+C:/Dev/atomic/AtomicGameEngine/Build/Source/Generated/WINDOWS/CSharp/Packages/Atomic/Managed/*.cs ^
+C:/Dev/atomic/AtomicGameEngine/Build/Source/Generated/WINDOWS/CSharp/Packages/AtomicNET/Managed/*.cs ^
+C:/Dev/atomic/AtomicGameEngine/Build/Source/Generated/WINDOWS/CSharp/Packages/AtomicPlayer/Managed/*.cs ^
+C:/Dev/atomic/AtomicGameEngine/Build/AtomicNET/AtomicNETEngine/*.cs
+
+csc /unsafe /out:C:/Users/Josh/Desktop/AtomicNETTools.dll /nostdlib+ /noconfig /t:library /w:0 /lib:C:/Dev/coreclr/x64/ ^
+/r:C:/Dev/coreclr/x64/System.Runtime.dll /r:C:/Dev/coreclr/x64/System.IO.dll ^
+/r:C:/Dev/coreclr/x64/mscorlib.dll ^
+/r:C:/Dev/coreclr/x64/System.Linq.dll /r:C:/Dev/coreclr/x64/System.Reflection.Primitives.dll /r:C:/Dev/coreclr/x64/System.Reflection.Metadata.dll ^
+/r:C:/Dev/coreclr/x64/System.Collections.dll /r:C:/Dev/coreclr/x64/System.Collections.Immutable.dll /r:C:/Dev/coreclr/x64/AtomicNETEngine.dll ^
+C:/Dev/atomic/AtomicGameEngine/Build/AtomicNET/AtomicNETTools/*.cs
+
+csc /out:C:/Users/Josh/Desktop/AtomicNETTest.dll /nostdlib+ /noconfig /t:library /w:0 /lib:C:/Dev/coreclr/x64/ ^
+/r:C:/Dev/coreclr/x64/System.Runtime.dll /r:C:/Dev/coreclr/x64/System.IO.dll /r:C:/Dev/coreclr/x64/System.IO.FileSystem.dll ^
+/r:C:/Dev/coreclr/x64/mscorlib.dll ^
+/r:C:/Users/Josh/Desktop/AtomicNETEngine.dll ^
+C:/Dev/atomic/AtomicGameEngine/Build/AtomicNETTest/MyClass.cs

+ 1 - 1
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -75,7 +75,7 @@ void AEEditorCommon::Setup()
     String netCoreErrorMsg;
     String netCoreErrorMsg;
 
 
 #ifdef ATOMIC_PLATFORM_WINDOWS
 #ifdef ATOMIC_PLATFORM_WINDOWS
-    String coreCLRAbsPath = "C:/Dev/coreclr/x64/";
+    String coreCLRAbsPath = "C:\\Dev\\coreclr\\x64\\";
 #else
 #else
     String coreCLRAbsPath = "/Users/josh/Desktop/OSX.x64.Debug/";
     String coreCLRAbsPath = "/Users/josh/Desktop/OSX.x64.Debug/";
 #endif
 #endif

+ 30 - 21
Source/AtomicNET/NETCore/NETCore.cpp

@@ -207,13 +207,15 @@ extern "C"
 // pinvoke is faster than [UnmanagedFunctionPointer] :/
 // pinvoke is faster than [UnmanagedFunctionPointer] :/
 // [SuppressUnmanagedCodeSecurity] <--- add this attribute, in any event
 // [SuppressUnmanagedCodeSecurity] <--- add this attribute, in any event
 
 
-int csb_Atomic_Test(unsigned id)
-{
-  //printf("Flibberty Gibbets %u", id);
-  return id;
-}
 
 
-ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
+#ifdef ATOMIC_PLATFORM_WINDOWS
+#pragma warning(disable: 4244) // possible loss of data
+#define ATOMIC_EXPORT_API __declspec(dllexport)
+#else
+#define ATOMIC_EXPORT_API
+#endif
+
+ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
 {
 {
     if (!refCounted)
     if (!refCounted)
         return 0;
         return 0;
@@ -221,12 +223,12 @@ ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
     return refCounted->GetClassID();
     return refCounted->GetClassID();
 }
 }
 
 
-RefCounted* csb_AtomicEngine_GetSubsystem(const char* name)
+ATOMIC_EXPORT_API RefCounted* csb_AtomicEngine_GetSubsystem(const char* name)
 {
 {
     return NETCore::GetContext()->GetSubsystem(name);
     return NETCore::GetContext()->GetSubsystem(name);
 }
 }
 
 
-void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
+ATOMIC_EXPORT_API void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
 {
 {
     if (!ref)
     if (!ref)
         return;
         return;
@@ -239,6 +241,10 @@ void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
 
 
 bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
 bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
 {
 {
+
+    return netHost_->CreateDelegate(assemblyName, qualifiedClassName, methodName, funcOut);
+
+    /*
     if (!sCreateDelegate)
     if (!sCreateDelegate)
         return false;
         return false;
 
 
@@ -257,6 +263,7 @@ bool NETCore::CreateDelegate(const String& assemblyName, const String& qualified
         *funcOut = 0;
         *funcOut = 0;
 
 
     return st >= 0;
     return st >= 0;
+    */
 
 
 }
 }
 
 
@@ -271,6 +278,21 @@ bool NETCore::Initialize(const String &coreCLRFilesAbsPath, String& errorMsg)
 
 
     netHost_->Initialize(coreCLRFilesAbsPath_);
     netHost_->Initialize(coreCLRFilesAbsPath_);
 
 
+    SharedPtr<NETManaged> managed(new NETManaged(context_));
+    context_->RegisterSubsystem(managed);
+
+    SharedPtr<CSEventDispatcher> dispatcher(new CSEventDispatcher(context_));
+    context_->RegisterSubsystem(dispatcher);
+    context_->AddGlobalEventListener(dispatcher);
+
+    if (!context_->GetEditorContext())
+    {
+        SubscribeToEvent(E_UPDATE, HANDLER(NETCore, HandleUpdate));
+    }
+
+    managed->Initialize();
+
+
 #ifdef disabled
 #ifdef disabled
 
 
     if (!InitCoreCLRDLL(errorMsg))
     if (!InitCoreCLRDLL(errorMsg))
@@ -443,19 +465,6 @@ bool NETCore::Initialize(const String &coreCLRFilesAbsPath, String& errorMsg)
             (unsigned int*)&exitCode);
             (unsigned int*)&exitCode);
     */
     */
 
 
-    SharedPtr<NETManaged> managed(new NETManaged(context_));
-    context_->RegisterSubsystem(managed);
-
-    SharedPtr<CSEventDispatcher> dispatcher(new CSEventDispatcher(context_));
-    context_->RegisterSubsystem(dispatcher);
-    context_->AddGlobalEventListener(dispatcher);
-
-    if (!context_->GetEditorContext())
-    {
-        SubscribeToEvent(E_UPDATE, HANDLER(NETCore, HandleUpdate));
-    }
-
-    managed->Initialize();
 
 
 #endif
 #endif
 
 

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

@@ -18,6 +18,7 @@ public:
     virtual ~NETHost();
     virtual ~NETHost();
 
 
     virtual bool Initialize(const String& coreCLRFilesAbsPath) = 0;
     virtual bool Initialize(const String& coreCLRFilesAbsPath) = 0;
+    virtual bool CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut) = 0;
 
 
 };
 };
 
 

+ 56 - 6
Source/AtomicNET/NETCore/Platforms/Windows/NETHostWindows.cpp

@@ -16,7 +16,8 @@ namespace Atomic
 NETHostWindows::NETHostWindows(Context* context) :
 NETHostWindows::NETHostWindows(Context* context) :
     NETHost(context),
     NETHost(context),
     clrRuntimeHost_(0),
     clrRuntimeHost_(0),
-    clrModule_(0)
+    clrModule_(0),
+    appDomainID_(0)
 {
 {
 
 
 }
 }
@@ -26,6 +27,21 @@ NETHostWindows::~NETHostWindows()
 
 
 }
 }
 
 
+bool NETHostWindows::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
+{
+    if (!clrRuntimeHost_)
+        return false;
+
+    HRESULT hr = clrRuntimeHost_->CreateDelegate(appDomainID_, WString(assemblyName).CString(), WString(qualifiedClassName).CString(), WString(methodName).CString(), (INT_PTR *)funcOut);
+
+    if (FAILED(hr))
+    {
+        return false;
+    }
+
+    return true;
+}
+
 bool NETHostWindows::Initialize(const String& coreCLRFilesAbsPath)
 bool NETHostWindows::Initialize(const String& coreCLRFilesAbsPath)
 {
 {
     // It is very important that this is the native path "\\" vs "/" as find files will return "/" or "\" depending
     // It is very important that this is the native path "\\" vs "/" as find files will return "/" or "\" depending
@@ -41,6 +57,43 @@ bool NETHostWindows::Initialize(const String& coreCLRFilesAbsPath)
     if (!CreateAppDomain())
     if (!CreateAppDomain())
         return false;
         return false;
 
 
+    // MOVE THIS!
+    typedef void (*StartupFunction)();
+    StartupFunction startup;
+
+    // The coreclr binding model will become locked upon loading the first assembly that is not on the TPA list, or
+    // upon initializing the default context for the first time. For this test, test assemblies are located alongside
+    // corerun, and hence will be on the TPA list. So, we should be able to set the default context once successfully,
+    // and fail on the second try.
+
+    // AssemblyLoadContext
+    // https://github.com/dotnet/corefx/issues/3054
+    // dnx loader
+    // https://github.com/aspnet/dnx/tree/dev/src/Microsoft.Dnx.Loader
+
+    bool result = CreateDelegate(
+                    "AtomicNETBootstrap",
+                    "Atomic.Bootstrap.AtomicLoadContext",
+                    "Startup",
+                    (void**) &startup);
+
+    if (result)
+    {
+        startup();
+    }
+
+    result = CreateDelegate(
+                    "AtomicNETEngine",
+                    "AtomicEngine.Atomic",
+                    "Initialize",
+                    (void**) &startup);
+
+    if (result)
+    {
+        startup();
+    }
+
+
     return true;
     return true;
 }
 }
 
 
@@ -131,10 +184,8 @@ bool NETHostWindows::CreateAppDomain()
         W("UseLatestBehaviorWhenTFMNotSpecified")
         W("UseLatestBehaviorWhenTFMNotSpecified")
     };
     };
 
 
-    DWORD domainId;
-
     HRESULT hr  = clrRuntimeHost_->CreateAppDomainWithManager(
     HRESULT hr  = clrRuntimeHost_->CreateAppDomainWithManager(
-                W("HelloWorld.exe"),   // The friendly name of the AppDomain
+                W("AtomicNETDomain"),   // The friendly name of the AppDomain
                 // Flags:
                 // Flags:
                 // APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS
                 // APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS
                 // - By default CoreCLR only allows platform neutral assembly to be run. To allow
                 // - By default CoreCLR only allows platform neutral assembly to be run. To allow
@@ -157,14 +208,13 @@ bool NETHostWindows::CreateAppDomain()
                 sizeof(property_keys)/sizeof(wchar_t*),  // The number of properties
                 sizeof(property_keys)/sizeof(wchar_t*),  // The number of properties
                 property_keys,
                 property_keys,
                 property_values,
                 property_values,
-                &domainId);
+                &appDomainID_);
 
 
     if (FAILED(hr)) {
     if (FAILED(hr)) {
         LOGERRORF("Failed call to CreateAppDomainWithManager. ERRORCODE:%u ",  hr);
         LOGERRORF("Failed call to CreateAppDomainWithManager. ERRORCODE:%u ",  hr);
         return false;
         return false;
     }
     }
 
 
-
     return true;
     return true;
 }
 }
 
 

+ 2 - 0
Source/AtomicNET/NETCore/Platforms/Windows/NETHostWindows.h

@@ -64,6 +64,7 @@ public:
     virtual ~NETHostWindows();
     virtual ~NETHostWindows();
 
 
     bool Initialize(const String& coreCLRFilesAbsPath);
     bool Initialize(const String& coreCLRFilesAbsPath);
+    bool CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut);
 
 
 private:
 private:
 
 
@@ -81,6 +82,7 @@ private:
 
 
     ICLRRuntimeHost2* clrRuntimeHost_;
     ICLRRuntimeHost2* clrRuntimeHost_;
     HMODULE clrModule_;
     HMODULE clrModule_;
+    DWORD appDomainID_;
 
 
     TPAStringBuffer tpaList_;
     TPAStringBuffer tpaList_;