123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- using UnityEngine.Rendering;
- using System;
- using System.Reflection;
- using System.Linq.Expressions;
- namespace UnityEditor.Rendering
- {
- /// <summary>Serialisation of BitArray, Utility class</summary>
- public static class SerializedBitArrayUtilities
- {
- /// <summary>Convert to 8bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray8</returns>
- public static SerializedBitArray8 ToSerializeBitArray8(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 8u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray8(serializedProperty);
- }
- /// <summary>Try convert to 8bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray8</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray8(this SerializedProperty serializedProperty, out SerializedBitArray8 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 8u))
- return false;
- serializedBitArray = new SerializedBitArray8(serializedProperty);
- return true;
- }
- /// <summary>Convert to 16bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray16</returns>
- public static SerializedBitArray16 ToSerializeBitArray16(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 16u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray16(serializedProperty);
- }
- /// <summary>Try convert to 16bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray16</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray16(this SerializedProperty serializedProperty, out SerializedBitArray16 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 16u))
- return false;
- serializedBitArray = new SerializedBitArray16(serializedProperty);
- return true;
- }
- /// <summary>Convert to 32bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray32</returns>
- public static SerializedBitArray32 ToSerializeBitArray32(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 32u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray32(serializedProperty);
- }
- /// <summary>Try convert to 32bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray32</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray32(this SerializedProperty serializedProperty, out SerializedBitArray32 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 32u))
- return false;
- serializedBitArray = new SerializedBitArray32(serializedProperty);
- return true;
- }
- /// <summary>Convert to 64bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray64</returns>
- public static SerializedBitArray64 ToSerializeBitArray64(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 64u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray64(serializedProperty);
- }
- /// <summary>Try convert to 64bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray64</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray64(this SerializedProperty serializedProperty, out SerializedBitArray64 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 64u))
- return false;
- serializedBitArray = new SerializedBitArray64(serializedProperty);
- return true;
- }
- /// <summary>Convert to 128bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray128</returns>
- public static SerializedBitArray128 ToSerializeBitArray128(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 128u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray128(serializedProperty);
- }
- /// <summary>Try convert to 128bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray128</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray128(this SerializedProperty serializedProperty, out SerializedBitArray128 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 128u))
- return false;
- serializedBitArray = new SerializedBitArray128(serializedProperty);
- return true;
- }
- /// <summary>Convert to 256bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <returns>A SerializedBitArray256</returns>
- public static SerializedBitArray256 ToSerializeBitArray256(this SerializedProperty serializedProperty)
- {
- if (!IsBitArrayOfCapacity(serializedProperty, 256u))
- throw new Exception("Cannot get SerializeBitArray of this Capacity");
- return new SerializedBitArray256(serializedProperty);
- }
- /// <summary>Try convert to 256bit</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- /// <param name="serializedBitArray">Out SerializedBitArray256</param>
- /// <returns>True if convertion was a success</returns>
- public static bool TryGetSerializeBitArray256(this SerializedProperty serializedProperty, out SerializedBitArray256 serializedBitArray)
- {
- serializedBitArray = null;
- if (!IsBitArrayOfCapacity(serializedProperty, 256u))
- return false;
- serializedBitArray = new SerializedBitArray256(serializedProperty);
- return true;
- }
- static bool IsBitArrayOfCapacity(SerializedProperty serializedProperty, uint capacity)
- {
- const string baseTypeName = "BitArray";
- string type = serializedProperty.type;
- uint serializedCapacity;
- return type.StartsWith(baseTypeName)
- && uint.TryParse(type.Substring(baseTypeName.Length), out serializedCapacity)
- && capacity == serializedCapacity;
- }
- }
- /// <summary>interface to handle generic SerializedBitArray</summary>
- public interface ISerializedBitArray
- {
- /// <summary>Capacity of the bitarray</summary>
- uint capacity { get; }
- /// <summary>Get the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Bit value</returns>
- bool GetBitAt(uint bitIndex);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- void SetBitAt(uint bitIndex, bool value);
- /// <summary>Does the bit at given index have multiple different values?</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: Multiple different value</returns>
- bool HasBitMultipleDifferentValue(uint bitIndex);
- }
- /// <summary>Abstract base classe of all SerializedBitArray</summary>
- public abstract class SerializedBitArray : ISerializedBitArray
- {
- // Note: this should be exposed at the same time as issue with type other than Int32 is fixed on C++ side
- /// <summary>Set the bit at given index</summary>
- protected static Action<SerializedProperty, int, bool> SetBitAtIndexForAllTargetsImmediate;
- /// <summary>Has multiple differente value bitwise</summary>
- protected static Func<SerializedProperty, int> HasMultipleDifferentValuesBitwise;
- static SerializedBitArray()
- {
- var type = typeof(SerializedProperty);
- var setBitAtIndexForAllTargetsImmediateMethodInfo = type.GetMethod("SetBitAtIndexForAllTargetsImmediate", BindingFlags.Instance | BindingFlags.NonPublic);
- var hasMultipleDifferentValuesBitwisePropertyInfo = type.GetProperty("hasMultipleDifferentValuesBitwise", BindingFlags.Instance | BindingFlags.NonPublic);
- var serializedPropertyParameter = Expression.Parameter(typeof(SerializedProperty), "property");
- var indexParameter = Expression.Parameter(typeof(int), "index");
- var valueParameter = Expression.Parameter(typeof(bool), "value");
- var hasMultipleDifferentValuesBitwiseProperty = Expression.Property(serializedPropertyParameter, hasMultipleDifferentValuesBitwisePropertyInfo);
- var setBitAtIndexForAllTargetsImmediateCall = Expression.Call(serializedPropertyParameter, setBitAtIndexForAllTargetsImmediateMethodInfo, indexParameter, valueParameter);
- var setBitAtIndexForAllTargetsImmediateLambda = Expression.Lambda<Action<SerializedProperty, int, bool>>(setBitAtIndexForAllTargetsImmediateCall, serializedPropertyParameter, indexParameter, valueParameter);
- var hasMultipleDifferentValuesBitwiseLambda = Expression.Lambda<Func<SerializedProperty, int>>(hasMultipleDifferentValuesBitwiseProperty, serializedPropertyParameter);
- SetBitAtIndexForAllTargetsImmediate = setBitAtIndexForAllTargetsImmediateLambda.Compile();
- HasMultipleDifferentValuesBitwise = hasMultipleDifferentValuesBitwiseLambda.Compile();
- }
- /// <summary>The underlying serialized property</summary>
- protected SerializedProperty m_SerializedProperty;
- SerializedProperty[] m_SerializedProperties;
- /// <summary>Capacity of the bitarray</summary>
- public uint capacity { get; }
- internal SerializedBitArray(SerializedProperty serializedProperty, uint capacity)
- {
- this.capacity = capacity;
- m_SerializedProperty = serializedProperty;
- }
- /// <summary>Initialisation of dedicated SerializedPropertiws</summary>
- /// <returns>Arrays of SerializedProperty</returns>
- protected SerializedProperty[] GetOrInitializeSerializedProperties()
- {
- if (m_SerializedProperties == null)
- {
- UnityEngine.Object[] targets = m_SerializedProperty.serializedObject.targetObjects;
- int size = targets.Length;
- if (size == 1)
- m_SerializedProperties = new[] { m_SerializedProperty };
- else
- {
- string propertyPath = m_SerializedProperty.propertyPath;
- m_SerializedProperties = new SerializedProperty[size];
- for (int i = 0; i < size; ++i)
- {
- m_SerializedProperties[i] = new SerializedObject(targets[i]).FindProperty(propertyPath);
- }
- }
- }
- return m_SerializedProperties;
- }
- /// <summary>Does the bit at given index have multiple different values?</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: Multiple different value</returns>
- public bool HasBitMultipleDifferentValue(uint bitIndex)
- {
- if (bitIndex >= capacity)
- throw new IndexOutOfRangeException("Index out of bound in BitArray" + capacity);
- return HasBitMultipleDifferentValue_Internal(bitIndex);
- }
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- abstract protected bool HasBitMultipleDifferentValue_Internal(uint bitIndex);
- /// <summary>
- /// Safety: serializedProperty must match its path
- /// </summary>
- /// <param name="propertyPath">serializedProperty must match its path</param>
- /// <param name="serializedProperty">serializedProperty must match its path</param>
- /// <param name="bitIndex"></param>
- /// <returns></returns>
- unsafe protected bool HasBitMultipleDifferentValue_For64Bits(string propertyPath, SerializedProperty serializedProperty, uint bitIndex)
- {
- if (!serializedProperty.hasMultipleDifferentValues)
- return false;
- var serializedProperties = GetOrInitializeSerializedProperties();
- int length = serializedProperties.Length;
- ulong mask = 1uL << (int)bitIndex;
- bool value = ((ulong)m_SerializedProperties[0].FindPropertyRelative(propertyPath).longValue & mask) != 0uL;
- for (int i = 1; i < length; ++i)
- {
- if ((((ulong)m_SerializedProperties[i].FindPropertyRelative(propertyPath).longValue & mask) != 0uL) ^ value)
- return true;
- }
- return false;
- }
- /// <summary>Get the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Bit value</returns>
- public bool GetBitAt(uint bitIndex)
- {
- if (bitIndex >= capacity)
- throw new IndexOutOfRangeException("Index out of bound in BitArray" + capacity);
- return GetBitAt_Internal(bitIndex);
- }
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- abstract protected bool GetBitAt_Internal(uint bitIndex);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- public void SetBitAt(uint bitIndex, bool value)
- {
- if (bitIndex >= capacity)
- throw new IndexOutOfRangeException("Index out of bound in BitArray" + capacity);
- SetBitAt_Internal(bitIndex, value);
- }
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- abstract protected void SetBitAt_Internal(uint bitIndex, bool value);
- /// <summary>Sync again every serializedProperty</summary>
- protected void ResyncSerialization()
- {
- foreach (var property in m_SerializedProperties)
- property.serializedObject.ApplyModifiedProperties();
- Update();
- }
- /// <summary>Sync the reflected value with target value change</summary>
- public void Update()
- {
- foreach (var property in m_SerializedProperties)
- property.serializedObject.Update();
- m_SerializedProperty.serializedObject.Update();
- }
- }
- /// <summary>SerializedBitArray spetialized for 8bit capacity</summary>
- public sealed class SerializedBitArray8 : SerializedBitArray
- {
- SerializedProperty m_Data;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray8(SerializedProperty serializedProperty) : base(serializedProperty, 8u)
- => m_Data = m_SerializedProperty.FindPropertyRelative("data");
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => (HasMultipleDifferentValuesBitwise(m_Data) & (1 << (int)bitIndex)) != 0;
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get8(bitIndex, (byte)m_Data.intValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- byte versionedData = (byte)property.FindPropertyRelative("data").intValue;
- BitArrayUtilities.Set8(bitIndex, ref versionedData, value);
- property.FindPropertyRelative("data").intValue = versionedData;
- }
- ResyncSerialization();
- }
- }
- /// <summary>SerializedBitArray spetialized for 16bit capacity</summary>
- public sealed class SerializedBitArray16 : SerializedBitArray
- {
- SerializedProperty m_Data;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray16(SerializedProperty serializedProperty) : base(serializedProperty, 16u)
- => m_Data = m_SerializedProperty.FindPropertyRelative("data");
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => (HasMultipleDifferentValuesBitwise(m_Data) & (1 << (int)bitIndex)) != 0;
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get16(bitIndex, (ushort)m_Data.intValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- ushort versionedData = (ushort)property.FindPropertyRelative("data").intValue;
- BitArrayUtilities.Set16(bitIndex, ref versionedData, value);
- property.FindPropertyRelative("data").intValue = versionedData;
- }
- ResyncSerialization();
- }
- }
- /// <summary>SerializedBitArray spetialized for 32bit capacity</summary>
- public sealed class SerializedBitArray32 : SerializedBitArray
- {
- SerializedProperty m_Data;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray32(SerializedProperty serializedProperty) : base(serializedProperty, 32u)
- => m_Data = m_SerializedProperty.FindPropertyRelative("data");
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => (HasMultipleDifferentValuesBitwise(m_Data) & (1 << (int)bitIndex)) != 0;
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get32(bitIndex, (uint)m_Data.intValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- int versionedData = property.FindPropertyRelative("data").intValue;
- uint trueData;
- unsafe
- {
- trueData = *(uint*)(&versionedData);
- }
- BitArrayUtilities.Set32(bitIndex, ref trueData, value);
- unsafe
- {
- versionedData = *(int*)(&trueData);
- }
- property.FindPropertyRelative("data").intValue = versionedData;
- }
- ResyncSerialization();
- }
- }
- /// <summary>SerializedBitArray spetialized for 64bit capacity</summary>
- public sealed class SerializedBitArray64 : SerializedBitArray
- {
- SerializedProperty m_Data;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray64(SerializedProperty serializedProperty) : base(serializedProperty, 64u)
- => m_Data = m_SerializedProperty.FindPropertyRelative("data");
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => HasBitMultipleDifferentValue_For64Bits("data", m_Data, bitIndex);
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get64(bitIndex, (ulong)m_Data.longValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- long versionedData = property.FindPropertyRelative("data").longValue;
- ulong trueData;
- unsafe
- {
- trueData = *(ulong*)(&versionedData);
- }
- BitArrayUtilities.Set64(bitIndex, ref trueData, value);
- unsafe
- {
- versionedData = *(long*)(&trueData);
- }
- property.FindPropertyRelative("data").longValue = versionedData;
- }
- ResyncSerialization();
- }
- }
- /// <summary>SerializedBitArray spetialized for 128bit capacity</summary>
- public sealed class SerializedBitArray128 : SerializedBitArray
- {
- SerializedProperty m_Data1;
- SerializedProperty m_Data2;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray128(SerializedProperty serializedProperty) : base(serializedProperty, 128u)
- {
- m_Data1 = m_SerializedProperty.FindPropertyRelative("data1");
- m_Data2 = m_SerializedProperty.FindPropertyRelative("data2");
- }
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => bitIndex < 64u
- ? HasBitMultipleDifferentValue_For64Bits("data1", m_Data1, bitIndex)
- : HasBitMultipleDifferentValue_For64Bits("data2", m_Data2, bitIndex - 64u);
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get128(
- bitIndex,
- (ulong)m_SerializedProperty.FindPropertyRelative("data1").longValue,
- (ulong)m_SerializedProperty.FindPropertyRelative("data2").longValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- long versionedData1 = property.FindPropertyRelative("data1").longValue;
- long versionedData2 = property.FindPropertyRelative("data2").longValue;
- ulong trueData1;
- ulong trueData2;
- unsafe
- {
- trueData1 = *(ulong*)(&versionedData1);
- trueData2 = *(ulong*)(&versionedData2);
- }
- BitArrayUtilities.Set128(bitIndex, ref trueData1, ref trueData2, value);
- unsafe
- {
- versionedData1 = *(long*)(&trueData1);
- versionedData2 = *(long*)(&trueData2);
- }
- property.FindPropertyRelative("data1").longValue = versionedData1;
- property.FindPropertyRelative("data2").longValue = versionedData2;
- }
- ResyncSerialization();
- }
- }
- /// <summary>SerializedBitArray spetialized for 256bit capacity</summary>
- public sealed class SerializedBitArray256 : SerializedBitArray
- {
- SerializedProperty m_Data1;
- SerializedProperty m_Data2;
- SerializedProperty m_Data3;
- SerializedProperty m_Data4;
- /// <summary>Constructor</summary>
- /// <param name="serializedProperty">The SerializedProperty</param>
- public SerializedBitArray256(SerializedProperty serializedProperty) : base(serializedProperty, 128u)
- {
- m_Data1 = m_SerializedProperty.FindPropertyRelative("data1");
- m_Data2 = m_SerializedProperty.FindPropertyRelative("data2");
- m_Data3 = m_SerializedProperty.FindPropertyRelative("data3");
- m_Data4 = m_SerializedProperty.FindPropertyRelative("data4");
- }
- /// <summary>Say if the properties have differente values</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>True: properties have different value</returns>
- protected override bool HasBitMultipleDifferentValue_Internal(uint bitIndex)
- => bitIndex < 128u
- ? bitIndex < 64u
- ? HasBitMultipleDifferentValue_For64Bits("data1", m_Data1, bitIndex)
- : HasBitMultipleDifferentValue_For64Bits("data2", m_Data2, bitIndex - 64u)
- : bitIndex < 192u
- ? HasBitMultipleDifferentValue_For64Bits("data3", m_Data3, bitIndex - 128u)
- : HasBitMultipleDifferentValue_For64Bits("data4", m_Data4, bitIndex - 192u);
- /// <summary>Get the value at index</summary>
- /// <param name="bitIndex">The index</param>
- /// <returns>Value at the index</returns>
- protected override bool GetBitAt_Internal(uint bitIndex)
- => BitArrayUtilities.Get256(
- bitIndex,
- (ulong)m_SerializedProperty.FindPropertyRelative("data1").longValue,
- (ulong)m_SerializedProperty.FindPropertyRelative("data2").longValue,
- (ulong)m_SerializedProperty.FindPropertyRelative("data3").longValue,
- (ulong)m_SerializedProperty.FindPropertyRelative("data4").longValue);
- /// <summary>Set the bit at given index</summary>
- /// <param name="bitIndex">The index</param>
- /// <param name="value">The value</param>
- protected override void SetBitAt_Internal(uint bitIndex, bool value)
- {
- foreach (var property in GetOrInitializeSerializedProperties())
- {
- long versionedData1 = property.FindPropertyRelative("data1").longValue;
- long versionedData2 = property.FindPropertyRelative("data2").longValue;
- long versionedData3 = property.FindPropertyRelative("data3").longValue;
- long versionedData4 = property.FindPropertyRelative("data4").longValue;
- ulong trueData1;
- ulong trueData2;
- ulong trueData3;
- ulong trueData4;
- unsafe
- {
- trueData1 = *(ulong*)(&versionedData1);
- trueData2 = *(ulong*)(&versionedData2);
- trueData3 = *(ulong*)(&versionedData3);
- trueData4 = *(ulong*)(&versionedData4);
- }
- BitArrayUtilities.Set256(bitIndex, ref trueData1, ref trueData2, ref trueData3, ref trueData4, value);
- unsafe
- {
- versionedData1 = *(long*)(&trueData1);
- versionedData2 = *(long*)(&trueData2);
- versionedData3 = *(long*)(&trueData3);
- versionedData4 = *(long*)(&trueData4);
- }
- property.FindPropertyRelative("data1").longValue = versionedData1;
- property.FindPropertyRelative("data2").longValue = versionedData2;
- property.FindPropertyRelative("data3").longValue = versionedData3;
- property.FindPropertyRelative("data4").longValue = versionedData4;
- }
- ResyncSerialization();
- }
- }
- }
|