Ejemplo n.º 1
0
        public void Req10Test()
        {
            StructArray <TestStruct> pool = new StructArray <TestStruct>(10,
                                                                         ClearAction, MoveAction);

            List <int> ids = new List <int>();

            for (int i = 0; i < 10; i++)
            {
                ids.Add(pool.Request());
            }

            foreach (int id in ids)
            {
                Assert.IsFalse(id == -1);
            }

            for (int i = 0; i < pool.Count; i++)
            {
                pool.Values[i].X += i;
            }

            foreach (int id in ids)
            {
                Assert.AreEqual(id, pool.Values[pool.IdsToIndices[id]].X);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a byte array to be read by the game from a <see cref="HintFile"/>.
        /// </summary>
        public byte[] ToArray()
        {
            // Make native entries.
            var entries = new Entry[Entries.Length];

            for (int x = 0; x < entries.Length; x++)
            {
                entries[x] = new Entry(Entries[x]);
            }

            // Write strings from managed entries to a byte region.
            var stringData = new List <byte>(entries.Length * LongStringLength);

            byte[] bytes;

            for (int x = 0; x < entries.Length; x++)
            {
                entries[x].Offset = stringData.Count;
                bytes             = _encoder.GetBytes(Entries[x].Text);
                stringData.AddRange(bytes);
                stringData.Add(0);
            }

            var nullEntry       = Entry.Null;
            var nullEntryBytes  = Struct.GetBytes(ref nullEntry);
            var stringDataBytes = stringData.ToArray();

            var entryData = StructArray.GetBytes(entries);
            var allData   = new byte[entryData.Length + nullEntryBytes.Length + stringData.Count];

            Buffer.BlockCopy(entryData, 0, allData, 0, entryData.Length);
            Buffer.BlockCopy(nullEntryBytes, 0, allData, entryData.Length, nullEntryBytes.Length);
            Buffer.BlockCopy(stringDataBytes, 0, allData, entryData.Length + nullEntryBytes.Length, stringDataBytes.Length);
            return(allData);
        }
Ejemplo n.º 3
0
    public static void Main(string[] args)
    {
        StructArray <int> a = new StructArray <int>(3);

        a.Array[0] = 0;
        a.Array[1] = 1;
        a.Array[2] = 2;

        RefArray <StructArray <double> > b = new RefArray <StructArray <double> >(3);

        b.Array[0] = new StructArray <double>(5);
        b.Array[1] = new StructArray <double>(10);
        b.Array[2] = new StructArray <double>(1005);

        BaseArray <Base> c = new BaseArray <Base>(3);

        c.Array[0] = new Base();
        c.Array[1] = new Derived();
        c.Array[2] = CreateInstance <Base>();

        BaseArray <Derived> d = new BaseArray <Derived>(3);

        d.Array[0] = new Derived();
        d.Array[1] = CreateInstance <Derived>();
        d.Array[2] = CreateInstance <Derived>();

        BaseArray <Derived> e = new BaseArray <Derived>(3);

        e.CopyArray <Derived>(d.Array);
    }
Ejemplo n.º 4
0
    static void Main(string[] args)
    {
        #region 값 형식, 참조 형식 제약조건
        StructArray <int> a = new StructArray <int>(3);  // T는 값 형식이어야 한다.
        a.Array[0] = 0;
        a.Array[1] = 1;
        a.Array[2] = 2;
        //StructArray<string> s = new StructArray<string>(3);  // error

        RefArray <StructArray <double> > b = new RefArray <StructArray <double> >(3);
        b.Array[0] = new StructArray <double>(5);
        b.Array[1] = new StructArray <double>(10);
        b.Array[2] = new StructArray <double>(15);
        //RefArray<int> i = new RefArray<int>(3);  // error
        #endregion

        BaseArray <Base> c = new BaseArray <Base>(3);
        c.Array[0] = new Base();
        c.Array[1] = new Derived();
        c.Array[2] = CreateInstance <Base>();

        BaseArray <Derived> d = new BaseArray <Derived>(2);
        d.Array[0] = new Derived();  // Base 형식은 여기에 할달 할 수 없다.
        d.Array[1] = CreateInstance <Derived>();

        BaseArray <Derived> e = new BaseArray <Derived>(2);
        e.CopyArray <Derived>(d.Array);
    }
Ejemplo n.º 5
0
        public EntityChunk(EntityArchetype arch)
        {
            unsafe
            {
                chunkVersion = 0;
                currentCount = 0;
                this.arch    = arch;

                TypeCache[] cTypes       = arch.GetComponenTypes();
                TypeCache[] sTypes       = arch.GetSharedTypes();
                int         entityAmount = arch.GetChunkCapacity(CHUNK_SIZE);

                entities = new StructArray <Entity>(entityAmount, true);
                for (int i = 0; i < entityAmount; i++)
                {
                    entities[i].id = 0;
                }

                componentCollections = new ComponentArray[cTypes.Length];
                for (int i = 0; i < componentCollections.Length; i++)
                {
                    componentCollections[i] = new ComponentArray(cTypes[i], entityAmount);
                }

                sharedComponents = new ISharedComponent[sTypes.Length];
            }
        }
Ejemplo n.º 6
0
        public void _test2()
        {
            var sa = new StructArray <MyStruct>();

            AssertTrue(sa.Array is Array);
            AssertEquals(16, sa.Array.Length);
            AssertNotNull(sa.Array[0]);
            AssertEquals(0, sa.Array[0].A);
        }
Ejemplo n.º 7
0
            public void Visit(StructArray array)
            {
                _buffers.Add(CreateBuffer(array.NullBitmapBuffer));

                for (int i = 0; i < array.Fields.Count; i++)
                {
                    array.Fields[i].Accept(this);
                }
            }
Ejemplo n.º 8
0
        /// <summary>
        /// Changes memory permissions to ensure memory can be read and reads a generic type array from a specified memory address.
        /// </summary>
        /// <typeparam name="T">An individual struct type of a class with an explicit StructLayout.LayoutKind attribute.</typeparam>
        /// <param name="memory"></param>
        /// <param name="memoryAddress">The memory address to read from.</param>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="arrayLength">The amount of array items to read.</param>
        /// <param name="marshal">Set this to true to enable struct marshalling.</param>
        public static void SafeRead <T>(this IMemory memory, IntPtr memoryAddress, out T[] value, int arrayLength, bool marshal = false)
        {
            int regionSize = StructArray.GetSize <T>(arrayLength, marshal);

            var oldProtection = memory.ChangePermission(memoryAddress, regionSize, Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE);

            memory.Read(memoryAddress, out value, arrayLength, marshal);
            memory.ChangePermission(memoryAddress, regionSize, oldProtection);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Changes memory permissions to ensure memory can be written and writes a generic type array to a specified memory address.
        /// </summary>
        /// <typeparam name="T">An individual struct type of a class with an explicit StructLayout.LayoutKind attribute.</typeparam>
        /// <param name="memory"></param>
        /// <param name="memoryAddress">The memory address to write to.</param>
        /// <param name="items">The array of items to write to the address.</param>
        /// <param name="marshal">Set this to true to enable struct marshalling.</param>
        public static void SafeWrite <T>(this IMemory memory, IntPtr memoryAddress, T[] items, bool marshal = false)
        {
            int regionSize = StructArray.GetSize <T>(items.Length, marshal);

            var oldProtection = memory.ChangePermission(memoryAddress, regionSize, Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE);

            memory.Write(memoryAddress, items, marshal);
            memory.ChangePermission(memoryAddress, regionSize, oldProtection);
        }
        /* Construction Helpers */

        private void CopyVertices(ManagedSpline managedSpline)
        {
            var memory   = Memory.Instance;
            var vertices = managedSpline.Vertices;

            int structSize = StructArray.GetSize <SplineVertex>(vertices.Length);

            VertexList = (SplineVertex *)memory.Allocate(structSize);
            StructArray.ToPtr((IntPtr)VertexList, vertices);
        }
Ejemplo n.º 11
0
 public Segment(Vector3 start, Vector3 end)
 {
     this.start = new Node()
     {
         pos = start
     }; this.end = new Node()
     {
         pos = end
     }; lengthToPercentLut = new StructArray(); length = 0;
 }
Ejemplo n.º 12
0
        /* Write Base Implementation */

        /// <summary>
        /// Writes a generic type array to a specified memory address.
        /// </summary>
        /// <typeparam name="T">An individual struct type of a class with an explicit StructLayout.LayoutKind attribute.</typeparam>
        /// <param name="memory"></param>
        /// <param name="memoryAddress">The memory address to write to.</param>
        /// <param name="items">The array of items to write to the address.</param>
        /// <param name="marshal">Set this to true to enable struct marshalling.</param>
        public static void Write <T>(this IMemory memory, IntPtr memoryAddress, T[] items, bool marshal = false)
        {
            IMemory oldSource = Struct.Source;

            Struct.Source = memory;

            StructArray.ToPtr(memoryAddress, items, marshal);

            Struct.Source = oldSource;
        }
        /* Read Base Implementation */

        /// <summary>
        /// Reads a generic type array from a specified memory address.
        /// </summary>
        /// <typeparam name="T">An individual struct type of a class with an explicit StructLayout.LayoutKind attribute.</typeparam>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="memory"></param>
        /// <param name="memoryAddress">The memory address to read from.</param>
        /// <param name="arrayLength">The amount of array items to read.</param>
        /// <param name="marshal">Set this to true to enable struct marshalling.</param>
        public static void Read <T>(this IMemory memory, IntPtr memoryAddress, out T[] value, int arrayLength, bool marshal = false)
        {
            IMemory oldSource = Struct.Source;

            Struct.Source = memory;

            value = new T[arrayLength];
            StructArray.FromPtr(memoryAddress, out value, arrayLength, marshal);

            Struct.Source = oldSource;
        }
        /// <summary>
        /// Imports a binary physics file.
        /// </summary>
        public void ImportFile(string filePath)
        {
            var file = File.ReadAllBytes(filePath);

            StructArray.FromArray <AdventurePhysics>(file, out var physics, true);

            // WARNING: Order of AllCharacters must match binary file!
            foreach (AllCharacters character in (AllCharacters[])Enum.GetValues(typeof(AllCharacters)))
            {
                Physics[character] = physics[(int)character];
            }
        }
Ejemplo n.º 15
0
        public void ReqAndRemoveTest()
        {
            StructArray <TestStruct> pool = new StructArray <TestStruct>(10,
                                                                         ClearAction, MoveAction);

            List <int> ids = new List <int>();

            for (int i = 0; i < 10; i++)
            {
                ids.Add(pool.Request());
            }

            foreach (int id in ids)
            {
                Assert.IsFalse(id == -1);
            }

            for (int i = 0; i < pool.Count; i++)
            {
                pool.Values[i].X += i;
            }

            foreach (int id in ids)
            {
                Assert.AreEqual(id, pool.Values[pool.IdsToIndices[id]].X);
            }

            for (int i = 5; i < 8; i++)
            {
                pool.ReturnId(ids[i]);                                    // return 5, 6, and 7
            }
            Assert.IsTrue(pool.Count == 7);                               // down from 10
            Assert.IsTrue(pool.Values[pool.IdsToIndices[ids[9]]].X == 9); // verify that accessing id9 still works

            // request 3 new structs, to fill back up to 10
            List <int> newids = new List <int>();

            for (int i = 0; i < 3; i++)
            {
                newids.Add(pool.Request());
            }

            foreach (int id in newids)
            {
                Assert.IsFalse(id == -1);
            }

            Assert.IsTrue(pool.Count == 10); // back to 10
            foreach (int id in newids)
            {
                Assert.AreEqual(0, pool.Values[pool.IdsToIndices[id]].X); // verify that returned structs were cleared
            }
        }
Ejemplo n.º 16
0
        public void Adding_1()
        {
            StructArray <Entity> entities = new StructArray <Entity>();
            Entity entity = new Entity()
            {
                id = 2, version = 1
            };

            entities.Add(entity);

            Assert.AreEqual(entities.Count, 1);
        }
        /* Construction/Destruction */
        public RandomUInt64Generator(int megabytes)
        {
            int totalBytes = Mathematics.MegaBytesToBytes(megabytes);
            int structs    = Mathematics.BytesToStructCount <UInt64>(totalBytes);

            Structs = new UInt64[structs];

            for (int x = 0; x < structs; x++)
            {
                Structs[x] = (UInt64)NextRandom(UInt64.MinValue, UInt64.MaxValue);
            }

            Bytes = StructArray.GetBytes(Structs);
        }
        /* Construction/Destruction */
        public RandomDoubleGenerator(int megabytes)
        {
            int totalBytes = Mathematics.MegaBytesToBytes(megabytes);
            int structs    = Mathematics.BytesToStructCount <Double>(totalBytes);

            Structs = new Double[structs];

            for (int x = 0; x < structs; x++)
            {
                Structs[x] = (Double)NextRandom(Double.MinValue, Double.MaxValue);
            }

            Bytes = StructArray.GetBytes(Structs);
        }
        /* Construction/Destruction */
        public RandomInt32Generator(int megabytes)
        {
            int totalBytes = Mathematics.MegaBytesToBytes(megabytes);
            int structs    = Mathematics.BytesToStructCount <Int32>(totalBytes);

            Structs = new Int32[structs];

            for (int x = 0; x < structs; x++)
            {
                Structs[x] = (Int32)NextRandom(Int32.MinValue, Int32.MaxValue);
            }

            Bytes = StructArray.GetBytes(Structs);
        }
Ejemplo n.º 20
0
        public void Insert_1()
        {
            StructArray <Entity> entities = new StructArray <Entity>();
            Entity entity = new Entity()
            {
                id = 2, version = 1
            };

            entities.Add(entity);

            entity.id = 3;
            entities.Insert(0, entity);

            Assert.AreEqual(entities[0].id, 3);
        }
        /* Construction/Destruction */
        public RandomIntStructGenerator(int megabytes)
        {
            int totalBytes = Mathematics.MegaBytesToBytes(megabytes);
            int structs    = Mathematics.BytesToStructCount <RandomIntStruct>(totalBytes);

            Structs = new RandomIntStruct[structs];

            for (int x = 0; x < structs; x++)
            {
                Structs[x] = RandomIntStruct.BuildRandomStruct();
            }

            Bytes = StructArray.GetBytes(Structs);
            File.WriteAllBytes(TestFileName, Bytes);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Obtains the AFS header from a specific file path.
        /// </summary>
        private AfsFileEntry[] GetEntriesFromFile(string filePath)
        {
            using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8192);

            var data = new byte[sizeof(AfsHeader)];

            stream.Read(data, 0, data.Length);
            Struct.FromArray(data, out AfsHeader header);

            data = new byte[sizeof(AfsFileEntry) * header.NumberOfFiles];
            stream.Read(data, 0, data.Length);
            StructArray.FromArray(data, out AfsFileEntry[] entries);

            return(entries);
        }
        /* Construction/Destruction */
        public RandomIntegerGenerator(int megabytes)
        {
            int totalBytes = Mathematics.MegaBytesToBytes(megabytes);
            int structs    = Mathematics.BytesToStructCount <int>(totalBytes);

            Structs = new int[structs];

            for (int x = 0; x < structs; x++)
            {
                Structs[x] = _random.Next();
            }

            Bytes = StructArray.GetBytes(Structs);
            File.WriteAllBytes(TestFileName, Bytes);
        }
Ejemplo n.º 24
0
        /* Read Base Implementation */

        /// <summary>
        /// Reads a generic type array from a specified memory address.
        /// </summary>
        /// <typeparam name="T">An individual struct type of a class with an explicit StructLayout.LayoutKind attribute.</typeparam>
        /// <param name="value">Local variable to receive the read in struct array.</param>
        /// <param name="memory"></param>
        /// <param name="memoryAddress">The memory address to read from.</param>
        /// <param name="arrayLength">The amount of array items to read.</param>
        /// <param name="marshal">Set this to true to enable struct marshalling.</param>
        public static void Read <T>(this IMemory memory, IntPtr memoryAddress, out T[] value, int arrayLength, bool marshal = false)
        {
            IMemory oldSource = Struct.Source;

            Struct.Source = memory;

#if NET5_0_OR_GREATER
            value = GC.AllocateUninitializedArray <T>(arrayLength, false);
#else
            value = new T[arrayLength];
#endif
            StructArray.FromPtr(memoryAddress, out value, arrayLength, marshal);

            Struct.Source = oldSource;
        }
Ejemplo n.º 25
0
            public void Visit(StructArray array)
            {
                Assert.IsAssignableFrom <StructArray>(_expectedArray);
                StructArray expectedArray = (StructArray)_expectedArray;

                Assert.Equal(expectedArray.Length, array.Length);
                Assert.Equal(expectedArray.NullCount, array.NullCount);
                Assert.Equal(expectedArray.Offset, array.Offset);
                Assert.Equal(expectedArray.Data.Children.Length, array.Data.Children.Length);
                Assert.Equal(expectedArray.Fields.Count, array.Fields.Count);

                for (int i = 0; i < array.Fields.Count; i++)
                {
                    array.Fields[i].Accept(new ArrayComparer(expectedArray.Fields[i]));
                }
            }
Ejemplo n.º 26
0
        protected override void OnDraw()
        {
            EntityWorld world = EntityWorld.Active;
            StructArray <EntityChunk> chunks = world.EntityManager.GetChunks();

            for (int i = 0; i < chunks.Count; i++)
            {
                ref EntityChunk chunk = ref chunks[i];
                bool            open  = ImGui.TreeNode($"[{i}] Chunk: {chunk.Count}/{chunk.Capacity}");
                if (ImGui.IsItemHovered())
                {
                    ImGui.SetTooltip(chunk.Archetype.ToString());
                }
                if (open)
                {
                    ImGuiTableFlags tFlags = ImGuiTableFlags.RowBg | ImGuiTableFlags.Resizable;
                    if (ImGui.BeginTable("Entities", 2, tFlags))
                    {
                        ImGui.TableSetupColumn("Id");
                        ImGui.TableSetupColumn("Version");
                        ImGui.TableHeadersRow();

                        for (int e = 0; e < chunk.Count; e++)
                        {
                            Entity entity = chunk.GetEntity(e);

                            ImGui.TableNextRow();
                            ImGui.TableNextColumn();

                            bool isSelected = ObjectSelections.CurrentObj?.Equals(entity) ?? false;
                            if (ImGui.Selectable($"##{e}", isSelected, ImGuiSelectableFlags.SpanAllColumns))
                            {
                                ObjectSelections.SelectObject(entity);
                            }
                            ImGui.SameLine();
                            ImGui.Text(entity.id.ToString());

                            ImGui.TableNextColumn();
                            ImGui.Text(entity.version.ToString());
                        }
                        ImGui.Separator();
                        ImGui.EndTable();
                    }
                    ImGui.TreePop();
                }
            }
Ejemplo n.º 27
0
        public void Removing_1()
        {
            StructArray <Entity> entities = new StructArray <Entity>();
            Entity entity = new Entity()
            {
                id = 2, version = 1
            };

            entities.Add(entity);

            entity.id = 3;
            entities.Add(entity);

            entity.id = 2;
            entities.SwapForLast(entity);

            Assert.AreEqual(entities.Count, 1);
        }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            StructArray <int> a = new StructArray <int>(3);

            a.Array[0] = 1;
            a.Array[1] = 3;
            a.Array[2] = 4;
            for (int i = 0; i < a.Array.Length; i++)
            {
                Console.WriteLine(a.Array[i]);
            }

            RefArray <StructArray <double> > b = new RefArray <StructArray <double> >(3);

            b.Array[0]          = new StructArray <double>(5);
            b.Array[0].Array[0] = 1;
            b.Array[0].Array[1] = 1;
            b.Array[0].Array[2] = 1;
            b.Array[0].Array[3] = 1;
            b.Array[0].Array[4] = 1;

            b.Array[1] = new StructArray <double>(10);
            b.Array[2] = new StructArray <double>(105);
            for (int i = 0; i < b.Array.Length; i++)
            {
                Console.WriteLine(b.Array[0].Array[i]);
            }

            BaseArray <Base> c = new BaseArray <Base>(3);

            c.Array[0] = new Base();
            c.Array[1] = new Derived();
            c.Array[2] = CreateInstance <Base>();

            BaseArray <Derived> d = new BaseArray <Derived>(3);

            d.Array[0] = new Derived();
            d.Array[1] = CreateInstance <Derived>();
            d.Array[2] = CreateInstance <Derived>();

            BaseArray <Derived> e = new BaseArray <Derived>(3);

            e.CopyArray <Derived>(d.Array);
        }
Ejemplo n.º 29
0
        public void DEBUG_LOG_INFO()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Entity Manager: \n");
            StructArray <EntityChunk> chunks = entityManager.GetChunks();

            sb.Append("Chunks Count: " + chunks.Count + "\n");
            sb.Append($"----------------------------------- \n");
            for (int i = 0; i < chunks.Count; i++)
            {
                EntityChunk chunk = chunks[i];
                sb.Append($"Entities: {chunk.Count} / Version: {chunk.ChunkVersion} \n");
                sb.Append($"Archetype: {chunk.Archetype} \n");
                sb.Append($"----------------------------------- \n");
            }
            sb.Append("\n");
            Console.WriteLine(sb);
        }
Ejemplo n.º 30
0
        public void Removing_2()
        {
            StructArray <Entity> entities = new StructArray <Entity>();
            Entity entity = new Entity();

            entities.Add(entity);

            entity.id = 1;
            entities.Add(entity);

            entity.id = 2;
            entities.Add(entity);

            entity.id = 3;
            entities.Add(entity);

            entities.SwapForLast(0);

            Assert.AreEqual(entities[0].id, 3);
        }
Ejemplo n.º 31
0
 public override object Clone()
 {
     StructArray newObj = new StructArray(this);
     // clone the elements of the array?
     array.CopyTo(newObj.array, 0);
     return newObj;
 }
Ejemplo n.º 32
0
			private StructArray(StructArray c) : base(c)
			{
				array = new _ElementType[c.Count];
			}