.Net scripting API progress, can not reload an assembly

This commit is contained in:
Antoine Pilote
2023-10-15 03:10:23 -04:00
parent 8c581fbc14
commit 48f264708c
5 changed files with 109 additions and 10 deletions

View File

@@ -0,0 +1,10 @@
{
"runtimeOptions": {
"tfm": "net7.0",
"rollForward": "LatestMinor",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "7.0.0"
}
}
}

View File

@@ -3,7 +3,6 @@
#include "src/Core/Logger.h"
#include "src/Core/FileSystem.h"
#include <Coral/HostInstance.hpp>
#include <Coral/GC.hpp>
#include <Coral/NativeArray.hpp>
@@ -26,15 +25,14 @@ namespace Nuake
.CoralDirectory = coralDir,
.ExceptionCallback = ExceptionCallback
};
m_HostInstance = CreateScope<Coral::HostInstance>();
m_HostInstance = new Coral::HostInstance();
m_HostInstance->Initialize(settings);
m_LoadContext = CreateScope<Coral::AssemblyLoadContext>(m_HostInstance->CreateAssemblyLoadContext("NuakeEngineContext"));
}
ScriptingEngineNet::~ScriptingEngineNet()
{
m_HostInstance->UnloadAssemblyLoadContext(*m_LoadContext);
m_HostInstance->Shutdown();
}
@@ -51,9 +49,9 @@ namespace Nuake
void ScriptingEngineNet::Initialize()
{
auto& assembly = m_LoadContext->LoadAssembly("NuakeNet.dll");
auto loadContext = m_HostInstance->CreateAssemblyLoadContext("NuakeEngineContext");
auto& assembly = loadContext.LoadAssembly("NuakeNet.dll");
assembly.AddInternalCall("Nuake.Net.Engine", "LoggerLogIcall", reinterpret_cast<void*>(&Log));
assembly.UploadInternalCalls();
@@ -61,9 +59,13 @@ namespace Nuake
auto engineInstance = engineType.CreateInstance();
Coral::NativeString param1 = Coral::NativeString::FromUTF8("Hello from CPP");;
engineInstance.InvokeMethod("Log", param1);
engineInstance.InvokeMethod("Log", std::string("Hello from CPP"));
engineInstance.Destroy();
Coral::GC::Collect();
Coral::GC::WaitForPendingFinalizers();
m_HostInstance->UnloadAssemblyLoadContext(loadContext);
}
}

View File

@@ -1,6 +1,9 @@
#pragma once
#include "src/Core/Core.h"
namespace Coral
{
class HostInstance;
@@ -12,8 +15,8 @@ namespace Nuake
class ScriptingEngineNet
{
private:
Scope<Coral::HostInstance> m_HostInstance;
Scope<Coral::AssemblyLoadContext> m_LoadContext;
Coral::HostInstance* m_HostInstance;
Coral::AssemblyLoadContext* m_LoadContext;
ScriptingEngineNet();
~ScriptingEngineNet();

View File

@@ -1,6 +1,7 @@
using Coral.Managed.Interop;
using System;
using System.Collections.Generic;
namespace Nuake.Net
{
@@ -12,6 +13,7 @@ namespace Nuake.Net
public class Engine
{
internal static unsafe delegate*<NativeString, void> LoggerLogIcall;
public Engine() { }
@@ -24,4 +26,86 @@ namespace Nuake.Net
unsafe { LoggerLogIcall(input + " - hello from c# man"); }
}
}
public class IComponent
{
public UInt32 EntityID { get; protected set; }
}
public class LightComponent : IComponent
{
public LightComponent(UInt32 entityId)
{
EntityID = entityId;
}
public float Intensity { get; set; }
}
public class TransformComponent : IComponent
{
public TransformComponent(UInt32 entityId)
{
EntityID = entityId;
}
public int LocalPosition { get; set; }
public int GlobalPosition { get; set; }
public void SetGlobalPosition() { }
public void GetGlobalPosition() { }
}
public class Entity
{
internal static unsafe delegate*<UInt32, NativeString, bool> EntityHasComponentIcall;
public UInt32 ID { get; private set; }
Entity(UInt32 id)
{
ID = id;
}
public virtual void OnInit() { }
public virtual void OnUpdate(float dt) { }
public virtual void OnFixedUpdate(float dt) { }
public virtual void OnDestroy() { }
bool HasComponent<T>()
{
if (typeof(T) == typeof(TransformComponent))
{
unsafe { return EntityHasComponentIcall(ID, typeof(TransformComponent).GetType().Name); };
}
else if (typeof(T) == typeof(LightComponent))
{
return true;
}
return false;
}
T GetComponent<T>()
{
if (typeof(T) == typeof(TransformComponent))
{
return (T)(object)new TransformComponent(ID);
}
else
{
throw new InvalidOperationException("Component not found");
}
}
}
public class Scene
{
public UInt32 GetEntityIDByName(string name)
{
return 0;
}
}
}