Exemplo n.º 1
0
        /// <summary>
        /// Checks if a file at a specified path is an AFS archive.
        /// </summary>
        private bool IsAfsFile(string filePath)
        {
            if (_isAfsFileCache.ContainsKey(filePath))
            {
                return(_isAfsFileCache[filePath]);
            }

            if (!File.Exists(filePath))
            {
                return(false);
            }

            using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, sizeof(AfsHeader));

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

            stream.Read(dataSpan);
            Struct.FromArray(dataSpan, out AfsHeader header);

            bool isAfsFile = header.IsAfsArchive;

            _isAfsFileCache[filePath] = isAfsFile;
            return(isAfsFile);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if a file at a specified path is an ONE archive.
        /// </summary>
        private bool IsOneFile(string filePath)
        {
            if (_isOneFileCache.ContainsKey(filePath))
            {
                return(_isOneFileCache[filePath]);
            }

            if (!File.Exists(filePath))
            {
                return(false);
            }

            using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, sizeof(OneArchiveHeader));

            var data     = new byte[sizeof(OneArchiveHeader)];
            var dataSpan = data.AsSpan();

            stream.Read(dataSpan);
            Struct.FromArray(dataSpan, out OneArchiveHeader header);

            var  sizeOfFile = stream.Seek(0, SeekOrigin.End);
            bool isOneFile  = header.FileSize + sizeof(OneArchiveHeader) == sizeOfFile;

            _isOneFileCache[filePath] = isOneFile;
            return(isOneFile);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void WriteManagedStruct()
        {
            var marshallingStruct = new MarshallingStruct
            {
                Name                 = "Chuck Norris",
                CompressedSize       = 420,
                UncompressedFileSize = 942
            };

            using (var extendedStream = new LittleEndianMemoryStream(new Reloaded.Memory.Streams.ExtendedMemoryStream()))
            {
                extendedStream.Write(marshallingStruct);
                Struct.FromArray <MarshallingStruct>(extendedStream.ToArray(), out var newStruct);
                Assert.Equal(marshallingStruct, newStruct);
            };
        }
Exemplo n.º 6
0
        public void WriteRegularStruct()
        {
            var randomIntStruct = new RandomIntStruct()
            {
                A = 1,
                B = 33,
                C = 4214
            };

            using (var extendedStream = new LittleEndianMemoryStream(new Reloaded.Memory.Streams.ExtendedMemoryStream()))
            {
                extendedStream.Write(randomIntStruct);
                Struct.FromArray <RandomIntStruct>(extendedStream.ToArray(), out var newStruct, 0);
                Assert.Equal(randomIntStruct, newStruct);
            };
        }
Exemplo n.º 7
0
 public TStruct Deserialize <TStruct>(byte[] serialized)
 {
     Struct.FromArray(serialized, out TStruct value, MarshalValues, 0);
     return(value);
 }
 private double NextRandom(double minimum, double maximum)
 {
     _random.NextBytes(_randomBuffer);
     Struct.FromArray(_randomBuffer, out long longRand, 0);
     return((double)(Math.Abs(longRand % (maximum - minimum)) + minimum));
 }
 private ulong NextRandom(ulong minimum, ulong maximum)
 {
     _random.NextBytes(_randomBuffer);
     Struct.FromArray(_randomBuffer, out ulong longRand, 0);
     return((longRand % (maximum - minimum)) + minimum);
 }
 private long NextRandom(long minimum, long maximum)
 {
     _random.NextBytes(_randomBuffer);
     Struct.FromArray(_randomBuffer, out long longRand, 0);
     return(Math.Abs(longRand % (maximum - minimum)) + minimum);
 }