Exemple #1
0
        public async Task Prepare(bool downloadFiles = false)
        {
            var skipFilePath = Path.Combine(Scanner.Configuration.Proxy.MirrorRoot, VersionName, ".skip");

            if (downloadFiles && File.Exists(skipFilePath))
            {
                return;
            }

            if (Loading)
            {
                return;
            }

            Directory.CreateDirectory(Path.GetDirectoryName(skipFilePath));
            File.Create(skipFilePath);

            Loading = true;

            await Task.Run(() =>
            {
                Scanner.WriteLine($"[{VersionName}] Downloading encoding {BuildConfiguration.Encoding[1].ToHexString()} ...");
                Encoding.FromNetworkResource(CDN.Hosts[0],
                                             $"/{CDN.Path}/data/{BuildConfiguration.Encoding[1][0]:x2}/{BuildConfiguration.Encoding[1][1]:x2}/{BuildConfiguration.Encoding[1].ToHexString()}");
                Scanner.WriteLine($"[{VersionName}] Encoding downloaded ({Encoding.Count} entries).");

                if (!Encoding.TryGetValue(BuildConfiguration.Root, out var rootEncodingEntry))
                {
                    Scanner.WriteLine($"[{VersionName}] Unable to find root file {BuildConfiguration.Root.ToHexString()} ...");
                    return;
                }

                Scanner.WriteLine($"[{VersionName}] Downloading root {rootEncodingEntry.Key.ToHexString()} ...");
                Root.FromStream(CDN.Hosts[0], $"/{CDN.Path}/data/{rootEncodingEntry.Key[0]:x2}/{rootEncodingEntry.Key[1]:x2}/{rootEncodingEntry.Key.ToHexString()}");
                Scanner.WriteLine($"[{VersionName}] Root downloaded ({Root.Count} entries).");

                Scanner.WriteLine($"[{VersionName}] Downloading {ContentConfiguration.Archives.Length} indices ...");
                Indices.FromStream(CDN.Hosts[0], ContentConfiguration.Archives);
                Scanner.WriteLine($"[{VersionName}] Indices downloaded ({Indices.Count} entries).");

                void InstallLoader(string host, string path, byte[] hash)
                {
                    Install.FromNetworkResource(host,
                                                $"/{path}/data/{hash[0]:x2}/{hash[1]:x2}/{hash.ToHexString()}");

                    if (Install.Loaded)
                    {
                        Scanner.WriteLine($"[{VersionName}] Install file loaded ({hash.ToHexString()}, {Install.Count} entries).");
                    }
                }

                InstallLoader(CDN.Hosts[0], CDN.Path, BuildConfiguration.Install[1]);
                if (!Install.Loaded)
                {
                    if (Encoding.TryGetValue(BuildConfiguration.Install[0], out var encodingEntry))
                    {
                        InstallLoader(CDN.Hosts[0], CDN.Path, encodingEntry.Key);
                    }
                    else
                    {
                        Scanner.WriteLine($"[{VersionName}] Install file not found!");
                    }
                }
            }).ConfigureAwait(false);

            OnReady?.Invoke();

            if (downloadFiles && Install.Loaded)
            {
                await Task.Run(() =>
                {
                    foreach (var kv in Scanner.Configuration.GetBranchInfo(Channel).AutoDownloads)
                    {
                        DownloadFile(kv.Value, kv.LocalName);

                        // If we see an exe, be horribly gullible and hope for a PDB
                        if (kv.Value.Contains(".exe"))
                        {
                            var remotePdbName = kv.Value.Replace(".exe", ".pdb");
                            var localPdbName  = kv.LocalName?.Replace(".exe", ".pdb");

                            DownloadFile(remotePdbName, localPdbName);
                        }
                    }

                    // Mark for collection
                    Expired = true;
                });
            }

            Loading = false;
        }