Cleanup + Attach scripts

This commit is contained in:
Simon Lübeß
2022-04-27 13:00:55 +02:00
parent e51fc77c3f
commit 2bac6a8d05
9 changed files with 167 additions and 136 deletions
+9 -13
View File
@@ -61,7 +61,7 @@ namespace GlitchyEditor
String runtimeConfig = scope String(exeDir, "/DotNetScriptingHelper.runtimeconfig.json"); String runtimeConfig = scope String(exeDir, "/DotNetScriptingHelper.runtimeconfig.json");
String assemblyPath = scope String(exeDir, "/DotNetScriptingHelper.dll"); String assemblyPath = scope String(exeDir, "/DotNetScriptingHelper.dll");
DotNet dotty = new DotNet(assemblyPath); DotNetContext dotty = new DotNetContext(assemblyPath);
defer delete dotty; defer delete dotty;
dotty.Init(); dotty.Init();
@@ -84,19 +84,20 @@ namespace GlitchyEditor
InstanceMethodDelegate update; InstanceMethodDelegate update;
dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "UpdateEntity", out update); dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "UpdateEntity", out update);
InstanceMethodDelegate freeInstance; FreeInstanceDelegate freeInstance;
dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, InteropHelper", "FreeInstance", out freeInstance); dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.InteropHelper, DotNetScriptingHelper", "FreeInstance", out freeInstance);
//void* reffy = DotNetScriptComponent.DelCreateInstance(typeName.Ptr, (int32)typeName.Length, EcsEntity.[Friend]CreateEntityID(1337, 420)); dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "CreateInstance", out DotNetScriptComponent.CreateInstanceFn);
dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "UpdateEntity", out DotNetScriptComponent.UpdateInstanceFn);
dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "DestroyEntity", out DotNetScriptComponent.DestroyInstanceFn);
/*void* reffy = DotNetScriptComponent.DelCreateInstance(typeName.Ptr, (int32)typeName.Length, EcsEntity.[Friend]CreateEntityID(1337, 420));
for (int i < 10) for (int i < 10)
{ {
update(instance); update(instance);
//update((.)reffy); update((.)reffy);
} }
freeInstance(instance);
/*
freeInstance(instance); freeInstance(instance);
freeInstance((.)reffy);*/ freeInstance((.)reffy);*/
@@ -153,11 +154,6 @@ namespace GlitchyEditor
*/*/ */*/
while(true)
{
}
_scene = scene; _scene = scene;
_ecsWorld = _scene.[Friend]_ecsWorld; _ecsWorld = _scene.[Friend]_ecsWorld;
+17 -5
View File
@@ -291,13 +291,20 @@ namespace GlitchyEngine.World
struct DotNetScriptComponent : IDisposableComponent struct DotNetScriptComponent : IDisposableComponent
{ {
public void* InstanceHandlePtr = null; public struct Instance : int {}
public Instance InstanceHandlePtr = 0;
private String TypeName = null; private String TypeName = null;
typealias CreateInstanceDelegate = function void*(void* typeNamePtr, int32 typeNameLength, EcsEntity entity); typealias CreateInstanceDelegate = function Instance(void* typeNamePtr, int32 typeNameLength, EcsEntity entity);
typealias InstanceDelegate = function void(Instance instance);
public static CreateInstanceDelegate DelCreateInstance; public static CreateInstanceDelegate CreateInstanceFn;
public static InstanceDelegate UpdateInstanceFn;
public static InstanceDelegate DestroyInstanceFn;
public const Self ss = Self();
public void Bind(StringView dotNetAssemblyQualifiedTypeName) mut public void Bind(StringView dotNetAssemblyQualifiedTypeName) mut
{ {
@@ -306,12 +313,17 @@ namespace GlitchyEngine.World
internal void CreateInstance(EcsEntity entity) mut internal void CreateInstance(EcsEntity entity) mut
{ {
//IntPtr CreateInstance(IntPtr typeNamePtr, int typeNameLength, EcsEntity entity) InstanceHandlePtr = CreateInstanceFn(TypeName.Ptr, (int32)TypeName.Length, entity);
InstanceHandlePtr = DelCreateInstance(TypeName.Ptr, (int32)TypeName.Length, entity);
} }
internal void UpdateInstance() internal void UpdateInstance()
{ {
UpdateInstanceFn(InstanceHandlePtr);
}
internal void DestroyInstance()
{
DestroyInstanceFn(InstanceHandlePtr);
} }
public void Dispose() mut public void Dispose() mut
+6 -1
View File
@@ -28,6 +28,11 @@ namespace GlitchyEngine.World
cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight); cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight);
}); });
Entity dotNetEntity = CreateEntity("DotNet rocks!");
dotNetEntity.AddComponent<SpriterRendererComponent>(.(Color(81, 43, 212)));
var vv = dotNetEntity.AddComponent<DotNetScriptComponent>();
vv.Bind("DotNetScriptingHelper.TestEntityScript, DotNetScriptingHelper");
} }
public ~this() public ~this()
@@ -52,7 +57,7 @@ namespace GlitchyEngine.World
for (var (entity, script) in _ecsWorld.Enumerate<DotNetScriptComponent>()) for (var (entity, script) in _ecsWorld.Enumerate<DotNetScriptComponent>())
{ {
if (script.InstanceHandlePtr == null) if (script.InstanceHandlePtr == 0)
{ {
script.[Friend]CreateInstance(entity); script.[Friend]CreateInstance(entity);
} }
+1 -1
View File
@@ -32,7 +32,7 @@ namespace GlitchyEngine.World
} }
else else
{ {
Log.EngineLogger.AssertDebug(false, "Queried component is not registered for this world. This is invalid because the query would never return any results."); //Log.EngineLogger.AssertDebug(false, "Queried component is not registered for this world. This is invalid because the query would never return any results.");
} }
} }
} }
@@ -11,19 +11,6 @@ public abstract class ScriptableEntity
protected virtual void OnDestroy() { } protected virtual void OnDestroy() { }
protected virtual void OnUpdate() { } protected virtual void OnUpdate() { }
[UnmanagedCallersOnly]
public static void UpdateEntity(IntPtr entityPtr)
{
Console.WriteLine("Trying to do stuff!");
GCHandle entityHandle = GCHandle.FromIntPtr(entityPtr);
if (entityHandle.Target is ScriptableEntity entity)
{
entity.OnUpdate();
}
}
[UnmanagedCallersOnly] [UnmanagedCallersOnly]
public static IntPtr CreateInstance(IntPtr typeNamePtr, int typeNameLength, EcsEntity entity) public static IntPtr CreateInstance(IntPtr typeNamePtr, int typeNameLength, EcsEntity entity)
{ {
@@ -32,14 +19,12 @@ public abstract class ScriptableEntity
string typeName = Marshal.PtrToStringUTF8(typeNamePtr, typeNameLength); string typeName = Marshal.PtrToStringUTF8(typeNamePtr, typeNameLength);
Type? type = Type.GetType(typeName, true); Type? type = Type.GetType(typeName, true);
if (type == null)
return IntPtr.Zero;
object? instance = Activator.CreateInstance(type);
if (type == null) Debug.Assert(type != null, "Type not found.");
return IntPtr.Zero;
object? instance = Activator.CreateInstance(type);
Debug.Assert(instance != null, "Instance could not be created");
Debug.Assert(instance is ScriptableEntity, "Activated instance doesn't inherit from ScriptableEntity"); Debug.Assert(instance is ScriptableEntity, "Activated instance doesn't inherit from ScriptableEntity");
@@ -58,4 +43,28 @@ public abstract class ScriptableEntity
throw; throw;
} }
} }
[UnmanagedCallersOnly]
public static void DestroyEntity(IntPtr entityPtr)
{
GCHandle entityHandle = GCHandle.FromIntPtr(entityPtr);
if (entityHandle.Target is ScriptableEntity entity)
{
entity.OnDestroy();
}
entityHandle.Free();
}
[UnmanagedCallersOnly]
public static void UpdateEntity(IntPtr entityPtr)
{
GCHandle entityHandle = GCHandle.FromIntPtr(entityPtr);
if (entityHandle.Target is ScriptableEntity entity)
{
entity.OnUpdate();
}
}
} }
+7 -96
View File
@@ -2,12 +2,11 @@ using System;
using System.IO; using System.IO;
using System.Interop; using System.Interop;
using System.Diagnostics; using System.Diagnostics;
using GlitchyEngineHelper.DotNet;
using internal GlitchyEngineHelper.DotNet;
namespace GlitchyEngineHelper namespace GlitchyEngineHelper
{ {
class DotNet class DotNetContext
{ {
#if BF_PLATFORM_WINDOWS #if BF_PLATFORM_WINDOWS
internal typealias char = char16; internal typealias char = char16;
@@ -25,8 +24,6 @@ namespace GlitchyEngineHelper
CoreClr.LoadAssemblyAndGetFunctionPointerFn LoadAssemblyAndGetFunctionPointerFn; CoreClr.LoadAssemblyAndGetFunctionPointerFn LoadAssemblyAndGetFunctionPointerFn;
CoreClr.GetFunctionPointerFn GetFunctionPointerFn; CoreClr.GetFunctionPointerFn GetFunctionPointerFn;
private const char* UNMANAGEDCALLERSONLY_METHOD = (char*)(void*)-1;
private String _configPath ~ delete _; private String _configPath ~ delete _;
public this(String configPath) public this(String configPath)
@@ -108,13 +105,11 @@ namespace GlitchyEngineHelper
return SetRuntimePropertyValueFn(cxt, RawPointer!(property), RawPointer!(value)); return SetRuntimePropertyValueFn(cxt, RawPointer!(property), RawPointer!(value));
} }
public int LoadAssemblyAndGetFunctionPointer<T>(String libraryPath, String typeName, String methodName, String delegateName, out T outDelegate) where T: operator explicit void* public int LoadAssemblyAndGetFunctionPointer<T>(StringView libraryPath, StringView typeName, StringView methodName, StringView delegateName, out T outDelegate) where T: operator explicit void*
{ {
void* funPtr = null; void* funPtr = null;
#if BF_PLATFORM_WINDOWS int rc = LoadAssemblyAndGetFunctionPointerFn(RawPointer!(libraryPath), RawPointer!(typeName), RawPointer!(methodName), RawPointer!(delegateName), null, out funPtr);
int rc = LoadAssemblyAndGetFunctionPointerFn(libraryPath.ToScopedNativeWChar!(), typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), delegateName.ToScopedNativeWChar!(), null, out funPtr);
#endif
outDelegate = (T)funPtr; outDelegate = (T)funPtr;
@@ -125,9 +120,7 @@ namespace GlitchyEngineHelper
{ {
void* funPtr = null; void* funPtr = null;
#if BF_PLATFORM_WINDOWS int rc = LoadAssemblyAndGetFunctionPointerFn(RawPointer!(libraryPath), RawPointer!(typeName), RawPointer!(methodName), CoreClr.UNMANAGEDCALLERSONLY_METHOD, null, out funPtr);
int rc = LoadAssemblyAndGetFunctionPointerFn(libraryPath.ToScopedNativeWChar!(), typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), UNMANAGEDCALLERSONLY_METHOD, null, out funPtr);
#endif
outDelegate = (T)funPtr; outDelegate = (T)funPtr;
@@ -138,9 +131,7 @@ namespace GlitchyEngineHelper
{ {
void* funPtr = null; void* funPtr = null;
#if BF_PLATFORM_WINDOWS int rc = GetFunctionPointerFn(RawPointer!(typeName), RawPointer!(methodName), RawPointer!(delegateName), null, null, out funPtr);
int rc = GetFunctionPointerFn(typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), delegateName.ToScopedNativeWChar!(), null, null, out funPtr);
#endif
outDelegate = (T)funPtr; outDelegate = (T)funPtr;
@@ -151,9 +142,7 @@ namespace GlitchyEngineHelper
{ {
void* funPtr = null; void* funPtr = null;
#if BF_PLATFORM_WINDOWS int rc = GetFunctionPointerFn(RawPointer!(typeName), RawPointer!(methodName), CoreClr.UNMANAGEDCALLERSONLY_METHOD, null, null, out funPtr);
int rc = GetFunctionPointerFn(typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), UNMANAGEDCALLERSONLY_METHOD, null, null, out funPtr);
#endif
outDelegate = (T)funPtr; outDelegate = (T)funPtr;
@@ -185,82 +174,4 @@ namespace GlitchyEngineHelper
} }
#endif #endif
} }
static class HostFxr
{
#if BF_PLATFORM_WINDOWS
internal typealias char = char16;
#else
internal typealias char = char8;
#endif
public enum DelegateType : c_int
{
ComActivation,
LoadInMemoryAssembly,
WinrtActivation,
ComRegister,
ComUnregister,
LoadAssemblyAndGetFunctionPointer,
GetFunctionPointer,
}
public typealias Handle = void*;
[CRepr]
public struct InitializeParameters
{
public c_size Size = sizeof(InitializeParameters);
public char* HostPath;
public char* DotnetRoot;
};
public typealias InitializeForRuntimeConfigFn = function [CallingConvention(.Cdecl)] int32(char* runtimeConfigPath, InitializeParameters* parameters, Handle* hostContextHandle);
public typealias InitializeForDotnetCommandLineFn = function [CallingConvention(.Cdecl)] int32(c_int argc, char** argv, InitializeParameters* parameters, Handle* hostContextHandle);
public typealias GetRuntimeDelegateFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle, DelegateType type, void** outDelegate);
public typealias SetRuntimePropertyValueFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle, char* name, char* value);
public typealias CloseFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle);
}
static class CoreClr
{
#if BF_PLATFORM_WINDOWS
internal typealias char = char16;
#else
internal typealias char = char8;
#endif
public typealias LoadAssemblyAndGetFunctionPointerFn = function [CallingConvention(.Stdcall)] c_int(
char* assemblyPath,
char* typeName,
char* methodName,
char* delegateTypeName,
void* reserved,
out void* outDelegate);
public typealias GetFunctionPointerFn = function [CallingConvention(.Stdcall)] c_int(
char* typeName,
char* MethodName,
char* delegateTypeName,
void* loadContext,
void* reserved,
out void* outDelegate
);
}
static class NetHost
{
public struct get_hostfxr_parameters
{
public c_size size;
public DotNet.char* assembly_path;
public DotNet.char* dotnet_root;
};
[CallingConvention(.Stdcall), LinkName("get_hostfxr_path")]
public static extern int get_hostfxr_path(
DotNet.char * buffer,
c_size * buffer_size,
get_hostfxr_parameters *parameters);
}
} }
+38
View File
@@ -0,0 +1,38 @@
using System;
using System.Interop;
using internal GlitchyEngineHelper.DotNet;
namespace GlitchyEngineHelper.DotNet
{
static
{
#if BF_PLATFORM_WINDOWS
internal typealias char = char16;
#else
internal typealias char = char8;
#endif
}
static class CoreClr
{
public const char* UNMANAGEDCALLERSONLY_METHOD = (char*)(void*)-1;
public typealias LoadAssemblyAndGetFunctionPointerFn = function [CallingConvention(.Stdcall)] c_int(
char* assemblyPath,
char* typeName,
char* methodName,
char* delegateTypeName,
void* reserved,
out void* outDelegate);
public typealias GetFunctionPointerFn = function [CallingConvention(.Stdcall)] c_int(
char* typeName,
char* MethodName,
char* delegateTypeName,
void* loadContext,
void* reserved,
out void* outDelegate
);
}
}
+37
View File
@@ -0,0 +1,37 @@
using System;
using System.Interop;
using internal GlitchyEngineHelper.DotNet;
namespace GlitchyEngineHelper.DotNet
{
static class HostFxr
{
public enum DelegateType : c_int
{
ComActivation,
LoadInMemoryAssembly,
WinrtActivation,
ComRegister,
ComUnregister,
LoadAssemblyAndGetFunctionPointer,
GetFunctionPointer,
}
public typealias Handle = void*;
[CRepr]
public struct InitializeParameters
{
public c_size Size = sizeof(InitializeParameters);
public char* HostPath;
public char* DotnetRoot;
};
public typealias InitializeForRuntimeConfigFn = function [CallingConvention(.Cdecl)] int32(char* runtimeConfigPath, InitializeParameters* parameters, Handle* hostContextHandle);
public typealias InitializeForDotnetCommandLineFn = function [CallingConvention(.Cdecl)] int32(c_int argc, char** argv, InitializeParameters* parameters, Handle* hostContextHandle);
public typealias GetRuntimeDelegateFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle, DelegateType type, void** outDelegate);
public typealias SetRuntimePropertyValueFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle, char* name, char* value);
public typealias CloseFn = function [CallingConvention(.Cdecl)] int32(Handle host_context_handle);
}
}
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Interop;
using internal GlitchyEngineHelper.DotNet;
namespace GlitchyEngineHelper.DotNet
{
static class NetHost
{
public struct get_hostfxr_parameters
{
public c_size size;
public char* assembly_path;
public char* dotnet_root;
};
[CallingConvention(.Stdcall), LinkName("get_hostfxr_path")]
public static extern int get_hostfxr_path(
DotNet.char * buffer,
c_size * buffer_size,
get_hostfxr_parameters *parameters);
}
}