Made release buildable

This commit is contained in:
Simon Lübeß
2025-08-24 16:54:26 +02:00
parent 7df651103b
commit c94ffbef64
15 changed files with 396 additions and 147 deletions
+3
View File
@@ -6,3 +6,6 @@ TargetType = "CustomBuild"
[Configs.Debug.Win64]
PostBuildCmds = ["dotnet build \"$(WorkspaceDir)/ScriptCore/ScriptCore.csproj\" -c Debug"]
[Configs.Release.Win64]
PostBuildCmds = ["dotnet build \"$(WorkspaceDir)/ScriptCore/ScriptCore.csproj\" -c Release"]
-2
View File
@@ -3,7 +3,6 @@
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<BaseOutputPath></BaseOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<RootNamespace>GlitchyEngine</RootNamespace>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -25,7 +24,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="System.Memory" Version="4.6.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
+87 -18
View File
@@ -1,6 +1,8 @@
using GlitchyEngine.Core;
using GlitchyEngine.Editor;
using GlitchyEngine.Extensions;
using GlitchyEngine.Native;
using GlitchyEngine.Physics;
using GlitchyEngine.Serialization;
using ImGuiNET;
using System;
@@ -11,8 +13,7 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using GlitchyEngine.Native;
using GlitchyEngine.Physics;
using System.Text;
namespace GlitchyEngine;
@@ -552,43 +553,111 @@ internal static unsafe partial class ScriptGlue
instance = null;
}
public static void Serialization_SerializeField(IntPtr serializationContext, SerializationType type, string fieldName, object? valueObject, string fullTypeName)
public static void Serialization_SerializeField(IntPtr serializationContext, SerializationType type, string fieldName, object? valueObject, string? fullTypeName)
{
StringView fieldNameConverted = StringView.FromManagedString(fieldName);
StringView fullTypeNameConverted = StringView.FromManagedString(fullTypeName);
StringView fieldNameConverted;
{
int maxByteCount = Encoding.UTF8.GetMaxByteCount(fieldName.Length);
byte* pointer = stackalloc byte[checked(maxByteCount + 1)];
int bytes = Encoding.UTF8.GetBytes((ReadOnlySpan<char>)fieldName, new Span<byte>(pointer, maxByteCount));
pointer[bytes] = 0;
fieldNameConverted = new StringView(pointer, bytes);
}
void* valueObjectConverted = null;
bool deleteValueObject = false;
StringView fullTypeNameConverted = new StringView();
if (fullTypeName != null)
{
int maxByteCount = Encoding.UTF8.GetMaxByteCount(fullTypeName.Length);
byte* pointer = stackalloc byte[checked(maxByteCount + 1)];
int bytes = Encoding.UTF8.GetBytes((ReadOnlySpan<char>)fullTypeName, new Span<byte>(pointer, maxByteCount));
pointer[bytes] = 0;
fullTypeNameConverted = new StringView(pointer, bytes);
}
switch (type)
{
case SerializationType.String:
case SerializationType.Enum:
bool onHeap = false;
StringView nativeString = new StringView();
if (valueObject is string stringValue)
{
StringView nativeString = StringView.FromManagedString(stringValue);
valueObjectConverted = nativeString.Utf8Ptr;
deleteValueObject = true;
int maxByteCount = Encoding.UTF8.GetMaxByteCount(stringValue.Length);
int actualByteCount = checked(maxByteCount + 1);
onHeap = actualByteCount > 256;
byte* pointer;
if (!onHeap)
{
byte* pointer2 = stackalloc byte[actualByteCount];
pointer = pointer2;
}
else
{
pointer = (byte*)NativeMemory.Alloc((nuint)actualByteCount);
}
int bytes = Encoding.UTF8.GetBytes((ReadOnlySpan<char>)stringValue, new Span<byte>(pointer, maxByteCount));
pointer[bytes] = 0;
nativeString = new StringView(pointer, bytes);
}
_engineFunctions.Serialization_SerializeField((void*)serializationContext, type, fieldNameConverted, &nativeString, fullTypeNameConverted);
if (onHeap)
StringView.FreeNativeMemory(nativeString);
break;
default:
if (valueObject is not null)
{
// This is probably the dirtiest piece of C# Code, that I have ever seen.
// We first take a pointer the Object (which itself is a reference type -> object* is a double pointer!)
// We then dereference this double pointer, which gives us a pointer to the objects internals.
// These are Header (64bit), Method Table (64bit), and the objects content (at least another 64bit)
// I believe, that the actual reference points to the method table however and not the header (compare gcenv.object.h)
// Below we calculate a pointer to the objects contents by adding the size of a pointer.
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
object?* objectRef = &valueObject;
// Skip Object Header (IntPtr) + Method Table (IntPtr)
valueObjectConverted = (byte*)*(IntPtr*)objectRef + sizeof(IntPtr);
byte* valueRef = (byte*)*(IntPtr*)objectRef + sizeof(IntPtr);
float* fRef = (float*)(void*)valueRef;
_engineFunctions.Serialization_SerializeField((void*)serializationContext, type, fieldNameConverted, valueRef, fullTypeNameConverted);
}
break;
}
_engineFunctions.Serialization_SerializeField((void*)serializationContext, type, fieldNameConverted, valueObjectConverted, fullTypeNameConverted);
NativeMemory.Free(fieldNameConverted.Utf8Ptr);
NativeMemory.Free(fullTypeNameConverted.Utf8Ptr);
if (deleteValueObject)
NativeMemory.Free(valueObjectConverted);
//StringView.FreeNativeMemory(fieldNameConverted);
//StringView.FreeNativeMemory(fullTypeNameConverted);
}
public static void Serialization_SerializeValueType<T>(IntPtr serializationContext, SerializationType type, string fieldName, in T value, string? fullTypeName) where T : unmanaged
{
StringView fieldNameConverted;
{
int maxByteCount = Encoding.UTF8.GetMaxByteCount(fieldName.Length);
byte* pointer = stackalloc byte[checked(maxByteCount + 1)];
int bytes = Encoding.UTF8.GetBytes((ReadOnlySpan<char>)fieldName, new Span<byte>(pointer, maxByteCount));
pointer[bytes] = 0;
fieldNameConverted = new StringView(pointer, bytes);
}
StringView fullTypeNameConverted = new StringView();
if (fullTypeName != null)
{
int maxByteCount = Encoding.UTF8.GetMaxByteCount(fullTypeName.Length);
byte* pointer = stackalloc byte[checked(maxByteCount + 1)];
int bytes = Encoding.UTF8.GetBytes((ReadOnlySpan<char>)fullTypeName, new Span<byte>(pointer, maxByteCount));
pointer[bytes] = 0;
fullTypeNameConverted = new StringView(pointer, bytes);
}
fixed (T* valuePtr = &value)
{
_engineFunctions.Serialization_SerializeField((void*)serializationContext, type, fieldNameConverted, valuePtr, fullTypeNameConverted);
}
}
#endregion Custom engine call implementations
@@ -27,7 +27,7 @@ public static class DictionarySerializer
if (fieldValue == null)
{
// Write a null pointer early out
container.AddField(fieldName, SerializationType.ObjectReference, UUID.Zero);
container.AddValueTypeField(fieldName, SerializationType.ObjectReference, UUID.Zero);
return;
}
@@ -43,7 +43,7 @@ public static class DictionarySerializer
IDictionary dictionary = (IDictionary)fieldValue;
ICollection collection = (ICollection)fieldValue;
context.AddField("Count", SerializationType.Int32, collection.Count);
context.AddValueTypeField("Count", SerializationType.Int32, collection.Count);
int index = 0;
@@ -61,7 +61,7 @@ public static class DictionarySerializer
}
// Write the reference to our dictionary into the field of the parent.
container.AddField(fieldName, SerializationType.ObjectReference, context.Id);
container.AddValueTypeField(fieldName, SerializationType.ObjectReference, context.Id);
}
/// <summary>
+27 -24
View File
@@ -97,6 +97,13 @@ public class SerializedObject
ScriptGlue.Serialization_SerializeField(_internalContext, serializationType, completeFieldName, value, fullTypeName);
}
public void AddValueTypeField<T>(string fieldName, SerializationType serializationType, in T value, string? fullTypeName = null) where T : unmanaged
{
string completeFieldName = $"{_structScopeName}{fieldName}";
ScriptGlue.Serialization_SerializeValueType(_internalContext, serializationType, completeFieldName, value, fullTypeName);
}
public void Serialize(Entity entity)
{
@@ -236,7 +243,7 @@ public class SerializedObject
{
if (listObject == null)
{
AddField(fieldName, SerializationType.ObjectReference, UUID.Zero);
AddValueTypeField(fieldName, SerializationType.ObjectReference, UUID.Zero);
}
else
{
@@ -248,7 +255,7 @@ public class SerializedObject
{
IList list = (IList)listObject;
context.AddField("Count", SerializationType.Int32, list.Count);
context.AddValueTypeField("Count", SerializationType.Int32, list.Count);
for (int i = 0; i < list.Count; i++)
{
@@ -257,47 +264,43 @@ public class SerializedObject
context.SerializeField($"{i}", element, element?.GetType() ?? elementType);
}
}
AddField(fieldName, SerializationType.ObjectReference, context._id);
AddValueTypeField(fieldName, SerializationType.ObjectReference, context._id);
}
}
public void SerializePrimitive(string fieldName, object fieldValue, Type fieldType)
{
SerializationType type = SerializationType.None;
if (fieldType == typeof(bool))
type = SerializationType.Bool;
AddValueTypeField(fieldName, SerializationType.Bool, (bool)fieldValue);
else if (fieldType == typeof(char))
type = SerializationType.Char;
AddValueTypeField(fieldName, SerializationType.Char, (char)fieldValue);
else if (fieldType == typeof(byte))
type = SerializationType.UInt8;
AddValueTypeField(fieldName, SerializationType.UInt8, (byte)fieldValue);
else if (fieldType == typeof(sbyte))
type = SerializationType.Int8;
AddValueTypeField(fieldName, SerializationType.Int8, (sbyte)fieldValue);
else if (fieldType == typeof(ushort))
type = SerializationType.UInt16;
AddValueTypeField(fieldName, SerializationType.UInt16, (ushort)fieldValue);
else if (fieldType == typeof(short))
type = SerializationType.Int16;
AddValueTypeField(fieldName, SerializationType.Int16, (short)fieldValue);
else if (fieldType == typeof(uint))
type = SerializationType.UInt32;
AddValueTypeField(fieldName, SerializationType.UInt32, (uint)fieldValue);
else if (fieldType == typeof(int))
type = SerializationType.Int32;
AddValueTypeField(fieldName, SerializationType.Int32, (int)fieldValue);
else if (fieldType == typeof(ulong))
type = SerializationType.UInt64;
AddValueTypeField(fieldName, SerializationType.UInt64, (ulong)fieldValue);
else if (fieldType == typeof(long))
type = SerializationType.Int64;
AddValueTypeField(fieldName, SerializationType.Int64, (long)fieldValue);
else if (fieldType == typeof(float))
type = SerializationType.Float;
AddValueTypeField(fieldName, SerializationType.Float, (float)fieldValue);
else if (fieldType == typeof(double))
type = SerializationType.Double;
AddValueTypeField(fieldName, SerializationType.Double, (double)fieldValue);
else if (fieldType == typeof(decimal))
type = SerializationType.Decimal;
AddValueTypeField(fieldName, SerializationType.Decimal, (decimal)fieldValue);
else
{
Log.Error($"The primitive {fieldType} is not implemented.");
}
AddField(fieldName, type, fieldValue);
}
public void SerializeEnum(string fieldName, object? fieldValue, Type fieldType)
@@ -318,13 +321,13 @@ public class SerializedObject
{
if (fieldType.IsSubclassOf(typeof(EngineObject)))
{
AddField(fieldName, SerializationType.EngineObjectReference, ((EngineObject?)fieldValue)?.UUID ?? UUID.Zero, fieldValue?.GetType().FullName);
AddValueTypeField(fieldName, SerializationType.ObjectReference, ((EngineObject?)fieldValue)?.UUID ?? UUID.Zero, fieldValue?.GetType().FullName);
}
else
{
if (fieldValue == null)
{
AddField(fieldName, SerializationType.ObjectReference, UUID.Zero);
AddValueTypeField(fieldName, SerializationType.ObjectReference, UUID.Zero);
}
else
{
@@ -335,7 +338,7 @@ public class SerializedObject
context.SerializeInstanceFields(fieldValue);
}
AddField(fieldName, SerializationType.ObjectReference, context._id);
AddValueTypeField(fieldName, SerializationType.ObjectReference, context._id);
}
}
}