Пример #1
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);
        }
Пример #2
0
        private List <PatternScanResult> GetPointerPatterns(int targetOffset)
        {
            // Now find all pointers to file name pattern.
            byte[] offsetBytes = Struct.GetBytes(targetOffset);
            var    scanPattern = new CompiledScanPattern(Utilities.Utilities.BytesToScanPattern(offsetBytes));

            return(Utilities.Utilities.FindAllPatterns(_scanner, scanPattern));
        }
        /// <summary>
        /// Converts the current configuration's physics profiles to bytes.
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            var values = Enum.GetValues(typeof(AllCharacters));
            var bytes  = new List <byte>(values.Length * Struct.GetSize <AdventurePhysics>(true));

            foreach (AllCharacters character in (AllCharacters[])values)
            {
                var physics = Physics[character];
                bytes.AddRange(Struct.GetBytes(ref physics, true));
            }

            return(bytes.ToArray());
        }
Пример #4
0
        public unsafe void IsBlittable()
        {
            // Requires C# 8. Please ignore if Intellisense complains. Will compile and run.
            double original = 0.7286906838f;
            double maximum  = 5f;

            var compressed = new CompressedNumber <double, Double, ushort, UShort>(original, maximum);
            var bytes      = Struct.GetBytes(compressed);

            Struct.FromArray(bytes, out CompressedNumber <double, Double, ushort, UShort> fromBytes, 0);
            var decompressed = fromBytes.GetValue(maximum);

            Assert.Equal(original, decompressed, 5);
        }
Пример #5
0
        /// <inheritdoc />
        public void Write <T>(IntPtr memoryAddress, ref T item, bool marshal)
        {
            byte[] bytes = Struct.GetBytes(ref item, marshal);

            fixed(byte *bytePtr = bytes)
            {
                bool succeeded = Kernel32.Kernel32.WriteProcessMemory(_processHandle, memoryAddress, (IntPtr)bytePtr, (UIntPtr)bytes.Length, out _);

                if (!succeeded)
                {
                    throw new MemoryException($"WriteProcessMemory failed to write {bytes.Length} bytes of memory to {memoryAddress.ToString("X")}");
                }
            }
        }
        public static void Write <T>(this Stream stream, ref T structure) where T : unmanaged
        {
#if FEATURE_NATIVE_SPAN
            if (sizeof(T) < MaxStackLimit)
            {
                Span <byte> stack = stackalloc byte[sizeof(T)];
                MemoryMarshal.Write(stack, ref structure);
                stream.Write(stack);
            }
            else
            {
                stream.Write(Struct.GetBytes(structure));
            }
#else
            stream.Write(Struct.GetBytes(structure));
#endif
        }
        public static void Write <T>(this Stream stream, ref T structure, bool marshalStructure = true)
        {
#if FEATURE_NATIVE_SPAN
            var size = Struct.GetSize <T>(true);
            if (size < MaxStackLimit)
            {
                Span <byte> stack = stackalloc byte[size];
                Struct.GetBytes(ref structure, marshalStructure, stack);
                stream.Write(stack);
            }
            else
            {
                stream.Write(Struct.GetBytes(structure, marshalStructure));
            }
#else
            stream.Write(Struct.GetBytes(structure, marshalStructure));
#endif
        }
Пример #8
0
        public static byte[] GetFrameBytes(Tile[] tiles)
        {
            var bytes = new List <byte> {
                (byte)tiles.Length
            };

            foreach (Tile t in tiles)
            {
                bytes.Add((byte)t.Id);
                bytes.Add((byte)t.Frames.Count());

                foreach (var f in t.Frames)
                {
                    bytes.AddRange(Struct.GetBytes(f));
                }
            }
            return(bytes.ToArray());
        }
 public void Write <T>(T structure, bool marshalStructure = true) => Write(Struct.GetBytes(structure, marshalStructure));
 public void WriteBigEndianPrimitive(ushort structure)
 {
     structure = Endian.Reverse(structure);
     Write(Struct.GetBytes(structure));
 }
Пример #11
0
 /// <summary>
 /// Adds a new item onto the circular buffer.
 /// </summary>
 /// <param name="item">The item to add onto the buffer.</param>
 /// <param name="marshalElement">The element to be marshalled.</param>
 /// <returns>Pointer to the recently added item to the buffer.</returns>
 public IntPtr Add <TStructure>(ref TStructure item, bool marshalElement = false)
 {
     return(Add(Struct.GetBytes(ref item, marshalElement)));
 }
 public void WriteBigEndianStruct <T>(T structure) where T : unmanaged, IEndianReversible
 {
     structure.SwapEndian();
     Write(Struct.GetBytes(structure));
 }
 public void Write <T>(T structure) where T : unmanaged => Write(Struct.GetBytes(structure));
Пример #14
0
 /// <summary>
 /// Appends a managed/marshalled structure onto the given <see cref="MemoryStream"/> and advances the position.
 /// </summary>
 public void Append <T>(T structure, bool marshalStructure = true) => Append(Struct.GetBytes(structure, marshalStructure));
Пример #15
0
 /// <summary>
 /// Appends an unmanaged structure onto the <see cref="MemoryStream"/> and advances the position.
 /// </summary>
 public void Append <T>(T structure) where T : unmanaged => Append(Struct.GetBytes(structure));
 public void WriteBigEndianPrimitive <T>(T structure) where T : unmanaged
 {
     Endian.Reverse(ref structure);
     Write(Struct.GetBytes(structure));
 }
Пример #17
0
 public byte[] Serialize <TStruct>(ref TStruct item)
 {
     byte[] bytes = Struct.GetBytes(ref item, MarshalValues);
     return(bytes);
 }
        /// <summary>
        /// Converts a <see cref="MotionPackage"/> into a native .MTP file.
        /// </summary>
        public byte[] ToMtp()
        {
            var bytes = new List <byte>(1000000);

            var header = Header;

            header.SwapEndian();
            bytes.AddRange(Struct.GetBytes(ref header));

            // Write entries
            var dummyAnimationEntry      = new AnimationEntry();
            var dummyAnimationEntryBytes = Struct.GetBytes(ref dummyAnimationEntry);

            int[] entryOffsets = Entries.Select(x => AddRange(bytes, dummyAnimationEntryBytes)).ToArray();

            // Write file names.
            int[] fileNameOffsets = Entries.Select(x =>
            {
                int firstRef = AddRange(bytes, String.GetNullTerminatedBytes(String.Win1252Encoder, x.FileName));
                // Must pad to next group of 4-bytes, otherwise game will fail to parse
                while (bytes.Count % 4 > 0)
                {
                    bytes.Add(0x00);
                }
                return(firstRef);
            }).ToArray();

            // Write file data.
            int[] fileDataOffsets = Entries.Select(x => AddRange(bytes, x.FileData)).ToArray();

            // Write extra properties.
            int[] filePropertyOffsets = Entries.Select(x =>
            {
                if (x.Tuples != null)
                {
                    // Temporarily swap out the endian of all tuples before writing to array, then swap back.
                    for (int i = 0; i < x.Tuples.Length; i++)
                    {
                        x.Tuples[i].SwapEndian();
                    }

                    var result = AddRange(bytes, StructArray.GetBytes(x.Tuples));

                    for (int i = 0; i < x.Tuples.Length; i++)
                    {
                        x.Tuples[i].SwapEndian();
                    }

                    return(result);
                }

                return(0);
            }).ToArray();

            // Fix Offsets
            var byteArray = bytes.ToArray();

            fixed(byte *byteArrayPtr = byteArray)
            {
                for (int x = 0; x < Entries.Length; x++)
                {
                    ref var entry = ref Unsafe.AsRef <AnimationEntry>(byteArrayPtr + entryOffsets[x]);
                    Endian.Reverse(ref fileNameOffsets[x]);
                    Endian.Reverse(ref fileDataOffsets[x]);
                    Endian.Reverse(ref filePropertyOffsets[x]);

                    entry.FileNamePtr      = fileNameOffsets[x];
                    entry.FileDataPtr      = fileDataOffsets[x];
                    entry.PropertyTuplePtr = filePropertyOffsets[x];
                }
            }