Beispiel #1
0
 private void Button_ClearCache_Click(object sender, RoutedEventArgs e)
 {
     if (TTSPlayer.fileList.Count > 0)
     {
         AsyncDialog.Open($"还有 {TTSPlayer.fileList.Count} 个语音在队列中,删除可能导致插件工作异常\n\n请在播放完毕后再试一次", icon: MessageBoxIcon.Warning);
         return;
     }
     try
     {
         long totalSize = 0;
         int  count     = 0;
         foreach (FileInfo fileInfo in (new DirectoryInfo(Vars.CacheDir)).GetFiles())
         {
             totalSize += fileInfo.Length;
             fileInfo.Delete();
             count++;
         }
         if (count == 0)
         {
             AsyncDialog.Open("无缓存文件,无需删除", icon: MessageBoxIcon.Information);
         }
         else
         {
             AsyncDialog.Open($"成功!\n\n已删除 {count} 个文件,共计释放 {((totalSize > 1048576L) ? $"{Math.Round((double)(totalSize / 1048576), 2)} MiB" : $"{Math.Round((double)(totalSize / 1024), 2)} KiB")} 的存储空间", icon: MessageBoxIcon.Information);
         }
     }
     catch (Exception ex)
     {
         AsyncDialog.Open($"出错: {ex}", icon: MessageBoxIcon.Error);
     }
 }
Beispiel #2
0
        private async void Button_TestGo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                await TTSPlayer.UnifiedPlay(TextBox_TTSTest.Text);

                TextBox_ExecutionResult.Text = "播放成功/已添加到队列";
            }
            catch (Exception ex)
            {
                AsyncDialog.Open($"错误: {ex}", "Re: TTSCat", MessageBoxIcon.Error);
                TextBox_ExecutionResult.Text = $"播放失败: {ex.Message}";
            }
        }
Beispiel #3
0
 private void Button_DeleteCache_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         foreach (FileInfo fileInfo in (new DirectoryInfo(Vars.CacheDir)).GetFiles())
         {
             fileInfo.Delete();
         }
         AsyncDialog.Open("OK");
         TextBox_ExecutionResult.Text = "已清理缓存";
     }
     catch (Exception ex)
     {
         AsyncDialog.Open("Error: " + ex.ToString(), "Re: TTSCat", MessageBoxIcon.Error);
         TextBox_ExecutionResult.Text = $"缓存清理失败: {ex.Message}";
     }
 }
Beispiel #4
0
        private void Button_PlayAudio_Click(object sender, RoutedEventArgs e)
        {
            var frame  = new DispatcherFrame();
            var thread = new Thread(() => {
                try
                {
                    var waveOut = new WaveOutEvent();
                    var reader  = new AudioFileReader(Interaction.InputBox("输入文件名", "Re: TTSCat"));
                    waveOut.Init(reader);
                    waveOut.Play();
                    frame.Continue = false;
                }
                catch (Exception ex)
                {
                    AsyncDialog.Open($"Error: {ex}", "Re: TTSCat", MessageBoxIcon.Error);
                    Dispatcher.Invoke(() => { TextBox_ExecutionResult.Text = $"播放错误: {ex.Message}"; });
                }
            });

            thread.Start();
            TextBox_ExecutionResult.Text = "已启动播放";
            Dispatcher.PushFrame(frame);
        }
Beispiel #5
0
        private async void Button_CheckConnectivity_Click(object sender, RoutedEventArgs e)
        {
            await DarkenAsync();

            TextBox_ExecutionResult.Text = "延迟测试已启动...";
            try
            {
                var thread = new Thread(() =>
                {
                    var window    = new LoadingWindowLight();
                    window.IsOpen = true;
                    var result    = new StringBuilder();
                    var ip        = new WebClient().DownloadString("https://apis.elepover.com/ip/");
                    result.Append($"设备 IP(公网): {ip}{Environment.NewLine}");
                    var listPing      = new List <List <long> >();
                    var listAddresses = new Dictionary <string, string>
                    {
                        { "Google", "https://translate.google.cn/" },
                        { "百度", "https://fanyi.baidu.com/" },
                        { "百度 (TSN)", "https://tsn.baidu.com/" },
                        { "有道", "http://tts.youdao.com/" }
                    };
                    var sw = new Stopwatch();
                    int i  = 0;
                    foreach (var item in listAddresses)
                    {
                        window.ProgressBar.Value = (double)(i * 100) / listAddresses.Count;
                        var list = new List <long>();
                        for (int j = 0; j < 11; j++)
                        {
                            var req          = WebRequest.CreateHttp(item.Value);
                            req.Method       = "HEAD";
                            req.Timeout      = 5000;
                            var frame        = new DispatcherFrame();
                            var getResWorker = new Thread(() =>
                            {
                                try
                                {
                                    using (var res = req.GetResponse()) { }
                                }
                                catch { }
                                frame.Continue = false;
                            });
                            sw.Restart();
                            getResWorker.Start();
                            Dispatcher.PushFrame(frame);
                            sw.Stop();
                            if (j != 0) // ditch first initial connection (stupid.png)
                            {
                                list.Add(sw.ElapsedMilliseconds);
                            }
                            window.ProgressBar.Value += (double)(100 / listAddresses.Count) / 10;
                        }
                        listPing.Add(list);
                        i++;
                    }
                    window.ProgressBar.IsIndeterminate = true;
                    // process data
                    _ = result.Append($"服务器 / 延迟(ms) / 平均值 / 最小 / 最大 / 标准差{Environment.NewLine}");
                    i = 0;
                    foreach (var item in listAddresses)
                    {
                        _ = result.Append($"{item.Key}");
                        for (int j = 0; j < listPing[i].Count; j++)
                        {
                            result.Append($" / {listPing[i][j]}");
                        }
                        _ = result.Append($" / avg {listPing[i].Average()}");
                        _ = result.Append($" / ↓ {listPing[i].Min()}");
                        _ = result.Append($" / ↑ {listPing[i].Max()}");
                        _ = result.Append($" / stdev {Math.Round(Math.Sqrt(listPing[i].Average(x => x * x) - Math.Pow(listPing[i].Average(), 2)), 2)}{Environment.NewLine}");
                        i++;
                    }
                    window.IsOpen = false;
                    AsyncDialog.Open(result.ToString(), "Re: TTSCat");
                    Dispatcher.Invoke(async() =>
                    {
                        TextBox_ExecutionResult.Text = "延迟测试完成";
                        await BrightenAsync();
                    });
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
            catch (Exception ex)
            {
                AsyncDialog.Open($"延迟测试错误: {ex}", "Re: TTSCat", MessageBoxIcon.Error);
                TextBox_ExecutionResult.Text = $"延迟测试错误: {ex.Message}";
            }
        }
Beispiel #6
0
        private async void Button_DLUpd_Click(object sender, RoutedEventArgs e)
        {
            if (updateDownloadURL == "undefined")
            {
                AsyncDialog.Open("先检查更新吧~", "Re: TTSCat", System.Windows.Forms.MessageBoxIcon.Warning, System.Windows.Forms.MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    StaysOpen = true;
                    ProgressBar_Indicator.Visibility      = Visibility.Visible;
                    ProgressBar_Indicator.IsIndeterminate = false;
                    Button_CheckUpd.IsEnabled             = false;
                    Button_DLUpd.IsEnabled = false;

                    TextBlock_Status.Text       = "正在准备更新...";
                    ProgressBar_Indicator.Value = 0;
                    await Task.Delay(100);

                    TextBlock_Status.Text       = "正在下载更新...";
                    ProgressBar_Indicator.Value = 10;

                    DownloadProgressChangedEventHandler progressChangedHandler = (_sender, progressData) =>
                    {
                        var progress = Math.Round((double)progressData.BytesReceived / progressData.TotalBytesToReceive, 4);
                        TextBlock_Status.Text       = $"正在下载更新... ({progress * 100}%)";
                        ProgressBar_Indicator.Value = progress * 100;
                    };

                    using (var downloader = new WebClient())
                    {
                        downloader.DownloadProgressChanged += progressChangedHandler;
                        await downloader.DownloadFileTaskAsync(updateDownloadURL, Vars.DownloadUpdateFilename);

                        downloader.DownloadProgressChanged -= progressChangedHandler;
                    }

                    TextBlock_Status.Text       = "正在备份...";
                    ProgressBar_Indicator.Value = 50;
                    var backupFilename = Path.Combine(Vars.ConfDir, $"Re_TTSCat_v{Vars.CurrentVersion}.dll");
                    if (File.Exists(backupFilename))
                    {
                        File.Delete(backupFilename);
                    }
                    File.Move(Vars.AppDllFileName, backupFilename);

                    TextBlock_Status.Text       = "正在解压...";
                    ProgressBar_Indicator.Value = 80;
                    using (var zip = ZipFile.OpenRead(Vars.DownloadUpdateFilename))
                    {
                        foreach (var entry in zip.Entries)
                        {
                            TextBlock_Status.Text = $"正在解压... ({entry.FullName})";
                            entry.ExtractToFile(Path.Combine(Vars.AppDllFilePath, entry.FullName), true);
                        }
                    }

                    TextBlock_Status.Text       = "正在清理...";
                    ProgressBar_Indicator.Value = 90;
                    if (File.Exists(Vars.DownloadUpdateFilename))
                    {
                        File.Delete(Vars.DownloadUpdateFilename);
                    }

                    Vars.UpdatePending          = true;
                    ProgressBar_Indicator.Value = 100;
                }
                catch (Exception ex)
                {
                    TextBlock_Status.Text = $"更新出错: {ex.Message}";
                }
                finally
                {
                    StaysOpen = false;
                    ProgressBar_Indicator.Visibility      = Visibility.Hidden;
                    ProgressBar_Indicator.IsIndeterminate = true;
                    Button_CheckUpd.IsEnabled             = true;
                    Button_DLUpd.IsEnabled = !Vars.UpdatePending;
                    DetectPendingUpdates();
                }
            }
        }