Ejemplo n.º 1
0
 private void DownloadButton_Click(object sender, RoutedEventArgs e)
 {
     if (ChartList.SelectedItems.Count > 0)
     {
         var dialog = new DownloadWindow();
         foreach (var item in ChartList.SelectedItems)
         {
             var sel = item as ChartSelection;
             var i   = new LayestaInfo()
             {
                 SongName = sel.SongName,
                 Charter  = sel.Charter,
                 GUID     = sel.GUID
             };
             dialog.Selections.Add(i);
         }
         dialog.ShowDialog();
     }
 }
Ejemplo n.º 2
0
        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); }));
            }
        }