diff --git a/GlitchyEditor/src/Editor.bf b/GlitchyEditor/src/Editor.bf index c27f15c..e4c4288 100644 --- a/GlitchyEditor/src/Editor.bf +++ b/GlitchyEditor/src/Editor.bf @@ -61,7 +61,7 @@ namespace GlitchyEditor String runtimeConfig = scope String(exeDir, "/DotNetScriptingHelper.runtimeconfig.json"); String assemblyPath = scope String(exeDir, "/DotNetScriptingHelper.dll"); - DotNet dotty = new DotNet(assemblyPath); + DotNetContext dotty = new DotNetContext(assemblyPath); defer delete dotty; dotty.Init(); @@ -84,19 +84,20 @@ namespace GlitchyEditor InstanceMethodDelegate update; dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, DotNetScriptingHelper", "UpdateEntity", out update); - InstanceMethodDelegate freeInstance; - dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.ScriptableEntity, InteropHelper", "FreeInstance", out freeInstance); - - //void* reffy = DotNetScriptComponent.DelCreateInstance(typeName.Ptr, (int32)typeName.Length, EcsEntity.[Friend]CreateEntityID(1337, 420)); + FreeInstanceDelegate freeInstance; + dotty.GetFunctionPointerUnmanagedCallersOnly("DotNetScriptingHelper.InteropHelper, DotNetScriptingHelper", "FreeInstance", out freeInstance); + + 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) { update(instance); - //update((.)reffy); + update((.)reffy); } - freeInstance(instance); - /* freeInstance(instance); freeInstance((.)reffy);*/ @@ -153,11 +154,6 @@ namespace GlitchyEditor */*/ - while(true) - { - - } - _scene = scene; _ecsWorld = _scene.[Friend]_ecsWorld; diff --git a/GlitchyEngine/src/World/Components.bf b/GlitchyEngine/src/World/Components.bf index 2765b9e..7e385c4 100644 --- a/GlitchyEngine/src/World/Components.bf +++ b/GlitchyEngine/src/World/Components.bf @@ -291,13 +291,20 @@ namespace GlitchyEngine.World struct DotNetScriptComponent : IDisposableComponent { - public void* InstanceHandlePtr = null; + public struct Instance : int {} + + public Instance InstanceHandlePtr = 0; 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 { @@ -306,12 +313,17 @@ namespace GlitchyEngine.World internal void CreateInstance(EcsEntity entity) mut { - //IntPtr CreateInstance(IntPtr typeNamePtr, int typeNameLength, EcsEntity entity) - InstanceHandlePtr = DelCreateInstance(TypeName.Ptr, (int32)TypeName.Length, entity); + InstanceHandlePtr = CreateInstanceFn(TypeName.Ptr, (int32)TypeName.Length, entity); } internal void UpdateInstance() { + UpdateInstanceFn(InstanceHandlePtr); + } + + internal void DestroyInstance() + { + DestroyInstanceFn(InstanceHandlePtr); } public void Dispose() mut diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index 9c38dac..a48635f 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -28,6 +28,11 @@ namespace GlitchyEngine.World cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight); }); + + Entity dotNetEntity = CreateEntity("DotNet rocks!"); + dotNetEntity.AddComponent(.(Color(81, 43, 212))); + var vv = dotNetEntity.AddComponent(); + vv.Bind("DotNetScriptingHelper.TestEntityScript, DotNetScriptingHelper"); } public ~this() @@ -52,7 +57,7 @@ namespace GlitchyEngine.World for (var (entity, script) in _ecsWorld.Enumerate()) { - if (script.InstanceHandlePtr == null) + if (script.InstanceHandlePtr == 0) { script.[Friend]CreateInstance(entity); } diff --git a/GlitchyEngine/src/World/WorldEnumerator.bf b/GlitchyEngine/src/World/WorldEnumerator.bf index d46bad2..ceac067 100644 --- a/GlitchyEngine/src/World/WorldEnumerator.bf +++ b/GlitchyEngine/src/World/WorldEnumerator.bf @@ -32,7 +32,7 @@ namespace GlitchyEngine.World } 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."); } } } diff --git a/GlitchyEngineHelper/DotNetScriptingHelper/ScriptableEntity.cs b/GlitchyEngineHelper/DotNetScriptingHelper/ScriptableEntity.cs index 8d04dfc..f5ea15f 100644 --- a/GlitchyEngineHelper/DotNetScriptingHelper/ScriptableEntity.cs +++ b/GlitchyEngineHelper/DotNetScriptingHelper/ScriptableEntity.cs @@ -11,19 +11,6 @@ public abstract class ScriptableEntity protected virtual void OnDestroy() { } 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] public static IntPtr CreateInstance(IntPtr typeNamePtr, int typeNameLength, EcsEntity entity) { @@ -32,14 +19,12 @@ public abstract class ScriptableEntity string typeName = Marshal.PtrToStringUTF8(typeNamePtr, typeNameLength); Type? type = Type.GetType(typeName, true); - - if (type == null) - return IntPtr.Zero; - - object? instance = Activator.CreateInstance(type); - if (type == null) - return IntPtr.Zero; + Debug.Assert(type != null, "Type not found."); + + 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"); @@ -58,4 +43,28 @@ public abstract class ScriptableEntity 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(); + } + } } diff --git a/GlitchyEngineHelper/src/DotNet.bf b/GlitchyEngineHelper/src/DotNet.bf index 6428fb7..ee244a6 100644 --- a/GlitchyEngineHelper/src/DotNet.bf +++ b/GlitchyEngineHelper/src/DotNet.bf @@ -2,12 +2,11 @@ using System; using System.IO; using System.Interop; using System.Diagnostics; - -using internal GlitchyEngineHelper.DotNet; +using GlitchyEngineHelper.DotNet; namespace GlitchyEngineHelper { - class DotNet + class DotNetContext { #if BF_PLATFORM_WINDOWS internal typealias char = char16; @@ -25,8 +24,6 @@ namespace GlitchyEngineHelper CoreClr.LoadAssemblyAndGetFunctionPointerFn LoadAssemblyAndGetFunctionPointerFn; CoreClr.GetFunctionPointerFn GetFunctionPointerFn; - private const char* UNMANAGEDCALLERSONLY_METHOD = (char*)(void*)-1; - private String _configPath ~ delete _; public this(String configPath) @@ -108,13 +105,11 @@ namespace GlitchyEngineHelper return SetRuntimePropertyValueFn(cxt, RawPointer!(property), RawPointer!(value)); } - public int LoadAssemblyAndGetFunctionPointer(String libraryPath, String typeName, String methodName, String delegateName, out T outDelegate) where T: operator explicit void* + public int LoadAssemblyAndGetFunctionPointer(StringView libraryPath, StringView typeName, StringView methodName, StringView delegateName, out T outDelegate) where T: operator explicit void* { void* funPtr = null; -#if BF_PLATFORM_WINDOWS - int rc = LoadAssemblyAndGetFunctionPointerFn(libraryPath.ToScopedNativeWChar!(), typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), delegateName.ToScopedNativeWChar!(), null, out funPtr); -#endif + int rc = LoadAssemblyAndGetFunctionPointerFn(RawPointer!(libraryPath), RawPointer!(typeName), RawPointer!(methodName), RawPointer!(delegateName), null, out funPtr); outDelegate = (T)funPtr; @@ -125,9 +120,7 @@ namespace GlitchyEngineHelper { void* funPtr = null; -#if BF_PLATFORM_WINDOWS - int rc = LoadAssemblyAndGetFunctionPointerFn(libraryPath.ToScopedNativeWChar!(), typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), UNMANAGEDCALLERSONLY_METHOD, null, out funPtr); -#endif + int rc = LoadAssemblyAndGetFunctionPointerFn(RawPointer!(libraryPath), RawPointer!(typeName), RawPointer!(methodName), CoreClr.UNMANAGEDCALLERSONLY_METHOD, null, out funPtr); outDelegate = (T)funPtr; @@ -138,9 +131,7 @@ namespace GlitchyEngineHelper { void* funPtr = null; -#if BF_PLATFORM_WINDOWS - int rc = GetFunctionPointerFn(typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), delegateName.ToScopedNativeWChar!(), null, null, out funPtr); -#endif + int rc = GetFunctionPointerFn(RawPointer!(typeName), RawPointer!(methodName), RawPointer!(delegateName), null, null, out funPtr); outDelegate = (T)funPtr; @@ -151,9 +142,7 @@ namespace GlitchyEngineHelper { void* funPtr = null; -#if BF_PLATFORM_WINDOWS - int rc = GetFunctionPointerFn(typeName.ToScopedNativeWChar!(), methodName.ToScopedNativeWChar!(), UNMANAGEDCALLERSONLY_METHOD, null, null, out funPtr); -#endif + int rc = GetFunctionPointerFn(RawPointer!(typeName), RawPointer!(methodName), CoreClr.UNMANAGEDCALLERSONLY_METHOD, null, null, out funPtr); outDelegate = (T)funPtr; @@ -185,82 +174,4 @@ namespace GlitchyEngineHelper } #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); - } } \ No newline at end of file diff --git a/GlitchyEngineHelper/src/DotNet/CoreClr.bf b/GlitchyEngineHelper/src/DotNet/CoreClr.bf new file mode 100644 index 0000000..3a6a6b6 --- /dev/null +++ b/GlitchyEngineHelper/src/DotNet/CoreClr.bf @@ -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 + ); + } +} \ No newline at end of file diff --git a/GlitchyEngineHelper/src/DotNet/HostFxr.bf b/GlitchyEngineHelper/src/DotNet/HostFxr.bf new file mode 100644 index 0000000..d420b4c --- /dev/null +++ b/GlitchyEngineHelper/src/DotNet/HostFxr.bf @@ -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); + } +} \ No newline at end of file diff --git a/GlitchyEngineHelper/src/DotNet/NetHost.bf b/GlitchyEngineHelper/src/DotNet/NetHost.bf new file mode 100644 index 0000000..7fdf652 --- /dev/null +++ b/GlitchyEngineHelper/src/DotNet/NetHost.bf @@ -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); + } +} \ No newline at end of file