private async Task DownloadBranchesAsync(IEnumerable <SteamAppBranch> toDownload, CancellationToken ct)
 {
     foreach (var branch in toDownload)
     {
         await DownloadBranchAsync(branch, ct);
     }
     DepotDownloaderExt.ContentDownloaderShutdownSteam3();
 }
        private IEnumerable <SteamAppBranch> GetAllBranches()
        {
            DepotDownloaderExt.AccountSettingsStoreLoadFromFile("account.config");
            DepotDownloaderExt.DepotDownloaderProgramInitializeSteam(_options.SteamLogin, _options.SteamPassword);
            DepotDownloaderExt.ContentDownloadersteam3RequestAppInfo(_options.SteamAppId);
            var depots   = DepotDownloaderExt.ContentDownloaderGetSteam3AppSection(_options.SteamAppId);
            var branches = depots["branches"];

            return(branches.Children.Select(c => ConvertVersion(c.Name !, c["buildid"].Value !)));
        }
        public async Task ExecuteAsync(CancellationToken ct)
        {
            var nugetFeed = new NuGetFeed(_options.FeedUrl, _options.FeedUser, _options.FeedPassword, _options.PackageBaseName);

            Trace.WriteLine("Checking NuGet...");
            var packages = await nugetFeed.GetVersionsAsync(ct);

            foreach (var(key, value) in packages)
            {
                Trace.WriteLine($"{key}: [{string.Join(", ", value)}]");
            }

            Trace.WriteLine("Checking branches...");
            var branches = GetAllBranches().ToList();

            Trace.WriteLine("Getting new versions...");
            var prefixes = new HashSet <BranchType>(branches.Select(branch => branch.Prefix).Where(b => b != BranchType.Unknown));

            var coreVersions = prefixes.ToDictionary(branchType => branchType,
                                                     branchType => packages.TryGetValue(GetPackageName("Core", branchType), out var pkg)
                    ? pkg.Select(x => x.BuildId)
                    : Array.Empty <uint>());

            var publicBranch        = branches.First(branch => branch.Name == "public");
            var otherBranches       = branches.Where(branch => branch.Prefix != BranchType.Unknown);
            var matchedPublicBranch = otherBranches.FirstOrDefault(branch => branch.BuildId == publicBranch.BuildId);

            if (matchedPublicBranch.BuildId == 0)
            {
                Trace.WriteLine("Public Branch does not match any version branch! Report to TaleWorlds!");
                return;
            }

            Trace.WriteLine($"Public Branch Matches: {matchedPublicBranch.Name}");
            var toDownload = branches.Where(branch =>
                                            branch.Prefix != BranchType.Unknown &&
                                            !coreVersions[branch.Prefix].Contains(branch.BuildId)
                                            // TODO: Fix parsing meta from older versions
                                            && Version.TryParse(branch.Name.Remove(0, 1), out var v) && v >= new Version("1.1.0")
                                            ).ToList();

            if (toDownload.Count == 0)
            {
                Trace.WriteLine("No new version detected! Exiting...");
                DepotDownloaderExt.ContentDownloaderShutdownSteam3();
                return;
            }

            Trace.WriteLine("New versions:");
            foreach (var br in toDownload)
            {
                Trace.WriteLine($"{br.Name}: ({br.AppId} {br.DepotId} {br.BuildId})");
            }

            Trace.WriteLine("Checking downloading...");
            await DownloadBranchesAsync(toDownload, ct);

            Trace.WriteLine("Generating references...");
            GenerateReferences(toDownload);

            Trace.WriteLine("Generating packages...");

            GeneratePackages(toDownload);

            Trace.WriteLine("Done!");
        }
        public Tool(GenerateOptions options)
        {
            DepotDownloaderExt.Init();

            _options = options;
        }
        private async Task DownloadBranchAsync(SteamAppBranch steamAppBranch, CancellationToken ct)
        {
            var folder = await(await(await ExecutableFolder
                                     .CreateFolderAsync("depots", CreationCollisionOption.OpenIfExists, ct))
                               .CreateFolderAsync(_options.SteamDepotId.ToString(), CreationCollisionOption.OpenIfExists, ct))
                         .CreateFolderAsync(steamAppBranch.BuildId.ToString(), CreationCollisionOption.OpenIfExists, ct);

            DepotDownloaderExt.ContentDownloaderConfigSetMaxDownloads(4);
            DepotDownloaderExt.ContentDownloaderConfigSetInstallDirectory(folder.Path);

            try
            {
                var fileListData = Resourcer.Resource.AsString("Resources/FileFilters.regexp");
                var files        = fileListData.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

                DepotDownloaderExt.ContentDownloaderConfigSetUsingFileList(true);
                var filesToDownload = DepotDownloaderExt.ContentDownloaderConfigGetFilesToDownload();
                filesToDownload.Clear();
                var filesToDownloadRegex = DepotDownloaderExt.ContentDownloaderConfigGetFilesToDownloadRegex();
                filesToDownloadRegex.Clear();

                foreach (var fileEntry in files)
                {
                    if (fileEntry.StartsWith("regex:"))
                    {
                        var rgx = new Regex(fileEntry.Substring(6), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        filesToDownloadRegex.Add(rgx);
                    }
                    else
                    {
                        filesToDownload.Add(fileEntry);
                    }
                    // require all expressions to be valid and with proper slashes
                }

                Trace.WriteLine("Using file filters:");

                ++Trace.IndentLevel;
                foreach (var file in files)
                {
                    Trace.WriteLine(file);
                }
                --Trace.IndentLevel;
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Warning: Unable to load file filters: {ex}");
            }

            await DepotDownloaderExt.ContentDownloaderDownloadAppAsync(
                _options.SteamAppId,
                new List <(uint depotId, ulong manifestId)> {
                (_options.SteamDepotId, ulong.MaxValue)
            },
                steamAppBranch.Name,
                _options.SteamOS,
                _options.SteamOSArch,
                null !,
                false,
                false).ConfigureAwait(false);
        }