コード例 #1
0
        public async Task AddZippedVersion(string path)
        {
            var archiveFile = new FileInfo(path);

            if (FactorioFile.TryLoad(archiveFile, out var file))
            {
                if (file.Is64Bit == Environment.Is64BitOperatingSystem)
                {
                    var folder = await ExtractToFolder(file);

                    var factorioVersion = new FactorioVersion(folder);
                    FactorioVersions.Add(factorioVersion);
                    factorioVersion.BeginEdit();
                }
                else
                {
                    MessageBox.Show(Window,
                                    App.Instance.GetLocalizedMessage("IncompatiblePlatform", MessageType.Error),
                                    App.Instance.GetLocalizedMessageTitle("IncompatiblePlatform", MessageType.Error),
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show(Window,
                                App.Instance.GetLocalizedMessage("InvalidFactorioArchive", MessageType.Error),
                                App.Instance.GetLocalizedMessageTitle("InvalidFactorioArchive", MessageType.Error),
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #2
0
        /// <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();
                }
            }
        }