Exemplo n.º 1
0
#pragma warning restore CS0169, CS0649

            // Fills the specified array (given as pointer and length) with offsets of this struct's fields that need to be in sync with plugin struct fields.
            public unsafe static int FillFieldOffsetsArray(int startFrom, int *fieldOffsets, int fieldOffsetsLength)
            {
                // List of (field name, isPublic) pairs
                List <KeyValuePair <string, bool> > fieldNamesAndAccess = new List <KeyValuePair <string, bool> >()
                {
                    new KeyValuePair <string, bool>("m_NumElementsAndBytesUsed", false),
                    new KeyValuePair <string, bool>("m_BlockIndexInStream", false),
                    new KeyValuePair <string, bool>("m_NextBlock", false),
                    new KeyValuePair <string, bool>("m_AllocatorDebug", false),
                    new KeyValuePair <string, bool>("m_BlockStreamDebug", false)
                };

                // We need to account for m_DataStart* separately
                int myFieldCount = fieldNamesAndAccess.Count + 1;

                if (startFrom + myFieldCount > fieldOffsetsLength)
                {
                    throw new ArgumentException($"{nameof(fieldOffsets)} (length {fieldOffsetsLength}) is not big enough for all {myFieldCount} fields of this struct (starting from {startFrom})!");
                }

                int fieldIndex = startFrom;

                UnsafeEx.FillFieldOffsetsArray(typeof(Block), fieldNamesAndAccess, fieldOffsets, ref fieldIndex);

                // Handle m_DataStart* fields
                Block temp = default;

                fieldOffsets[fieldIndex] = UnsafeEx.CalculateOffset((void *)temp.Start(), ref temp);
                fieldIndex++;

                Assert.AreEqual(myFieldCount, fieldIndex - startFrom);
                return(fieldIndex);
            }
Exemplo n.º 2
0
        // There's more fields of in-place array here, but we don't care about them
#pragma warning restore CS0649, CS0169

        // Fills the specified array (given as pointer and length) with offsets of this struct's fields that need to be in sync with plugin struct fields.
        public unsafe static int FillFieldOffsetsArray(int startFrom, int *fieldOffsets, int fieldOffsetsLength)
        {
            // List of (field name, isPublic) pairs
            List <KeyValuePair <string, bool> > fieldNamesAndAccess = new List <KeyValuePair <string, bool> >()
            {
                new KeyValuePair <string, bool>("m_Allocator", false),
                new KeyValuePair <string, bool>("m_NumTotalElements", false),
                new KeyValuePair <string, bool>("m_PartiallyFreed", false),
                new KeyValuePair <string, bool>("m_IsLocked", false)
            };

            // We need to account for m_Data* separately
            int myFieldCount = fieldNamesAndAccess.Count + 1;

            if (startFrom + myFieldCount > fieldOffsetsLength)
            {
                throw new ArgumentException($"{nameof(fieldOffsets)} (length {fieldOffsetsLength}) is not big enough for all {myFieldCount} fields of this struct (starting from {startFrom})!");
            }

            int fieldIndex = startFrom;

            UnsafeEx.FillFieldOffsetsArray(typeof(HpBlockStream), fieldNamesAndAccess, fieldOffsets, ref fieldIndex);

            if (BuildPlatformUtil.is32Bit())
            {
                // 32bit
                fieldOffsets[fieldIndex] = UnsafeUtility.GetFieldOffset(typeof(HpBlockStream).GetField("m_Data32Bit", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
            }
            else
            {
                // 64bit
                fieldOffsets[fieldIndex] = UnsafeUtility.GetFieldOffset(typeof(HpBlockStream).GetField("m_Data64Bit", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
            }

            fieldIndex++;

            Assert.AreEqual(myFieldCount, fieldIndex - startFrom);
            return(fieldIndex);
        }