Example #1
0
 private static void LoadModsFromFileDictionary(Dictionary <string, ModFileCollection> fileDictionary, ModCollection parentCollection, ModpackCollection modpackCollection)
 {
     foreach (var modFileList in fileDictionary.Select(kvp => kvp.Value))
     {
         var mod = new Mod(modFileList, parentCollection, modpackCollection);
         parentCollection.Add(mod);
     }
 }
Example #2
0
        /// <summary>
        /// Downloads a mod.
        /// </summary>
        /// <param name="release">The mods release to be downloaded.</param>
        /// <param name="username">The username.</param>
        /// <param name="token">The login token.</param>
        /// <param name="progress">A progress object used to report the progress of the operation.</param>
        /// <param name="cancellationToken">A cancelation token that can be used to cancel the operation.</param>
        /// <param name="parentCollection">The collection to contain the mods.</param>
        /// <param name="modpackCollection">The collection containing all modpacks.</param>
        public static async Task <Mod> DownloadReleaseAsync(ModRelease release, string username, string token, IProgress <double> progress, CancellationToken cancellationToken,
                                                            ModCollection parentCollection, ModpackCollection modpackCollection)
        {
            DirectoryInfo modDirectory = App.Instance.Settings.GetModDirectory(release.InfoFile.FactorioVersion);

            if (!modDirectory.Exists)
            {
                modDirectory.Create();
            }

            var downloadUrl = BuildUrl(release, username, token);
            var file        = new FileInfo(Path.Combine(modDirectory.FullName, release.FileName));

            try
            {
                await WebHelper.DownloadFileAsync(downloadUrl, file, progress, cancellationToken);

                if (!cancellationToken.IsCancellationRequested)
                {
                    if (ModFile.TryLoadFromFile(file, out ModFile modFile))
                    {
                        if (modFile.InfoFile.FactorioVersion == release.InfoFile.FactorioVersion)
                        {
                            return(await Mod.Add(modFile, parentCollection, modpackCollection, false));
                        }
                    }

                    throw new InvalidOperationException("The server sent an invalid mod file.");
                }
            }
            catch (Exception)
            {
                if (file.Exists)
                {
                    file.Delete();
                }

                throw;
            }

            return(null);
        }