From f4c5dce0ff57560667e98776e874e3c0ff41e8ef Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Tue, 18 Mar 2025 17:58:20 +0800 Subject: [PATCH] stash --- .../ComponentTamplateGenerator/Generator.cs | 166 ++++++++++++++---- 1 file changed, 136 insertions(+), 30 deletions(-) diff --git a/src/Tools/ComponentTamplateGenerator/Generator.cs b/src/Tools/ComponentTamplateGenerator/Generator.cs index d7de7a8..559377d 100644 --- a/src/Tools/ComponentTamplateGenerator/Generator.cs +++ b/src/Tools/ComponentTamplateGenerator/Generator.cs @@ -1,8 +1,6 @@ #if UNITY_EDITOR using DCFApixels.DragonECS; using DCFApixels.DragonECS.PoolsCore; -using DCFApixels.DragonECS.Unity; -using DCFApixels.DragonECS.Unity.Editors; using System; using System.Collections.Generic; using System.Linq; @@ -143,20 +141,37 @@ public static class GeneratorUtility public delegate void ApplyHandler(ref TComponent data); public delegate void ApplyHandler(ref TComponent data, TPool pool); - public static bool SkanTypeStructure(Type type) + public static TypeScan SkanTypeStructure(Type type) { - + return TypeScan.Scan(type); } private static bool IsSerializableField(FieldInfo fieldInfo) { return fieldInfo.IsPublic || fieldInfo.GetCustomAttribute() != null || fieldInfo.GetCustomAttribute() != null; } - private static bool IsCanUnsafeOverride(FieldInfo fieldInfo) + private static bool IsCanUnsafeOverwrite(FieldInfo fieldInfo, bool isLeaf) { + if (isLeaf) + { + return true; + } if (fieldInfo.FieldType.IsValueType) { return true; } + + return false; + } + private static bool IsLeafField(FieldInfo fieldInfo) + { + if (fieldInfo.FieldType.IsEnum) + { + return true; + } + if (fieldInfo.FieldType.IsPrimitive) + { + return true; + } if (fieldInfo.FieldType == typeof(string)) { return true; @@ -169,35 +184,126 @@ public static class GeneratorUtility { return true; } - return false; } - //public interface IUnityCompilatorInfo // defines hack - //{ - // /// VMT size - // public int ObjectVirtualDataSize { get; } - // /// can rewrite VMT - // public bool IsSupportRewriteObjectVirtualData { get; } - //} - //private class UnityCompilatorInfo : IUnityCompilatorInfo - //{ - // public int ObjectVirtualDataSize - // { - // get - // { - // return 8; - // } - // } - // public bool IsSupportRewriteObjectVirtualData - // { - // get - // { - // return true; - // } - // } - //} + public class TypeScan + { + public static readonly TypeScan NoSerializableScan = new TypeScan(); + + public readonly bool IsSerializable; + public readonly bool IsCanUnsafeOverwrite; + + public readonly bool IsClass; + public readonly bool IsStruct; + + public readonly FieldScan[] Fields = Array.Empty(); + public readonly Type SerializableType; + + //public readonly string[] EnumFlagNames; + + private TypeScan() { } + private TypeScan(Type type) + { + IsSerializable = true; + SerializableType = type; + + + + IsCanUnsafeOverwrite = true; + ReadOnlySpan fields = FieldScan.GetFieldScans(type); + for (int i = 0; i < fields.Length; i++) + { + var field = fields[i]; + if (field.IsCanUnsafeOverwrite == false) + { + IsCanUnsafeOverwrite = false; + break; + } + } + + Fields = fields.ToArray(); + } + public static TypeScan Scan(Type type) + { + if (type.GetCustomAttribute() == null) + { + return NoSerializableScan; + } + return new TypeScan(type); + } + } + + public class FieldScan + { + public static readonly FieldScan NoSerializableScan = new FieldScan(); + + public readonly bool IsSerializable; + + public readonly bool IsSerializeField; + public readonly bool IsSerializeReference; + public readonly bool IsUnityObject; + + public readonly bool IsLeaf; + public readonly bool IsCanUnsafeOverwrite; + public readonly TypeScan ValueTypeScan; + public readonly FieldInfo SerializableFieldInfo; + + //public readonly + + private FieldScan() { } + private FieldScan(FieldInfo field) + { + IsSerializable = true; + SerializableFieldInfo = field; + IsLeaf = IsLeafField(field); + IsCanUnsafeOverwrite = IsCanUnsafeOverwrite(field, IsLeaf); + + if (field.FieldType.IsValueType) + { + ValueTypeScan = TypeScan.Scan(field.FieldType); + } + } + + + private static FieldScan[] FieldsBuffer = new FieldScan[64]; + private static int FieldsBufferCount = 0; + public static ReadOnlySpan GetFieldScans(Type type) + { + var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + if (FieldsBuffer == null || FieldsBuffer.Length < fields.Length) + { + var newsize = DCFApixels.DragonECS.Unity.Internal.ArrayUtility.NormalizeSizeToPowerOfTwo(fields.Length); + FieldsBuffer = new FieldScan[newsize]; + } + for (int i = 0; i < fields.Length; i++) + { + FieldsBuffer[i] = Scan(fields[i]); + FieldsBufferCount++; + } + return new ReadOnlySpan(FieldsBuffer, 0, FieldsBufferCount); + } + public static void CleanGetFieldScansBuffer() + { + for (int i = 0; i < FieldsBufferCount; i++) + { + FieldsBuffer[i] = null; + } + } + public static FieldScan Scan(FieldInfo field) + { + if (IsSerializableField(field) == false) + { + return NoSerializableScan; + } + + return new FieldScan(field); + } + } + + //public class AttributeScan { } // TODO } + public abstract class ConverterWrapperBase { public abstract void Apply(ref TStencil component, short worldID, int entityID);