Example #1
0
        public void StartRemoteCLI()
        {
            if (_client.Connected)
            {
                using (DataStringPacket _packet = new DataStringPacket(_stream))
                {
                    _packet.data = Convert.ToString(Console.BufferWidth);
                    _packet.SendPacket(_aes);
                    _packet.ReceivePacket(_aes);
                    CLIOutput.WriteToConsole(_packet.data + "\n");
                }

                using (CLIPacket _cliP = new CLIPacket(_stream))
                {
                    _cliP.serverAnswer = "";

                    while (true)
                    {
                        // write cursor
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write(cursor);
                        Console.ForegroundColor = ConsoleColor.White;

                        // read input
                        _cliInput = CLIInput.ReadInput(cursor.Length);

                        if (_cliInput != "")
                        {
                            _cliP.consoleWidth = Console.BufferWidth;
                            _cliP.cliInput     = _cliInput;
                            _cliP.SendPacket(_aes);

                            _cliP.ReceivePacket(_aes);

                            if (_cliP.serverAnswer == "EXIT_CLI")
                            {
                                break;
                            }

                            CLIOutput.WriteToConsole(_cliP.serverAnswer + "\n");
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Not connected!");
            }
        }
 private void OnConsoleDataRecv(object sender, DataReceivedEventArgs e)
 {
     if (!String.IsNullOrEmpty(e.Data))
     {
         console_buffer.Append(e.Data);
         console_buffer.Append(Environment.NewLine);
         // if (queueFlag) return;
         // queueFlag = true;
         Dispatcher.BeginInvoke(new Action(delegate
         {
             // queueFlag = false;
             CLIOutput.Focus();
             this.CLIOutput.AppendText(e.Data);
             this.CLIOutput.AppendText(Environment.NewLine);
             CLIOutput.Select(CLIOutput.Text.Length, 0);
         }), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);
     }
 }
Example #3
0
        static Info()
        {
            System.IO.Directory.CreateDirectory(Directory);

            try
            {
                if (Autostart && GetAutostartPath(Name) != ExecutablePath)
                {
                    SetAutostartPath(Name, ExecutablePath);
                }
            }catch (Exception e)
            {
                CLIOutput.WriteLine("Failed to set autostart path: probably program doesn't have permissions to do that!", ConsoleColor.DarkYellow);
#if DEBUG
                CLIOutput.WriteLine(e.StackTrace);
#endif
            }
        }
Example #4
0
 private static void SetAutostartPath(string name, string path)
 {
     try
     {
         using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
         {
             if (path != null)
             {
                 rk.SetValue(name, path, RegistryValueKind.String);
             }
             else
             {
                 rk.DeleteValue(name);
             }
         }
     }catch (Exception)
     {
         CLIOutput.WriteLine("Failed to set autostart path: probably program doesn't have permissions to do that!", ConsoleColor.DarkYellow);
     }
 }
Example #5
0
        public void Start()
        {
            CLIOutput.WriteToConsole(GetBanner(Console.BufferWidth) + "\n");

            while (true)
            {
                Command _command = null;

                Console.ForegroundColor = _cursorColor;
                Console.Write(_cursor);
                Console.ForegroundColor = _inputColor;
                string _cliInput = CLIInput.ReadInput(Console.CursorLeft);
                string _response;

                // This is the old method to read input from cli.
                //string _cliInput = Console.ReadLine();

                _cliInput = _cliInput.Trim();
                if (_cliInput != "")
                {
                    _response = CLIInterpreter(ref _command, _cliInput);
                    if (_response == "VALID_PARAMETERS")
                    {
                        try
                        {
                            _response = _command.Execute(Console.BufferWidth);
                            if (_response == "EXIT_CLI")
                            {
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            _response = "<color><red>" + e.Message;
                        }
                    }

                    CLIOutput.WriteToConsole(_response + "\n");
                }
            }
        }
Example #6
0
 public void Log(string str, params object[] parameters)
 {
     CLIOutput.WriteLine("| " + str, ConsoleColor.Gray, parameters);
 }
        private void OnRun(object sender, RoutedEventArgs e)
        {
            if (!File.Exists("waifu2x-ncnn-vulkan.exe"))
            {
                MessageBox.Show(@"waifu2x-ncnn-vulkan.exe is missing!");
                return;
            }

            if (param_src == null)
            {
                MessageBox.Show(@"source images is not found!");
                return;
            }

            // Sets Source
            // The source must be a file or folder that exists
            if (System.Text.RegularExpressions.Regex.IsMatch(
                    txtGPU_ID.Text,
                    @"^(\d+)$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                param_gpu_id.Clear();
                param_gpu_id.Append("-g ");
                param_gpu_id.Append(txtGPU_ID.Text);
            }
            else
            {
                param_gpu_id.Clear();
            }

            if (System.Text.RegularExpressions.Regex.IsMatch(
                    txtBlocksize.Text,
                    @"^(\d+)$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                if (int.Parse(txtBlocksize.Text) % 4 != 0)
                {
                    MessageBox.Show(@"Block size must be a multiple of 4");
                    return;
                }
                else
                {
                    param_block.Clear();
                    param_block.Append("-t ");
                    param_block.Append(txtBlocksize.Text);
                }
            }
            else
            {
                param_block.Clear();
            }

            if (((int)slider_zoom.Value & ((int)slider_zoom.Value - 1)) != 0)
            {
                MessageBox.Show(@"Magnification must be a power of two.");
                return;
            }

            if (System.Text.RegularExpressions.Regex.IsMatch(
                    txtThread.Text,
                    @"^(\d+:\d+:\d+)$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                param_thread.Clear();
                param_thread.Append("-j ");
                param_thread.Append(txtThread.Text);
            }
            else
            {
                param_thread.Clear();
            }

            binary_path.Clear();
            if (checkPrecision_fp32.IsChecked == true)
            {
                binary_path.Append(".\\fp32\\waifu2x-ncnn-vulkan.exe ");
            }
            else
            {
                binary_path.Append(".\\waifu2x-ncnn-vulkan.exe ");
            }

            // logをクリアする
            this.CLIOutput.Clear();
            if (this.txtDstPath.Text.Trim() != "")
            {
                if (Directory.Exists(this.txtDstPath.Text) == false)
                {
                    try
                    {
                        Directory.CreateDirectory(this.txtDstPath.Text);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show(@"Failed to create output destination folder.");
                        return;
                    }
                }
            }
            Commandline.Clear();
            int FileCount = 0;

            for (int i = 0; i < param_src.Length; i++)
            {
                FileCount++;
            }
            Commandline.Append("@echo off\r\n");
            Commandline.Append("chcp 65001 >nul\r\n");
            Commandline.Append("if \"%~1\"==\"sub_rename\" goto sub_rename\r\n");
            Commandline.Append("set \"FileCount=" + FileCount + "\"\r\n");
            Commandline.Append("set \"ProcessedCount=0\"\r\n");
            Commandline.Append("set \"mode=" + param_mode.ToString() + "\"\r\n");
            Commandline.Append("set \"Scale_ratio=" + this.slider_zoom.Value.ToString() + "\"\r\n");
            Commandline.Append("if \"%mode%\"==\"noise\" set Scale_ratio=1\r\n");
            Commandline.Append("set \"Output_no_overwirit=" + checkOutput_no_overwirit.IsChecked.ToString() + "\"\r\n");
            Commandline.Append("set \"Prevent_double_extensions=" + checkPrevent_double_extensions.IsChecked.ToString() + "\"\r\n");
            Commandline.Append("if not \"%FileCount%\"==\"1\" echo progress %ProcessedCount%/%FileCount%\r\n");
            for (int i = 0; i < param_src.Length; i++)
            {
                param_dst.Clear();
                if (this.txtDstPath.Text.Trim() != "")
                {
                    param_dst.Append("\"");
                    param_dst.Append(this.txtDstPath.Text);
                    param_dst.Append("\\");
                    param_dst.Append(System.IO.Path.GetFileNameWithoutExtension(param_src[i].Replace("%", "%%%%")));
                    if (File.Exists(param_src[i]))
                    {
                        param_dst.Append(".png\"");
                    }
                    else if (Directory.Exists(param_src[i]))
                    {
                        param_dst.Append("\"");
                    }
                }
                else
                {
                    param_dst.Append("\"");
                    System.IO.DirectoryInfo hDirInfo = System.IO.Directory.GetParent(param_src[i]);
                    param_dst.Append(hDirInfo.FullName);
                    param_dst.Append("\\");
                    param_dst.Append(System.IO.Path.GetFileNameWithoutExtension(param_src[i].Replace("%", "%%%%")));
                    if (param_model.ToString().Replace("-m ", "") == "models-cunet")
                    {
                        param_dst.Append("(CUnet)");
                    }
                    if (param_model.ToString().Replace("-m ", "") == "models-upconv_7_anime_style_art_rgb")
                    {
                        param_dst.Append("(UpRGB)");
                    }
                    if (param_model.ToString().Replace("-m ", "") == "models-upconv_7_photo")
                    {
                        param_dst.Append("(UpPhoto)");
                    }
                    param_dst.Append("(");
                    param_dst.Append(param_mode.ToString().Replace("-m ", ""));
                    param_dst.Append(")");
                    if (param_mode.ToString() == "noise" || param_mode.ToString() == "noise_scale" || param_mode.ToString() == "auto_scale")
                    {
                        param_dst.Append("(");
                        param_dst.Append("Level");
                        param_dst.Append(param_denoise.ToString());
                        param_dst.Append(")");
                    }

                    if (param_mode.ToString() == "scale" || param_mode.ToString() == "noise_scale" || param_mode.ToString() == "auto_scale")
                    {
                        param_dst.Append("(x");
                        param_dst.Append(this.slider_zoom.Value.ToString());
                        param_dst.Append(")");
                    }
                    if (File.Exists(param_src[i]))
                    {
                        param_dst.Append(".png\"");
                    }
                    else if (Directory.Exists(param_src[i]))
                    {
                        param_dst.Append("\"");
                    }
                }
                param_mag.Clear();
                param_mag.Append("-s ");
                param_mag.Append(this.slider_value.Text);

                param_denoise2.Clear();
                param_denoise2.Append(param_denoise.ToString());

                // Set mode
                if (param_mode.ToString() == "noise")
                {
                    param_mag.Clear();
                    param_mag.Append("-s ");
                    param_mag.Append("1");
                }
                if (param_mode.ToString() == "scale")
                {
                    param_denoise2.Clear();
                    param_denoise2.Append("-1");
                }

                Commandline.Append("call :waifu2x_run " + "\"" + param_src[i].Replace("%", "%%%%") + "\" " + param_dst + "\r\n");
            }
            string full_param = String.Join(" ",
                                            "-v ",
                                            "-i \"%~1\"",
                                            "-o \"%~2\"",
                                            param_mag.ToString(),
                                            "-n %noise_level%",
                                            param_block.ToString(),
                                            param_model.ToString(),
                                            param_gpu_id.ToString(),
                                            param_thread.ToString());

            string multiple_full_param = String.Join(" ",
                                                     "-v ",
                                                     "-i %Temporary_input%",
                                                     "-o %Temporary_output%",
                                                     "-s 2",
                                                     "-n %Temporary_noise_level%",
                                                     param_block.ToString(),
                                                     param_model.ToString(),
                                                     param_gpu_id.ToString(),
                                                     param_thread.ToString());

            Guid g = System.Guid.NewGuid();

            random32.Clear();
            random32.Append(g.ToString("N").Substring(0, 32));

            Commandline.Append("exit /b\r\n");
            Commandline.Append("\r\n");
            Commandline.Append(":waifu2x_run\r\n");
            Commandline.Append("for %%i in (\"%~2\") do set \"Source_name=%%~ni\"\r\n");
            Commandline.Append("if \"%Output_no_overwirit%\"==\"True\" if exist \"%~2\" goto waifu2x_run_skip\r\n");
            Commandline.Append("set \"noise_level=" + param_denoise2 + "\"\r\n");
            Commandline.Append("if \"" + param_mode.ToString() + "\"==\"auto_scale\" if not \"%input_image_jpg%\"==\"1\" set \"noise_level=-1\"\r\n");
            Commandline.Append("for %%i in (\"%~1\") do set \"Attribute=%%~ai\"\r\n");
            Commandline.Append("if %Scale_ratio% LEQ 2 if \"%Attribute:~0,1%\"==\"d\" if not exist \"%~2\" (\r\n");
            Commandline.Append("   echo mkdir \"%~2\"\r\n");
            Commandline.Append("   mkdir \"%~2\"\r\n");
            Commandline.Append(")\r\n");
            Commandline.Append("set Temporary_input=\"%~1\"\r\n");
            Commandline.Append("if %Scale_ratio% GTR 2 (\r\n");
            Commandline.Append("    for /L %%i in (2,2,%Scale_ratio%) do (\r\n");
            Commandline.Append("        call :sub_multiple_magnify %%i\r\n");
            Commandline.Append("    )\r\n");
            Commandline.Append(") else (\r\n");
            Commandline.Append("    echo " + binary_path + full_param + "\r\n");
            Commandline.Append("    " + binary_path + full_param + "\r\n");
            Commandline.Append(")\r\n");
            Commandline.Append("if %Scale_ratio% LEQ 2 if \"%Attribute:~0,1%\"==\"d\" if \"%Prevent_double_extensions%\"==\"True\" (\r\n");
            Commandline.Append("    pushd \"%~2\"\r\n");
            Commandline.Append("    PowerShell \"Get-ChildItem *.png | Rename-Item -NewName { $_.Name -replace '(.png.png|.jpg.png|.jpeg.png|.bmp.png|.gif.png|.tif.png|.tiff.png|.webp.png)$','.png' }\"\r\n");
            Commandline.Append("    popd\r\n");
            Commandline.Append(")\r\n");
            Commandline.Append("if %Scale_ratio% GTR 2 (\r\n");
            Commandline.Append("    move /y %Temporary_output% \"%~2\" >nul 2>&1\r\n");
            Commandline.Append("    rd /s /q \"%TEMP%\\" + random32.ToString() + "\\\"\r\n");
            Commandline.Append(")\r\n");
            Commandline.Append(":waifu2x_run_skip\r\n");
            Commandline.Append("set /a ProcessedCount=%ProcessedCount%+1\r\n");
            Commandline.Append("if not \"%FileCount%\"==\"1\" echo progress %ProcessedCount%/%FileCount%\r\n");
            Commandline.Append("exit /b\r\n");
            Commandline.Append("\r\n");
            Commandline.Append(":sub_multiple_magnify\r\n");
            Commandline.Append("for %%i in (2,4,8,16,32,64,128,256) do if \"%~1\"==\"%%i\" goto sub_multiple_magnify2\r\n");
            Commandline.Append("exit /b\r\n");
            Commandline.Append(":sub_multiple_magnify2\r\n");
            Commandline.Append("set Temporary_output=\"%TEMP%\\" + random32.ToString() + "\\%Source_name%_x%~1.png\"\r\n");
            Commandline.Append("if \"%Attribute:~0,1%\"==\"d\" set Temporary_output=\"%TEMP%\\" + random32.ToString() + "\\%Source_name%_x%~1\"\r\n");
            Commandline.Append("if \"%Attribute:~0,1%\"==\"d\" mkdir %Temporary_output%\r\n");
            Commandline.Append("if not \"%Attribute:~0,1%\"==\"d\" if not exist \"%TEMP%\\" + random32.ToString() + "\\\" mkdir \"%TEMP%\\" + random32.ToString() + "\\\"\r\n");
            Commandline.Append("set Temporary_noise_level=-1\r\n");
            Commandline.Append("if \"%~1\"==\"2\" if not \"%noise_level%\"==\"-1\" set \"Temporary_noise_level=%noise_level%\"\r\n");
            Commandline.Append("echo " + binary_path + multiple_full_param + "\r\n");
            Commandline.Append(binary_path + multiple_full_param + "\r\n");
            Commandline.Append("if \"%Attribute:~0,1%\"==\"d\" if \"%Prevent_double_extensions%\"==\"True\" (\r\n");
            Commandline.Append("    pushd %Temporary_output%\r\n");
            Commandline.Append("    PowerShell \"Get-ChildItem *.png | Rename-Item -NewName { $_.Name -replace '(.png.png|.jpg.png|.jpeg.png|.bmp.png|.gif.png|.tif.png|.tiff.png|.webp.png)$','.png' }\"\r\n");
            Commandline.Append("    popd\r\n");
            Commandline.Append(")\r\n");
            Commandline.Append("set Temporary_input=%Temporary_output%\r\n");
            Commandline.Append("exit /b\r\n");
            waifu2x_bat.Clear();
            waifu2x_bat.Append(System.IO.Path.GetTempPath() + "waifu2x_" + random32.ToString() + ".bat");

            System.IO.StreamWriter sw = new System.IO.StreamWriter(
                @waifu2x_bat.ToString(),
                false
                // ,
                // System.Text.Encoding.GetEncoding("utf-8")
                );
            sw.Write(Commandline);
            sw.Close();

            this.btnRun.IsEnabled   = false;
            this.btnAbort.IsEnabled = true;

            // Setup ProcessStartInfo
            psinfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
            psinfo.StandardErrorEncoding  = Encoding.UTF8;
            psinfo.StandardOutputEncoding = Encoding.UTF8;
            psinfo.Arguments              = "/c \"" + waifu2x_bat.ToString() + "\"";
            psinfo.RedirectStandardInput  = true;
            psinfo.RedirectStandardError  = true;
            psinfo.RedirectStandardOutput = true;
            psinfo.UseShellExecute        = false;
            psinfo.WorkingDirectory       = System.AppDomain.CurrentDomain.BaseDirectory;
            psinfo.CreateNoWindow         = true;
            psinfo.WindowStyle            = ProcessWindowStyle.Hidden;
            pHandle.StartInfo             = psinfo;
            pHandle.EnableRaisingEvents   = true;
            if (EventHandler_Flag == false)
            {
                pHandle.OutputDataReceived += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.ErrorDataReceived  += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.Exited             += new EventHandler(OnProcessExit);
                EventHandler_Flag           = true;
            }

            // Starts working
            console_buffer.Clear();

            try
            {
                //MessageBox.Show(full_param);
                bool pState = pHandle.Start();
            }
            catch (Exception)
            {
                try
                {
                    pHandle.Kill();
                }
                catch (Exception) { /*Nothing*/ }
                Errormessage("Failed to start waifu2x.exe.");
                //throw;
            }

            Dispatcher.BeginInvoke(new Action(delegate
            {
                CLIOutput.Focus();
                // this.CLIOutput.AppendText("waifu2x.exe " + full_param.ToString());
                // this.CLIOutput.AppendText(Environment.NewLine);
                // this.CLIOutput.AppendText(Environment.NewLine);
                CLIOutput.Select(CLIOutput.Text.Length, 0);
            }), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);

            try
            {
                pHandle.BeginOutputReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginOutputReadLine crashed...\n";
            }

            try
            {
                pHandle.BeginErrorReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginErrorReadLine crashed...\n";
            }

            //pHandle.BeginErrorReadLine();
            //MessageBox.Show("Some parameters do not mix well and crashed...");

            //pHandle.WaitForExit();

            /*
             * pHandle.CancelOutputRead();
             * pHandle.Close();
             * this.btnAbort.IsEnabled = false;
             * this.btnRun.IsEnabled = true;
             * this.CLIOutput.Text = console_buffer.ToString();
             */
        }
Example #8
0
        private void OnRun(object sender, RoutedEventArgs e)
        {
            if (!File.Exists("waifu2x.exe"))
            {
                MessageBox.Show(@"waifu2x.exe is missing!");
                return;
            }
            // Sets Source
            // The source must be a file or folder that exists
            if (File.Exists(this.txtSrcPath.Text) || Directory.Exists(this.txtSrcPath.Text))
            {
                if (this.txtSrcPath.Text.Trim() == "") //When source path is empty, replace with current folder
                {
                    param_src.Clear();
                    // param_src.Append("-i ");
                    param_src.Append("\"");
                    param_src.Append(App.directory);
                    param_src.Append("\"");
                }
                else
                {
                    param_src.Clear();
                    // param_src.Append("-i ");
                    param_src.Append("\"");
                    param_src.Append(this.txtSrcPath.Text);
                    param_src.Append("\"");
                }
            }
            else
            {
                Errormessage(@"The source folder or file does not exists!");
                return;
            }

            param_mag.Clear();
            // param_mag.Append("-s ");
            param_mag.Append(this.slider_value.Text);

            // 数字が入力されてなかったらクリアする

            // logをクリアする
            this.CLIOutput.Clear();

            // Set Destination
            if (this.txtDstPath.Text.Trim() == "")
            {
                param_dst.Clear();
                param_dst.Append("\"");
                System.IO.DirectoryInfo hDirInfo = System.IO.Directory.GetParent(this.txtSrcPath.Text);
                param_dst.Append(hDirInfo.FullName);
                param_dst.Append("\\");
                param_dst.Append(System.IO.Path.GetFileNameWithoutExtension(this.txtSrcPath.Text));
                param_dst.Append("(CUnet)");
                param_dst.Append("(");
                param_dst.Append(param_mode.ToString().Replace("-m ", ""));
                param_dst.Append(")");
                if (param_mode.ToString() == "-m noise" || param_mode.ToString() == "-m noise_scale")
                {
                    param_dst.Append("(");
                    param_dst.Append("Level");
                    param_dst.Append(param_denoise.ToString());
                    param_dst.Append(")");
                }
                if (param_mode.ToString() == "-m scale" || param_mode.ToString() == "-m noise_scale")
                {
                    param_dst.Append("(x");
                    param_dst.Append(this.slider_zoom.Value.ToString());
                    param_dst.Append(")");
                }
                param_dst.Append(".png\"");
            }
            else
            {
                param_dst.Clear();
                param_dst.Append("\"");
                param_dst.Append(this.txtDstPath.Text);
                param_dst.Append("\"");
            }

            // Set input format
            // param_informat.Clear();
            //param_informat.Append("-l ");
            // param_informat.Append(this.txtExt.Text);
            //param_informat.Append(@":");
            //param_informat.Append(this.txtExt.Text.ToUpper());

            // Set output format
            //param_outformat.Clear();
            //param_outformat.Append("-e ");
            //param_outformat.Append(this.txtOExt.Text);

            // Set scale ratio

            param_denoise2.Clear();
            param_denoise2.Append(param_denoise.ToString());

            // Set mode
            if (param_mode.ToString() == "-m noise")
            {
                param_mag.Clear();
                param_mag.Append("1");
            }
            if (param_mode.ToString() == "-m scale")
            {
                param_denoise2.Clear();
                param_denoise2.Append("-1");
            }

            this.btnRun.IsEnabled   = false;
            this.btnAbort.IsEnabled = true;

            // Assemble parameters
            string full_param = String.Join(" ",
                                            param_src.ToString(),
                                            param_dst.ToString(),
                                            // param_informat.ToString(),
                                            // param_mode.ToString(),
                                            param_denoise2.ToString(),
                                            param_mag.ToString()
                                            // param_block.ToString(),
                                            );

            // Setup ProcessStartInfo
            psinfo.FileName               = "waifu2x.exe";
            psinfo.Arguments              = full_param;
            psinfo.RedirectStandardError  = true;
            psinfo.RedirectStandardOutput = true;
            psinfo.UseShellExecute        = false;
            psinfo.WorkingDirectory       = System.AppDomain.CurrentDomain.BaseDirectory;
            psinfo.CreateNoWindow         = true;
            psinfo.WindowStyle            = ProcessWindowStyle.Hidden;
            pHandle.StartInfo             = psinfo;
            pHandle.EnableRaisingEvents   = true;
            if (EventHandler_Flag == false)
            {
                pHandle.OutputDataReceived += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.ErrorDataReceived  += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.Exited             += new EventHandler(OnProcessExit);
                EventHandler_Flag           = true;
            }

            // Starts working
            console_buffer.Clear();

            try
            {
                //MessageBox.Show(full_param);
                bool pState = pHandle.Start();
            }
            catch (Exception)
            {
                try
                {
                    pHandle.Kill();
                }
                catch (Exception) { /*Nothing*/ }
                Errormessage("Failed to start waifu2x.exe.");
                //throw;
            }

            Dispatcher.BeginInvoke(new Action(delegate
            {
                CLIOutput.Focus();
                this.CLIOutput.AppendText("waifu2x.exe " + full_param.ToString());
                this.CLIOutput.AppendText(Environment.NewLine);
                this.CLIOutput.AppendText(Environment.NewLine);
                CLIOutput.Select(CLIOutput.Text.Length, 0);
            }), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);

            try
            {
                pHandle.BeginOutputReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginOutputReadLine crashed...\n";
            }

            try
            {
                pHandle.BeginErrorReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginErrorReadLine crashed...\n";
            }

            //pHandle.BeginErrorReadLine();
            //MessageBox.Show("Some parameters do not mix well and crashed...");

            //pHandle.WaitForExit();

            /*
             * pHandle.CancelOutputRead();
             * pHandle.Close();
             * this.btnAbort.IsEnabled = false;
             * this.btnRun.IsEnabled = true;
             * this.CLIOutput.Text = console_buffer.ToString();
             */
        }
Example #9
0
        private void OnRun(object sender, RoutedEventArgs e)
        {
            // Sets Source
            // The source must be a file or folder that exists
            if (File.Exists(this.txtSrcPath.Text) || Directory.Exists(this.txtSrcPath.Text))
            {
                if (this.txtSrcPath.Text.Trim() == "") //When source path is empty, replace with current folder
                {
                    param_src.Clear();
                    param_src.Append("-i ");
                    param_src.Append("\"");
                    param_src.Append(App.directory);
                    param_src.Append("\"");
                }
                else
                {
                    param_src.Clear();
                    param_src.Append("-i ");
                    param_src.Append("\"");
                    param_src.Append(this.txtSrcPath.Text);
                    param_src.Append("\"");
                    param_WorkingDirectory.Clear();
                    if (Directory.Exists(this.txtSrcPath.Text))
                    {
                        param_WorkingDirectory.Append(this.txtSrcPath.Text);
                    }
                    if (File.Exists(this.txtSrcPath.Text))
                    {
                        System.IO.DirectoryInfo hDirInfo = System.IO.Directory.GetParent(this.txtSrcPath.Text);
                        param_WorkingDirectory.Append(hDirInfo);
                    }
                }
            }
            else
            {
                Errormessage(@"The source folder or file does not exists!");
                return;
            }


            // waifu2x-chainerのパスを設定する
            param_waifu2x_chainer_path.Clear();
            if (File.Exists(this.txtWaifu2x_chainerPath.Text + "\\waifu2x.py"))
            {
                param_waifu2x_chainer_path.Append("\"" + this.txtWaifu2x_chainerPath.Text + "\\waifu2x.py\"");
            }
            else
            {
                Errormessage("waifu2x-chainer folder is missing!");
                return;
            }

            // モデルフォルダを決定する
            param_model.Clear();
            if (param_arch.ToString().Trim() == "-a 0")
            {
                param_model.Append("-d \"" + this.txtWaifu2x_chainerPath.Text + "\\models\\vgg7\"");
            }
            if (param_arch.ToString().Trim() == "-a 1")
            {
                param_model.Append("-d \"" + this.txtWaifu2x_chainerPath.Text + "\\models\\upconv7\"");
            }
            if (param_arch.ToString().Trim() == "-a 2")
            {
                param_model.Append("-d \"" + this.txtWaifu2x_chainerPath.Text + "\\models\\resnet10\"");
            }
            if (param_arch.ToString().Trim() == "-a 3")
            {
                param_model.Append("-d \"" + this.txtWaifu2x_chainerPath.Text + "\\models\\upresnet10\"");
            }

            param_color.Clear();
            param_color.Append("-c " + ComboColor_Mode.SelectedValue.ToString().ToLowerInvariant());

            param_mag.Clear();
            param_mag.Append("-s ");
            param_mag.Append(this.slider_value.Text);

            // 数字が入力されてなかったらクリアする

            param_width.Clear();
            if (!System.Text.RegularExpressions.Regex.IsMatch(
                    output_width.Text,
                    @"^\d+$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                output_width.Clear();
            }
            else
            {
                param_mag.Clear();
                param_width.Append("-W " + this.output_width.Text);
            }

            param_height.Clear();
            if (!System.Text.RegularExpressions.Regex.IsMatch(
                    output_height.Text,
                    @"^\d+$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                output_height.Clear();
            }
            else
            {
                param_mag.Clear();
                param_height.Append("-H " + this.output_height.Text);
            }

            if (System.Text.RegularExpressions.Regex.IsMatch(
                    txtDevice.Text,
                    @"^(\d+)$",
                    System.Text.RegularExpressions.RegexOptions.ECMAScript))
            {
                param_device.Clear();
                param_device.Append("-g ");
                param_device.Append(txtDevice.Text);
            }
            else
            {
                param_device.Clear();
            }

            param_tta.Clear();
            if (cbTTA.IsChecked == true)
            {
                param_tta.Append("-t -T ");
                param_tta.Append(ComboTTA_level.SelectedValue.ToString());
            }

            // logをクリアする
            this.CLIOutput.Clear();

            // Set Destination
            if (this.txtDstPath.Text.Trim() == "")
            {
                param_dst.Clear();
            }
            else
            {
                param_dst.Clear();
                param_dst.Append("-o ");
                param_dst.Append("\"");
                param_dst.Append(this.txtDstPath.Text);
                param_dst.Append("\"");
            }

            // Set input format
            // param_informat.Clear();
            //param_informat.Append("-l ");
            // param_informat.Append(this.txtExt.Text);
            //param_informat.Append(@":");
            //param_informat.Append(this.txtExt.Text.ToUpper());

            // Set output format
            //param_outformat.Clear();
            //param_outformat.Append("-e ");
            //param_outformat.Append(this.txtOExt.Text);

            // Set scale ratio

            param_denoise2.Clear();
            param_denoise2.Append(param_denoise.ToString());

            // Set mode
            if (param_mode.ToString() == "-m noise")
            {
                param_width.Clear();
                param_height.Clear();
                param_mag.Clear();
            }
            if (param_mode.ToString() == "-m scale")
            {
                param_denoise2.Clear();
            }

            this.btnRun.IsEnabled   = false;
            this.btnAbort.IsEnabled = true;

            // Assemble parameters
            string full_param = String.Join(" ", param_waifu2x_chainer_path.ToString(),
                                            param_src.ToString(),
                                            param_dst.ToString(),
                                            // param_informat.ToString(),
                                            // param_outformat.ToString(),
                                            param_mode.ToString(),
                                            param_mag.ToString(),
                                            param_width.ToString(),
                                            param_height.ToString(),
                                            param_denoise2.ToString(),
                                            param_arch.ToString(),
                                            param_model.ToString(),
                                            param_color.ToString(),
                                            param_block.ToString(),
                                            param_batch.ToString(),
                                            param_device.ToString(),
                                            param_tta.ToString());

            // Setup ProcessStartInfo
            psinfo.FileName               = "python";
            psinfo.Arguments              = full_param;
            psinfo.RedirectStandardError  = true;
            psinfo.RedirectStandardOutput = true;
            psinfo.UseShellExecute        = false;
            psinfo.WorkingDirectory       = param_WorkingDirectory.ToString();
            psinfo.CreateNoWindow         = true;
            psinfo.WindowStyle            = ProcessWindowStyle.Hidden;
            pHandle.StartInfo             = psinfo;
            pHandle.EnableRaisingEvents   = true;
            if (EventHandler_Flag == false)
            {
                pHandle.OutputDataReceived += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.ErrorDataReceived  += new DataReceivedEventHandler(OnConsoleDataRecv);
                pHandle.Exited             += new EventHandler(OnProcessExit);
                EventHandler_Flag           = true;
            }

            // Starts working
            console_buffer.Clear();

            try
            {
                //MessageBox.Show(full_param);
                bool pState = pHandle.Start();
            }
            catch (Exception)
            {
                try
                {
                    pHandle.Kill();
                }
                catch (Exception) { /*Nothing*/ }
                Errormessage("Failed to start Python.");
                //throw;
            }

            Dispatcher.BeginInvoke(new Action(delegate
            {
                CLIOutput.Focus();
                this.CLIOutput.AppendText("WorkingDirectory " + param_WorkingDirectory.ToString());
                this.CLIOutput.AppendText(Environment.NewLine);
                this.CLIOutput.AppendText("python " + full_param.ToString());
                this.CLIOutput.AppendText(Environment.NewLine);
                this.CLIOutput.AppendText(Environment.NewLine);
                CLIOutput.Select(CLIOutput.Text.Length, 0);
            }), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);

            try
            {
                pHandle.BeginOutputReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginOutputReadLine crashed...\n";
            }

            try
            {
                pHandle.BeginErrorReadLine();
            }
            catch (Exception)
            {
                this.CLIOutput.Clear();
                this.CLIOutput.Text = "BeginErrorReadLine crashed...\n";
            }

            //pHandle.BeginErrorReadLine();
            //MessageBox.Show("Some parameters do not mix well and crashed...");

            //pHandle.WaitForExit();

            /*
             * pHandle.CancelOutputRead();
             * pHandle.Close();
             * this.btnAbort.IsEnabled = false;
             * this.btnRun.IsEnabled = true;
             * this.CLIOutput.Text = console_buffer.ToString();
             */
        }
Example #10
0
        static int Main(string[] args)
        {
            IPAddress _serverIp   = null;
            int       _serverPort = 0;
            string    _aesPass    = null;

            if (args.Length != 0)
            {
                Program.CONFIG_FILE = args[0];
            }

            try
            {
                if (File.Exists(Program.CONFIG_FILE))
                {
                    ConfigReader.ReadConfig(Program.CONFIG_FILE, out _serverIp, out _serverPort, out _aesPass);
                }
                else
                {
                    ConfigReader.CreateConfig(Program.CONFIG_FILE);
                    CLIOutput.WriteToConsole("<color><white>No config found.\n");
                    CLIOutput.WriteToConsole("<color><white>Creating default config at '" + Program.CONFIG_FILE + "'.\n");
                    CLIOutput.WriteToConsole("<color><white>Edit file as needed and start client ... \n");
                    CLIOutput.WriteToConsole("<color><gray>Press any key to close ...\n");
                    Console.ReadKey(false);
                    return(0);
                }
            }
            catch (Exception e)
            {
                CLIOutput.WriteToConsole("<color><red>(ERROR) " + e.Message + "\n" + e.StackTrace);
                return(0);
            }

            CLIOutput.WriteToConsole("<color><yellow>" + "".PadLeft(Console.BufferWidth, '_') + "\n");
            CLIOutput.WriteToConsole("<color><yellow>Address:   <color><white>" + _serverIp.ToString() + "\n");
            CLIOutput.WriteToConsole("<color><yellow>Port:      <color><white>" + _serverPort + "\n");
            CLIOutput.WriteToConsole("<color><yellow>Pass:      <color><white>" + "".PadLeft(_aesPass.Length, '*') + "\n");
            CLIOutput.WriteToConsole("<color><yellow>" + "".PadLeft(Console.BufferWidth, '_') + "\n");

            CLIClient _client = new CLIClient(new IPEndPoint(_serverIp, _serverPort), _aesPass);

            try
            {
                _client.Connect();
                _client.GetServerInfo();

                CLIOutput.WriteToConsole("<color><yellow>SERVER-HEADER:   <color><white>" + _client.serverHeader + "\n");
                CLIOutput.WriteToConsole("<color><yellow>SERVER-VERSION:  <color><white>" + _client.serverVersion + "\n");
                CLIOutput.WriteToConsole("<color><yellow>" + "".PadLeft(Console.BufferWidth, '_') + "\n");

                _client.StartRemoteCLI();
            }
            catch (System.Security.Cryptography.CryptographicException)
            {
                CLIOutput.WriteToConsole("<color><red>(ERROR) Password wrong!\n");
            }
            catch (Exception e)
            {
                CLIOutput.WriteToConsole("<color><red>(ERROR) " + e.Message + "\n");
            }

            CLIOutput.WriteToConsole("<color><gray>\nPress any key to close ...");
            Console.ReadKey();

            return(0);
        }