public override unsafe byte PreInit(DisplayConfig *value, ulong mask, ref UIPropertyWriterContext context) { value->display = VisibilityStyle.Visible; value->visible = VisibilityStyle.Visible; value->overflow = VisibilityStyle.Visible; value->opacity = 1f; return(0); }
public override unsafe byte PreInit(SizeConfig *value, ulong mask, ref UIPropertyWriterContext context) { value->minWidth = 0f.Px(); value->maxWidth = float.PositiveInfinity.Px(); value->width = float.NaN.Px(); value->height = float.NaN.Px(); value->minHeight = 0f.Px(); value->maxHeight = float.PositiveInfinity.Px(); return(0); }
public static byte PreInit(Type type, IntPtr value, ulong mask, ref UIPropertyWriterContext context) { if (configurators.TryGetValue(type, out UIConfigurationHandler configurator)) { return(configurator.PreInit(value, mask, ref context)); } else { return(0); } }
/// <summary> /// Serializes a UIModel in the following format: /// Total Length: ulong (excluding this field) /// Node Count: int /// For each node present: /// Node Size: int (excluding this field) /// Header Data: HeaderConfig /// Children: int[], total length of children array can be found inside the HeaderConfig. /// ConfigBlocks: Which config blocks are present can be found by querying the configuration mask in the headerconfig /// </summary> public static void Write(this UIModel model, Stream stream, UISchema schema) { Contract.Assert(model.group != null); using var modelWriter = new MemoryBinaryWriter(); modelWriter.Write(0UL); modelWriter.Write(model.nodes.Count); var context = new UIPropertyWriterContext { group = model.group }; var decomposer = new TypeDecomposer(); var types = new List <Type>(); long totalSize = UnsafeUtility.SizeOf <int>(); Configure(model, -1, 0, schema, modelWriter, decomposer, context, types, ref totalSize); /* for (int i = 0; i < model.nodes.Count; i++) { * totalSize += Configure(model.nodes[i], modelWriter, decomposer, context, types) + UnsafeUtility.SizeOf<int>(); * } */ UnsafeUtility.MemCpy(modelWriter.Data, UnsafeUtility.AddressOf(ref totalSize), UnsafeUtility.SizeOf <ulong>()); using var modelStream = new UnmanagedMemoryStream(modelWriter.Data, modelWriter.Length); modelStream.CopyTo(stream); }
public override unsafe byte PostInit(TextConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { TMP_FontAsset fontAsset = null; var fontConfigPtr = UIConfigUtility.GetConfig(mask, UIConfigLayoutTable.FontConfig, config); if (fontConfigPtr == IntPtr.Zero) { return(0); } FontConfig *fontConfig = ((FontConfig *)fontConfigPtr); var guid = fontConfig->asset.ToHex(); #if UNITY_EDITOR fontAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(UnityEditor.AssetDatabase.GUIDToAssetPath(guid)); #else //var task = Addressables.LoadAssetAsync<TMP_FontAsset>(guid); //fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(guid)); #endif var index = context.group.GetAtlasIndex(guid); if (fontAsset != null && index >= 0) { value->fontInfo = new FontInfo(fontAsset); value->charInfoOffset = extraBytesStream.Length; for (int charIndex = 0; charIndex < value->text.length; charIndex++) { UnsafeUtility.CopyPtrToStructure(new IntPtr(((IntPtr)extraBytesStream.Data).ToInt64() + (value->text.offset - extraByteStreamOffset) + (charIndex * 2)).ToPointer(), out char character); var charInfo = fontAsset.characterLookupTable[character]; var charInfoValue = new CharInfo { uvs = new float4(charInfo.glyph.glyphRect.x / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.y / (float)fontAsset.atlasHeight, charInfo.glyph.glyphRect.width / (float)fontAsset.atlasWidth, charInfo.glyph.glyphRect.height / (float)fontAsset.atlasHeight), metrics = charInfo.glyph.metrics, index = (byte)index, unicode = charInfo.unicode }; extraBytesStream.WriteBytes(UnsafeUtility.AddressOf(ref charInfoValue), UnsafeUtility.SizeOf <CharInfo>()); } value->charInfoLength = value->text.length; //value->charInfoLength = (int)((extraBytesStream.Length - value->charInfoOffset) / UnsafeUtility.SizeOf<CharInfo>()); value->charInfoOffset += extraByteStreamOffset; } return(0); }
public override unsafe byte PostInit(NameConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { return((byte)(value->name.IsCreated ? 1 : 0)); }
public override unsafe byte PreInit(BackgroundConfig *value, ulong mask, ref UIPropertyWriterContext context) { value->color = Color.white; value->imageTint = Color.white; return(0); }
public override unsafe byte PreInit(FontConfig *value, ulong mask, ref UIPropertyWriterContext context) { value->size = 12f.Px(); value->color = Color.black; return(0); }
public static void Write(this Dictionary <Type, IUIPropertyWriter> writers, string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { if (typeof(ICompositeData).IsAssignableFrom(fieldData.type)) { var target = fieldData.type.GenericTypeArguments[0]; if (UnsafeUtility.SizeOf(target) * 4 == UnsafeUtility.SizeOf(fieldData.type)) { writers.FirstOrDefault(x => x.Key.IsAssignableFrom(target)).Value?.WriteComposite(target, s, ptr, fieldData, extraBytesStream, extraByteStreamOffset, context); } } else if (fieldData.type.IsEnum) { WriteEnum(fieldData.type, s, ptr, fieldData, extraBytesStream, extraByteStreamOffset, context); } else if (fieldData.type.IsConstructedGenericType && typeof(FunctionPointer <>).IsAssignableFrom(fieldData.type.GetGenericTypeDefinition())) { WriteFunctionPointerTypeless(s, ptr, fieldData, extraBytesStream, extraByteStreamOffset, context); } else { writers.FirstOrDefault(x => x.Key.IsAssignableFrom(fieldData.type)).Value?.Write(s, ptr, fieldData, extraBytesStream, extraByteStreamOffset, context); } }
public virtual byte PostInit(IntPtr value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { return(0); }
public unsafe void Write(string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { var current = extraBytesStream.Length; var bytes = Encoding.Unicode.GetBytes(s); extraBytesStream.Write(bytes); var str = new LocalizedStringPtr { offset = current + extraByteStreamOffset, length = (extraBytesStream.Length - current) / 2 }; UnsafeUtility.CopyStructureToPtr(ref str, (ptr + fieldData.offset).ToPointer()); }
public unsafe void Write(string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { if (context.group != null && context.group.TryGetUVs(s, out Rect uvs)) { var uvData = new UVData { value = new float4(uvs.x, uvs.y, uvs.width, uvs.height) }; UnsafeUtility.CopyStructureToPtr(ref uvData, (ptr + fieldData.offset).ToPointer()); } }
private static unsafe void WriteComposite(this IUIPropertyWriter targetWriter, Type type, string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { var elements = s.Split(' ').Where(x => targetWriter.CanParse(x)).Take(4).ToArray(); switch (elements.Length) { case 0: return; case 1: targetWriter.DoCompositeWrite(type, ptr, fieldData, elements[0], elements[0], elements[0], elements[0], extraBytesStream, extraByteStreamOffset, context); return; case 2: targetWriter.DoCompositeWrite(type, ptr, fieldData, elements[0], elements[1], elements[0], elements[1], extraBytesStream, extraByteStreamOffset, context); return; case 3: targetWriter.DoCompositeWrite(type, ptr, fieldData, elements[0], elements[1], elements[1], elements[2], extraBytesStream, extraByteStreamOffset, context); return; default: targetWriter.DoCompositeWrite(type, ptr, fieldData, elements[0], elements[1], elements[2], elements[3], extraBytesStream, extraByteStreamOffset, context); return; } }
private static void DoCompositeWrite(this IUIPropertyWriter targetWriter, Type type, IntPtr ptr, TypeDecomposer.FieldData fieldData, string sx, string sy, string sz, string sw, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { var size = UnsafeUtility.SizeOf(type); targetWriter.Write(sx, ptr, new TypeDecomposer.FieldData { offset = fieldData.offset, length = size, type = type, isAssetReference = false }, extraBytesStream, extraByteStreamOffset, context); targetWriter.Write(sy, ptr, new TypeDecomposer.FieldData { offset = fieldData.offset + size, length = size, type = type, isAssetReference = false }, extraBytesStream, extraByteStreamOffset, context); targetWriter.Write(sz, ptr, new TypeDecomposer.FieldData { offset = fieldData.offset + (size * 2), length = size, type = type, isAssetReference = false }, extraBytesStream, extraByteStreamOffset, context); targetWriter.Write(sw, ptr, new TypeDecomposer.FieldData { offset = fieldData.offset + (size * 3), length = size, type = type, isAssetReference = false }, extraBytesStream, extraByteStreamOffset, context); }
private static void WriteEnum(Type type, string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { var parser = typeof(UIPropertyWriterFactory).GetMethod(nameof(TryParseEnum), BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(type).CreateDelegate(typeof(UIPropertyParser <>).MakeGenericType(type)); var writer = Activator.CreateInstance(typeof(UIPropertyWriter <>).MakeGenericType(type), parser) as IUIPropertyWriter; writer.Write(s, ptr, fieldData, extraBytesStream, extraByteStreamOffset, context); }
private static void WriteFunctionPointerTypeless(string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { var delegateType = fieldData.type.GenericTypeArguments[0]; var separatorIndex = s.IndexOf('#'); var containerName = s.Substring(0, separatorIndex); var methodName = s.Substring(separatorIndex + 1); var method = FindMethodInfo(containerName, methodName, delegateType); if (method != null) { typeof(UIPropertyWriterFactory).GetMethod(nameof(WriteFunctionPointer), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(delegateType).Invoke(null, new object[] { ptr, fieldData, method }); } }
public static byte PostInit(Type type, IntPtr value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { if (configurators.TryGetValue(type, out UIConfigurationHandler configurator)) { return(configurator.PostInit(value, config, mask, extraBytesStream, extraByteStreamOffset, context)); } else { return(0); } }
public virtual byte PreInit(IntPtr value, ulong mask, ref UIPropertyWriterContext context) { return(0); }
public override unsafe byte PreInit(MaterialConfig *value, ulong mask, ref UIPropertyWriterContext context) { return(1); }
public unsafe void Write(string s, IntPtr ptr, TypeDecomposer.FieldData fieldData, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { if (CanParse(s)) { if (GUID.TryParse(s, out GUID result)) { UnsafeUtility.CopyStructureToPtr(ref result, (ptr + fieldData.offset).ToPointer()); } } }
private static void Configure(UIModel model, int parent, int index, UISchema schema, MemoryBinaryWriter modelWriter, TypeDecomposer decomposer, UIPropertyWriterContext context, List <Type> types, ref long totalSize) { var node = model.nodes[index]; var header = new HeaderConfig { configurationMask = node.mask, schemaIndex = schema.elements.FindIndex((element) => element.identifier == node.identifier), flags = 0, childCount = node.children.Count, parent = parent }; var sizeOffset = modelWriter.Length; var headerOffset = modelWriter.Length + sizeof(int); int size = 0; modelWriter.Write(0); modelWriter.WriteBytes(UnsafeUtility.AddressOf(ref header), UnsafeUtility.SizeOf <HeaderConfig>()); foreach (var child in node.children) { modelWriter.Write(child); } size += UnsafeUtility.SizeOf <HeaderConfig>() + (UnsafeUtility.SizeOf <int>() * node.children.Count); size += ConfigureBlocks(node, modelWriter, decomposer, ref context, types, size, out header.flags); UnsafeUtility.MemCpy((((IntPtr)modelWriter.Data) + sizeOffset).ToPointer(), UnsafeUtility.AddressOf(ref size), UnsafeUtility.SizeOf <int>()); UnsafeUtility.MemCpy((((IntPtr)modelWriter.Data) + headerOffset).ToPointer(), UnsafeUtility.AddressOf(ref header), UnsafeUtility.SizeOf <HeaderConfig>()); totalSize += UnsafeUtility.SizeOf <int>() + size; foreach (var child in node.children) { Configure(model, index, child, schema, modelWriter, decomposer, context, types, ref totalSize); } }
public override unsafe byte PostInit(SizeConfig *value, IntPtr config, ulong mask, MemoryBinaryWriter extraBytesStream, long extraByteStreamOffset, UIPropertyWriterContext context) { return(0); }
private static int ConfigureBlocks(UIModel.Node node, MemoryBinaryWriter writer, TypeDecomposer decomposer, ref UIPropertyWriterContext context, List <Type> types, int headerSize, out byte flags) { var configBlocks = new List <object>(); UIConfigUtility.GetTypes(node.mask, types); UIConfigUtility.CreateConfiguration(node.mask, configBlocks); var configSize = UIConfigUtility.GetLength(node.mask); using var extraBytesStream = new MemoryBinaryWriter(); IntPtr configData = (IntPtr)UnsafeUtility.Malloc(configSize, 0, Allocator.Temp); int configBlockOffset = 0; var configFields = new Dictionary <string, TypeDecomposer.FieldData>(); flags = 0; foreach (var configBlock in configBlocks) { decomposer.Decompose(configBlock.GetType(), configFields, configBlock.GetType().GetCustomAttribute <UIConfigBlockAttribute>()?.Name, configBlockOffset, '-'); Marshal.StructureToPtr(configBlock, configData + configBlockOffset, true); flags |= StandardConfigurationHandlers.PreInit(configBlock.GetType(), configData + configBlockOffset, node.mask, ref context); configBlockOffset += UnsafeUtility.SizeOf(configBlock.GetType()); } foreach (var property in node.properties) { if (configFields.TryGetValue(property.path, out TypeDecomposer.FieldData fieldData)) { StandardPropertyWriters.writers.Write(property.Value, configData, fieldData, extraBytesStream, configSize + headerSize, context); } } configBlockOffset = 0; foreach (var configBlockType in types) { flags |= StandardConfigurationHandlers.PostInit(configBlockType, configData + configBlockOffset, configData, node.mask, extraBytesStream, configSize + headerSize, context); configBlockOffset += UnsafeUtility.SizeOf(configBlockType); } int length = configSize + extraBytesStream.Length; writer.WriteBytes(configData.ToPointer(), configSize); if (extraBytesStream.Length > 0) { writer.WriteBytes(extraBytesStream.Data, extraBytesStream.Length); } UnsafeUtility.Free(configData.ToPointer(), Allocator.Temp); return(length); }