Ejemplo n.º 1
0
 public static string CreateRequestUrl(this BeatModsQuery query)
 {
     return(AllModsUrl
            .SetQueryParam("gameVersion", query.forGameVersion)
            .SetQueryParam("sort", query.sort.GetSortName())
            .SetQueryParam("status", query.status.GetStatusName())
            .SetQueryParam("search", query.search)
            .SetQueryParam("sortDirection", query.sortDescending ? -1 : 1));
 }
Ejemplo n.º 2
0
        public Task <List <Mod> > GetModsOnline(BeatModsQuery query)
        {
            return(Task.Run(() =>
            {
                List <Mod> matchingMods = new List <Mod>();

                using (WebClient wc = new WebClient())
                {
                    string allModsJson = wc.DownloadString(query.CreateRequestUrl());

                    AllMods = JsonConvert.DeserializeObject <List <Mod> >(allModsJson);
                }

                return matchingMods;
            }));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Refreshes the <see cref="AllMods"/> list with freshly new downloaded mod informations.
        /// Keeps the information about mods up-to-date.
        /// </summary>
        public Task RefreshMods(bool useCachedOldMods = false, int timeout = 12000)
        {
            return(Task.Run(async() =>
            {
                AllMods = new List <Mod>();
                OfflineMods = new List <Mod>();
                AllGameVersions = new List <string>();
                LastDidRefreshMods = DateTime.Now;

                CancellationTokenSource cts = new CancellationTokenSource(timeout);

                using (HttpClient hp = new HttpClient())
                {
                    string cacheFile = ALL_MODS_FILE;
                    CacheConfig cache = null;
                    if (File.Exists(cacheFile))
                    {
                        cache = JsonConvert.DeserializeObject <CacheConfig>(File.ReadAllText(cacheFile));
                    }

                    // Load offline available mods
                    if (File.Exists(OFFLINE_FILE))
                    {
                        OfflineMods.AddRange(JsonConvert.DeserializeObject <List <Mod> >(File.ReadAllText(OFFLINE_FILE)));
                        Console.WriteLine($"Loaded { OfflineMods.Count } offline available mods.");
                    }

                    try
                    {
                        HttpResponseMessage gameVersionsResponse = await hp.GetAsync(BeatModsUrlBuilder.AllGameVersionsUrl, cts.Token);
                        gameVersionsResponse.EnsureSuccessStatusCode();
                        string allGameVersionsJson = await gameVersionsResponse.Content.ReadAsStringAsync();
                        AllGameVersions = JsonConvert.DeserializeObject <List <string> >(allGameVersionsJson)
                                          .OrderByDescending((e) => SemVersionExtenions.AsNumber(e)).ToList();

                        if (cache != null && (cache.allGameVersions == null || !cache.allGameVersions.SequenceEqual(AllGameVersions)))
                        {
                            cache.allGameVersions = AllGameVersions;
                            File.WriteAllText(cacheFile, JsonConvert.SerializeObject(cache));

                            Console.WriteLine("Cache file game versions changed.");
                        }

                        IsOffline = false;
                    }
                    catch (Exception e) when(e is HttpRequestException || e is TaskCanceledException)
                    {
                        IsOffline = true;

                        if (cache != null)
                        {
                            AllGameVersions.AddRange(cache.allGameVersions);
                        }
                        AllMods.AddRange(OfflineMods);
                        return;
                    }

                    string mostRecentGameVersion = AllGameVersions.First();
                    BeatModsQuery query = BeatModsQuery.All;

                    // If mod cache is enabled, only download mod info about the most recent mods
                    if (cache != null && useCachedOldMods && cache.cacheMaxGameVersion == mostRecentGameVersion)
                    {
                        Console.WriteLine($"Loading { cache.cachedMods.Count } mods from cache '{ cacheFile }' until game version { cache.cacheMaxGameVersion }.");

                        query.forGameVersion = cache.cacheMaxGameVersion;
                        AllMods.AddRange(cache.cachedMods);
                    }

                    HttpResponseMessage modsResponse = await hp.GetAsync(query.CreateRequestUrl(), cts.Token);
                    modsResponse.EnsureSuccessStatusCode();
                    string modsJson = await modsResponse.Content.ReadAsStringAsync();

                    AllMods.AddRange(JsonConvert.DeserializeObject <List <Mod> >(modsJson));
                    AllMods = AllMods.Union(OfflineMods).ToList();

                    if (useCachedOldMods)
                    {
                        // Cache file does not exist, create it and cache all mods but the recent version
                        if (cache == null || cache.cacheMaxGameVersion != mostRecentGameVersion)
                        {
                            cache = new CacheConfig()
                            {
                                cachedMods = AllMods.Where((e) => e.GetGameVersion() < mostRecentGameVersion.FixOddVersion()).ToList(),
                                cacheMaxGameVersion = mostRecentGameVersion,
                                allGameVersions = AllGameVersions
                            };

                            File.WriteAllText(cacheFile, JsonConvert.SerializeObject(cache));

                            Console.WriteLine($"Created new cache file '{ cacheFile }' for { cache.cachedMods.Count } mods with max game version { cache.cacheMaxGameVersion }.");
                        }
                    }

                    Console.WriteLine($"Loaded in total { AllMods.Count } mods.");
                }
            }));
        }
Ejemplo n.º 4
0
 public IOrderedEnumerable <Mod> GetModsCached(BeatModsQuery query)
 {
     return(query.sort.SortModList(AllMods.Where((e) => query.MatchesIgnoreSort(e)), !query.sortDescending));
 }