Esempio n. 1
0
        /// <summary>
        /// Reads the new file table format in v9 and v10
        /// </summary>
        private void readNewFileTable(Stream header, bool readHash = false)
        {
            uint numFiles = header.ReadUInt32LE();
            var  files    = new OffsetFile[numFiles];

            for (var i = 0; i < numFiles; i++)
            {
                // Version 3 uses 32-bit file offsets
                long   arkFileOffset = header.ReadInt64LE();
                string path          = header.ReadLengthPrefixedString(System.Text.Encoding.UTF8);
                var    flags         = header.ReadInt32LE();
                uint   size          = header.ReadUInt32LE();
                if (readHash)
                {
                    header.Seek(4, SeekOrigin.Current);   // Skips checksum
                }
                var finalSlash = path.LastIndexOf('/');
                var fileDir    = path.Substring(0, finalSlash < 0 ? 0 : finalSlash);
                var fileName   = path.Substring(finalSlash < 0 ? 0 : (finalSlash + 1));
                var parent     = makeOrGetDir(fileDir);
                var file       = new OffsetFile(fileName, parent, contentFileMeta, arkFileOffset, size);
                file.ExtendedInfo["id"]    = i;
                file.ExtendedInfo["flags"] = flags;
                files[i] = file;
                parent.AddFile(file);
            }

            var numFiles2 = header.ReadUInt32LE();

            if (numFiles != numFiles2)
            {
                throw new Exception("Ark header appears invalid (file count mismatch)");
            }
            for (var i = 0; i < numFiles2; i++)
            {
                files[i].ExtendedInfo["flags2"] = header.ReadInt32LE();
            }
        }
        public async Task Initialize()
        {
            OffsetFile localOffset  = null;
            OffsetFile remoteOffset = null;

            // Get the local offset file.
            try
            {
                if (File.Exists(LocalOffsetFile))
                {
                    var offsets = JsonConvert.DeserializeObject <OffsetFile>(File.ReadAllText(LocalOffsetFile));
                    if (offsets != null)
                    {
                        localOffset = offsets;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(new Exception("Couldn't load offset file", ex), "OffsetService");
            }

            // Fetch latest offsets from online.
            using (var client = new HttpClient())
            {
                try
                {
                    var response = await client.GetAsync(Url);

                    response.EnsureSuccessStatusCode();
                    var body = await response.Content.ReadAsStringAsync();

                    remoteOffset = JsonConvert.DeserializeObject <OffsetFile>(body);
                }
                catch (HttpRequestException ex)
                {
                    Log.Write(new Exception("Couldn't fetch offsets from online!", ex), "OffsetService");
                }
            }

            if (remoteOffset == null && localOffset == null)
            {
                throw new Exception("Wasn't able to resolve any offsets");
            }

            // If the offsets are the same.
            if (remoteOffset?.GameVersion == localOffset?.GameVersion)
            {
                // Check which offset version is greater and use it.
                if (remoteOffset?.OffsetVersion > localOffset?.OffsetVersion)
                {
                    Offsets = remoteOffset;
                }
                else
                {
                    Offsets = localOffset;
                }
            }
            else
            {
                // Use the offset with the latest game version.
                if (remoteOffset?.GameVersion.CompareTo(localOffset?.GameVersion) > 0)
                {
                    Offsets = remoteOffset;
                }
                else
                {
                    Offsets = localOffset;
                }
            }

            if (Offsets == null)
            {
                throw new Exception("Wasn't able to resolve any offsets");
            }
        }