Author SHA1 Message Date
Simon Lübeß 75513bea3d Removed obsolete texture viewer 2023-07-28 21:09:33 +02:00
Simon Lübeß 16c3d7c44d Updated ImGui 2023-07-28 13:38:59 +02:00
Simon Lübeß 7ae069baf6 Fixed loading UI font 2023-07-28 12:26:15 +02:00
Simon Lübeß 00dc3faa19 Failed to show and edit script sctruct
+ SizeInBytes for MonoFields
2023-07-28 11:48:01 +02:00
Simon Lübeß f39acdadb3 Show enum fields in Edit mode 2023-07-28 00:07:10 +02:00
Simon Lübeß 1254d01545 Handle exceptions from C# Scripts + Log window + EditorLogger
+ Renamed Platform/DX11/ImGui.bf to .../Dx11ImGui.bf for clarity
+ ImGuiExtension: ImageButtonEx thata takes a TextureViewBinding
+ EditorLogger that logs to the LogWindow
+ Current logger can now be changed
+ Info, Trace, Warning and Error Icons
2023-07-27 21:58:42 +02:00
Simon Lübeß 0bc517842f Allow changing logger 2023-07-27 00:42:42 +02:00
Simon Lübeß 3e63281508 Fixed mono debugging... 2023-07-26 22:56:43 +02:00
Simon Lübeß ef486b7082 Basic entity highlighting 2023-07-25 19:37:13 +02:00
Simon Lübeß 81a61d5a90 Drag'n'drop for components
+ Added CreateInstance to ScriptClass
+ Added CreateComponentInstance to ScriptInstance
2023-07-25 16:37:41 +02:00
Simon Lübeß 37197cbdf1 Allow dragging and dropping of Entities in editor
+ New Dictionary Helpers
+ Made ImGui Payloads safer
+ Start of Component drag'n'drop
2023-07-24 23:17:00 +02:00
Simon Lübeß b53ed2d8f2 Made script deserialization more robust 2023-07-24 21:28:58 +02:00
57 changed files with 2146 additions and 1643 deletions
-3
View File
@@ -31,6 +31,3 @@
[submodule "GlitchyEngine/vendor/Beef.Linq"]
path = GlitchyEngine/vendor/Beef.Linq
url = https://github.com/aharabada/Beef.Linq.git
[submodule "GlitchyEngine/vendor/NetHostBeef"]
path = GlitchyEngine/vendor/NetHostBeef
url = https://github.com/aharabada/NetHostBeef.git
+2 -2
View File
@@ -1,7 +1,7 @@
FileVersion = 1
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}, box2d-beef = {Path = "GlitchyEngine/vendor/box2D"}, "Beef.Linq" = {Path = "GlitchyEngine/vendor/Beef.Linq/src"}, ScriptCore = {Path = "ScriptCore"}, NetHostBeef = {Path = "GlitchyEngine/vendor/NetHostBeef/NetHostBeef"}}
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}, msdfgen-beef = {Path = "GlitchyEngine/vendor/msdfgen/msdfgen-beef"}, ImGui = {Path = "GlitchyEngine/vendor/imgui/ImGui"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplDX11"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui/ImGuiImplWin32"}, ImGuizmo = {Path = "GlitchyEngine/vendor/imgui/ImGuizmo"}, GlitchyEngineHelper = {Path = "GlitchyEngineHelper"}, bon = {Path = "GlitchyEngine/vendor/bon"}, box2d-beef = {Path = "GlitchyEngine/vendor/box2D"}, "Beef.Linq" = {Path = "GlitchyEngine/vendor/Beef.Linq/src"}, ScriptCore = {Path = "ScriptCore"}}
Unlocked = ["corlib"]
WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper", "ScriptCore"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon", "box2d-beef", "Beef.Linq", "NetHostBeef"]}
WorkspaceFolders = {GlitchyEngine = ["GlitchyEngine", "GlitchLog", "GlitchyEngineHelper", "ScriptCore"], "GlitchyEngine/Dependencies" = ["cgltf-beef", "DirectX", "FreeType", "ImGui", "ImGuiImplDX11", "ImGuiImplWin32", "ImGuizmo", "LodePng", "msdfgen-beef", "bon", "box2d-beef", "Beef.Linq"]}
[Workspace]
StartupProject = "GlitchyEditor"
@@ -15,39 +15,32 @@ public class Camera : Entity
public float DontFollowRadius = 3.0f;
void OnCreate()
void OnUpdate(float deltaTime)
{
Log.Info("Camera Create");
}
Entity player = FindEntityWithName("Player");
public void OnUpdate(float deltaTime)
{
Log.Info($"Camera Update {deltaTime}");
if (player != null)
{
float2 playerPosition = player.Transform.Translation.XY;
//Entity player = FindEntityWithName("Player");
float2 cameraPosition = Transform.Translation.XY;
//if (player != null)
//{
// float2 playerPosition = player.Transform.Translation.XY;
float2 distanceVector = playerPosition - cameraPosition;
// float2 cameraPosition = Transform.Translation.XY;
float distance = length(distanceVector);
// float2 distanceVector = playerPosition - cameraPosition;
float2 neededMovement = float2.Zero;
// float distance = length(distanceVector);
if (distance > DontFollowRadius)
{
neededMovement = distanceVector - (distanceVector / distance) * DontFollowRadius;
}
// float2 neededMovement = float2.Zero;
// if (distance > DontFollowRadius)
// {
// neededMovement = distanceVector - (distanceVector / distance) * DontFollowRadius;
// }
// Transform.Translation = new float3(cameraPosition + neededMovement, DistanceFromPlayer);
//}
//else
//{
// Log.Error("Player not found!");
//}
Transform.Translation = new float3(cameraPosition + neededMovement, DistanceFromPlayer);
}
else
{
Log.Error("Player not found!");
}
}
}
@@ -1,4 +1,5 @@
using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using GlitchyEngine;
using GlitchyEngine.Editor;
using GlitchyEngine.Math;
@@ -10,9 +11,9 @@ namespace Sandbox
public enum MyEnum
{
Yes,
Yes = 1,
No,
Maybe
Maybe = 1337
}
public struct MyStruct
@@ -24,36 +25,38 @@ namespace Sandbox
class MyTestEntity : Entity
{
//[ShowInEditor]
[ShowInEditor]
RigidBody2D _rigidBody;
public bool Bo;
//public bool Bo;
public byte By;
public ushort Us;
public uint Ui;
public ulong Ul;
//public byte By;
//public ushort Us;
//public uint Ui;
//public ulong Ul;
public sbyte Sb;
public short Sh;
public int In;
public long Lo;
//public sbyte Sb;
//public short Sh;
//public int In;
//public long Lo;
public float Fl;
public double Do;
public float2 V2;
public float3 V3;
public float4 V4;
//public float Fl;
//public double Do;
//public float2 V2;
//public float3 V3;
//public float4 V4;
public Entity TheEntity;
public float JumpForce = 2000;
[ShowInEditor] float MoveForce = 1000;
[ShowInEditor] private int MyNumber = 1337;
[ShowInEditor] public double MyDouble = 1000.0f;
//[ShowInEditor] public double MyDouble = 1000.0f;
public MyStruct AStruct;
public MyEnum AEnum = MyEnum.Maybe;
public Camera Camera;
/// <summary>
@@ -67,12 +70,37 @@ namespace Sandbox
//_rigidBody ??= GetComponent<RigidBody2D>() ?? AddComponent<RigidBody2D>();
//Camera = FindEntityWithName("Camera").As<Camera>();
if (_rigidBody == null)
{
Log.Error("_rigidBody was not set in editor.");
}
//if (Camera == null)
//{
// Log.Error("Camera not found.");
//}
if (Camera == null)
{
Log.Warning("Camera wasn't set in editor. Searching...");
Camera = FindEntityWithName("Camera").As<Camera>();
if (Camera == null)
{
Log.Error("Camera not found.");
}
}
Log.Warning("Achtung.");
try
{
SubVoid();
}
catch (Exception e)
{
Console.WriteLine(e);
throw e;
}
}
void SubVoid()
{
throw new Exception("Ouha!", new IndexOutOfRangeException("Bist du jecke2?!", new AccessViolationException("Haleluja")));
}
/// <summary>
@@ -81,38 +109,39 @@ namespace Sandbox
/// <param name="deltaTime"></param>
void OnUpdate(float deltaTime)
{
Log.Info($"On update: {deltaTime}");
float2 force = float2.Zero;
//float2 force = float2.Zero;
if (Input.IsKeyPressed(Key.A))
{
force.X -= MoveForce * deltaTime;
}
//if (Input.IsKeyPressed(Key.A))
//{
// force.X -= MoveForce * deltaTime;
//}
if (Input.IsKeyPressed(Key.D))
{
force.X += MoveForce * deltaTime;
}
//if (Input.IsKeyPressed(Key.D))
//{
// force.X += MoveForce * deltaTime;
//}
if (Input.IsKeyPressed(Key.Q))
Camera.DistanceFromPlayer -= deltaTime;
//if (Input.IsKeyPressed(Key.Q))
// Camera.DistanceFromPlayer -= deltaTime;
if (Input.IsKeyPressed(Key.E))
Camera.DistanceFromPlayer += deltaTime;
//if (Input.IsKeyPressed(Key.E))
// Camera.DistanceFromPlayer += deltaTime;
if (Input.IsKeyPressing(Key.Space))
{
force.Y += JumpForce;
}
//if (Input.IsKeyPressing(Key.Space))
//{
// force.Y += JumpForce;
//}
if (Input.IsKeyPressing(Key.N))
SubVoid();
//_rigidBody.ApplyForceToCenter(force);
_rigidBody.ApplyForceToCenter(force);
//if (Input.IsMouseButtonReleasing(MouseButton.MiddleButton))
//{
// Log.Info($"Ouha! {MyNumber}");
// Physics2D.Gravity *= new float2(1, -1);
//}
if (Input.IsMouseButtonReleasing(MouseButton.MiddleButton))
{
Log.Info($"Ouha! {MyNumber}");
Physics2D.Gravity *= new float2(1, -1);
}
}
/// <summary>
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("Sanbox")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Sanbox")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("7a70819c-5317-402a-8553-95c5a001e370")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -1,11 +0,0 @@
{
"profiles": {
"Start Editor": {
"commandName": "Executable",
"executablePath": "D:\\Development\\Projects\\Beef\\GlitchyEngine\\build\\Debug_Win64\\GlitchyEditor\\GlitchyEditor.exe",
"commandLineArgs": "\"content\\Scenes\\physics2D.scene\"",
"workingDirectory": "D:\\Development\\Projects\\Beef\\GlitchyEngine\\GlitchyEditor",
"nativeDebugging": true
}
}
}
@@ -1,57 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7A70819C-5317-402A-8553-95C5A001E370}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sandbox</RootNamespace>
<AssemblyName>Sandbox</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="MyTestEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\ScriptCore\ScriptCore.csproj">
<Project>{6222c226-fc78-498c-80ba-5cf10ac9123c}</Project>
<Name>ScriptCore</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -1,16 +1,57 @@
<Project Sdk="Microsoft.NET.Sdk">
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<OutputPath>bin</OutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<EnableDynamicLoading>true</EnableDynamicLoading>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7A70819C-5317-402A-8553-95C5A001E370}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sandbox</RootNamespace>
<AssemblyName>Sandbox</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\ScriptCore\ScriptCore.csproj" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="MyTestEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\ScriptCore\ScriptCore.csproj">
<Project>{6222c226-fc78-498c-80ba-5cf10ac9123c}</Project>
<Name>ScriptCore</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -1,29 +0,0 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using GlitchyEngine;
namespace Sandbox;
public static class TestClass
{
[UnmanagedCallersOnly]
public static void TestFunction()
{
Console.WriteLine("Hello from the second Assembly!");
}
public static void InternalTest()
{
Console.WriteLine("Ja, ich existiere in der Tat");
Log.Info("Yeah!");
//Console.WriteLine("Und nun bin ich anders");
}
public static int Test(IntPtr arg, int sizeofArg)
{
Console.WriteLine($"YUPPYPLASDPASDOIUZASD");
return 7;
}
}
+22 -47
View File
@@ -71,8 +71,8 @@
SpriteRendererComponent = {
Color = {
R = 1,
G = 0.941886961,
B = 0.401484549,
G = 0.941887021,
B = 0.401484489,
A = 1
},
Sprite = "",
@@ -162,7 +162,7 @@
OrthographicHeight = 10,
OrthographicNearPlane = 0,
OrthographicFarPlane = 10,
AspectRatio = 0.884925187,
AspectRatio = 2.05582929,
FixedAspectRatio = false
},
ScriptComponent = {
@@ -195,8 +195,8 @@
},
TransformComponent = {
Position = {
X = -0.121203184,
Y = 0.660160959,
X = -0.121203206,
Y = 0.660161078,
Z = 0
},
Rotation = {
@@ -234,37 +234,12 @@
ScriptComponent = {
ScriptClass = "Sandbox.MyTestEntity",
Fields = [
Bo = (Bool)false,
By = (Byte)0,
Us = (UShort)57,
Ui = (UInt)54,
Ul = (ULong)53,
Sb = (SByte)-128,
Sh = (Short)95,
In = (Int)-149,
Lo = (Long)82,
Fl = (Float)-98.122963,
Do = (Double)118,
V2 = (float2){
X = 11,
Y = 12
},
V3 = (float3){
X = 13,
Y = 14,
Z = 15
},
V4 = (float4){
X = 14,
Y = 17,
Z = 18,
W = 19
},
_rigidBody = (Component)15710354273720487680,
TheEntity = (Entity)0,
JumpForce = (Float)200,
MoveForce = (Float)148,
MyNumber = (Int)22,
MyDouble = (Double)23
Camera = (Entity)1491484622542812645
] }
},
{
@@ -301,7 +276,7 @@
Color = {
R = 0.985467017,
G = 1,
B = 0.569978058
B = 0.569977939
}
}
},
@@ -348,8 +323,8 @@
SpriteRendererComponent = {
Color = {
R = 0.425658971,
G = 0.855207562,
B = 0.0558161102,
G = 0.855207443,
B = 0.0558161177,
A = 1
},
Sprite = "",
@@ -363,14 +338,14 @@
TransformComponent = {
Position = {
X = 6.74610233,
Y = -2.03225899,
Y = -2.03225946,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = -0.331792623,
W = 0.943352342
W = 0.94335258
},
Scale = {
X = 5.29557419,
@@ -409,7 +384,7 @@
},
SpriteRendererComponent = {
Color = {
R = 0.666116953,
R = 0.666117013,
G = 0.0600316115,
B = 1,
A = 1
@@ -424,8 +399,8 @@
},
TransformComponent = {
Position = {
X = 15.2321301,
Y = -1.98709249,
X = 15.2321281,
Y = -1.98709273,
Z = 0
},
Rotation = {
@@ -435,7 +410,7 @@
W = 0.990847468
},
Scale = {
X = 13.9764566,
X = 13.9764528,
Y = 1,
Z = 1
},
@@ -471,7 +446,7 @@
},
SpriteRendererComponent = {
Color = {
R = 0.176544324,
R = 0.176544309,
G = 0.666713238,
B = 0.687030852,
A = 1
@@ -487,24 +462,24 @@
TransformComponent = {
Position = {
X = -6.27842855,
Y = 2.62550211,
Y = 2.62550163,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = 0.187087834,
Z = 0.187087879,
W = 0.982343197
},
Scale = {
X = 0.999999523,
X = 0.999999464,
Y = 6.49634314,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0.376393586
Z = 0.376393616
}
},
Rigidbody2D = {
@@ -522,7 +497,7 @@
},
Density = 1,
Friction = 0.5,
Restitution = 0.899999976,
Restitution = 0.899999857,
RestitutionThreshold = 0.5
}
}
Binary file not shown.
@@ -9,8 +9,8 @@
AddressModeU = .Clamp,
AddressModeV = .Clamp,
AddressModeW = .Clamp,
MipMinLOD = -340282346638528859811704183484516925440,
MipMaxLOD = 340282346638528859811704183484516925440,
MipMinLOD = -3.40282347e+38,
MipMaxLOD = 3.40282347e+38,
MaxAnisotropy = 1,
BorderColor = {
R = 1,
Binary file not shown.
@@ -1,15 +1,34 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v7.0",
"name": ".NETStandard,Version=v2.0/",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v7.0": {
".NETStandard,Version=v2.0": {},
".NETStandard,Version=v2.0/": {
"ScriptCore/1.0.0": {
"dependencies": {
"Half": "1.0.0",
"NETStandard.Library": "2.0.3"
},
"runtime": {
"ScriptCore.dll": {}
}
},
"Half/1.0.0": {
"runtime": {
"lib/netstandard2.0/System.Half.dll": {
"assemblyVersion": "1.0.0.0",
"fileVersion": "1.0.0.0"
}
}
},
"Microsoft.NETCore.Platforms/1.1.0": {},
"NETStandard.Library/2.0.3": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0"
}
}
}
},
@@ -18,6 +37,27 @@
"type": "project",
"serviceable": false,
"sha512": ""
},
"Half/1.0.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-EPLLPQA1UjF9QM3ymNhWLhgcnBNc8e1po4qWRWTD3Hd9xiohYlkCob0QjDIbJs6Nvfd4eAbsjxIwRyh7httyXA==",
"path": "half/1.0.0",
"hashPath": "half.1.0.0.nupkg.sha512"
},
"Microsoft.NETCore.Platforms/1.1.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==",
"path": "microsoft.netcore.platforms/1.1.0",
"hashPath": "microsoft.netcore.platforms.1.1.0.nupkg.sha512"
},
"NETStandard.Library/2.0.3": {
"type": "package",
"serviceable": true,
"sha512": "sha512-st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==",
"path": "netstandard.library/2.0.3",
"hashPath": "netstandard.library.2.0.3.nupkg.sha512"
}
}
}
@@ -1,9 +0,0 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\Simon\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\Simon\\.nuget\\packages",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
]
}
}
@@ -1,10 +0,0 @@
{
"runtimeOptions": {
"tfm": "net7.0",
"rollForward": "LatestMinor",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "7.0.0"
}
}
}
@@ -33,7 +33,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -78,7 +78,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -120,7 +120,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value);
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value))
if (ImGui.ColorEdit3(displayName.Ptr, ref *(float[3]*)&value))
{
value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value);
material.SetVariable(variable.Name, value);
@@ -132,7 +132,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value))
if (ImGui.ColorEdit4(displayName.Ptr, ref *(float[4]*)&value))
{
value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
material.SetVariable(variable.Name, value);
@@ -149,7 +149,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value);
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value, .HDR | .Float))
if (ImGui.ColorEdit3(displayName.Ptr, ref *(float[3]*)&value, .HDR | .Float))
{
value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value);
material.SetVariable(variable.Name, value);
@@ -161,7 +161,7 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value, .HDR | .Float))
if (ImGui.ColorEdit4(displayName.Ptr, ref *(float[4]*)&value, .HDR | .Float))
{
value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
material.SetVariable(variable.Name, value);
+56 -6
View File
@@ -31,7 +31,58 @@ class AssetViewer : EditorWindow
ImGui.PushStyleVar(.CellPadding, .(0, 0));
bool alt_pressed = ImGui.GetIO().KeyAlt;
ImGui.Columns(2);
if (ImGui.BeginChild("Assets"))
{
DrawAssetList();
ImGui.EndChild();
}
ImGui.NextColumn();
if (ImGui.BeginChild("Files"))
{
DrawAssetViewer();
ImGui.EndChild();
}
ImGui.Columns(1);
/*if (ImGui.BeginTable("AssetViewerTable", 2, .BordersInnerV | .Resizable | .Reorderable | .NoPadOuterX))
{
if (alt_pressed)
{
// Header anzeigen, damit sie neu angeordnet werden können
ImGui.TableSetupColumn("Assets");
ImGui.TableSetupColumn("Viewer");
ImGui.TableHeadersRow();
}
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(0);
if (ImGui.BeginChild("Assets"))
{
//DrawAssetList();
ImGui.EndChild();
}
ImGui.TableNextColumn();
if (ImGui.BeginChild("Files"))
{
//DrawAssetViewer();
ImGui.EndChild();
}
ImGui.EndTable();
}*/
/*bool alt_pressed = ImGui.GetIO().KeyAlt;
if (ImGui.BeginTable("AssetViewerTable", 2, .BordersInnerV | .Resizable | .Reorderable | .NoPadOuterX))
{
@@ -42,13 +93,12 @@ class AssetViewer : EditorWindow
ImGui.TableSetupColumn("Viewer");
ImGui.TableHeadersRow();
}
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(0);
if (ImGui.BeginChild("Assets"))
{
DrawAssetList();
//DrawAssetList();
ImGui.EndChild();
}
@@ -57,13 +107,13 @@ class AssetViewer : EditorWindow
if (ImGui.BeginChild("Files"))
{
DrawAssetViewer();
//DrawAssetViewer();
ImGui.EndChild();
}
ImGui.EndTable();
}
}*/
ImGui.PopStyleVar(1);
@@ -211,7 +261,7 @@ class TexturererViewerer
float maxDimension = max(width, height);
ImGui.SliderFloat2("Position", *(float[2]*)&_position, 2 * -maxDimension * _zoom, 2 * maxDimension * _zoom);
ImGui.SliderFloat2("Position", ref *(float[2]*)&_position, 2 * -maxDimension * _zoom, 2 * maxDimension * _zoom);
ImGui.Separator();
@@ -7,6 +7,8 @@ using GlitchyEngine.Renderer;
using GlitchyEngine;
using GlitchyEngine.Content;
using GlitchyEngine.Scripting;
using GlitchyEngine.Core;
using Mono;
namespace GlitchyEditor.EditWindows
{
@@ -278,7 +280,7 @@ namespace GlitchyEditor.EditWindows
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -306,7 +308,7 @@ namespace GlitchyEditor.EditWindows
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -338,7 +340,7 @@ namespace GlitchyEditor.EditWindows
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -453,8 +455,7 @@ namespace GlitchyEditor.EditWindows
StringView search = StringView();
//char8* scriptLabel = scriptComponent.Instance?.ScriptClass.FullName.ToScopeCStr!() ?? "Select Script...";
char8* scriptLabel = scriptComponent.Instance?.ScriptClass.FullName.CStr() ?? "Select Script...";
char8* scriptLabel = scriptComponent.Instance?.ScriptClass.FullName.ToScopeCStr!() ?? "Select Script...";
if (ImGui.Button(scriptLabel))
ImGui.OpenPopup("SelectScript");
@@ -480,14 +481,12 @@ namespace GlitchyEditor.EditWindows
scriptComponent.ScriptClassName = scriptClass.FullName;
//ScriptEngine.CreateScriptFieldMap(entity);
ScriptEngine.CreateScriptFieldMap(entity);
}
}
ImGui.EndPopup();
}
/*
ScriptClass scriptClass = ScriptEngine.GetScriptClass(scriptComponent.ScriptClassName);
/*T GetFieldValue<T>()
@@ -515,11 +514,89 @@ namespace GlitchyEditor.EditWindows
scriptInstance.SetFieldValue<T>(scriptField, value);
}
T GetData<T>(void* instance, ScriptField scriptField)
{
T data = default;
Mono.mono_field_get_value((.)instance, scriptField.[Friend]_monoField, &data);
return data;
}
void GetData<T>(void* instance, ScriptField scriptField, T* data)
{
Mono.mono_field_get_value((.)instance, scriptField.[Friend]_monoField, data);
}
void SetData<T>(void* instance, ScriptField scriptField, in T value)
{
Mono.mono_field_set_value((.)instance, scriptField.[Friend]_monoField, &value);
}
void SetData<T>(void* instance, ScriptField scriptField, T* data)
{
Mono.mono_field_set_value((.)instance, scriptField.[Friend]_monoField, data);
}
if (scriptComponent.Instance?.IsInitialized == true)
{
SharpClass sharpClass = scriptComponent.Instance.ScriptClass;
ScriptInstance scriptInstance = scriptComponent.Instance;
/*void DoFunnyStuff(void* instance, SharpType sharpType)
{
for (let (fieldName, scriptField) in sharpType.Fields)
{
var monoField = scriptField.[Friend]_monoField;
switch (scriptField.FieldType)
{
case .Float:
var value = GetData<float>(instance, scriptField);
if (ImGui.DragScalar(fieldName.ToScopeCStr!(), .Float, &value))
SetData(instance, scriptField, value);
case .Struct:
{
var v = Mono.mono_field_get_value_object(ScriptEngine.[Friend]s_AppDomain, scriptField.[Friend]_monoField, (.)instance);
void* dataPtr = Mono.mono_object_unbox(v);
Span<uint8> data = .((.)dataPtr, scriptField.SizeInBytes);
if (ImGui.CollapsingHeader(fieldName.Ptr))
{
DoFunnyStuff(dataPtr, scriptField.SharpType);
}
SetData<MonoObject>(instance, scriptField, v);
//Mono.mono_field_set_value_object(ScriptEngine.[Friend]s_AppDomain, scriptField.[Friend]_monoField, (.)instance);
//uint8* data = ;
/*uint8[] data = scope .[scriptField.SizeInBytes];
GetData(instance, scriptField, data.Ptr);
if (ImGui.CollapsingHeader(fieldName.Ptr))
{
DoFunnyStuff(data.Ptr, scriptField.SharpType);
}
MonoObject* boxed = Mono.mono_value_box(ScriptEngine.[Friend]s_AppDomain, ((SharpClass)scriptField.SharpType).[Friend]_monoClass, data.Ptr);
SetData<MonoObject>(instance, scriptField, boxed);*/
//Mono.mono_field_get_value(instance, scriptField.[Friend]_monoField, data.Ptr);
}
default:
//Log.EngineLogger.Error($"Unhandled field type {scriptField.FieldType}");
}
}
}
DoFunnyStuff(scriptInstance.[Friend]_instance, sharpClass);*/
for (let (fieldName, scriptField) in sharpClass.Fields)
{
var monoField = scriptField.[Friend]_monoField;
@@ -595,6 +672,22 @@ namespace GlitchyEditor.EditWindows
// TODO!
case .Struct:
// TODO!
/*{
uint8[] data = scope .[scriptField.SizeInBytes];
Mono.mono_field_get_value(scriptInstance.[Friend]_instance, scriptField.[Friend]_monoField, data.Ptr);
//scriptField.
//scriptInstance.GetFieldValue();
//scriptInstance.ScriptClass.Fields[]
/*
var value = GetFieldValue<double>(scriptInstance, scriptField);
if (ImGui.DragScalar(fieldName.ToScopeCStr!(), .Double, &value))
SetFieldValue(scriptInstance, scriptField, value);
*/
}*/
default:
Log.EngineLogger.Error($"Unhandled field type {scriptField.FieldType}");
}
@@ -604,114 +697,311 @@ namespace GlitchyEditor.EditWindows
{
let scriptFields = ScriptEngine.GetScriptFieldMap(entity);
for (var (name, field) in ref scriptFields)
for (var (fieldName, field) in ref scriptFields)
{
switch (field.Type)
{
case .Bool:
var value = field.GetData<bool>();
if (ImGui.Checkbox(name.CStr(), &value))
if (ImGui.Checkbox(fieldName.CStr(), &value))
field.SetData(value);
case .SByte:
var value = field.GetData<int8>();
if (ImGui.DragScalar(name.CStr(), .S8, &value))
if (ImGui.DragScalar(fieldName.CStr(), .S8, &value))
field.SetData(value);
case .Short:
var value = field.GetData<int16>();
if (ImGui.DragScalar(fieldName.CStr(), .S16, &value))
field.SetData(value);
case .Short:
var value = field.GetData<int16>();
if (ImGui.DragScalar(name.CStr(), .S16, &value))
field.SetData(value);
case .Int:
var value = field.GetData<int32>();
if (ImGui.DragScalar(name.CStr(), .S32, &value))
field.SetData(value);
var value = field.GetData<int32>();
if (ImGui.DragScalar(fieldName.CStr(), .S32, &value))
field.SetData(value);
case .Int2:
var value = field.GetData<int2>();
if (ImGui.DragScalarN(name.CStr(), .S32, &value, 2))
field.SetData(value);
var value = field.GetData<int2>();
if (ImGui.DragScalarN(fieldName.CStr(), .S32, &value, 2))
field.SetData(value);
case .Int3:
var value = field.GetData<int3>();
if (ImGui.DragScalarN(name.CStr(), .S32, &value, 3))
field.SetData(value);
var value = field.GetData<int3>();
if (ImGui.DragScalarN(fieldName.CStr(), .S32, &value, 3))
field.SetData(value);
case .Int4:
var value = field.GetData<int4>();
if (ImGui.DragScalarN(name.CStr(), .S32, &value, 4))
field.SetData(value);
var value = field.GetData<int4>();
if (ImGui.DragScalarN(fieldName.CStr(), .S32, &value, 4))
field.SetData(value);
case .Long:
var value = field.GetData<int64>();
if (ImGui.DragScalar(name.CStr(), .S64, &value))
field.SetData(value);
var value = field.GetData<int64>();
if (ImGui.DragScalar(fieldName.CStr(), .S64, &value))
field.SetData(value);
case .Byte:
var value = field.GetData<uint8>();
if (ImGui.DragScalar(name.CStr(), .U8, &value))
field.SetData(value);
var value = field.GetData<uint8>();
if (ImGui.DragScalar(fieldName.CStr(), .U8, &value))
field.SetData(value);
case .UShort:
var value = field.GetData<uint16>();
if (ImGui.DragScalar(name.CStr(), .U16, &value))
field.SetData(value);
var value = field.GetData<uint16>();
if (ImGui.DragScalar(fieldName.CStr(), .U16, &value))
field.SetData(value);
case .UInt:
var value = field.GetData<uint32>();
if (ImGui.DragScalar(name.CStr(), .U32, &value))
field.SetData(value);
var value = field.GetData<uint32>();
if (ImGui.DragScalar(fieldName.CStr(), .U32, &value))
field.SetData(value);
case .ULong:
var value = field.GetData<uint64>();
if (ImGui.DragScalar(name.CStr(), .U64, &value))
field.SetData(value);
var value = field.GetData<uint64>();
if (ImGui.DragScalar(fieldName.CStr(), .U64, &value))
field.SetData(value);
case .Float:
var value = field.GetData<float>();
if (ImGui.DragScalar(name.CStr(), .Float, &value))
field.SetData(value);
var value = field.GetData<float>();
if (ImGui.DragScalar(fieldName.CStr(), .Float, &value))
field.SetData(value);
case .float2:
var value = field.GetData<float2>();
if (ImGui.Editfloat2(name, ref value))
field.SetData(value);
var value = field.GetData<float2>();
if (ImGui.Editfloat2(fieldName, ref value))
field.SetData(value);
case .float3:
var value = field.GetData<float3>();
if (ImGui.Editfloat3(name, ref value))
field.SetData(value);
var value = field.GetData<float3>();
if (ImGui.Editfloat3(fieldName, ref value))
field.SetData(value);
case .float4:
var value = field.GetData<float4>();
if (ImGui.Editfloat4(name, ref value))
field.SetData(value);
var value = field.GetData<float4>();
if (ImGui.Editfloat4(fieldName, ref value))
field.SetData(value);
case .Double:
var value = field.GetData<double>();
if (ImGui.DragScalar(name.CStr(), .Double, &value))
field.SetData(value);
var value = field.GetData<double>();
if (ImGui.DragScalar(fieldName.CStr(), .Double, &value))
field.SetData(value);
case .Double2:
var value = field.GetData<double2>();
if (ImGui.DragScalarN(name.CStr(), .Double, &value, 2))
field.SetData(value);
var value = field.GetData<double2>();
if (ImGui.DragScalarN(fieldName.CStr(), .Double, &value, 2))
field.SetData(value);
case .Double3:
var value = field.GetData<double3>();
if (ImGui.DragScalarN(name.CStr(), .Double, &value, 3))
field.SetData(value);
var value = field.GetData<double3>();
if (ImGui.DragScalarN(fieldName.CStr(), .Double, &value, 3))
field.SetData(value);
case .Double4:
var value = field.GetData<double4>();
if (ImGui.DragScalarN(name.CStr(), .Double, &value, 4))
field.SetData(value);
var value = field.GetData<double4>();
if (ImGui.DragScalarN(fieldName.CStr(), .Double, &value, 4))
field.SetData(value);
case .Enum:
// TODO!
ShowEnumSelector(field, fieldName, scriptClass);
//case .String:
// TODO!
case .Entity:
// TODO!
ShowEntityReceiver(field, fieldName, scriptClass);
case .Component:
ShowComponentReceiver(field, fieldName, scriptClass);
case .Struct:
// TODO!
case .Class:
// We don't support editing classes
default:
Log.EngineLogger.Error($"Unhandled field type {field.Type}");
}
}
}
}
*/
private static void ShowEnumSelector(ScriptFieldInstance* field, StringView fieldName, ScriptClass scriptClass)
{
ScriptField scriptField = scriptClass.Fields[fieldName];
SharpEnum enumType = scriptField.SharpType as SharpEnum;
Log.EngineLogger.Assert(enumType != null, "Enum must have a SharpEnum!");
// Simply get Enum as a uint64
var fieldValue = field.GetData<uint64>();
StringView valueName = "<Invalid Value>";
if (enumType.Values.TryGetValue(fieldValue, let enumValue))
valueName = enumValue.Name;
if (ImGui.BeginCombo(fieldName.Ptr, valueName.Ptr))
{
for (let (entryValue, enumEntry) in enumType.Values)
{
if (ImGui.Selectable(enumEntry.Name.Ptr, fieldValue == entryValue))
field.SetData(entryValue);
}
ImGui.EndCombo();
}
}
private static Entity? ShowEntitySelector()
{
static char8[128] entitySearch = .();
Entity? result = null;
if (ImGui.BeginPopup("ENTITY_SELECTOR"))
{
ImGui.TextUnformatted("Search:");
ImGui.SameLine();
if (ImGui.InputText("##entitySearcher", &entitySearch, (uint64)entitySearch.Count))
{
}
ImGui.EndPopup();
}
return result;
}
private static void ShowEntityReceiver(ScriptFieldInstance* field, StringView fieldName, ScriptClass scriptClass)
{
var entityId = field.GetData<UUID>();
Result<Entity> fieldEntity = Editor.Instance.CurrentScene.GetEntityByID(entityId);
String entityName = scope .(32);
if (entityId == UUID(0))
entityName.Set("None");
else if (fieldEntity case .Ok(Entity e))
entityName.Set(e.Name);
else
entityName..Clear().AppendF($"Missing entity ({entityId})");
ImGui.Text($"{fieldName}: ");
ImGui.SameLine();
if (ImGui.Button(entityName))
{
if (fieldEntity case .Ok)
Editor.Instance.EntityHierarchyWindow.HighlightEntity(fieldEntity);
}
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* peekPayload = ImGui.AcceptDragDropPayload(.Entity, .AcceptPeekOnly);
bool allowDrop = false;
if (peekPayload != null)
{
Entity draggedEntity = *(Entity*)peekPayload.Data;
ScriptClass draggedScriptClass = ScriptEngine.[Friend]s_EntityRoot;
if (draggedEntity.TryGetComponent<ScriptComponent>(let draggedScript))
{
var draggedClassName = draggedScript.ScriptClassName;
draggedScriptClass = ScriptEngine.GetScriptClass(draggedClassName);
}
// TODO: I don't like the fact, that we are using mono directly
ScriptField scriptField = scriptClass.Fields[fieldName];
var fieldMonoClass = Mono.mono_type_get_class(scriptField.GetMonoType());
allowDrop = draggedScriptClass.[Friend]IsSubclass(fieldMonoClass);
}
if (allowDrop)
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.Entity);
if (payload != null)
{
Log.EngineLogger.AssertDebug(payload.DataSize == sizeof(Entity));
Entity droppedEntity = *(Entity*)payload.Data;
if (droppedEntity.IsValid)
field.SetData<UUID>(droppedEntity.UUID);
}
}
ImGui.EndDragDropTarget();
}
}
//private static Entity?
private static void ShowComponentReceiver(ScriptFieldInstance* field, StringView fieldName, ScriptClass scriptClass)
{
var entityId = field.GetData<UUID>();
Result<Entity> fieldEntity = Editor.Instance.CurrentScene.GetEntityByID(entityId);
String entityName = scope .(32);
if (entityId == UUID(0))
entityName.Set("None");
else if (fieldEntity case .Ok(Entity e))
entityName.Set(e.Name);
else
entityName..Clear().AppendF($"Missing reference ({entityId})");
ImGui.Text($"{fieldName}: ");
ImGui.SameLine();
if (ImGui.Button(entityName))
{
if (fieldEntity case .Ok)
Editor.Instance.EntityHierarchyWindow.HighlightEntity(fieldEntity);
}
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* peekPayload = ImGui.AcceptDragDropPayload(.Entity, .AcceptPeekOnly);
bool allowDrop = false;
if (peekPayload != null)
{
Entity draggedEntity = *(Entity*)peekPayload.Data;
// TODO: I don't like the fact, that we are using mono directly
ScriptField scriptField = scriptClass.Fields[fieldName];
// For some reason we have to retrieve the MonoType like this. Using scriptField.GetMonoType() directly returns the wrong type...
MonoReflectionType* reflectionType = Mono.mono_type_get_object(ScriptEngine.[Friend]s_AppDomain, scriptField.GetMonoType());
MonoType* actualMonoType = Mono.mono_reflection_type_get_type(reflectionType);
// TODO: We shouldn't abuse the script glue like that.
// ScriptGlue should only be called by C#, not by Beef.
if (ScriptGlue.[Friend]s_HasComponentMethods.TryGetValue(actualMonoType, let has_component))
{
allowDrop = has_component(draggedEntity);
}
else
{
Log.EngineLogger.Warning($"No HasComponent-Function found for field {scriptField.Name}");
allowDrop = false;
}
}
if (allowDrop)
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.Entity);
if (payload != null)
{
Log.EngineLogger.AssertDebug(payload.DataSize == sizeof(Entity));
Entity droppedEntity = *(Entity*)payload.Data;
if (droppedEntity.IsValid)
field.SetData<UUID>(droppedEntity.UUID);
}
}
ImGui.EndDragDropTarget();
}
}
private static void LabelColumn(StringView label)
@@ -778,7 +1068,7 @@ namespace GlitchyEditor.EditWindows
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -270,7 +270,7 @@ namespace GlitchyEditor.EditWindows
SubTexture2D image = s_FolderTexture;
ImGui.ImageButton(image, (.)(DirectoryItemSize - padding));
ImGui.ImageButton("", image, (.)(DirectoryItemSize - padding));
ImGui.PopStyleColor();
@@ -310,7 +310,7 @@ namespace GlitchyEditor.EditWindows
// TODO: preview images
SubTexture2D image = entry->IsDirectory ? s_FolderTexture : s_FileTexture;
ImGui.ImageButton(image, (.)(DirectoryItemSize - padding));
ImGui.ImageButton("FileImage", image, (.)(DirectoryItemSize - padding));
ImGui.PopStyleColor();
@@ -324,7 +324,7 @@ namespace GlitchyEditor.EditWindows
Path.Fixup(fullpath);
ImGui.SetDragDropPayload("CONTENT_BROWSER_ITEM", fullpath.CStr(), (.)fullpath.Length, .Once);
ImGui.SetDragDropPayload(.ContentBrowserItem, fullpath.CStr(), (.)fullpath.Length, .Once);
ImGui.EndDragDropSource();
}
@@ -370,7 +370,7 @@ namespace GlitchyEditor.EditWindows
String fullpath = scope String(entry->Path);
fullpath.AppendF($"#{subAsset.Name}");
ImGui.SetDragDropPayload("CONTENT_BROWSER_ITEM", fullpath.CStr(), (.)fullpath.Length, .Once);
ImGui.SetDragDropPayload(.ContentBrowserItem, fullpath.CStr(), (.)fullpath.Length, .Once);
ImGui.EndDragDropSource();
}
@@ -137,7 +137,7 @@ namespace GlitchyEditor.EditWindows
{
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
@@ -21,8 +21,26 @@ namespace GlitchyEditor.EditWindows
private List<Entity> _selectedEntities = new .() ~ delete _;
private Entity _entityToHighlight;
public List<Entity> SelectedEntities => _selectedEntities;
private List<Entity> _entitiesToUnfold = new .() ~ delete _;
public void HighlightEntity(Entity e)
{
_entityToHighlight = e;
Entity walker = _entityToHighlight;
while (walker.Parent != null)
{
walker = walker.Parent.Value;
_entitiesToUnfold.Add(walker);
}
}
public this(Editor editor, Scene scene)
{
_editor = editor;
@@ -322,8 +340,21 @@ namespace GlitchyEditor.EditWindows
if(inSelectedList)
flags |= .Selected;
if (_entitiesToUnfold.Contains(tree.Value))
{
_entitiesToUnfold.Remove(tree.Value);
ImGui.SetNextItemOpen(true, .None);
}
bool isOpen = ImGui.TreeNodeEx((void*)(uint)tree.Value.Handle.[Friend]Index, flags, $"{name}");
if (_entityToHighlight == tree.Value)
{
ImGui.SetScrollHereY(0);
_entityToHighlight = .();
}
ImGui.PushID((void*)(uint)tree.Value.Handle.[Friend]Index);
bool deleted = false;
@@ -341,9 +372,13 @@ namespace GlitchyEditor.EditWindows
if (deleted)
return;
bool isDragged = false;
if(ImGui.BeginDragDropSource())
{
ImGui.SetDragDropPayload("DND_Entity", &tree.Value, sizeof(Entity));
isDragged = true;
ImGui.SetDragDropPayload(.Entity, &tree.Value, sizeof(Entity));
ImGui.Text(name);
@@ -352,7 +387,7 @@ namespace GlitchyEditor.EditWindows
if(ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("DND_Entity");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.Entity);
if(payload != null)
{
@@ -407,6 +442,12 @@ namespace GlitchyEditor.EditWindows
}
if (clicked || clickedRight)
{
lastClickedEntity = tree.Value;
}
//if (!isDragged && (clicked || clickedRight))
if (!isDragged && (!ImGui.IsMouseDown(.Left) && !ImGui.IsMouseDown(.Right) && ImGui.IsItemHovered()) && tree.Value == lastClickedEntity)
{
if (inSelectedList && !clickedRight)
{
@@ -418,9 +459,13 @@ namespace GlitchyEditor.EditWindows
SelectEntity(tree.Value, !ImGui.GetIO().KeyCtrl && !clickedRight);
inSelectedList = true;
}
lastClickedEntity = .();
}
}
private static Entity lastClickedEntity;
private void ShowEntityHierarchy()
{
StringView searchString = StringView(&_entitySearchChars);
@@ -464,7 +509,7 @@ namespace GlitchyEditor.EditWindows
{
if(ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("DND_Entity");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.Entity);
if(payload != null)
{
@@ -527,6 +572,10 @@ namespace GlitchyEditor.EditWindows
ImGuiPrintEntityTree(scope .(entity));
}
}
// If the mouse was released and no entity took the chance to become selected we probably hovered the background while releasing -> select no entity
if (!ImGui.IsMouseDown(.Left) && !ImGui.IsMouseDown(.Right))
lastClickedEntity = .();
}
}
}
@@ -91,7 +91,7 @@ namespace GlitchyEditor.EditWindows
{
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
+375
View File
@@ -0,0 +1,375 @@
using System;
using ImGui;
using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.World;
using GlitchLog;
using GlitchyEngine.Scripting;
using GlitchyEngine.Renderer;
namespace GlitchyEditor.EditWindows;
enum MessageType
{
case None = 0;
case Trace = 1;
case Info = 2;
case Warning = 4;
case Error = 8;
public this(LogLevel level)
{
switch (level)
{
case .Error:
this = Error;
case .Warning:
this = Warning;
case .Info:
this = Info;
case .Trace:
this = Trace;
default:
this = None;
}
}
}
class MessageSource
{
public UUID? Entity = null;
public StringView? ScriptName = null;
public int? Line = null;
/// If true, the message is only meant for engine developers... so only me :(
public bool IsEngineMessage = false;
public MonoExceptionHelper Exception = null ~ _?.ReleaseRef();
public String AdditionalData = null ~ delete _;
}
class LogMessage
{
private String _message ~ delete _;
public MessageType MessageType { get; private set; }
public DateTime Timestamp { get; private set; }
public MessageSource Source { get; private set; } ~ delete _;
public StringView Message => _message;
public this(DateTime timestamp, StringView message, MessageType logLevel, MessageSource ownSource)
{
Timestamp = timestamp;
_message = new String(message);
MessageType = logLevel;
Source = ownSource;
}
}
class LogWindow : EditorWindow
{
public const String s_WindowTitle = "Log";
private append List<LogMessage> _messages = .() ~ ClearAndDeleteItems!(_);
public static SubTexture2D s_ErrorIcon;
public static SubTexture2D s_WarningIcon;
public static SubTexture2D s_InfoIcon;
public static SubTexture2D s_TraceIcon;
private bool _showGameMessages = true;
private bool _showEngineMessages = false;
private bool _autoScroll = true;
private bool _collapseMessages = true;
private MessageType _visibleMessageTypes = .Error | .Warning | .Info | .Trace;
protected override void InternalShow()
{
defer { ImGui.End(); }
if(!ImGui.Begin(s_WindowTitle, &_open, .MenuBar))
return;
ShowMenuBar();
ShowMessages();
}
private void ShowMenuBar()
{
if(ImGui.BeginMenuBar())
{
if (ImGui.MenuItem("Clear"))
{
ClearLog();
}
if (ImGui.BeginMenu("Filter"))
{
ImGui.Checkbox("Show game messages", &_showGameMessages);
ImGui.AttachTooltip("If checked, the log will show messages generated by the game (e.g. scripts).");
ImGui.Checkbox("Show engine messages", &_showEngineMessages);
ImGui.AttachTooltip("""
If checked, the log will show messages generated by the engine.
These messages are usually only necessary for engine debugging/development and don't provide practical information for game developers.
""");
ImGui.EndMenu();
}
ImGui.Checkbox("Collapse", &_collapseMessages);
ImGui.AttachTooltip("If checked, identical messages will be collapsed into one.");
var maxSpace = ImGui.GetFontSize();
ImGui.Vec2 buttonSize = .(maxSpace, maxSpace);
var col = ImGui.GetStyleColorVec4(.Button);
ImGui.PushStyleVar(.FramePadding, ImGui.Vec2(2, 2));
if (_visibleMessageTypes.HasFlag(.Trace))
ImGui.PushStyleColor(.Button, *col);
else
ImGui.PushStyleColor(.Button, .(0, 0, 0, 0));
if (ImGui.ImageButtonEx(1, s_TraceIcon, buttonSize, .Zero, .Ones))
_visibleMessageTypes ^= .Trace;
ImGui.PopStyleColor();
if (_visibleMessageTypes.HasFlag(.Info))
ImGui.PushStyleColor(.Button, *col);
else
ImGui.PushStyleColor(.Button, .(0, 0, 0, 0));
if (ImGui.ImageButtonEx(2, s_InfoIcon, buttonSize, .Zero, .Ones))
_visibleMessageTypes ^= .Info;
ImGui.PopStyleColor();
if (_visibleMessageTypes.HasFlag(.Warning))
ImGui.PushStyleColor(.Button, *col);
else
ImGui.PushStyleColor(.Button, .(0, 0, 0, 0));
if (ImGui.ImageButtonEx(3, s_WarningIcon, buttonSize, .Zero, .Ones))
_visibleMessageTypes ^= .Warning;
ImGui.PopStyleColor();
if (_visibleMessageTypes.HasFlag(.Error))
ImGui.PushStyleColor(.Button, *col);
else
ImGui.PushStyleColor(.Button, .(0, 0, 0, 0));
if (ImGui.ImageButtonEx(4, s_ErrorIcon, buttonSize, .Zero, .Ones))
_visibleMessageTypes ^= .Error;
ImGui.PopStyleColor();
ImGui.PopStyleVar();
ImGui.EndMenuBar();
}
}
private void ShowMessages()
{
if (ImGui.BeginTable("Messages", 3, .BordersInnerH | .SizingFixedFit))
{
ImGui.TableSetupColumn("", .WidthFixed);
ImGui.TableSetupColumn("", .WidthStretch);
ImGui.TableSetupColumn("", .WidthFixed);
//LogMessage lastMessage = null;
int count = 1;
// Message ID for ImGui
int imGuiMessageId = 0;
for (let message in _messages)
{
if (!_visibleMessageTypes.HasFlag(message.MessageType))
continue;
if ((message.Source.IsEngineMessage && !_showEngineMessages) || (!message.Source.IsEngineMessage && !_showGameMessages))
continue;
/*defer
{
lastMessage = message;
}*/
do
{
LogMessage lastMessage = (message != _messages.Back) ? _messages[@message.Index + 1] : null;
if (_collapseMessages && lastMessage != null)
{
if (message.MessageType != lastMessage.MessageType)
break;
if (message.Message != lastMessage.Message)
break;
if (message.Source.Entity != lastMessage.Source.Entity)
break;
// For exceptions the stack trace is basically the only relevant thing
if (message.Source.Exception?.StackTrace != lastMessage.Source.Exception?.StackTrace)
break;
// We collapse this message with the previous one:
// Increment counter and go to next message.
count++;
continue;
}
}
// Push current index as ID
ImGui.PushID((void*)++imGuiMessageId);
ImGui.TableNextRow();
ImGui.TableSetColumnIndex(0);
switch (message.MessageType)
{
case .Error:
ImGui.Image(s_ErrorIcon, ImGui.Vec2(32, 32));
ImGui.AttachTooltip("Error");
case .Warning:
ImGui.Image(s_WarningIcon, ImGui.Vec2(32, 32));
ImGui.AttachTooltip("Warning");
case .Info:
ImGui.Image(s_InfoIcon, ImGui.Vec2(32, 32));
ImGui.AttachTooltip("Info");
case .Trace:
ImGui.Image(s_TraceIcon, ImGui.Vec2(32, 32));
ImGui.AttachTooltip("Trace");
default:
ImGui.TextUnformatted("Unknown");
}
ImGui.TableNextColumn();
// Timestamp
ImGui.TextWrapped($"[{message.Timestamp:HH:mm:ss.fff}]");
if (message.Source.IsEngineMessage)
{
ImGui.SameLine();
ImGui.TextUnformatted("Engine");
}
// Show entity
if (message.Source?.Entity != null)
{
ImGui.SameLine();
Result<Entity> entity = Editor.Instance.CurrentScene.GetEntityByID(message.Source.Entity.Value);
if (entity case .Ok(let e))
{
ImGui.Text($"Entity: \"{e.Name}\" (ID: {message.Source.Entity})");
if (ImGui.IsItemClicked())
Editor.Instance.EntityHierarchyWindow.HighlightEntity(entity);
}
else
{
ImGui.Text($"Entity: (ID: {message.Source.Entity})");
}
}
if (message.Source.Exception != null)
{
if (ImGui.CollapsingHeader(message.Message.Ptr))
{
// Show the native to managed entry point only if we show engine messages
if (_showEngineMessages)
ImGui.TextUnformatted(message.Source.Exception.StackTrace);
else
ImGui.TextUnformatted(message.Source.Exception.CleanStackTrace);
ImGui.NewLine();
}
}
else
{
ImGui.TextUnformatted(message.Message);
}
// Dont show the counter if we only have one message.
if (count > 1)
{
// the message is not collapsible with the previous one.
// Print message count for last message and reset counter.
// This message will be printed normally.
ImGui.TableNextColumn();
ImGui.Text($"{count}");
}
count = 1;
ImGui.PopID();
}
if (_autoScroll)
{
if (ImGui.GetIO().MouseWheel > 0)
{
_autoScroll = false;
}
else
{
ImGui.SetScrollY(ImGui.GetScrollMaxY());
}
}
else
{
if (ImGui.GetScrollMaxY() == ImGui.GetScrollY())
{
_autoScroll = true;
}
}
ImGui.EndTable();
}
}
public void ClearLog()
{
ClearAndDeleteItems!(_messages);
}
public void Log(DateTime timestamp, LogLevel severity, StringView message, MessageSource source)
{
LogMessage logMessage = new LogMessage(timestamp, message, MessageType(severity), source);
_messages.Add(logMessage);
}
public void LogException(DateTime timestamp, MonoExceptionHelper exception)
{
StringView firstLine = exception.StackTrace;
int firstInIndex = exception.StackTrace.IndexOf("\n");
if (firstInIndex != -1)
firstLine = firstLine.Substring(0, firstInIndex);
String message = scope .(128);
message.AppendF($"Exception: \"{exception.FullName}\" | Message: \"{exception.Message}\" {firstLine}\0");
// TODO: are mono exceptions never engine only?
LogMessage logMessage = new LogMessage(timestamp, message, .Error, new MessageSource(){Entity = exception.Instance, Exception = exception..AddRef(), IsEngineMessage = false});
_messages.Add(logMessage);
}
}
+16
View File
@@ -22,6 +22,7 @@ namespace GlitchyEditor
private ContentBrowserWindow _contentBrowserWindow ~ delete _;
private PropertiesWindow _propertiesWindow ~ delete _;
private AssetViewer _assetViewer ~ delete _;
private LogWindow _logWindow ~ delete _;
public Scene CurrentScene
{
@@ -45,6 +46,7 @@ namespace GlitchyEditor
public ContentBrowserWindow ContentBrowserWindow => _contentBrowserWindow;
public PropertiesWindow PropertiesWindow => _propertiesWindow;
public AssetViewer AssetViewer => _assetViewer;
public LogWindow LogWindow => _logWindow;
public EditorCamera* CurrentCamera { get; set; }
@@ -53,15 +55,27 @@ namespace GlitchyEditor
public SceneRenderer GameSceneRenderer {get; set;}
public SceneRenderer EditorSceneRenderer {get; set;}
private static Editor s_Instance;
public static Editor Instance => s_Instance;
/// Creates a new editor for the given world
public this(Scene scene, EditorContentManager contentManager)
{
Log.EngineLogger.AssertDebug(s_Instance == null, "Cannot create a second instance of a singleton.");
s_Instance = this;
_scene = scene;
_contentManager = contentManager;
InitWindows();
}
public ~this()
{
s_Instance = null;
}
private void InitWindows()
{
_sceneViewportWindow = new EditorViewportWindow(this);
@@ -71,6 +85,7 @@ namespace GlitchyEditor
_contentBrowserWindow = new ContentBrowserWindow((.)Application.Get().ContentManager);
_propertiesWindow = new PropertiesWindow(this);
_assetViewer = new AssetViewer((.)Application.Get().ContentManager);
_logWindow = new LogWindow();
}
public void Update()
@@ -82,6 +97,7 @@ namespace GlitchyEditor
_contentBrowserWindow.Show();
_propertiesWindow.Show();
_assetViewer.Show();
_logWindow.Show();
}
}
}
+3
View File
@@ -12,6 +12,9 @@ namespace GlitchyEditor
public this(String[] args)
{
PushLayer(new EditorLayer(args, _contentManager));
Log.ClientLogger = new EditorLogger();
Log.EngineLogger = new EditorLogger() { IsEngineLogger = true };
}
protected override IContentManager InitContentManager()
+8
View File
@@ -19,6 +19,10 @@ namespace GlitchyEditor
public SubTexture2D Simulate ~ _.ReleaseRef();
public SubTexture2D Pause ~ _.ReleaseRef();
public SubTexture2D SingleStep ~ _.ReleaseRef();
public SubTexture2D Error ~ _.ReleaseRef();
public SubTexture2D Warning ~ _.ReleaseRef();
public SubTexture2D Info ~ _.ReleaseRef();
public SubTexture2D Trace ~ _.ReleaseRef();
public SamplerState SamplerState
{
@@ -41,6 +45,10 @@ namespace GlitchyEditor
Simulate = GetNextGridTexture(ref pen, iconSize);
Pause = GetNextGridTexture(ref pen, iconSize);
SingleStep = GetNextGridTexture(ref pen, iconSize);
Error = GetNextGridTexture(ref pen, iconSize);
Warning = GetNextGridTexture(ref pen, iconSize);
Info = GetNextGridTexture(ref pen, iconSize);
Trace = GetNextGridTexture(ref pen, iconSize);
}
private SubTexture2D GetNextGridTexture(ref float2 pen, float2 iconSize)
+18 -15
View File
@@ -171,6 +171,11 @@ namespace GlitchyEditor
ContentBrowserWindow.s_FolderTexture = _editorIcons.Folder;
ContentBrowserWindow.s_FileTexture = _editorIcons.File;
LogWindow.s_ErrorIcon = _editorIcons.Error;
LogWindow.s_WarningIcon = _editorIcons.Warning;
LogWindow.s_InfoIcon = _editorIcons.Info;
LogWindow.s_TraceIcon = _editorIcons.Trace;
}
private void InitEditor()
@@ -365,17 +370,8 @@ namespace GlitchyEditor
dispatcher.Dispatch<MouseScrolledEvent>(scope (e) => OnMouseScrolled(e));
}
ImGui.ID _mainDockspaceId;
TextureViewer viewer = new TextureViewer() ~ delete _;
private bool OnImGuiRender(ImGuiRenderEvent event)
{
// TODO: make window
//Input.ImGuiDebugDraw();
//viewer.ViewTexture(Renderer.[Friend]_gBuffer.Target);
ImGui.Viewport* viewport = ImGui.GetMainViewport();
ImGui.DockSpaceOverViewport(viewport);
@@ -428,16 +424,20 @@ namespace GlitchyEditor
ImGui.SameLine();
ImGui.SetCursorPosX(centerX - totalWidth / 2);
if (ImGui.ImageButton(_editorIcons.Play, .(size, size), .Zero, .Ones, 0))
ImGui.PushID(0);
if (ImGui.ImageButton("", _editorIcons.Play, .(size, size), .Zero, .Ones))
OnScenePlay();
ImGui.PopID();
ImGui.AttachTooltip("Play the game.");
ImGui.SameLine();
ImGui.PushID(1);
if (ImGui.ImageButton(_editorIcons.Simulate, .(size, size), .Zero, .Ones, 0))
if (ImGui.ImageButton("", _editorIcons.Simulate, .(size, size), .Zero, .Ones))
OnSceneSimulate();
ImGui.PopID();
@@ -456,7 +456,7 @@ namespace GlitchyEditor
//ImGui.SameLine(penX += size + 2 * padding);
ImGui.SameLine();
if (ImGui.ImageButton(_editorIcons.Pause, .(size, size), .Zero, .Ones, 0))
if (ImGui.ImageButton("", _editorIcons.Pause, .(size, size), .Zero, .Ones))
_isPaused = !_isPaused;
ImGui.PopID();
@@ -480,7 +480,7 @@ namespace GlitchyEditor
SubTexture2D pauseButtonIcon = _isPaused ? _editorIcons.Play : _editorIcons.Pause;
if (ImGui.ImageButton(pauseButtonIcon, .(size, size), .Zero, .Ones, 0))
if (ImGui.ImageButton("PlayPause", pauseButtonIcon, .(size, size), .Zero, .Ones))
{
if (_isPaused)
OnSceneResume();
@@ -498,7 +498,7 @@ namespace GlitchyEditor
SubTexture2D singleStepButtonIcon = _editorIcons.SingleStep;
if (ImGui.ImageButton(singleStepButtonIcon, .(size, size), .Zero, .Ones, 0))
if (ImGui.ImageButton("Step", singleStepButtonIcon, .(size, size), .Zero, .Ones))
{
DoSingleStep();
}
@@ -529,7 +529,7 @@ namespace GlitchyEditor
ImGui.SameLine();
ImGui.PushID(1);
if (ImGui.ImageButton(_editorIcons.Stop, .(size, size), .Zero, .Ones, 0))
if (ImGui.ImageButton("Stop", _editorIcons.Stop, .(size, size), .Zero, .Ones))
OnSceneStop();
ImGui.PopID();
@@ -808,6 +808,9 @@ namespace GlitchyEditor
if(ImGui.MenuItem(AssetViewer.s_WindowTitle))
_editor.AssetViewer.Open = true;
if(ImGui.MenuItem(LogWindow.s_WindowTitle))
_editor.LogWindow.Open = true;
ImGui.EndMenu();
}
+128
View File
@@ -0,0 +1,128 @@
using GlitchLog;
using System;
using System.Diagnostics;
using GlitchyEngine.Scripting;
namespace GlitchyEditor;
public class EditorLogger : Logger
{
// {l} = log level (first parameter)
// {t} = current date time (second parameter)
// {n} = logger name (third parameter)
// {m} = message
private String _name;
public override String Name
{
get => _name;
set => _name = value;
}
public bool IsEngineLogger {get; set;}
public this()
{
//Debug.Assert(Debug.IsDebuggerPresent, "The DebugLogger requires a debugger to be present.");
}
#if GL_NOLOG || GL_LOG_NOTRACE
[SkipCall]
#endif
[Inline]
public override void Trace(StringView format, params Object[] args)
{
InternalLog(.Trace, format, params args);
}
#if GL_NOLOG || GL_LOG_NOINFO
[SkipCall]
#endif
[Inline]
public override void Info(StringView format, params Object[] args)
{
InternalLog(.Info, format, params args);
}
#if GL_NOLOG || GL_LOG_NOWARNING
[SkipCall]
#endif
[Inline]
public override void Warning(StringView format, params Object[] args)
{
InternalLog(.Warning, format, params args);
}
#if GL_NOLOG || GL_LOG_NOERROR
[SkipCall]
#endif
[Inline]
public override void Error(StringView format, params Object[] args)
{
InternalLog(.Error, format, params args);
}
#if GL_NOLOG || GL_LOG_NOCRITICAL
[SkipCall]
#endif
[Inline]
public override void Critical(StringView format, params Object[] args)
{
InternalLog(.Critical, format, params args);
}
#if GL_NOLOG
[SkipCall]
#endif
[Inline]
public override void Log(LogLevel level, StringView format, params Object[] args)
{
InternalLog(level, format, params args);
}
public override void Assert(bool condition, String error = Compiler.CallerExpression[0], String filePath = Compiler.CallerFilePath, int line = Compiler.CallerLineNum)
{
if (!condition)
{
String failStr = scope .()..AppendF("Assert failed: {} at line {} in {}", error, line, filePath);
InternalLog(.Critical, failStr);
Internal.FatalError(failStr, 1);
}
}
public override void AssertDebug(bool condition, String error = Compiler.CallerExpression[0], String filePath = Compiler.CallerFilePath, int line = Compiler.CallerLineNum)
{
if (!condition)
{
String failStr = scope .()..AppendF("Assert failed: {} at line {} in {}", error, line, filePath);
InternalLog(.Critical, failStr);
Internal.FatalError(failStr, 1);
}
}
private void InternalLog(LogLevel level, StringView format, params Object[] args)
{
if(_logLevel > level)
return;
DateTime timestamp = DateTime.Now;
String message = scope String(4096);
message.AppendF(format, params args);
Debug.Write($"[{timestamp:HH:mm:ss.fff}] ({_name})|{level.UpperString}: {message}");
if (Editor.Instance == null)
return;
if (args.Count > 0 && (var ex = args[^1] as MonoExceptionHelper))
{
Editor.Instance.LogWindow.LogException(timestamp, ex);
}
else
{
Editor.Instance.LogWindow.Log(timestamp, level, message, new .() {IsEngineMessage = IsEngineLogger});
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using System;
namespace ImGui;
enum DragDropPayloadType
{
case ContentBrowserItem;
case Entity;
public String GetName()
{
switch (this)
{
case .ContentBrowserItem:
return "CONTENT_BROWSER_ITEM";
case .Entity:
return "ENTITY";
}
}
}
extension ImGui
{
public static Payload* AcceptDragDropPayload(DragDropPayloadType type, DragDropFlags flags = .None)
{
return AcceptDragDropPayload(type.GetName(), flags);
}
public static bool SetDragDropPayload(DragDropPayloadType type, void* data, size sz, Cond cond = .None)
{
return SetDragDropPayload(type.GetName(), data, sz, cond);
}
}
-247
View File
@@ -1,247 +0,0 @@
using GlitchyEngine.Renderer;
using GlitchyEngine;
using ImGui;
using GlitchyEngine.Math;
using System;
namespace GlitchyEditor
{
class TextureViewer
{
enum BackgroundMode : int32
{
White,
Black,
Checkerboard
}
enum SampleMode : int32
{
Point,
Linear
}
GraphicsContext _context ~ _.ReleaseRef();
Effect _effect ~ _.ReleaseRef();
float _zoom = 1.0f;
BackgroundMode _backgroundMode = .Checkerboard;
SampleMode _sampleMode = .Linear;
RenderTarget2D _target ~ _?.ReleaseRef();
// TODO: we don't need depth!
DepthStencilTarget _depth ~ _?.ReleaseRef();
SamplerState _samplerPoint ~ _.ReleaseRef();
SamplerState _samplerLinear ~ _.ReleaseRef();
public this()
{
_context = Application.Get().Window.Context..AddRef();
InitEffect();
InitState();
// TODO: rasterizerstate and depthstencilstate
}
private void InitEffect()
{
_effect = new Effect("content\\Shaders\\textureViewerShader.hlsl");
}
private void InitState()
{
SamplerStateDescription desc = .();
desc.MagFilter = .Linear;
desc.MinFilter = .Linear;
_samplerLinear = SamplerStateManager.GetSampler(desc);
desc.MagFilter = .Point;
desc.MinFilter = .Point;
_samplerPoint = SamplerStateManager.GetSampler(desc);
}
float2 _position;
bool _moving;
float _colorOffset = 0;
float _colorScale = 1;
float _alphaOffset = 0;
float _alphaScale = 1;
public void ViewTexture(Texture viewedTexture)
{
ImGui.Begin("Texture Viewer");
ImGui.SliderFloat("Zoom", &_zoom, 0.01f, 100.0f);
char8*[] items = scope .("White", "Black", "Checkerboard");
ImGui.Combo("Background", (.)&_backgroundMode, items.Ptr, (.)items.Count);
items = scope .("Point", "Linear");
ImGui.Combo("Sampler", (.)&_sampleMode, items.Ptr, (.)items.Count);
ImGui.SliderFloat2("Color offset and scale", *(float[2]*)&_colorOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Alpha offset and scale", *(float[2]*)&_alphaOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Position", *(float[2]*)&_position, 2 * -Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom, 2 * Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom);
ImGui.BeginChild("imageChild");
UpdateInput();
var viewportSize = ImGui.GetContentRegionAvail();
viewportSize.x = Math.Max(viewportSize.x, 1);
viewportSize.y = Math.Max(viewportSize.y, 1);
if(_target == null || viewportSize.x != _target.Width || viewportSize.y != _target.Height)
{
_target?.ReleaseRef();
_target = new RenderTarget2D(.(.R8G8B8A8_UNorm, (.)viewportSize.x, (.)viewportSize.y));
_target.SamplerState = SamplerStateManager.PointClamp;
_depth?.ReleaseRef();
_depth = new DepthStencilTarget((.)viewportSize.x, (.)viewportSize.y, .D16_UNorm);
}
RenderTexture(viewedTexture);
ImGui.Image(_target, viewportSize);
ImGui.EndChild();
ImGui.End();
}
float lastWheel;
private void UpdateInput()
{
var windowPos = ImGui.GetWindowPos();
var mousePos = ImGui.GetIO().MousePos;
float2 mouseInWindow = .(mousePos.x - windowPos.x, mousePos.y - windowPos.y);
bool windowHovered = ImGui.IsWindowHovered();
if(windowHovered && Input.IsMouseButtonPressing(.MiddleButton))
{
_moving = true;
}
else if(Input.IsMouseButtonReleased(.MiddleButton))
{
_moving = false;
}
if(windowHovered || _moving)
{
float mouseWheel = ImGui.GetIO().MouseWheel;
float delta = mouseWheel - lastWheel;
if(delta != 0)
{
_position -= mouseInWindow;
_position /= _zoom;
_zoom *= Math.Pow(1.1f, delta);
_position *= _zoom;
_position += mouseInWindow;
}
}
if(_moving)
{
int2 movement = Input.GetMouseMovement();
_position.X += movement.X;
_position.Y += movement.Y;
}
}
OrthographicCamera _camera = new OrthographicCamera() ~ delete _;
private void RenderTexture(Texture viewedTexture)
{
Viewport vp = .(0, 0, _target.Width, _target.Height);
RenderCommand.SetViewport(vp);
// TODO: don't clear pink!
RenderCommand.Clear(_target, .Pink);
RenderCommand.Clear(_depth, .Depth, 1.0f, 0);
//_target.Bind();
_context.SetRenderTarget(_target);
_context.SetDepthStencilTarget(_depth);
_context.BindRenderTargets();
float2 textureSize = float2(viewedTexture.Width, viewedTexture.Height);
float2 zoomedTextureSize = textureSize * _zoom;
float2 targetSize = float2(_target.Width, _target.Height);
_effect.Variables["ColorOffset"].SetData(_colorOffset);
_effect.Variables["ColorScale"].SetData(_colorScale);
_effect.Variables["AlphaOffset"].SetData(_alphaOffset);
_effect.Variables["AlphaScale"].SetData(_alphaScale);
_camera.Left = 0;
_camera.Top = 0;
_camera.Right = targetSize.X;
_camera.Bottom = -targetSize.Y;
_camera.NearPlane = -5;
_camera.FarPlane = 5;
_camera.Update();
Renderer2D.BeginScene(_camera);
switch(_backgroundMode)
{
case .Black:
Renderer2D.DrawQuadPivotCorner(float3(0, 0, 1), targetSize, 0, .Black);
case .White:
Renderer2D.DrawQuadPivotCorner(float3(0, 0, 1), targetSize, 0, .White);
case .Checkerboard:
float quadSize = 50.0f;
float2 numQuads = (targetSize / 500f) * 10f;
for(float x = 0; x < numQuads.X; x++)
{
for(float y = 0; y < numQuads.Y; y++)
{
Renderer2D.DrawQuadPivotCorner(float3(x * quadSize, -y * quadSize, 1), quadSize.XX, 0, ((x + y) % 2 == 0) ? .White : .Gray);
}
}
break;
}
Renderer2D.EndScene();
var sampler = viewedTexture.SamplerState;
switch(_sampleMode)
{
case .Point:
viewedTexture.SamplerState = _samplerPoint;
case .Linear:
viewedTexture.SamplerState = _samplerLinear;
}
Renderer2D.BeginScene(_camera, .SortByTexture, _effect);
Renderer2D.DrawQuad(float3(_position * .(1, -1), 0), zoomedTextureSize, 0, viewedTexture);
Renderer2D.EndScene();
viewedTexture.SamplerState = sampler;
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
FileVersion = 1
Dependencies = {GlitchLog = "*", corlib = "*", DirectX = "*", FreeType = "*", cgltf-beef = "*", msdfgen-beef = "*", ImGui = "*", ImGuiImplDX11 = "*", ImGuiImplWin32 = "*", ImGuizmo = "*", Beefy2D = "*", LodePng = "*", GlitchyEngineHelper = "*", bon = "*", box2d-beef = "*", "Beef.Linq" = "*", NetHostBeef = "*"}
Dependencies = {GlitchLog = "*", corlib = "*", DirectX = "*", FreeType = "*", cgltf-beef = "*", msdfgen-beef = "*", ImGui = "*", ImGuiImplDX11 = "*", ImGuiImplWin32 = "*", ImGuizmo = "*", Beefy2D = "*", LodePng = "*", GlitchyEngineHelper = "*", bon = "*", box2d-beef = "*", "Beef.Linq" = "*"}
[Project]
Name = "GlitchyEngine"
+6 -9
View File
@@ -71,26 +71,23 @@ namespace GlitchyEngine
}
}
public static mixin ClearDictionaryAndDeleteValues(var dictionary)
public static mixin ClearDictionaryAndReleaseKeys(var dictionary)
{
if (dictionary != null)
{
for (var value in dictionary)
delete value.value;
delete dictionary;
value.key?.ReleaseRef();
dictionary.Clear();
}
}
public static mixin ClearDictionaryAndDeleteKeysAndValues(var dictionary)
public static mixin ClearDictionaryAndReleaseValues(var dictionary)
{
if (dictionary != null)
{
for (var value in dictionary)
{
delete value.key;
delete value.value;
}
delete dictionary;
value.value?.ReleaseRef();
dictionary.Clear();
}
}
}
+15 -3
View File
@@ -87,17 +87,29 @@ namespace ImGui
public static extern void Image(TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 tint_col = Vec4.Ones, Vec4 border_col = Vec4.Zero);
public static bool ImageButton(SubTexture2D subTexture, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, int32 frame_padding = -1, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
public static bool ImageButton(char8* id, SubTexture2D subTexture, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
{
if (uv0 != .Zero || uv1 != .Ones)
Runtime.NotImplemented();
float2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
return ImageButton(subTexture.Texture.GetViewBinding(), size, (.)subTexture.TexCoords.XY, (.)v, frame_padding, bg_col, tint_col);
return ImageButton(id, subTexture.Texture.GetViewBinding(), size, (.)subTexture.TexCoords.XY, (.)v, bg_col, tint_col);
}
public static extern bool ImageButton(TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, int32 frame_padding = -1, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones);
public static extern bool ImageButton(char8* id, TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones);
public static bool ImageButtonEx(uint32 id, SubTexture2D subTexture, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
{
if (uv0 != .Zero || uv1 != .Ones)
Runtime.NotImplemented();
float2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
return ImageButtonEx(id, subTexture.Texture.GetViewBinding(), size, (.)subTexture.TexCoords.XY, (.)v, bg_col, tint_col);
}
public static extern bool ImageButtonEx(uint32 id, TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones);
public static void TextUnformatted(StringView text) => TextUnformattedImpl(text.Ptr, text.Ptr + text.Length);
+30 -17
View File
@@ -86,29 +86,42 @@ namespace GlitchyEngine.ImGui
ImGuizmo.BeginFrame();
}
private void LoadFont()
{
var settings = Application.Instance.Settings.ImGuiSettings;
ImGui.GetIO().Fonts.Clear();
Stream fontFile = Application.Instance.ContentManager.GetStream(settings.FontName);
if (fontFile != null)
{
// We need to use ImGuis memory allocator because ImGui takes ownership
uint8* fontData = (uint8*)ImGui.MemAlloc((uint64)fontFile.Length);
var result = fontFile.TryRead(Span<uint8>(fontData, fontFile.Length));
if (result case .Err)
Log.EngineLogger.Error($"Failed to load font \"{settings.FontName}\" from stream.");
ImGui.GetIO().Fonts.AddFontFromMemoryTTF(fontData, (int32)fontFile.Length, settings.FontSize);
}
else
{
ImGui.GetIO().Fonts.AddFontDefault();
}
ImGui.GetIO().Fonts.AddFontDefault();
delete fontFile;
}
public void ImGuiRender()
{
Debug.Profiler.ProfileFunction!();
if (SettingsInvalid)
{
var settings = Application.Get().Settings.ImGuiSettings;
ImGui.GetIO().Fonts.Clear();
// TODO: Fix fonts
//String fullpath = scope String();
//Application.Get().ContentManager.GetFilePath(fullpath, settings.FontName);
/*Application.Get().ContentManager.GetStream(settings.FontName);
if (File.Exists(fullpath))
{
ImGui.GetIO().Fonts.AddFontFromMemoryTTF();
ImGui.GetIO().Fonts.AddFontFromFileTTF(fullpath, settings.FontSize);
}
else
{*/
ImGui.GetIO().Fonts.AddFontDefault();
//}
LoadFont();
#if GE_GRAPHICS_DX11
ImGuiImplDX11.CreateDeviceObjects();
+27 -4
View File
@@ -8,10 +8,33 @@ namespace GlitchyEngine
static Logger _engineLogger ~ delete _;
static Logger _clientLogger ~ delete _;
[Inline]
public static Logger EngineLogger => _engineLogger;
[Inline]
public static Logger ClientLogger => _clientLogger;
public static Logger EngineLogger
{
[Inline]
get => _engineLogger;
set
{
if (_engineLogger == value)
return;
delete _engineLogger;
_engineLogger = value;
}
}
public static Logger ClientLogger
{
[Inline]
get => _clientLogger;
set
{
if (_clientLogger == value)
return;
delete _clientLogger;
_clientLogger = value;
}
}
static this()
{
@@ -22,12 +22,24 @@ namespace ImGui
textureViewBinding.Release();
}
public static override bool ImageButton(TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, int32 frame_padding = -1, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
public static override bool ImageButton(char8* id, TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
{
var view = textureViewBinding._nativeShaderResourceView..AddRef();
_resourceViews.Add(view);
bool pressed = ImGui.ImageButton(view, size, uv0, uv1, frame_padding, bg_col, tint_col);
bool pressed = ImGui.ImageButton(id, view, size, uv0, uv1, bg_col, tint_col);
textureViewBinding.Release();
return pressed;
}
public static override bool ImageButtonEx(uint32 id, TextureViewBinding textureViewBinding, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 bg_col = Vec4.Zero, Vec4 tint_col = Vec4.Ones)
{
var view = textureViewBinding._nativeShaderResourceView..AddRef();
_resourceViews.Add(view);
bool pressed = ImGui.ImageButtonEx(id, view, size, uv0, uv1, bg_col, tint_col);
textureViewBinding.Release();
@@ -1,166 +0,0 @@
using NetHostBeef;
using System;
using System.Interop;
using GlitchyEngine.Core;
namespace GlitchyEngine.Scripting;
static class CoreClrHelper
{
static HostFxr.InitializeForDotnetCommandLineFn HostFxr_Init;
static HostFxr.GetRuntimeDelegateFn HostFxr_GetDelegate;
static HostFxr.CloseFn HostFxr_Close;
static CoreClr.LoadAssemblyAndGetFunctionPointerFn LoadAssemblyAndGetFunctionPointerFn;
static CoreClr.GetFunctionPointerFn GetFunctionPointerFn;
private function void LoadScriptAssemblyFunc(uint8* appData, int64 appLength, uint8* pdbData, int64 pdbLength);
//static function void(char8* path) LoadScriptAssembly;
static LoadScriptAssemblyFunc _loadScriptAssembly;
static function void() _unloadAssemblies;
// public static unsafe void GetScriptClasses(void** outBuffer, long* length)
private function void GetScriptClassesFunc(void** outBuffer, int64* length);
static GetScriptClassesFunc _getScriptClasses;
static function void() _freeScriptClassNames;
//public static unsafe ScriptFunctions CreateScriptInstance(UUID entityId, byte* scriptClassName)
private function ScriptFunctionPointers CreateScriptInstanceFunc(UUID entityId, char8* scriptClassName);
static CreateScriptInstanceFunc _createScriptInstance;
public static void Init(StringView coreAssemblyPath)
{
LoadHostFxr();
InitAndStartRuntime(coreAssemblyPath);
PrepareFunction();
}
static void LoadHostFxr()
{
char_t[256] buffer = ?;
c_size bufferSize = buffer.Count;
int rc = NetHost.get_hostfxr_path(&buffer, &bufferSize, null);
Log.EngineLogger.AssertDebug(rc == 0, "Failed to get HostFxr path.");
// Load hostfxr and get desired exports
void* lib = NetHostHelper.LoadLibrary(&buffer);
HostFxr_Init = NetHostHelper.GetExport<HostFxr.InitializeForDotnetCommandLineFn>(lib, "hostfxr_initialize_for_dotnet_command_line");
HostFxr_GetDelegate = NetHostHelper.GetExport<HostFxr.GetRuntimeDelegateFn>(lib, "hostfxr_get_runtime_delegate");
HostFxr_Close = NetHostHelper.GetExport<HostFxr.CloseFn>(lib, "hostfxr_close");
Log.EngineLogger.AssertDebug(HostFxr_Init != null && HostFxr_GetDelegate != null && HostFxr_Close != null, "Retrieving at least one HostFxr function failed.");
}
static void InitAndStartRuntime(StringView coreAssembly)
{
char_t* ptr = NetHostHelper.GetScopedRawPtr!(coreAssembly);
char_t*[2] args = char_t*[](
ptr,
null
);
HostFxr.Handle cxt = null;
int rc = HostFxr_Init(1, &args, null, &cxt);
if (rc != 0 || cxt == null)
{
Log.EngineLogger.Error($"Failed to initialize HostFxr: {rc}");
HostFxr_Close(cxt);
}
Log.EngineLogger.Assert(rc == 0, "Failed to initialize HostFxr.");
rc = HostFxr_GetDelegate(cxt, .LoadAssemblyAndGetFunctionPointer, (void**)&LoadAssemblyAndGetFunctionPointerFn);
Log.EngineLogger.Assert(rc == 0, scope $"Get delegate failed: {rc}. (LoadAssemblyAndGetFunctionPointer)");
rc = HostFxr_GetDelegate(cxt, .GetFunctionPointer, (void**)&GetFunctionPointerFn);
Log.EngineLogger.Assert(rc == 0, scope $"Get delegate failed: {rc}. (GetFunctionPointer)");
HostFxr_Close(cxt);
}
static void PrepareFunction()
{
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "LoadScriptAssembly", out _loadScriptAssembly);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "UnloadAssemblies", out _unloadAssemblies);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "GetScriptClasses", out _getScriptClasses);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "FreeScriptClassNames", out _freeScriptClassNames);
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "CreateScriptInstance", out _createScriptInstance);
}
public static void LoadAppAssembly(Span<uint8> appAssemblyData, Span<uint8> pdbData)
{
_loadScriptAssembly(appAssemblyData.Ptr, appAssemblyData.Length, pdbData.Ptr, pdbData.Length);
}
public static void UnloadAssemblies()
{
_unloadAssemblies();
}
/// Gets an array of script class infos. Use FreeScriptClassNames to release the buffer.
public static void GetScriptClasses(out void* outBuffer, out int64 entryCount)
{
outBuffer = null;
entryCount = 0;
_getScriptClasses(&outBuffer, &entryCount);
}
/// Releases the buffer created by GetScriptClasses
public static void FreeScriptClassNames()
{
_freeScriptClassNames();
}
public static ScriptFunctionPointers CreateScriptInstance(UUID entityId, StringView scriptName)
{
return _createScriptInstance(entityId, scriptName.Ptr);
}
public static int GetFunctionPointer<T>(StringView typeName, StringView methodName, StringView delegateName, out T outDelegate) where T: operator explicit void*
{
void* funPtr = null;
int rc = GetFunctionPointerFn(NetHostHelper.GetScopedRawPtr!(typeName),
NetHostHelper.GetScopedRawPtr!(methodName), NetHostHelper.GetScopedRawPtr!(delegateName),
null, null, out funPtr);
outDelegate = (T)funPtr;
return rc;
}
public static int GetFunctionPointerDefaultDelegate(StringView typeName, StringView methodName, out CoreClr.DefaultEntryPoint outDelegate)
{
void* funPtr = null;
int rc = GetFunctionPointerFn(NetHostHelper.GetScopedRawPtr!(typeName),
NetHostHelper.GetScopedRawPtr!(methodName), null, null, null, out funPtr);
outDelegate = (CoreClr.DefaultEntryPoint)funPtr;
return rc;
}
public static int GetFunctionPointerUnmanagedCallersOnly<T>(StringView typeName, StringView methodName, out T outDelegate) where T: operator explicit void*
{
void* funPtr = null;
int rc = GetFunctionPointerFn(NetHostHelper.GetScopedRawPtr!(typeName),
NetHostHelper.GetScopedRawPtr!(methodName), CoreClr.UNMANAGEDCALLERSONLY_METHOD, null, null, out funPtr);
outDelegate = (T)funPtr;
return rc;
}
}
@@ -0,0 +1,81 @@
using System;
using GlitchyEngine.Core;
using Mono;
namespace GlitchyEngine.Scripting;
public class MonoExceptionHelper : RefCounter
{
private String _fullName ~ delete _;
private String _message ~ delete _;
private String _stackTrace ~ delete _;
/// The clean stack trace only contains the Managed Stack (the full trace contains one line for the native-to-managed entry)
private StringView _cleanStackTrace;
private MonoExceptionHelper _innerException ~ _?.ReleaseRef();
public StringView FullName => _fullName;
public StringView Message => _message;
public StringView StackTrace => _stackTrace;
public StringView CleanStackTrace => _cleanStackTrace;
public MonoExceptionHelper InnerException => _innerException;
public UUID Instance { get; set; }
public this(MonoException* exception)
{
MonoObject* exObject = (MonoObject*)exception;
MonoClass* monoClass = Mono.mono_object_get_class(exObject);
StringView classNamespace = .(Mono.mono_class_get_namespace(monoClass));
StringView className = .(Mono.mono_class_get_name(monoClass));
_fullName = new $"{classNamespace}.{className}";
GetMessage(exObject, monoClass);
GetStackTrace(exception);
GetInnerException(exObject, monoClass);
}
private void GetMessage(MonoObject* exceptionObject, MonoClass* monoClass)
{
var messageProperty = Mono.mono_class_get_property_from_name(monoClass, "Message");
MonoObject* message = Mono.mono_property_get_value(messageProperty, exceptionObject, null, null);
char8* exMessage = Mono.mono_string_to_utf8((.)message);
_message = new String(exMessage);
Mono.mono_free(exMessage);
}
private void GetStackTrace(MonoException* exception)
{
char8* stacktracePtr = Mono.mono_exception_get_managed_backtrace(exception);
_stackTrace = new String(stacktracePtr);
int entryIndex = _stackTrace.IndexOf("at (wrapper native-to-managed)");
if (entryIndex != -1)
_cleanStackTrace = _stackTrace.Substring(0, entryIndex);
else
_cleanStackTrace = _stackTrace;
}
private void GetInnerException(MonoObject* exceptionObject, MonoClass* monoClass)
{
MonoProperty* innerExceptionProperty = Mono.mono_class_get_property_from_name(monoClass, "InnerException");
MonoObject* innerException = Mono.mono_property_get_value(innerExceptionProperty, exceptionObject, null, null);
if (innerException != null)
_innerException = new MonoExceptionHelper((MonoException*)innerException);
}
}
+160 -39
View File
@@ -13,13 +13,44 @@ public struct ScriptField
internal MonoClassField* _monoField;
public bool IsStatic;
public ScriptFieldType FieldType;
public SharpType SharpType;
public int SizeInBytes;
internal this(StringView name, MonoClassField* monoField, bool isStatic, ScriptFieldType fieldType)
internal this(StringView name, MonoClassField* monoField, bool isStatic, ScriptFieldType fieldType, int sizeInBytes, SharpType sharpType)
{
Name = name;
_monoField = monoField;
IsStatic = isStatic;
FieldType = fieldType;
SharpType = sharpType;
SizeInBytes = sizeInBytes;
}
public bool IsType(SharpClass otherClass, bool checkIfSubtype)
{
return IsType(otherClass.GetMonoType(), checkIfSubtype);
}
public bool IsType(MonoType* otherType, bool checkIfSubtype)
{
MonoType* myType = GetMonoType();
if (checkIfSubtype)
{
MonoClass* myClass = Mono.mono_type_get_class(myType);
MonoClass* otherClass = Mono.mono_type_get_class(otherType);
return Mono.mono_class_is_subclass_of(myClass, otherClass, false);
}
else
{
return myType == otherType;
}
}
public MonoType* GetMonoType()
{
return Mono.mono_field_get_type(_monoField);
}
}
@@ -90,9 +121,19 @@ class SharpClass : SharpType
ExtractFields();
}
internal MonoType* GetMonoType()
{
return Mono.mono_class_get_type(_monoClass);
}
internal bool IsType(MonoType* type)
{
return Mono.mono_class_get_type(_monoClass) == type;
return GetMonoType() == type;
}
internal bool IsSubclass(MonoClass* @class)
{
return Mono.mono_class_is_subclass_of(_monoClass, @class, false);
}
private void ExtractFields()
@@ -110,10 +151,12 @@ class SharpClass : SharpType
ScriptFieldType fieldType = ScriptEngineHelper.GetScriptFieldType(type);
SharpType sharpType = null;
// If field type is none the field might be a struct, class or enum
if (fieldType == .None)
{
SharpType sharpType = ScriptEngine.GetSharpType(type);
sharpType = ScriptEngine.GetSharpType(type);
fieldType = sharpType?.ScriptType ?? .None;
if (sharpType == null)
@@ -132,7 +175,79 @@ class SharpClass : SharpType
(attributes != null &&
Mono.mono_custom_attrs_has_attr(attributes, ScriptEngine.Attributes.s_ShowInEditorAttribute)))
{
_monoFields[name] = .(name, currentField, flags.HasFlag(.Static), fieldType);
MonoClass* fieldClass = Mono.mono_type_get_class(type);
int sizeInBytes = 8;
if (fieldClass != null)
{
sizeInBytes = Mono.mono_class_instance_size(fieldClass);
}
else
{
}
_monoFields[name] = .(name, currentField, flags.HasFlag(.Static), fieldType, sizeInBytes, sharpType);
}
}
}
}
struct EnumValue
{
public StringView Name;
public uint64 Value;
public this(StringView name, uint64 value)
{
Name = name;
Value = value;
}
}
class SharpEnum : SharpClass
{
private append Dictionary<uint64, EnumValue> _values = .();
public Dictionary<uint64, EnumValue> Values => _values;
private int _underlyingSize = 0;
public this(StringView classNamespace, StringView className, MonoImage* image)
: base(classNamespace, className, image, .Enum)
{
ExtractEnumValues();
}
private void ExtractEnumValues()
{
_underlyingSize = Mono.mono_class_instance_size(_monoClass);
Log.EngineLogger.Assert(_underlyingSize != 0);
var vtable = Mono.mono_class_vtable(ScriptEngine.[Friend]s_AppDomain, _monoClass);
void* iterator = null;
MonoClassField* currentField = null;
while ((currentField = Mono.mono_class_get_fields(_monoClass, &iterator)) != null)
{
MonoType* fieldType = Mono.mono_field_get_type(currentField);
MonoClass* fieldClass = Mono.mono_type_get_class(fieldType);
FieldAttribute fieldFlags = (.)Mono.mono_field_get_flags(currentField);
if (fieldFlags.HasFlag(.Public) && fieldFlags.HasFlag(.Static) &&
fieldClass != null && Mono.mono_class_is_subclass_of(fieldClass, _monoClass, false))
{
StringView fieldName = StringView(Mono.mono_field_get_name(currentField));
uint64 value = 0;
Mono.mono_field_static_get_value(vtable, currentField, &value);
EnumValue enumValue = .(fieldName, value);
_values.Add(value, enumValue);
}
}
}
@@ -180,8 +295,8 @@ class ScriptClass : SharpClass
public Dictionary<StringView, ScriptField> Fields => _monoFields;*/
[AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image) :
base(classNamespace, className, image)
public this(StringView classNamespace, StringView className, MonoImage* image, ScriptFieldType scriptFieldType = .Class) :
base(classNamespace, className, image, scriptFieldType)
{
//_constructor = (ConstructorMethod)GetMethodThunk(".ctor", 1); // GetMethod(".ctor", 1);//
_constructor = GetMethod(".ctor", 1);
@@ -190,59 +305,50 @@ class ScriptClass : SharpClass
_onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy");
}
public void OnCreate(MonoObject* instance)
public void OnCreate(MonoObject* instance, out MonoException* exception)
{
MonoException* exception = null;
exception = null;
if (_onCreate != null)
_onCreate(instance, &exception);
}
public void OnUpdate(MonoObject* instance, float deltaTime)
public void OnUpdate(MonoObject* instance, float deltaTime, out MonoException* exception)
{
MonoException* exception = null;
exception = null;
if (_onUpdate != null)
_onUpdate(instance, deltaTime, &exception);
if (exception != null)
{
char8* str = Mono.mono_string_to_utf8(exception.Message);
Log.EngineLogger.Error($"Exception in \"{_fullName}.OnUpdate\". Message:\"{StringView(str)}\"");
Mono.mono_free(str);
}
}
public void OnDestroy(MonoObject* instance)
public void OnDestroy(MonoObject* instance, out MonoException* exception)
{
MonoException* exception;
exception = null;
if (_onDestroy != null)
_onDestroy(instance, &exception);
}
public MonoObject* CreateInstance(UUID uuid)
public MonoObject* CreateInstance(UUID uuid, out MonoException* exception)
{
MonoObject* instance = Mono.mono_object_new(ScriptEngine.[Friend]s_AppDomain, _monoClass);
// TODO: I think this is a bit dirty
// Invoke empty constructor to fill fields
Mono.mono_runtime_object_init(instance);
// Invoke constructor with UUID
#unwarn
ScriptEngine.[Friend]s_EngineObject.Invoke(ScriptEngine.[Friend]s_EngineObject._constructor, instance, &uuid);
ScriptEngine.[Friend]s_EngineObject.Invoke(ScriptEngine.[Friend]s_EngineObject._constructor, instance, out exception, &uuid);
//MonoException* exception = null;
//#unwarn
//ScriptEngine.[Friend]s_EntityRoot._constructor(instance, uuid, &exception);
//ScriptEngine.[Friend]s_EntityRoot.Invoke(_constructor, instance, &uuid);
return instance;
}
/*MonoObject* exception = null;
#unwarn*/
//Mono.mono_runtime_invoke(_constructor, instance, (.)&uuid, &exception);
//Mono.mono_runtime_object_init(instance);
//MonoException* exception;
//_constructor(instance, uuid, &exception);
public MonoObject* CreateInstance()
{
MonoObject* instance = Mono.mono_object_new(ScriptEngine.[Friend]s_AppDomain, _monoClass);
// Invoke empty constructor to fill fields
Mono.mono_runtime_object_init(instance);
return instance;
}
@@ -265,12 +371,22 @@ class ScriptClass : SharpClass
public MonoObject* Invoke(MonoMethod* method, MonoObject* instance, void** args = null)
{
MonoObject* exception = null;
return Mono.mono_runtime_invoke(method, instance, args, &exception);
MonoObject* result = Mono.mono_runtime_invoke(method, instance, args, &exception);
if (exception != null)
ScriptEngine.HandleMonoException((MonoException*)exception);
return result;
}
public MonoObject* Invoke(MonoMethod* method, MonoObject* instance, params void*[] args)
public MonoObject* Invoke(MonoMethod* method, MonoObject* instance, out MonoException* exception, params void*[] args)
{
return Mono.mono_runtime_invoke(method, instance, args.Ptr, null);
exception = null;
MonoObject* result = Mono.mono_runtime_invoke(method, instance, args.Ptr, (.)&exception);
return result;
}
public T Invoke<T>(MonoMethod* method, MonoObject* instance, params void*[] args)
@@ -282,12 +398,17 @@ class ScriptClass : SharpClass
public T GetFieldValue<T>(MonoObject* instance, MonoClassField* field)
{
T value = default;
Mono.Mono.mono_field_get_value(instance, field, &value);
Mono.mono_field_get_value(instance, field, &value);
return value;
}
public void SetFieldValue<T>(MonoObject* instance, MonoClassField* field, in T value)
{
Mono.Mono.mono_field_set_value(instance, field, &value);
Mono.mono_field_set_value(instance, field, &value);
}
public void SetFieldValue<T>(MonoObject* instance, MonoClassField* field, in T value) where T : struct*
{
Mono.mono_field_set_value(instance, field, value);
}
}
+294 -261
View File
@@ -5,8 +5,6 @@ using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using GlitchyEngine.World;
using NetHostBeef;
using System.Interop;
namespace GlitchyEngine.Scripting;
@@ -47,73 +45,6 @@ static sealed class ScriptEngineHelper
}
}
public enum ScriptMethods : uint32
{
None = 0,
OnCreate = 0x1,
OnUpdate = 0x2,
OnDestroy = 0x4,
}
class SexyScriptClass
{
public String FullName ~ delete _;
public StringView Name;
public Guid Guid;
public ScriptMethods Methods;
public this(StringView fullName, Guid guid, ScriptMethods methods)
{
FullName = new String(fullName);
Guid = guid;
int lastDotIndex = FullName.LastIndexOf('.');
if (lastDotIndex == -1)
Name = FullName;
else
Name = FullName.Substring(lastDotIndex + 1);
Methods = methods;
}
}
class SexyScriptInstance
{
public SexyScriptClass ScriptClass;
public ScriptFunctionPointers Functions;
public this(SexyScriptClass scriptClass, ScriptFunctionPointers functions)
{
ScriptClass = scriptClass;
Functions = functions;
}
public void InvokeOnCreate()
{
Functions.OnCreateMethod();
}
/*public void InvokeOnUpdate(float deltaTime)
{
CoreClrHelper.InvokeOnUpdate(float deltaTime);
//Functions.OnUpdateMethod(deltaTime);
}*/
public void InvokeOnDestroy()
{
Functions.OnDestroyMethod();
}
}
public struct ScriptFunctionPointers
{
public function void() OnCreateMethod;
public function void(float) OnUpdateMethod;
public function void() OnDestroyMethod;
}
static class ScriptEngine
{
private static MonoDomain* s_RootDomain;
@@ -127,16 +58,25 @@ static class ScriptEngine
private static Scene s_Context ~ _?.ReleaseRef();
private static ScriptClass s_ComponentRoot ~ _?.ReleaseRef();
private static ScriptClass s_EntityRoot ~ _?.ReleaseRef();
private static ScriptClass s_EngineObject ~ _?.ReleaseRef();
private static Dictionary<StringView, SharpType> _sharpClasses = new .() ~ DeleteDictionaryAndReleaseValues!(_);
private static Dictionary<StringView, SexyScriptClass> _entityScripts = new .() ~ DeleteDictionaryAndValues!(_);
private static Dictionary<StringView, ScriptClass> _entityScripts = new .() ~ DeleteDictionaryAndReleaseValues!(_);
private static Dictionary<StringView, ScriptClass> _componentClasses = new .() ~ DeleteDictionaryAndReleaseValues!(_);
private static Dictionary<UUID, SexyScriptInstance> _entityScriptInstances = new .() ~ DeleteDictionaryAndValues!(_);
private static Dictionary<UUID, ScriptInstance> _entityScriptInstances = new .() ~ {
for (var entry in _)
{
entry.value?.ReleaseRef();
}
delete _;
}
public static Dictionary<StringView, SexyScriptClass> EntityClasses => _entityScripts;
public static Dictionary<StringView, ScriptClass> EntityClasses => _entityScripts;
public static Dictionary<StringView, ScriptClass> ComponentClasses => _componentClasses;
public static Scene Context => s_Context;
@@ -160,34 +100,48 @@ static class ScriptEngine
internal static MonoClass* s_ShowInEditorAttribute;
}
private static String _coreAssemblyPath = "resources/scripts/ScriptCore.dll";
private static String _appAssemblyPath = "SandboxProject/Assets/Scripts/bin/Sandbox.dll";
public static void Init()
{
CoreClrHelper.Init(_coreAssemblyPath);
InitMono();
ScriptGlue.Init();
GetFunctions();
//ScriptGlue.Init();
LoadScriptAssembly();
LoadScriptAssemblies();
InitAssemblyWatcher();
}
static void InitMono()
{
Mono.mono_set_assemblies_path("mono/lib");
if (_debuggingEnabled)
{
char8*[2] options = .(
"--debugger-agent=transport=dt_socket,address=127.0.0.1:2550,server=y,suspend=n,loglevel=3,logfile=MonoDebugger.log",
"--soft-breakpoints"
);
Mono.mono_jit_parse_options(options.Count, &options);
Mono.mono_debug_init(.Mono);
}
s_RootDomain = Mono.mono_jit_init("GlitchyEngineJITRuntime");
Log.EngineLogger.Assert(s_RootDomain != null, "Failed to initialize mono root domain");
if (_debuggingEnabled)
{
Mono.mono_debug_domain_create(s_RootDomain);
}
Mono.mono_thread_set_main(Mono.mono_thread_current());
}
static void InitAssemblyWatcher()
{
if (_userAssemblyWatcher == null)
{
String directoryPath = scope String();
Path.GetDirectoryPath(_appAssemblyPath, directoryPath);
String fileName = scope String();
Path.GetFileName(_appAssemblyPath, fileName);
// TODO: Obviously don't hardcode path
_userAssemblyWatcher = new FileSystemWatcher(directoryPath, fileName);
_userAssemblyWatcher = new FileSystemWatcher("SandboxProject/Assets/Scripts/bin/", "*/Sandbox.dll");
_userAssemblyWatcher.OnChanged.Add(new (fileName) =>
{
// TODO: Temporary, we want to be able to reload while in play-mode. (+ Editor Scripts will be a thing some day)
@@ -214,44 +168,24 @@ static class ScriptEngine
_userAssemblyWatcher.StartRaisingEvents();
}
static void LoadScriptAssembly()
static void LoadScriptAssemblies()
{
List<uint8> data = new List<uint8>(1024);
File.ReadAll(_appAssemblyPath, data);
CreateAppDomain("GlitchyEngineScriptRuntime");
(s_CoreAssembly, s_CoreAssemblyImage) = LoadAssembly("resources/scripts/ScriptCore.dll", _debuggingEnabled);
(s_AppAssembly, s_AppAssemblyImage) = LoadAssembly("SandboxProject/Assets/Scripts/bin/Sandbox.dll", _debuggingEnabled);
List<uint8> pdbData = new List<uint8>(1024);
ClearDictionaryAndReleaseValues!(_sharpClasses);
String pdbPath = scope .();
Path.ChangeExtension(_appAssemblyPath, ".pdb", pdbPath);
GetCoreAttributes();
GetCoreClasses();
File.ReadAll(pdbPath, pdbData);
CoreClrHelper.LoadAppAssembly(data, pdbData);
delete data;
delete pdbData;
GetComponentsFromAssemblies();
GetEntitiesFromAssemblies();
//ScriptGlue.RegisterManagedComponents();
ScriptGlue.RegisterManagedComponents();
}
public static void ReloadAssemblies()
{
Debug.Profiler.ProfileFunction!();
CoreClrHelper.UnloadAssemblies();
LoadScriptAssembly();
// TODO: ScriptFields might get added
// TODO: ScriptField Types may change after reload!
// TODO: Scripts may be renamed (probably not detectable (trivially))
// TODO: Reload in play mode
// Only scripts that were changed should actually be reinstatiated
}
public static void SetContext(Scene scene)
{
SetReference!(s_Context, scene);
@@ -259,54 +193,39 @@ static class ScriptEngine
public static void OnRuntimeStop()
{
ThrowUnimplemented();
// TODO: Also check, if something has to happen on the C# side!
ClearDictionaryAndDeleteValues!(_entityScriptInstances);
/*for (var entry in _entityScriptInstances)
for (var entry in _entityScriptInstances)
{
entry.value.ReleaseRef();
}
_entityScriptInstances.Clear();
*/
SetContext(null);
}
private function void InvokeOnUpdateFunc(UUID entityId, float deltaTime);
private static InvokeOnUpdateFunc _InvokeOnUpdate;
private static void GetFunctions()
{
CoreClrHelper.GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "InvokeOnUpdate", out _InvokeOnUpdate);
}
public static bool InitializeInstance(Entity entity, ScriptComponent* script)
{
SexyScriptClass scriptClass = GetScriptClass(script.ScriptClassName);
ScriptClass scriptClass = GetScriptClass(script.ScriptClassName);
if (scriptClass == null)
return false;
ScriptFunctionPointers functions = CoreClrHelper.CreateScriptInstance(entity.UUID, scriptClass.FullName);
UUID entityId = entity.UUID;
script.Instance = new SexyScriptInstance(scriptClass, functions);//new ScriptInstance(scriptClass);
// script.Instance..ReleaseRef();
script.Instance = new ScriptInstance(entityId, scriptClass);
script.Instance..ReleaseRef();
_entityScriptInstances[entity.UUID] = script.Instance;
_entityScriptInstances[entityId] = script.Instance..AddRef();
// CopyEditorFieldsToInstance(entity, script);
script.Instance.Instantiate(entityId);
return true;
}
private static void CopyEditorFieldsToInstance(Entity entity, ScriptComponent* script)
public static void CopyEditorFieldsToInstance(Entity entity, ScriptComponent* script)
{
ThrowUnimplemented();
Log.EngineLogger.AssertDebug(script.Instance != null);
/*// Technically the map is for a different entity (namely the editor-entity),
// Technically the map is for a different entity (namely the editor-entity),
// however the UUID is the same, so we get the correct field map
let fields = GetScriptFieldMap(entity);
@@ -316,95 +235,125 @@ static class ScriptEngine
ScriptField scriptField = script.Instance.ScriptClass.Fields[fieldName];
script.Instance.SetFieldValue(scriptField, field._data);
}*/
switch (scriptField.FieldType)
{
case .Entity:
// On the C# side we actually differentiate between an Entity and the Script
// in the sense that getting an entity and a script yields two different results (one creates a new Entity-Class instance, the other returns the actual instance).
// But here its just easier to always use the script instance.
// Obviously breaks once we support multiple scripts per entity.
UUID referencedId = field.GetData<UUID>();
MonoObject* referencedEntity = GetManagedInstance(referencedId);
script.Instance.SetFieldValue(scriptField, referencedEntity);
case .Component:
// We create a new instance of a component class
MonoType* type = scriptField.GetMonoType();
SharpType sharpType = ScriptEngine.GetSharpType(type);
var componentClass = ComponentClasses[sharpType.FullName];
MonoObject* componentInstance = script.Instance.CreateComponentInstance(componentClass);
script.Instance.SetFieldValue(scriptField, componentInstance);
sharpType.ReleaseRef();
default:
script.Instance.SetFieldValue(scriptField, field._data);
}
}
}
struct ScriptClassInfo
private static MonoAssembly* LoadCSharpAssembly(StringView assemblyPath, bool loadPDB = false)
{
public char8* Name;
public Guid Guid;
public ScriptMethods Methods;
List<uint8> data = new List<uint8>(1024);
File.ReadAll(assemblyPath, data);
// NOTE: We can't use this image for anything other than loading the assembly because this image doesn't have a reference to the assembly
MonoImageOpenStatus status = .ImageInvalid;
MonoImage* image = Mono.mono_image_open_from_data_full(data.Ptr, (.)data.Count, true, &status, false);
delete data;
if (status != .Ok)
{
char8* errorMessage = Mono.mono_image_strerror(status);
Log.EngineLogger.Error($"Failed to load C# Assembly: \"{StringView(errorMessage)}\"");
return null;
}
if (loadPDB)
{
String pdbPath = scope .();
Path.ChangeExtension(assemblyPath, ".pdb", pdbPath);
if (File.Exists(pdbPath))
{
Log.EngineLogger.Trace($"Loading PDB \"{pdbPath}\"...");
List<uint8> pdbData = new List<uint8>(1024);
let result = File.ReadAll(pdbPath, pdbData);
if (result case .Err(let error))
{
Log.EngineLogger.Error("Failed to load PDB file ({error}).");
}
Mono.mono_debug_open_image_from_memory(image, pdbData.Ptr, (int32)pdbData.Count);
delete pdbData;
}
else
{
Log.EngineLogger.Warning($"Debugging enabled but no PDB-File found \"{assemblyPath}\".");
}
}
MonoAssembly* assembly = Mono.mono_assembly_load_from_full(image, assemblyPath.ToScopeCStr!(), &status, 0);
Mono.mono_image_close(image);
return assembly;
}
static void CreateAppDomain(StringView name)
{
s_AppDomain = Mono.mono_domain_create_appdomain(name.ToScopeCStr!(), null);
Mono.mono_domain_set(s_AppDomain, true);
}
static (MonoAssembly* assembly, MonoImage* image) LoadAssembly(StringView filepath, bool loadPDB = false)
{
MonoAssembly* assembly = LoadCSharpAssembly(filepath, loadPDB);
MonoImage* image = Mono.mono_assembly_get_image(assembly);
return (assembly, image);
}
/// Retrieves the base classes from which every Component or Entity inherits
static void GetCoreClasses()
{
s_EngineObject?.ReleaseRef();
s_EntityRoot?.ReleaseRef();
s_ComponentRoot?.ReleaseRef();
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", s_CoreAssemblyImage, .Class);
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", s_CoreAssemblyImage, .Entity);
s_ComponentRoot = new ScriptClass("GlitchyEngine", "Component", s_CoreAssemblyImage, .Component);
}
/// Retrieves the classes for Attributes that are defined in the Core library
static void GetCoreAttributes()
{
Attributes.s_ShowInEditorAttribute = Mono.mono_class_from_name(s_CoreAssemblyImage, "GlitchyEngine.Editor", "ShowInEditorAttribute");
}
private static void GetEntitiesFromAssemblies()
{
CoreClrHelper.GetScriptClasses(let data, let entryCount);
Span<ScriptClassInfo> scriptClasses = .((.)data, entryCount);
List<SexyScriptClass> newClasses = scope .();
for (var entry in _entityScripts)
{
bool remove = true;
// We can only rely on guids.
for (var newEntry in scriptClasses)
{
if (entry.value.Guid == newEntry.Guid)
{
remove = false;
if (!String.Equals(entry.value.FullName.CStr(), newEntry.Name))
{
@entry.Remove();
entry.value.FullName.Set(StringView(newEntry.Name));
}
newClasses.Add(entry.value);
}
}
if (remove)
{
@entry.Remove();
delete entry.value;
}
}
for (var entry in scriptClasses)
{
SexyScriptClass scriptClass = new .(StringView(entry.Name), entry.Guid, entry.Methods);
newClasses.Add(scriptClass);
}
for (var scriptClass in newClasses)
{
_entityScripts.Add(scriptClass.FullName, scriptClass);
}
CoreClrHelper.FreeScriptClassNames();
// TODO: Cleanup
/*for (var entry in _entityScripts)
{
entry.value.ReleaseRef();
}
_entityScripts.Clear();
for (var sharpClass in _sharpClasses)
{
sharpClass.value.ReleaseRef();
}
_sharpClasses.Clear();
if (s_EngineObject != null)
{
s_EngineObject.ReleaseRef();
s_EntityRoot.ReleaseRef();
}
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", s_CoreAssemblyImage);
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", s_CoreAssemblyImage);
Log.EngineLogger.Assert(s_EntityRoot != null);
Attributes.s_ShowInEditorAttribute = Mono.mono_class_from_name(s_CoreAssemblyImage, "GlitchyEngine.Editor", "ShowInEditorAttribute");
ClearDictionaryAndReleaseValues!(_entityScripts);
MonoTableInfo* typeDefinitionsTable = Mono.mono_image_get_table_info(s_AppAssemblyImage, .MONO_TABLE_TYPEDEF);
int32 numTypes = Mono.mono_table_info_get_rows(typeDefinitionsTable);
@@ -419,25 +368,71 @@ static class ScriptEngine
MonoClass* monoClass = Mono.mono_class_from_name(s_AppAssemblyImage, nameSpace, name);
// Check if it is an entity
if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, s_EntityRoot.[Friend]_monoClass, false))
{
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name), s_AppAssemblyImage);
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name), s_AppAssemblyImage, .Entity);
_entityScripts.Add(entityScript.FullName, entityScript);
Log.EngineLogger.Info($"Added entity \"{entityScript.FullName}\"");
}
}*/
}
}
private static void GetComponentsFromAssemblies()
{
ClearDictionaryAndReleaseValues!(_componentClasses);
MonoTableInfo* typeDefinitionsTable = Mono.mono_image_get_table_info(s_CoreAssemblyImage, .MONO_TABLE_TYPEDEF);
int32 numTypes = Mono.mono_table_info_get_rows(typeDefinitionsTable);
for (int32 i = 0; i < numTypes; i++)
{
int32[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE] cols = .();
Mono.mono_metadata_decode_row(typeDefinitionsTable, i, (.)&cols, (.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE);
char8* nameSpace = Mono.mono_metadata_string_heap(s_CoreAssemblyImage, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAMESPACE]);
char8* name = Mono.mono_metadata_string_heap(s_CoreAssemblyImage, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAME]);
MonoClass* monoClass = Mono.mono_class_from_name(s_CoreAssemblyImage, nameSpace, name);
// Check if it is a component but not the root Component class
if (monoClass != null && monoClass != s_ComponentRoot.[Friend]_monoClass && Mono.mono_class_is_subclass_of(monoClass, s_ComponentRoot.[Friend]_monoClass, false))
{
ScriptClass componentClass = new ScriptClass(StringView(nameSpace), StringView(name), s_CoreAssemblyImage, .Component);
_componentClasses.Add(componentClass.FullName, componentClass);
Log.EngineLogger.Info($"Added component \"{componentClass.FullName}\"");
}
}
}
public static void ReloadAssemblies()
{
Debug.Profiler.ProfileFunction!();
Mono.mono_domain_set(s_RootDomain, false);
Mono.mono_domain_unload(s_AppDomain);
LoadScriptAssemblies();
// TODO: ScriptFields might get added
// TODO: ScriptField Types may change after reload!
// TODO: Scripts may be renamed (probably not detectable (trivially))
// TODO: Reload in play mode
// Only scripts that were changed should actually be reinstatiated
}
public static void Shutdown()
{
/*Mono.mono_domain_set(s_RootDomain, false);
Mono.mono_domain_set(s_RootDomain, false);
Mono.mono_domain_unload(s_AppDomain);
s_AppDomain = null;
Mono.mono_jit_cleanup(s_RootDomain);
s_RootDomain = null;*/
s_RootDomain = null;
}
internal static void RegisterSharpType(SharpType sharpType)
@@ -463,34 +458,44 @@ static class ScriptEngine
StringView classNamespace = StringView(Mono.mono_class_get_namespace(monoClass));
// TODO: at the moment only allow user-structs
if (classNamespace.StartsWith("GlitchyEngine"))
return null;
//if (classNamespace.StartsWith("GlitchyEngine"))
// return null;
switch (fieldType)
ScriptFieldType scriptType = .None;
if (Mono.mono_class_is_enum(monoClass))
{
case .Class, .Valuetype, .Enum:
ScriptFieldType scriptType = .None;
scriptType = .Enum;
if (fieldType == .Class)
scriptType = .Class;
else if (fieldType == .Enum)
scriptType = .Enum;
else if (fieldType == .Valuetype)
scriptType = .Struct;
Log.EngineLogger.AssertDebug(scriptType != .None);
return new SharpClass(classNamespace, className, Mono.mono_class_get_image(monoClass), scriptType);
default:
return null;
return new SharpEnum(classNamespace, className, Mono.mono_class_get_image(monoClass));
}
else if (fieldType == .Class)
{
if (Mono.mono_class_is_subclass_of(monoClass, s_EntityRoot._monoClass, false))
{
scriptType = .Entity;
}
else if (Mono.mono_class_is_subclass_of(monoClass, s_ComponentRoot._monoClass, false))
{
scriptType = .Component;
}
else
{
scriptType = .Class;
}
}
else if (fieldType == .Valuetype)
{
scriptType = .Struct;
}
Log.EngineLogger.AssertDebug(scriptType != .None);
return new SharpClass(classNamespace, className, Mono.mono_class_get_image(monoClass), scriptType);
}
public static void CreateScriptFieldMap(Entity entity)
{
Log.EngineLogger.Error("Not implemented");
/*
Log.EngineLogger.AssertDebug(entity.IsValid);
if (_entityFields.TryGetValue(entity.UUID, var entityFields))
@@ -509,16 +514,28 @@ static class ScriptEngine
Log.EngineLogger.AssertDebug(scriptClass != null);
for (let (fieldName, field) in scriptClass.Fields)
void AddFieldsToMap(Dictionary<StringView, ScriptField> classFields, StringView baseName)
{
entityFields.Add(new String(fieldName), ScriptFieldInstance(field.FieldType));
}*/
for (let (fieldName, field) in classFields)
{
/*if (field.FieldType == .Struct && field.SharpType != null)
{
AddFieldsToMap(field.SharpType.Fields, scope $"{baseName}{fieldName}.");
}
else
{
entityFields.Add(new $"{baseName}{fieldName}", ScriptFieldInstance(field.FieldType));
}*/
entityFields.Add(new String(fieldName), ScriptFieldInstance(field.FieldType));
}
}
AddFieldsToMap(scriptClass.Fields, "");
}
public static ScriptFieldMap GetScriptFieldMap(Entity entity)
{
ThrowUnimplemented();
Log.EngineLogger.AssertDebug(entity.IsValid);
let uuid = entity.UUID;
@@ -531,23 +548,39 @@ static class ScriptEngine
public static MonoObject* GetManagedInstance(UUID entityId)
{
ThrowUnimplemented();
/*if (_entityScriptInstances.TryGetValue(entityId, let scriptInstance))
return scriptInstance.MonoInstance;*/
if (_entityScriptInstances.TryGetValue(entityId, let scriptInstance))
return scriptInstance.MonoInstance;
return null;
}
public static SexyScriptClass GetScriptClass(StringView name)
public static ScriptClass GetScriptClass(StringView name)
{
EntityClasses.TryGetValue(name, let scriptClass);
return scriptClass;
}
public static void InvokeOnUpdate(UUID entityId, float deltaTime)
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
{
_InvokeOnUpdate(entityId, deltaTime);
MonoExceptionHelper wrappedException = new MonoExceptionHelper(exception);
String entityInfo = scope .();
if (sourceInstance != null)
{
wrappedException.Instance = sourceInstance.EntityId;
Result<Entity> sourceEntity = Context.GetEntityByID(sourceInstance.EntityId);
if (sourceEntity case .Ok(let e))
{
entityInfo.AppendF($" ({e.Name} | {sourceInstance.EntityId})");
}
}
Log.ClientLogger.Error($"Mono Exception \"{wrappedException.FullName}\": \"{wrappedException.Message}\"{entityInfo}\nStackTrace:\n{wrappedException.StackTrace}", wrappedException);
wrappedException.ReleaseRef();
}
}
@@ -27,6 +27,7 @@ enum ScriptFieldType
case Double, Double2, Double3, Double4;
case Entity;
case Component;
public Type GetBeefType()
{
@@ -79,6 +80,8 @@ enum ScriptFieldType
case .Entity:
return typeof(UUID);
case .Component:
return typeof(UUID);
default:
return null;
+12 -9
View File
@@ -29,7 +29,7 @@ static class ScriptGlue
}
}
/*/* Adding this attribute to a method will log method entry and returned Result<T> errors */
/* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)]
struct RegisterMethodAttribute : Attribute, IOnMethodInit
{
@@ -48,13 +48,13 @@ static class ScriptGlue
}
}
}
}*/
}
public static void Init()
{
//RegisterCalls();
RegisterCalls();
//RegisterMathFunctions();
RegisterMathFunctions();
}
public static void RegisterManagedComponents()
@@ -67,11 +67,11 @@ static class ScriptGlue
RegisterComponent<Rigidbody2DComponent>("GlitchyEngine.RigidBody2D");
}
/*[RegisterMethod]
[RegisterMethod]
private static void RegisterCalls()
{
// Generated at CompTime
}*/
}
private static void RegisterComponent<T>(StringView cSharpClassName = "") where T : struct, new
{
@@ -104,10 +104,13 @@ static class ScriptGlue
}
[RegisterCall("Log::LogMessage_Impl")]
[Export, LinkName("Log::LogMessage_Impl"), CallingConvention(.Cdecl)]
static void Log(int32 logLevel, char8* message)
static void Log(int32 logLevel, MonoString* message)
{
Log.ClientLogger.Log((LogLevel)logLevel, StringView(message));
char8* utfMessage = Mono.mono_string_to_utf8(message);
Log.ClientLogger.Log((LogLevel)logLevel, StringView(utfMessage));
Mono.mono_free(utfMessage);
}
#region Input
+46 -8
View File
@@ -4,6 +4,8 @@ using System;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
class ScriptInstance : RefCounter
{
private ScriptClass _scriptClass;
@@ -11,6 +13,8 @@ class ScriptInstance : RefCounter
private MonoObject* _instance;
private uint32 _gcHandle;
private UUID _entityId;
public ScriptClass ScriptClass => _scriptClass;
/// Gets whether or not the instance has ben initialized.
@@ -21,9 +25,15 @@ class ScriptInstance : RefCounter
private bool _isCreated = false;
public this(ScriptClass scriptClass)
internal MonoObject* MonoInstance => _instance;
public UUID EntityId => _entityId;
public this(UUID entityId, ScriptClass scriptClass)
{
Log.EngineLogger.AssertDebug(scriptClass != null);
_entityId = entityId;
_scriptClass = scriptClass..AddRef();
}
@@ -31,34 +41,44 @@ class ScriptInstance : RefCounter
{
if (_instance != null)
{
_scriptClass.OnDestroy(_instance);
InvokeOnDestroy();
Mono.mono_gchandle_free(_gcHandle);
}
_scriptClass?.ReleaseRef();
}
internal MonoObject* MonoInstance => _instance;
public void Instantiate(UUID uuid)
{
_instance = _scriptClass.CreateInstance(uuid);
_instance = _scriptClass.CreateInstance(uuid, let exception);
_gcHandle = Mono.mono_gchandle_new(_instance, true);
if (exception != null)
ScriptEngine.HandleMonoException(exception, this);
}
public void InvokeOnCreate()
{
_scriptClass.OnCreate(_instance);
_scriptClass.OnCreate(_instance, let exception);
_isCreated = true;
if (exception != null)
ScriptEngine.HandleMonoException(exception, this);
}
public void InvokeOnUpdate(float deltaTime)
{
_scriptClass.OnUpdate(_instance, deltaTime);
_scriptClass.OnUpdate(_instance, deltaTime, let exception);
if (exception != null)
ScriptEngine.HandleMonoException(exception, this);
}
public void InvokeOnDestroy()
{
_scriptClass.OnDestroy(_instance);
_scriptClass.OnDestroy(_instance, let exception);
if (exception != null)
ScriptEngine.HandleMonoException(exception, this);
}
public T GetFieldValue<T>(ScriptField field)
@@ -70,4 +90,22 @@ class ScriptInstance : RefCounter
{
_scriptClass.SetFieldValue<T>(_instance, field.[Friend]_monoField, value);
}
/// Creates a new instance of the given component class and initializes it for the current entity.
public MonoObject* CreateComponentInstance(ScriptClass componentClassType)
{
MonoObject* componentInstance = componentClassType.CreateInstance();
// TODO: We could cache the property, but this might be fine
MonoProperty* entityProperty = Mono.mono_class_get_property_from_name(componentClassType.[Friend]_monoClass, "Entity");
MonoObject* exception = null;
Mono.mono_property_set_value(entityProperty, componentInstance, (void**)&_instance, &exception);
if (exception != null)
ScriptEngine.HandleMonoException((MonoException*)exception, this);
return componentInstance;
}
}
@@ -509,7 +509,7 @@ namespace GlitchyEngine.World
{
private String _scriptClassName = null;
private SexyScriptInstance _instance = null;
private ScriptInstance _instance = null;
public StringView ScriptClassName
{
@@ -523,24 +523,22 @@ namespace GlitchyEngine.World
}
}
public SexyScriptInstance Instance
public ScriptInstance Instance
{
[Inline]
get => _instance;
[Inline]
set mut => _instance = value;//SetReference!(_instance, value);
set mut => SetReference!(_instance, value);
}
// public bool IsInitialized => _instance?.IsInitialized ?? false;
public bool IsInitialized => _instance?.IsInitialized ?? false;
// public bool IsCreated => _instance?.IsCreated ?? false;
public bool IsCreated => _instance != null;
public bool IsCreated => _instance?.IsCreated ?? false;
public void Dispose() mut
{
delete _scriptClassName;
//ReleaseRefAndNullify!(_instance);
ReleaseRefAndNullify!(_instance);
}
}
}
+15 -6
View File
@@ -100,6 +100,15 @@ namespace GlitchyEngine.World
ScriptEngine.InitializeInstance(targetEntity, targetComponent);
}
// Copy values to entities.
// We do this in a separate loop because we might reference other entities.
// If we did it in a single loop these entities might not exist yet.
for (let (handle, script) in target._ecsWorld.Enumerate<ScriptComponent>())
{
Entity entity = .(handle, this);
ScriptEngine.CopyEditorFieldsToInstance(entity, script);
}
// Copy transforms... needs special handling for the Parent<->Child relations
for (let (sourceHandle, sourceTransform) in _ecsWorld.Enumerate<TransformComponent>())
{
@@ -272,15 +281,15 @@ namespace GlitchyEngine.World
{
if (!script.IsCreated)
{
ScriptEngine.InitializeInstance(Entity(entity, this), script);
if (!script.IsInitialized)
ScriptEngine.InitializeInstance(Entity(entity, this), script);
//if (mode.HasFlag(.Runtime))
//script.Instance.InvokeOnCreate();
if (mode.HasFlag(.Runtime))
script.Instance.InvokeOnCreate();
}
if (mode.HasFlag(.Runtime) && script.Instance.ScriptClass.Methods.HasFlag(.OnUpdate))
ScriptEngine.InvokeOnUpdate(Entity(entity, this).UUID, gameTime.DeltaTime);
//script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
if (mode.HasFlag(.Runtime))
script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
}
}
+40 -36
View File
@@ -191,9 +191,7 @@ class SceneSerializer
{
Serialize.Value(writer, "ScriptClass", component.ScriptClassName);
ThrowUnimplemented();
/*//if (component.HasScript)
//if (component.HasScript)
// TODO: Thats not a good check, I think. At least we know the script class is valid
if (ScriptEngine.GetScriptClass(component.ScriptClassName) != null)
{
@@ -223,7 +221,7 @@ class SceneSerializer
}
}
}
}*/
}
});
}
@@ -581,9 +579,9 @@ class SceneSerializer
// But that is a bug for me to rediscover in the distant future, so in case this bug occurred and it took ages for you to
// figure out what happened: You are welcome :)
//ScriptEngine.CreateScriptFieldMap(entity);
ScriptEngine.CreateScriptFieldMap(entity);
//var fields = ScriptEngine.GetScriptFieldMap(entity);
var fields = ScriptEngine.GetScriptFieldMap(entity);
Try!(reader.EntryEnd());
@@ -615,47 +613,53 @@ class SceneSerializer
// Allocate a string on the stack, because the dictionary uses a string as key
String fieldNameString = scope .(fieldName);
//if (fields.ContainsKey(fieldNameString))
//{
//var field = ref fields[fieldNameString];
Result<StringView> fieldTypeName = reader.Type();
Result<StringView> fieldTypeName = reader.Type();
if (fieldTypeName case .Err)
{
Log.EngineLogger.Error($"Failed to read field type for field \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}
/*if (fieldTypeName case .Err)
{
Log.EngineLogger.Error($"Failed to read field type for field \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}*/
Result<ScriptFieldType> fieldType = Enum.Parse<ScriptFieldType>(fieldTypeName, true);
Result<ScriptFieldType> fieldType = Enum.Parse<ScriptFieldType>(fieldTypeName, true);
if ((fieldType case .Err))
{
Log.EngineLogger.Error($"Error deserializing field type (Raw string: \"{fieldTypeName}\" of field: \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}
/*if ((fieldType case .Err) || (fieldType != field.Type))
uint8[sizeof(Matrix)] data = .();
if (Deserialize.Value(reader, ValueView(fieldType.Value.GetBeefType(), &data), gBonEnv) case .Err)
{
Log.EngineLogger.Error($"Failed to deserialize data for field: \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}
if (fields.ContainsKey(fieldNameString))
{
var field = ref fields[fieldNameString];
// Make sure the type we deserialized actually is correct.
if (fieldType != field.Type)
{
Log.EngineLogger.Error($"Unexpected field type (\"{fieldTypeName}\" instead of \"{field.Type}\" for field: \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}*/
//void* data = &field.[Friend]_data;
uint8[128] data;
//if (Deserialize.Value(reader, ValueView(field.Type.GetBeefType(), data), gBonEnv) case .Err)
if (Deserialize.Value(reader, ValueView(fieldType.Value.GetBeefType(), &data), gBonEnv) case .Err)
{
Log.EngineLogger.Error($"Failed to deserialize data for field: \"{fieldName}\" in script \"{component.ScriptClassName}\" of entity {entity.UUID} (\"{entity.Name}\")");
reader.FileEntrySkip(1);
dontRemoveComma = true;
continue;
}
/*}
field.SetData(data);
}
else
{
Log.EngineLogger.Error($"Script \"{component.ScriptClassName}\" doesn't have a field with name \"{fieldName}\". (Entity {entity.UUID} (\"{entity.Name}\"))");
}*/
}
}
Try!(reader.ArrayBlockEnd());
+44
View File
@@ -92,6 +92,9 @@ static class Mono
[LinkName(.C)]
public static extern MonoType* mono_class_get_type(MonoClass* monoClass);
[LinkName(.C)]
public static extern int32 mono_class_instance_size(MonoClass* @class);
[LinkName(.C)]
public static extern char8* mono_class_get_namespace(MonoClass* monoClass);
@@ -158,6 +161,9 @@ static class Mono
[LinkName(.C)]
public static extern MonoType* mono_reflection_type_get_type(MonoReflectionType* reflectionType);
[LinkName(.C)]
public static extern MonoReflectionType* mono_type_get_object(MonoDomain *domain, MonoType *type);
[LinkName(.C)]
public static extern MonoClassField* mono_class_get_fields(MonoClass* klass, gpointer* iter);
@@ -179,6 +185,9 @@ static class Mono
[LinkName(.C)]
public static extern void mono_field_get_value(MonoObject* object, MonoClassField* field, void* value);
[LinkName(.C)]
public static extern void mono_field_static_get_value(MonoVTable* vt, MonoClassField* field, void* value);
/// Gets the field as object, boxes the value if it is a valuetype.
[LinkName(.C)]
public static extern MonoObject* mono_field_get_value_object(MonoDomain* domain, MonoClassField* field, MonoObject* obj);
@@ -229,6 +238,37 @@ static class Mono
[LinkName(.C)]
public static extern MonoThread* mono_thread_current();
#region Property
[LinkName(.C)]
public static extern MonoProperty* mono_class_get_property_from_name(MonoClass *klass, char8* name);
[LinkName(.C)]
public static extern void mono_property_set_value(MonoProperty *prop, void *obj, void **@params, MonoObject **exc);
[LinkName(.C)]
public static extern MonoObject* mono_property_get_value(MonoProperty *prop, void *obj, void** @params, MonoObject** exc);
#endregion
[LinkName(.C)]
public static extern char8* mono_exception_get_managed_backtrace(MonoException* exc);
[LinkName(.C)]
public static extern MonoClass* mono_object_get_class(MonoObject* obj);
[LinkName(.C)]
public static extern mono_bool mono_class_is_enum(MonoClass* @class);
[LinkName(.C)]
public static extern MonoVTable* mono_class_vtable(MonoDomain* domain, MonoClass* @class);
[LinkName(.C)]
public static extern MonoObject* mono_value_box(MonoDomain* domain, MonoClass* klass, gpointer value);
}
struct MonoDomain;
@@ -247,8 +287,12 @@ struct MonoObject;
struct MonoMethod;
struct MonoProperty;
struct MonoThread;
struct MonoVTable;
struct MonoException
{
void* _bla;
-11
View File
@@ -1,13 +1,2 @@
# glitchy-engine-beef
A complete refocusation, reimagination and reimplementation of the world-renowned "Glitchy Engine"
# Building
In order to build this repository you need the following requirements:
- Windows 10 or newer
- [Beef](https://www.beeflang.org/) (at least 0.43.5)
- Visual Studio 2022 with the Workloads: "Desktop development with C++" and ".NET desktop development" (TODO: I think it's probably possible to just install some standalone stuff, but idk...)
- TODO: I feel like I'm missing something...
If all of the requirements are met you need to run the following command in powershell (in the root directory of the repository): `./bin/prepare_dev.ps1`. This will run a script that initializes the dependencies. TODO: This script is currently not finished
+3 -3
View File
@@ -86,10 +86,10 @@ namespace Sandbox
ImGui.Combo("Sampler", (.)&_sampleMode, items.Ptr, (.)items.Count);
ImGui.SliderFloat2("Color offset and scale", *(float[2]*)&_colorOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Alpha offset and scale", *(float[2]*)&_alphaOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Color offset and scale", ref *(float[2]*)&_colorOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Alpha offset and scale", ref *(float[2]*)&_alphaOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Position", *(float[2]*)&_position, 2 * -Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom, 2 * Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom);
ImGui.SliderFloat2("Position", ref *(float[2]*)&_position, 2 * -Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom, 2 * Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom);
ImGui.BeginChild("imageChild");
@@ -2,6 +2,12 @@ using System;
namespace GlitchyEngine.Editor;
// TODO: Maybe rename, definitely move into different namespace
/* Why?
* - this attribute also means that the Field is being serialized
* - Editor-Namespace wont be available for distribution builds of the game
*/
[AttributeUsage(AttributeTargets.Field)]
public sealed class ShowInEditorAttribute : Attribute
{
public string DisplayName { get; set; } = null;
+2 -4
View File
@@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace GlitchyEngine;
@@ -46,7 +45,6 @@ public class Log
LogMessage_Impl(LogLevel.Critical, message);
}
//[MethodImpl(MethodImplOptions.InternalCall)]
[DllImport("GlitchyEditor.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Log::LogMessage_Impl")]
public static extern void LogMessage_Impl(LogLevel logLevel, string message);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string LogMessage_Impl(LogLevel logLevel, string message);
}
+5 -8
View File
@@ -1,23 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<BaseOutputPath></BaseOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<EnableDynamicLoading>true</EnableDynamicLoading>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<!--<DebugType>embedded</DebugType>-->
<!--<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>-->
</PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="PowerShell ./postbuild.ps1 -sourceDir $(OutDir) -destinationDir &quot;..\GlitchyEditor\resources\scripts&quot;" />
</Target>
<ItemGroup>
<PackageReference Include="Half" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
-398
View File
@@ -1,18 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mime;
using System.Net.Security;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Text;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
@@ -88,389 +75,4 @@ internal static class ScriptGlue
#endregion
private static AssemblyLoadContext? _scriptAssemblyContext;
private static Assembly? _appAssembly;
[UnmanagedCallersOnly]
public static unsafe void LoadScriptAssembly(byte* assemblyData, long assemblyLength, byte* pdbData, long pdbLength)
{
using UnmanagedMemoryStream assemblyStream = new(assemblyData, assemblyLength);
using UnmanagedMemoryStream? pdbStream = (pdbData != null) ? new UnmanagedMemoryStream(pdbData, pdbLength) : null;
LoadAssembly(assemblyStream, pdbStream);
}
private static void LoadAssembly(Stream assemblyStream, Stream? pdbStream)
{
try
{
_scriptAssemblyContext ??= new AssemblyLoadContext("ScriptContext", true);
_appAssembly = _scriptAssemblyContext.LoadFromStream(assemblyStream, pdbStream);
Debug.Assert(_appAssembly != null);
// Test the loaded assembly
_appAssembly.GetType("Sandbox.TestClass")?.GetMethod("InternalTest")?.Invoke(null, null);
AssemblyLoadContext.EnterContextualReflection(_appAssembly);
}
catch (Exception e)
{
Console.WriteLine($"Fehler: {e}");
}
}
[UnmanagedCallersOnly]
public static void UnloadAssemblies()
{
_scriptAssemblyContext?.Unload();
_scriptAssemblyContext = null;
}
struct ScriptClassInfo
{
public byte[] Name;
public Guid Guid;
}
private static NativeScriptClassInfo[]? _unsafeClasses;
struct NativeScriptClassInfo
{
public IntPtr Name;
public Guid Guid;
public ScriptMethods Methods;
}
[Flags]
public enum ScriptMethods
{
None = 0,
OnCreate = 0x1,
OnUpdate = 0x2,
OnDestroy = 0x4,
}
[UnmanagedCallersOnly]
public static unsafe void GetScriptClasses(void** outBuffer, long* length)
{
AssemblyLoadContext.EnterContextualReflection(_appAssembly);
Debug.Assert(_appAssembly != null);
Internal_FreeScriptClassNames();
var types = _appAssembly.GetTypes();
List<(string Name, Guid Guid, ScriptMethods Methods)> scriptClasses = new();
_updateMethods.Clear();
foreach (var type in types)
{
if (type.IsSubclassOf(typeof(Entity)))
{
string? name = type.FullName;
if (name == null)
continue;
Guid guid = type.GUID;
ScriptMethods methods = ScriptMethods.None;
MethodInfo? onUpdateMethod = type.GetMethod("OnUpdate");
if (onUpdateMethod != null)
{
_updateMethods[type] = onUpdateMethod;
methods |= ScriptMethods.OnUpdate;
}
scriptClasses.Add((name, guid, methods));
}
}
_unsafeClasses = new NativeScriptClassInfo[scriptClasses.Count];
for (int i = 0; i < _unsafeClasses.Length; i++)
{
_unsafeClasses[i] = new NativeScriptClassInfo()
{
Guid = scriptClasses[i].Guid,
Name = Marshal.StringToCoTaskMemUTF8(scriptClasses[i].Name),
Methods = scriptClasses[i].Methods
};
}
*outBuffer = (void*)Marshal.UnsafeAddrOfPinnedArrayElement(_unsafeClasses, 0);
*length = _unsafeClasses.Length;
}
[UnmanagedCallersOnly]
public static void FreeScriptClassNames()
{
Internal_FreeScriptClassNames();
}
internal static void Internal_FreeScriptClassNames()
{
if (_unsafeClasses == null)
return;
foreach (NativeScriptClassInfo info in _unsafeClasses)
{
Marshal.FreeCoTaskMem(info.Name);
}
_unsafeClasses = null;
}
private static Dictionary<UUID, (Entity Entity, Type Type)> _entityScripts = new();
private static Dictionary<Type, MethodInfo> _updateMethods = new();
public struct ScriptFunctions
{
public IntPtr OnCreateMethod;
public IntPtr OnUpdateMethod;
public IntPtr OnDestroyMethod;
}
// Define delegate with the same signature as your method
delegate void OnCreateMethodDelegate();
// Define delegate with the same signature as your method
delegate void OnUpdateMethodDelegate(float deltaTime);
[UnmanagedCallersOnly]
public static void InvokeOnUpdate(UUID entityId, float deltaTime)
{
AssemblyLoadContext.EnterContextualReflection(_appAssembly);
Debug.Assert(_entityScripts.ContainsKey(entityId));
(Entity Entity, Type Type) entity = _entityScripts[entityId];
entity.Type.GetMethod("OnUpdate", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(entity.Entity, new object?[]{deltaTime });
_updateMethods[entity.Type].Invoke(entity.Entity, BindingFlags.NonPublic | BindingFlags.Instance, null,
new object[] { deltaTime }, null);
}
[UnmanagedCallersOnly]
public static unsafe ScriptFunctions CreateScriptInstance(UUID entityId, byte* scriptClassName)
{
AssemblyLoadContext.EnterContextualReflection(_appAssembly);
Debug.Assert(_appAssembly != null);
string? typeName = Marshal.PtrToStringUTF8((IntPtr)scriptClassName);
Debug.Assert(typeName != null);
Type? scriptType = _appAssembly.GetType(typeName);
Debug.Assert(scriptType != null, "Script class Type not found.");
//Entity? scriptInstance = Activator.CreateInstance(scriptType, BindingFlags.Instance | BindingFlags.NonPublic, null, entityId) as Entity;
// Get the private constructor
ConstructorInfo constructor = scriptType.BaseType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(UUID) }, // replace this with the types of your constructor parameters
null);
// Call the constructor to create an instance
Entity? scriptInstance = constructor?.Invoke(new object[] { entityId }) as Entity;
ScriptFunctions functions = new();
_entityScripts.Add(entityId, (scriptInstance!, scriptType));
//MethodInfo? onCreateMethod = scriptType.GetMethod("OnCreate", BindingFlags.Instance | BindingFlags.NonPublic);
//if (onCreateMethod != null)
//{
// var v = MethodHelpers.GetFunctionPointerForNativeCode(onCreateMethod, null);
//}
//if (onCreateMethod != null)
//{
// //Delegate del = CreateDelegateWithTarget(onCreateMethod, scriptInstance);
// //var createDelegate = onCreateMethod.CreateDelegate<Action>(scriptInstance);
// //var createDelegate = onCreateMethod.CreateDelegate(typeof(OnCreateMethodDelegate), scriptInstance);
// //onCreateMethod.CreateDelegate(scriptInstance);
// //functions.OnCreateMethod = Marshal.GetFunctionPointerForDelegate(createDelegate);
//}
//MethodInfo? onUpdateMethod = scriptType.GetMethod("OnUpdate", BindingFlags.Instance | BindingFlags.NonPublic);
//if (onUpdateMethod != null)
//{
// // Create the delegate from your method and instance
// OnUpdateMethodDelegate onUpdateDelegate = (OnUpdateMethodDelegate)Delegate.CreateDelegate(typeof(OnUpdateMethodDelegate), scriptInstance, onUpdateMethod);
// // Get the function pointer from your delegate
// functions.OnUpdateMethod = Marshal.GetFunctionPointerForDelegate(onUpdateDelegate);
//}
// //functions.OnUpdateMethod = Marshal.GetFunctionPointerForDelegate((float deltaTime) => onUpdateMethod.Invoke(scriptInstance, new object?[]{ deltaTime }));
//MethodInfo? onDestroyMethod = scriptType.GetMethod("OnUpdate", BindingFlags.Instance | BindingFlags.NonPublic);
//if (onDestroyMethod != null)
// functions.OnDestroyMethod = Marshal.GetFunctionPointerForDelegate(() => onDestroyMethod.Invoke(scriptInstance, null));
return functions;
}
static Delegate CreateDelegate(MethodInfo method)
{
if (method == null)
{
throw new ArgumentNullException(nameof(method));
}
if (!method.IsStatic)
{
throw new ArgumentException("The provided method must be static.", nameof(method));
}
if (method.IsGenericMethod)
{
throw new ArgumentException("The provided method must not be generic.", nameof(method));
}
return method.CreateDelegate(Expression.GetDelegateType(
(from parameter in method.GetParameters() select parameter.ParameterType)
.Concat(new[] { method.ReturnType })
.ToArray()));
}
/// <summary>
/// Create delegate by methodinfo in target
/// </summary>
/// <param name="method">method info</param>
/// <param name="target">A instance of the object which contains the method where will be execute</param>
/// <returns>delegate or null</returns>
public static Delegate? CreateDelegateWithTarget(MethodInfo? method, object? target)
{
if (method is null ||
target is null)
return null;
//if (method.IsStatic)
// return null;
if (method.IsGenericMethod)
return null;
return method.CreateDelegate(Expression.GetDelegateType(
(from parameter in method.GetParameters() select parameter.ParameterType)
.Concat(new[] { method.ReturnType })
.ToArray()), target);
}
internal static class MethodHelpers
{
private const string DelegateTypesAssemblyName = "JitDelegateTypes";
private static ModuleBuilder _modBuilder;
private static ConcurrentDictionary<(string, object), Delegate> _delegatesCache;
private static ConcurrentDictionary<string, Type> _delegateTypesCache;
static MethodHelpers()
{
AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(DelegateTypesAssemblyName), AssemblyBuilderAccess.Run);
_modBuilder = asmBuilder.DefineDynamicModule(DelegateTypesAssemblyName);
_delegatesCache = new ConcurrentDictionary<(string, object), Delegate>();
_delegateTypesCache = new ConcurrentDictionary<string, Type>();
}
public static IntPtr GetFunctionPointerForNativeCode(MethodInfo meth, object instance = null)
{
string funcName = GetFullName(meth);
Delegate dlg = _delegatesCache.GetOrAdd((funcName, instance), (_) =>
{
Type[] parameters = meth.GetParameters().Select(x => x.ParameterType).ToArray();
Type delegateType = GetDelegateType(parameters, meth.ReturnType);
return Delegate.CreateDelegate(delegateType, instance, meth);
});
return Marshal.GetFunctionPointerForDelegate<Delegate>(dlg);
}
private static string GetFullName(MethodInfo meth)
{
return $"{meth.DeclaringType.FullName}.{meth.Name}";
}
private static Type GetDelegateType(Type[] parameters, Type returnType)
{
string key = GetFunctionSignatureKey(parameters, returnType);
return _delegateTypesCache.GetOrAdd(key, (_) => MakeDelegateType(parameters, returnType, key));
}
private const MethodAttributes CtorAttributes =
MethodAttributes.RTSpecialName |
MethodAttributes.HideBySig |
MethodAttributes.Public;
private const MethodImplAttributes ImplAttributes =
MethodImplAttributes.Runtime |
MethodImplAttributes.Managed;
private const MethodAttributes InvokeAttributes =
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.NewSlot |
MethodAttributes.Virtual;
private const TypeAttributes DelegateTypeAttributes =
TypeAttributes.Class |
TypeAttributes.Public |
TypeAttributes.Sealed |
TypeAttributes.AnsiClass |
TypeAttributes.AutoClass;
private static readonly Type[] _delegateCtorSignature = { typeof(object), typeof(IntPtr) };
private static Type MakeDelegateType(Type[] parameters, Type returnType, string name)
{
TypeBuilder builder = _modBuilder.DefineType(name, DelegateTypeAttributes, typeof(MulticastDelegate));
builder.DefineConstructor(CtorAttributes, CallingConventions.Standard, _delegateCtorSignature).SetImplementationFlags(ImplAttributes);
builder.DefineMethod("Invoke", InvokeAttributes, returnType, parameters).SetImplementationFlags(ImplAttributes);
return builder.CreateTypeInfo();
}
private static string GetFunctionSignatureKey(Type[] parameters, Type returnType)
{
string sig = GetTypeName(returnType);
foreach (Type type in parameters)
{
sig += '_' + GetTypeName(type);
}
return sig;
}
private static string GetTypeName(Type type)
{
return type.FullName.Replace(".", string.Empty);
}
}
}
-16
View File
@@ -1,16 +0,0 @@
Write-Output "Preparing repository for development..."
# Change working directory to repository root
Push-Location ($PSScriptRoot + "/..")
$RootDirectory = $(Get-Location);
# Note: When adding new scripts make sure to use "Set-Location $RootDirectory" if the script changes the working directory
# Prepare NetHost
Push-Location "GlitchyEngine/vendor/NetHostBeef"
Invoke-Expression -Command "./downloadNethost.ps1"
Pop-Location
# Return to original working directory
Pop-Location