public void GetModels(OnlineModels onlineModels = null) { string sortDirection = SortDescending ? "Descending" : "Ascending"; MainWindow.ToggleLoading(true, $"Loading online {ModelType.ToString().ToLower()}s", $"Sorting by: {CurrentSort} - {sortDirection}"); _ = Task.Run(async() => { try { OnlineModels = await App.ModelSaberApi.GetOnlineModels(ModelType, CurrentSort, SortDescending, Filters, onlineModels); } catch (Exception e) { string description = e.Message; if (e.InnerException != null && !e.Message.Contains(e.InnerException.Message)) { description += $" ({e.InnerException.Message})"; } await MainWindow.ShowMessageAsync($"Online {ModelType}s", description); } }); }
public async Task <OnlineModels> GetOnlineModels(ModelType modelType, Sort sort, bool descending, List <Filter> filters, OnlineModels cachedOnlineModels = null) { try { using (WebClient webClient = new WebClient()) { string projectName = Assembly.GetEntryAssembly().GetName().Name; webClient.Headers.Add(HttpRequestHeader.UserAgent, projectName); int startIndex = cachedOnlineModels is null ? 0 : cachedOnlineModels.CurrentPage * 10; int endIndex = startIndex + 10; string sortDirection = descending ? "desc" : "asc"; string filtersText = string.Join(",", filters.Select(x => $"{x.Type.ToString().ToLower()}:{x.Text}")); string json = null; string apiString = $"{modelSaberApi}?type={modelType.ToString().ToLower()}&sort={sort.ToString().ToLower()}&sortDirection={sortDirection}"; if (cachedOnlineModels != null) { apiString += $"&start={startIndex}&end={endIndex}"; } if (filters != null && filters.Count > 0) { apiString += $"&filter={filtersText}"; } json = await webClient.DownloadStringTaskAsync(apiString); Dictionary <string, JToken> jsonDictionary = JsonConvert.DeserializeObject <Dictionary <string, JToken> >(json); OnlineModels onlineModels; if (cachedOnlineModels is null || jsonDictionary.Count > cachedOnlineModels.TotalModels) { cachedOnlineModels = null; onlineModels = new OnlineModels { TotalModels = jsonDictionary.Count }; } else { onlineModels = new OnlineModels(cachedOnlineModels, false); } List <JToken> jTokens = jsonDictionary.Take(10).Select(x => x.Value).ToList(); string extension = null; string filesPath = null; switch (modelType) { case ModelType.None: break; case ModelType.Saber: extension = ".saber"; filesPath = SabersPath; break; case ModelType.Avatar: extension = ".avatar"; filesPath = AvatarsPath; break; case ModelType.Platform: extension = ".plat"; filesPath = PlatformsPath; break; case ModelType.Bloq: extension = ".bloq"; filesPath = BloqsPath; break; default: break; } string[] modelsDownloaded = Directory.GetFiles(filesPath, $"*{extension}"); foreach (JToken jToken in jTokens) { OnlineModel onlineModel = JsonConvert.DeserializeObject <OnlineModel>(jToken.ToString()); string filePath = null; foreach (string downloadedModel in modelsDownloaded) { string[] modelNames = Path.GetFileNameWithoutExtension(downloadedModel).Split(" ", 2); if (modelNames.Length == 1) { if (modelNames[0] == onlineModel.Name) { filePath = downloadedModel; } } else if (int.TryParse(modelNames[0], out int id)) { if (onlineModel.Id == id) { filePath = downloadedModel; } } } if (string.IsNullOrEmpty(filePath)) { if (Downloading.Any(x => x.Id == onlineModel.Id)) { onlineModel.IsDownloading = true; } } else { onlineModel.IsDownloaded = true; onlineModel.ModelPath = filePath; } onlineModels.Models.Add(onlineModel); } return(RefreshOnlinePages(onlineModels)); }