Пример #1
0
        /* concept:
         *
         * foreach mod on the portal, check if we have an entry for said mod alraedy
         * if we do, compare latest release release date
         * if it differs, get ResultEntryFull from the protal
         * for every release with a maintained factorio version,
         * check if we already have this version downloaded
         * if we don't download it
         *
         * if we don't have an entry already, create an entry for the mod
         * and then do the same download logic as above
         * this includes checking for existing ones, so mods that were manually
         * copied into the mods folder would be identified
         *
         */

        public async Task RunAsync()
        {
            if (argsService.GetArgs().CreateConfig)
            {
                return;
            }

            Console.WriteLine("Syncronizing with mod portal...");

            ProgramData      programData      = programDataService.GetProgramData();
            HashSet <string> modNamesToDelete = programData.Mods.Values.Select(m => m.Name).ToHashSet();

            Console.WriteLine($"Loaded {programData.Mods.Count} mods from data file.");

            DateTime lastSaveTime       = DateTime.Now;
            int      attemptedSaveCount = 0;

            void SaveChanges(bool bypassConditions = false)
            {
                // only save every 5 minutes, but also only if there have been 32 attempts
                // to save, which indicates that something changed 32 times
                ++attemptedSaveCount;
                if (bypassConditions ||
                    (
                        attemptedSaveCount >= 32 &&
                        (DateTime.Now - lastSaveTime).Minutes >= 5
                    ))
                {
                    Console.WriteLine($"Saving {attemptedSaveCount} changes.");
                    attemptedSaveCount = 0;
                    programDataService.SetProgramData(programData);
                    lastSaveTime = DateTime.Now;
                }
            }

            // create and update
            await foreach (var entry in client.EnumerateAsync())
            {
                if (programData.Mods.TryGetValue(entry.Name, out var modData))
                {
                    modNamesToDelete.Remove(entry.Name);

                    if ((entry.LatestRelease?.ReleasedAt ?? null) != (modData.LatestRelease?.ReleasedAt ?? null))
                    {
                        SyncMod(entry, await client.GetResultEntryFullAsync(entry.Name), modData);
                        Console.WriteLine($"Changed {entry.Name}.");
                        SaveChanges();
                    }
                    else
                    {
                        SyncModPartial(entry, modData);
                    }
                }
                else
                {
                    var entryFull = await client.GetResultEntryFullAsync(entry.Name);

                    modData = mapperService.MapToModData(entryFull);
                    SyncMod(entry, entryFull, modData);
                    programData.Mods.Add(modData.Name, modData);
                    Console.WriteLine($"Added   {entry.Name}.");
                    SaveChanges();
                }
            }

            // delete mods
            if (modNamesToDelete.Count > 0)
            {
                foreach (var modNameToDelete in modNamesToDelete)
                {
                    programData.Mods.Remove(modNameToDelete);
                    Console.WriteLine($"Removed {modNameToDelete}.");
                }
                SaveChanges();
            }

            if (attemptedSaveCount > 0)
            {
                --attemptedSaveCount;
                SaveChanges(bypassConditions: true);
            }

            Console.WriteLine("Determining which releases should be maintained. Downloading and deleting accordingly.");

            var maintainedVersions = configService.GetConfig().MaintainedFactorioVersions;

            List <FactorioVersion> cachedReleases = new List <FactorioVersion>();

            foreach (var mod in programData.Mods.Values)
            {
                await SyncMaintainedReleasesAsync(mod, maintainedVersions, cachedReleases);
            }
        }
Пример #2
0
 private void LoadConfigFileName()
 {
     configPath = argsService.GetArgs().ConfigFilePath ??
                  File.ReadAllText(ConfigFilePointerPath, Encoding.UTF8);
 }