public async Task TryLogin(bool force_login = false) { try { if (!force_login && File.Exists("./cache.auth")) { API.ApplyAuth("5d79baff-c3fd-4023-a1ab-502eb1b95725", File.ReadAllText("./cache.auth")); } else { await API.Login("5d79baff-c3fd-4023-a1ab-502eb1b95725", "8397121f-8fa4-495e-85b9-e2b023f6b285"); File.WriteAllText("./cache.auth", API.Token); } } catch (LayestaWebAPIException ex) { ErrorWindow.ShowException(ex, true); } catch (Exception ex) { ErrorWindow.ShowException(ex, true); } }
private async Task Process(LayestaInfo level) { PROCESS: try { var basefilename = setting.SaveAsGuid ? level.GUID : string.Format("[{0}] {1}", level.Charter, level.SongName); foreach (char c in Path.GetInvalidFileNameChars()) { basefilename = basefilename.Replace(c, '_'); } var layestafile = Path.Combine(setting.SavePath, basefilename + ".layesta"); if (!File.Exists(layestafile) || setting.AllowOverwrite) { await MainWindow.API.DownloadChart(level.GUID, layestafile); } if (setting.SaveAsLap) { string lappath = Path.Combine(setting.SavePath, basefilename); if (Directory.Exists(lappath)) { if (setting.AllowOverwrite) { Directory.Delete(lappath, true); } else { Current += 1; return; } } Directory.CreateDirectory(lappath); bool ExtractResult = false; await Task.Run(() => { try { ZipFile.ExtractToDirectory(layestafile, lappath); ExtractResult = true; } catch (InvalidDataException) { File.Delete(layestafile); } catch (Exception ex) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { ErrorWindow.ShowException(ex, true); })); Current += 1; } }); if (!ExtractResult) { goto PROCESS; } File.Delete(layestafile); File.Delete(Path.Combine(lappath, "background_blur.png")); File.Delete(Path.Combine(lappath, "info.bytes")); var lap = new LapFile(); var charts = new List <string>(); foreach (var file in Directory.GetFiles(lappath)) { var filename = System.IO.Path.GetFileName(file); if (lap.BGA0Path == null && filename.Equals("background_gray.jpg")) { lap.BGA0Path = file; } if (lap.BGA1Path == null && filename.Equals("background_linear.jpg")) { lap.BGA1Path = file; } if (Path.GetExtension(file).Equals(".txt")) { var b64name = filename.Replace("chart_", "").Replace(".txt", ""); string decode_b64name = Encoding.UTF8.GetString(Convert.FromBase64String(b64name)) + ".txt"; File.Move(file, Path.Combine(lappath, decode_b64name)); charts.Add(decode_b64name); } } await Task.Run(() => { var mp3file = Path.Combine(lappath, "music.mp3"); AudioConverter.MP3ToOGG(mp3file, Path.Combine(lappath, Path.Combine(lappath, "music.ogg"))); File.Delete(mp3file); }); var basebgpath = Path.Combine(lappath, "background.jpg"); if (lap.BGA0Path == null) { lap.BGA0Path = basebgpath; } else if (lap.BGA1Path == null) { lap.BGA1Path = basebgpath; } else { lap.BGA2Path = basebgpath; } lap.Name = level.SongName; lap.Designer = level.Charter; lap.ProjectFolder = lappath; lap.MusicPath = Path.Combine(lappath, "music.ogg"); lap.ChartPath = charts.First(); File.WriteAllText(Path.Combine(lappath, basefilename + ".lap"), lap.Serialization()); if (setting.SaveAsZip) { var zippath = Path.Combine(setting.SavePath, basefilename + ".zip"); if (File.Exists(zippath)) { File.Delete(zippath); } ZipFile.CreateFromDirectory(lappath, Path.Combine(setting.SavePath, basefilename + ".zip")); Directory.Delete(lappath, true); } } Current += 1; } catch (LayestaWebAPINeedLoginException ex) { var mw = Owner as MainWindow; mw.TryLogin(); goto PROCESS; //goto fuckyeah!!!! } catch (InvalidDataException ex) { Console.WriteLine(ex.Source); File.Delete(ex.Source); goto PROCESS; } catch (Exception ex) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { ErrorWindow.ShowException(ex, true); })); Current += 1; } finally { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { Progress.Value = (double)(Current / Total * 100.0); })); } }
private async Task RefreshContent() { try { var respond = await API.GetLevelList(); var respond_contest = await API.GetContestLevelList(); var selections = new List <ChartSelection>(); foreach (var level in respond.Levels) { var item = new ChartSelection() { Charter = level.Designer, DownloadCount = level.DownloadCount, Artist = level.SongArtist, SongName = level.Title, GUID = level.Guid, Index = selections.Count + 1, Rate = level.Rating }; if (respond_contest.Levels.Exists((x) => x.Guid.Equals(item.GUID))) { item.ParticipantCurrentContest = true; } selections.Add(item); } await Task.Run(() => { Parallel.ForEach(respond.Levels, async level => { var image = await DownloadImage(level.Guid); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { var item = selections.Find(x => x.GUID.Equals(level.Guid)); item.CoverURL = image; })); }); }); if (ChartList.ItemsSource != null) { (ChartList.ItemsSource as List <ChartSelection>).Clear(); ChartList.Items.SortDescriptions.Clear(); } ChartList.ItemsSource = selections; ICollectionView view = CollectionViewSource.GetDefaultView(ChartList.ItemsSource); view.Filter = (o) => { bool flag = false; ChartSelection item = o as ChartSelection; if (CheckBox_ContestChart.IsChecked.Value) { flag = item.ParticipantCurrentContest; } else { flag = true; } if (flag) { if (SearchBar.Text.Length > 0) { string query = SearchBar.Text.ToLower(); return(item.SongName.ToLower().Contains(query) || item.Artist.ToLower().Contains(query) || item.Charter.ToLower().Contains(query)); } return(true); } return(false); }; } catch (LayestaWebAPINeedLoginException ex) { await TryLogin(true); await RefreshContent(); } catch (Exception ex) { ErrorWindow.ShowException(ex); } }