示例#1
0
        /// <summary>
        /// Gets information on available update packages for a given platform.
        /// </summary>
        /// <param name="platform">The target platform.</param>
        /// <param name="username">Username for authentication.</param>
        /// <param name="token">Login token for authentication.</param>
        public async static Task <IReadOnlyList <UpdatePackageInfo> > GetUpdatePackagesAsync(Platform platform, string username, string token)
        {
            try
            {
                string url      = $"{PacketUrl}?apiVersion=2&username={username}&token={token}";
                string document = await WebHelper.RequestDocumentAsync(url);

                var response = JsonConvert.DeserializeObject <Dictionary <string, List <UpdatePackageInfo> > >(document);
                var packages = response?[platform.ToActualString()];
                if (packages is null)
                {
                    return(Array.Empty <UpdatePackageInfo>());
                }

                for (int i = packages.Count; i >= 0; i--)
                {
                    var package = packages[i];
                    if (package.From == package.To)
                    {
                        packages.RemoveAt(i);
                    }
                }

                return(packages);
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#2
0
        /// <summary>
        /// Requests a mod page from the server.
        /// </summary>
        /// <param name="pageSize">The page size. Negative values are interpreted as maximum page size (all mods on one page).</param>
        /// <param name="pageIndex">The 1-based index of the page to request based on the specified page size. Has no functionality if maximum page size is used.</param>
        public static async Task <ModPage> RequestPageAsync(int pageSize = -1, int pageIndex = 1)
        {
            if (pageSize == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(pageSize));
            }
            if (pageIndex < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(pageIndex));
            }

            string sizeStr = pageSize > 0 ? pageSize.ToString() : "max";
            string url     = $"{ModsUrl}?page_size={sizeStr}";

            if (pageSize > 0)
            {
                url += $"&page={pageIndex}";
            }

            try
            {
                string document = await WebHelper.RequestDocumentAsync(url);

                return(JsonConvert.DeserializeObject <ModPage>(document));
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#3
0
 /// <summary>
 /// Downloads a mod.
 /// </summary>
 /// <param name="release">The specific release to download.</param>
 /// <param name="username">Username for authentication.</param>
 /// <param name="token">Login token for authentication.</param>
 /// <param name="file">Destination file.</param>
 public static async Task DownloadModReleaseAsync(ModReleaseInfo release, string username, string token, FileInfo file,
                                                  CancellationToken cancellationToken = default, IProgress <double> progress = null)
 {
     try
     {
         string url = $"{release.DownloadUrl}?username={username}&token={token}";
         await WebHelper.DownloadFileAsync(url, file, cancellationToken, progress);
     }
     catch (WebException ex)
     {
         throw ApiException.FromWebException(ex);
     }
 }
示例#4
0
        /// <summary>
        /// Requests extended information on a specific mod.
        /// </summary>
        public static async Task <ApiModInfo> RequestModInfoAsync(string modName)
        {
            try
            {
                string url      = $"{ModsUrl}/{modName}/full";
                string document = await WebHelper.RequestDocumentAsync(url);

                return(JsonConvert.DeserializeObject <ApiModInfo>(document));
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#5
0
        /// <summary>
        /// Downloads an update package.
        /// </summary>
        /// <param name="platform">The target platform.</param>
        /// <param name="from">The version to update from.</param>
        /// <param name="to">The version to update to.</param>
        /// <param name="file">The destination file.</param>
        /// <param name="username">Username for authentication.</param>
        /// <param name="token">Login token for authentication.</param>
        public async static Task DownloadUpdatePackageAsync(Platform platform, AccurateVersion from, AccurateVersion to, FileInfo file, string username, string token,
                                                            CancellationToken cancellationToken = default, IProgress <double> progress = null)
        {
            try
            {
                string url = await GetPackageLinkAsync(platform, from, to, username, token);

                await WebHelper.DownloadFileAsync(url, file, cancellationToken, progress);
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#6
0
        private static async Task <ModPage> RequestPageInternalAsync(int pageSize, int pageIndex)
        {
            string url = $"{ModsUrl}?page_size={pageSize}&page={pageIndex}";

            try
            {
                string document = await WebHelper.RequestDocumentAsync(url);

                return(JsonConvert.DeserializeObject <ModPage>(document));
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#7
0
        /// <summary>
        /// Gets information about the latest stable and experimental releases of Factorio.
        /// </summary>
        public async static Task <(ReleaseInfo stable, ReleaseInfo experimental)> GetReleasesAsync()
        {
            try
            {
                string document = await WebHelper.RequestDocumentAsync(ReleasesUrl);

                dynamic response         = JsonConvert.DeserializeObject(document) !;
                var     stableDict       = ((JToken)response.stable).ToObject <Dictionary <string, string> >() !;
                var     experimentalDict = ((JToken)response.experimental).ToObject <Dictionary <string, string> >() !;
                return(new ReleaseInfo(stableDict), new ReleaseInfo(experimentalDict));
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }
示例#8
0
        /// <summary>
        /// Downloads a specific release of Factorio.
        /// </summary>
        /// <param name="version">The version of Factorio.</param>
        /// <param name="build">The build of Factorio.</param>
        /// <param name="platform">The target platform.</param>
        /// <param name="username">Username for authentication.</param>
        /// <param name="token">Login token for authentication.</param>
        /// <param name="file">The destination file.</param>
        public async static Task DownloadReleaseAsync(AccurateVersion version, FactorioBuild build, Platform platform, string username, string token, FileInfo file,
                                                      CancellationToken cancellationToken = default, IProgress <double>?progress = null)
        {
            string versionStr  = version.ToString(3);
            string buildStr    = build.ToActualString();
            string platformStr = platform.ToActualString();
            string url         = $"{DownloadUrl}/{versionStr}/{buildStr}/{platformStr}?username={username}&token={token}";

            try
            {
                await WebHelper.DownloadFileAsync(url, file, cancellationToken, progress);
            }
            catch (WebException ex)
            {
                throw ApiException.FromWebException(ex);
            }
        }