/// <summary> /// Downloads a Factorio package. /// </summary> /// <param name="version">The version of Factorio to be downloaded.</param> /// <param name="downloadDirectory">The directory the package is downloaded to.</param> /// <param name="container">The cookie container the session cookie is stored in.</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> public static async Task <FactorioVersion> DownloadFactorioPackageAsync(FactorioOnlineVersion version, DirectoryInfo downloadDirectory, CookieContainer container, IProgress <double> progress, CancellationToken cancellationToken) { if (!downloadDirectory.Exists) { downloadDirectory.Create(); } string filePath = Path.Combine(downloadDirectory.FullName, "package.zip"); var file = new FileInfo(filePath); await WebHelper.DownloadFileAsync(version.DownloadUrl, container, file, progress, cancellationToken); if (!cancellationToken.IsCancellationRequested) { progress.Report(2); FactorioVersion factorioVersion = await Task.Run(() => { ZipFile.ExtractToDirectory(file.FullName, downloadDirectory.FullName); string versionString = version.Version.ToString(3); var versionDirectory = downloadDirectory.EnumerateDirectories($"Factorio_{versionString}*").First(); versionDirectory.MoveTo(Path.Combine(downloadDirectory.FullName, versionString)); file.Delete(); return(new FactorioVersion(versionDirectory, version.Version)); }); return(factorioVersion); } return(null); }
/// <summary> /// Reads the Factorio version list. /// </summary> /// <param name="container">The cookie container the session cookie is stored in.</param> /// <param name="versions">Out. The list of available Factorio versions.</param> /// <returns>Returns false if the version list could not be retrieved, otherwise true.</returns> public static bool GetVersions(CookieContainer container, out List <FactorioOnlineVersion> versions) { const string downloadPage = "https://www.factorio.com/download"; const string experimentalDownloadPage = "https://www.factorio.com/download/experimental"; const string pattern = @"<h3> *(?<version>[0-9]+\.[0-9]+\.[0-9]+) *\((?<modifier>[a-z]+)\) *</h3>"; string[] allowedModifiers = { "alpha" }; versions = new List <FactorioOnlineVersion>(); // Get stable versions. string document = WebHelper.GetDocument(downloadPage, container); MatchCollection matches = Regex.Matches(document, pattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); foreach (Match match in matches) { string versionString = match.Groups["version"].Value; string modifierString = match.Groups["modifier"].Value; Version version = Version.Parse(versionString); if (allowedModifiers.Contains(modifierString) && VersionCompatibleWithPlatform(version)) { var factorioVersion = new FactorioOnlineVersion(version, modifierString, false); versions.Add(factorioVersion); } } // Get experimental versions. document = WebHelper.GetDocument(experimentalDownloadPage, container); matches = Regex.Matches(document, pattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); foreach (Match match in matches) { string versionString = match.Groups["version"].Value; string modifierString = match.Groups["modifier"].Value; if (allowedModifiers.Contains(modifierString)) { var version = new FactorioOnlineVersion(Version.Parse(versionString), modifierString, true); versions.Add(version); } } return(true); }
private static void GetVersionsFromUrl(string url, bool isExperimental, List <FactorioOnlineVersion> versionList) { const string pattern = @"<h3> *(?<version>\d+\.\d+\.\d+) +\(.+\) *<\/h3>"; string document = WebHelper.GetDocument(url); var matches = Regex.Matches(document, pattern); foreach (Match match in matches) { string versionString = match.Groups["version"].Value; var version = Version.Parse(versionString); if (!VersionListContains(versionList, version)) { var onlineVersion = new FactorioOnlineVersion(version, isExperimental); versionList.Add(onlineVersion); } } }
/// <summary> /// Downloads Factorio. /// </summary> /// <param name="version">The version of Factorio to be downloaded.</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> public static async Task <FactorioVersion> DownloadFactorioAsync(FactorioOnlineVersion version, string username, string token, IProgress <double> progress, CancellationToken cancellationToken) { var factorioDirectory = App.Instance.Settings.GetFactorioDirectory(); if (!factorioDirectory.Exists) { factorioDirectory.Create(); } var file = new FileInfo(Path.Combine(factorioDirectory.FullName, "package.zip")); string url = version.DownloadUrl + $"?username={username}&token={token}"; await WebHelper.DownloadFileAsync(new Uri(url), null, file, progress, cancellationToken); try { if (cancellationToken.IsCancellationRequested) { return(null); } progress.Report(2); if (!FactorioFile.TryLoad(file, out var factorioFile)) { return(null); } var factorioFolder = await FactorioFolder.FromFileAsync(factorioFile, factorioDirectory); return(new FactorioVersion(factorioFolder)); } finally { if (file.Exists) { file.Delete(); } } }