Compare commits

...

6 Commits

Author SHA1 Message Date
Captain ALM 8d08b0e1a1
Update to 0.4.0.1.
Fix issue with allowing concurrent update.
2022-09-18 15:16:32 +01:00
Captain ALM e3841aa9ec
Fix label positioning. 2022-07-19 21:51:46 +01:00
Captain ALM 78575a2bec
Update to 0.4.0.0.
Add support for adding extra arguments.
2022-07-19 21:42:46 +01:00
Captain ALM 45866fbb18
Fix license. 2022-07-11 16:59:09 +01:00
Captain ALM 4de243ca7a
Update to 0.3.0.1.
Fix closing the input stream in the process wrapper.
2022-07-11 13:30:19 +01:00
Captain ALM c17822995e
Update to 0.3.0.0:
Fix the way processes are handled in the application.
Output the about information at startup.
Output the downloading video on pressing go.
2022-07-11 13:28:19 +01:00
9 changed files with 379 additions and 190 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) <year> <owner>.
Copyright (c) 2022 Captain ALM.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

View File

@ -30,14 +30,16 @@
{
this.tableLayoutPanelMain = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanelEntry = new System.Windows.Forms.TableLayoutPanel();
this.textBoxExtraArgs = new System.Windows.Forms.TextBox();
this.buttonSaveTo = new System.Windows.Forms.Button();
this.textBoxSaveDir = new System.Windows.Forms.TextBox();
this.textBoxEntry = new System.Windows.Forms.TextBox();
this.buttonGo = new System.Windows.Forms.Button();
this.labelExtraArgs = new System.Windows.Forms.Label();
this.textBoxOutput = new System.Windows.Forms.TextBox();
this.buttonInstall = new System.Windows.Forms.Button();
this.buttonExit = new System.Windows.Forms.Button();
this.backgroundWorkerMain = new System.ComponentModel.BackgroundWorker();
this.textBoxSaveDir = new System.Windows.Forms.TextBox();
this.buttonSaveTo = new System.Windows.Forms.Button();
this.folderBrowserDialogMain = new System.Windows.Forms.FolderBrowserDialog();
this.tableLayoutPanelMain.SuspendLayout();
this.tableLayoutPanelEntry.SuspendLayout();
@ -56,11 +58,12 @@
this.tableLayoutPanelMain.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanelMain.Margin = new System.Windows.Forms.Padding(1);
this.tableLayoutPanelMain.Name = "tableLayoutPanelMain";
this.tableLayoutPanelMain.RowCount = 4;
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 15F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 55F));
this.tableLayoutPanelMain.RowCount = 5;
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12.5F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12.5F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12.5F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 12.5F));
this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanelMain.Size = new System.Drawing.Size(284, 161);
this.tableLayoutPanelMain.TabIndex = 0;
//
@ -70,25 +73,58 @@
this.tableLayoutPanelMain.SetColumnSpan(this.tableLayoutPanelEntry, 2);
this.tableLayoutPanelEntry.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 75F));
this.tableLayoutPanelEntry.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanelEntry.Controls.Add(this.textBoxExtraArgs, 0, 2);
this.tableLayoutPanelEntry.Controls.Add(this.buttonSaveTo, 1, 1);
this.tableLayoutPanelEntry.Controls.Add(this.textBoxSaveDir, 0, 1);
this.tableLayoutPanelEntry.Controls.Add(this.textBoxEntry, 0, 0);
this.tableLayoutPanelEntry.Controls.Add(this.buttonGo, 1, 0);
this.tableLayoutPanelEntry.Controls.Add(this.labelExtraArgs, 1, 2);
this.tableLayoutPanelEntry.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanelEntry.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanelEntry.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanelEntry.Name = "tableLayoutPanelEntry";
this.tableLayoutPanelEntry.RowCount = 2;
this.tableLayoutPanelMain.SetRowSpan(this.tableLayoutPanelEntry, 2);
this.tableLayoutPanelEntry.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanelEntry.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanelEntry.Size = new System.Drawing.Size(284, 48);
this.tableLayoutPanelEntry.RowCount = 3;
this.tableLayoutPanelMain.SetRowSpan(this.tableLayoutPanelEntry, 3);
this.tableLayoutPanelEntry.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tableLayoutPanelEntry.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
this.tableLayoutPanelEntry.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
this.tableLayoutPanelEntry.Size = new System.Drawing.Size(284, 60);
this.tableLayoutPanelEntry.TabIndex = 0;
//
// textBoxExtraArgs
//
this.textBoxExtraArgs.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxExtraArgs.Location = new System.Drawing.Point(0, 39);
this.textBoxExtraArgs.Margin = new System.Windows.Forms.Padding(0);
this.textBoxExtraArgs.Name = "textBoxExtraArgs";
this.textBoxExtraArgs.Size = new System.Drawing.Size(213, 20);
this.textBoxExtraArgs.TabIndex = 4;
//
// buttonSaveTo
//
this.buttonSaveTo.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonSaveTo.Location = new System.Drawing.Point(213, 19);
this.buttonSaveTo.Margin = new System.Windows.Forms.Padding(0);
this.buttonSaveTo.Name = "buttonSaveTo";
this.buttonSaveTo.Size = new System.Drawing.Size(71, 20);
this.buttonSaveTo.TabIndex = 3;
this.buttonSaveTo.Text = "Save To...";
this.buttonSaveTo.UseVisualStyleBackColor = true;
this.buttonSaveTo.Click += new System.EventHandler(this.buttonSaveTo_Click);
//
// textBoxSaveDir
//
this.textBoxSaveDir.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxSaveDir.Location = new System.Drawing.Point(0, 19);
this.textBoxSaveDir.Margin = new System.Windows.Forms.Padding(0);
this.textBoxSaveDir.Name = "textBoxSaveDir";
this.textBoxSaveDir.Size = new System.Drawing.Size(213, 20);
this.textBoxSaveDir.TabIndex = 2;
//
// textBoxEntry
//
this.textBoxEntry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxEntry.Location = new System.Drawing.Point(0, 2);
this.textBoxEntry.Location = new System.Drawing.Point(0, 0);
this.textBoxEntry.Margin = new System.Windows.Forms.Padding(0);
this.textBoxEntry.Name = "textBoxEntry";
this.textBoxEntry.Size = new System.Drawing.Size(213, 20);
@ -100,33 +136,43 @@
this.buttonGo.Location = new System.Drawing.Point(213, 0);
this.buttonGo.Margin = new System.Windows.Forms.Padding(0);
this.buttonGo.Name = "buttonGo";
this.buttonGo.Size = new System.Drawing.Size(71, 24);
this.buttonGo.Size = new System.Drawing.Size(71, 19);
this.buttonGo.TabIndex = 1;
this.buttonGo.Text = "Go!";
this.buttonGo.UseVisualStyleBackColor = true;
this.buttonGo.Click += new System.EventHandler(this.buttonGo_Click);
//
// labelExtraArgs
//
this.labelExtraArgs.Dock = System.Windows.Forms.DockStyle.Fill;
this.labelExtraArgs.Location = new System.Drawing.Point(216, 39);
this.labelExtraArgs.Name = "labelExtraArgs";
this.labelExtraArgs.Size = new System.Drawing.Size(65, 21);
this.labelExtraArgs.TabIndex = 5;
this.labelExtraArgs.Text = ": Extra Args";
this.labelExtraArgs.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// textBoxOutput
//
this.tableLayoutPanelMain.SetColumnSpan(this.textBoxOutput, 2);
this.textBoxOutput.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxOutput.Location = new System.Drawing.Point(1, 73);
this.textBoxOutput.Location = new System.Drawing.Point(1, 81);
this.textBoxOutput.Margin = new System.Windows.Forms.Padding(1);
this.textBoxOutput.Multiline = true;
this.textBoxOutput.Name = "textBoxOutput";
this.textBoxOutput.ReadOnly = true;
this.textBoxOutput.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBoxOutput.Size = new System.Drawing.Size(282, 87);
this.textBoxOutput.Size = new System.Drawing.Size(282, 79);
this.textBoxOutput.TabIndex = 4;
this.textBoxOutput.WordWrap = false;
//
// buttonInstall
//
this.buttonInstall.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonInstall.Location = new System.Drawing.Point(0, 48);
this.buttonInstall.Location = new System.Drawing.Point(0, 60);
this.buttonInstall.Margin = new System.Windows.Forms.Padding(0);
this.buttonInstall.Name = "buttonInstall";
this.buttonInstall.Size = new System.Drawing.Size(142, 24);
this.buttonInstall.Size = new System.Drawing.Size(142, 20);
this.buttonInstall.TabIndex = 2;
this.buttonInstall.Text = "Install";
this.buttonInstall.UseVisualStyleBackColor = true;
@ -135,10 +181,10 @@
// buttonExit
//
this.buttonExit.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonExit.Location = new System.Drawing.Point(142, 48);
this.buttonExit.Location = new System.Drawing.Point(142, 60);
this.buttonExit.Margin = new System.Windows.Forms.Padding(0);
this.buttonExit.Name = "buttonExit";
this.buttonExit.Size = new System.Drawing.Size(142, 24);
this.buttonExit.Size = new System.Drawing.Size(142, 20);
this.buttonExit.TabIndex = 3;
this.buttonExit.Text = "Exit";
this.buttonExit.UseVisualStyleBackColor = true;
@ -148,27 +194,6 @@
//
this.backgroundWorkerMain.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorkerMain_DoWork);
//
// textBoxSaveDir
//
this.textBoxSaveDir.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxSaveDir.Location = new System.Drawing.Point(0, 26);
this.textBoxSaveDir.Margin = new System.Windows.Forms.Padding(0);
this.textBoxSaveDir.Name = "textBoxSaveDir";
this.textBoxSaveDir.Size = new System.Drawing.Size(213, 20);
this.textBoxSaveDir.TabIndex = 2;
//
// buttonSaveTo
//
this.buttonSaveTo.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonSaveTo.Location = new System.Drawing.Point(213, 24);
this.buttonSaveTo.Margin = new System.Windows.Forms.Padding(0);
this.buttonSaveTo.Name = "buttonSaveTo";
this.buttonSaveTo.Size = new System.Drawing.Size(71, 24);
this.buttonSaveTo.TabIndex = 3;
this.buttonSaveTo.Text = "Save To...";
this.buttonSaveTo.UseVisualStyleBackColor = true;
this.buttonSaveTo.Click += new System.EventHandler(this.buttonSaveTo_Click);
//
// Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -202,6 +227,8 @@
private System.Windows.Forms.TextBox textBoxSaveDir;
private System.Windows.Forms.Button buttonSaveTo;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialogMain;
private System.Windows.Forms.TextBox textBoxExtraArgs;
private System.Windows.Forms.Label labelExtraArgs;
}
}

View File

@ -9,12 +9,13 @@ using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using com.captainalm.YTDLNetFrontEnd.util;
namespace com.captainalm.YTDLNetFrontEnd
{
public partial class Main : Form
{
private Process theProcess;
private MonitorableProcess theProcess;
public Main()
{
@ -33,12 +34,14 @@ namespace com.captainalm.YTDLNetFrontEnd
}
Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
textBoxSaveDir.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
textBoxOutput.AppendText("Starting: " + ProductName + " : " + ProductVersion + Environment.NewLine);
textBoxOutput.AppendText("Copyright: (C) " + this.CompanyName + " : BSD-3-Clause License" + Environment.NewLine);
}
private void buttonGo_Click(object sender, EventArgs e)
{
if (!backgroundWorkerMain.IsBusy) backgroundWorkerMain.RunWorkerAsync(BWArg.Go);
}
private void buttonInstall_Click(object sender, EventArgs e)
@ -78,21 +81,43 @@ namespace com.captainalm.YTDLNetFrontEnd
private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e)
{
setEnableButtons(false);
if (theProcess != null)
{
if (((Control.ModifierKeys & Keys.Shift) == Keys.Shift)) theProcess.kill();
theProcess.waitForExit();
theProcess.close();
}
switch ((BWArg)e.Argument)
{
case BWArg.Install:
setEnableButtons(false);
var loctxt =
YTDL.executeInstall((YTDL.getInstalled() != ApplicationType.Unavailable) ? YTDL.getInstalled() :
(((Control.ModifierKeys & Keys.Shift) == Keys.Shift) ? ApplicationType.YoutubeDL : ApplicationType.YT_DLP),
YTDL.getInstalled() != ApplicationType.Unavailable) +
Environment.NewLine;
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText(loctxt);
}));
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText("Updating YT-DL BackEnd..." + Environment.NewLine);
}));
using (var locpro =
YTDL.executeInstall((YTDL.getInstalled() != ApplicationType.Unavailable) ? YTDL.getInstalled() :
(((Control.ModifierKeys & Keys.Shift) == Keys.Shift) ? ApplicationType.YoutubeDL : ApplicationType.YT_DLP),
YTDL.getInstalled() != ApplicationType.Unavailable))
{
locpro.addOuputReceiver(new OutputReceiverTextbox(textBoxOutput));
locpro.addErrorReceiver(new OutputReaderPrefixed(textBoxOutput, "Error: "));
try
{
locpro.start();
locpro.waitForExit();
}
catch (Exception ex)
{
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText("Could not start python, is python installed?" + Environment.NewLine
+ ex.GetType().FullName + " : " + ex.Message + Environment.NewLine);
}));
}
}
if (YTDL.getInstalled() != ApplicationType.Unavailable)
{
@ -106,32 +131,44 @@ namespace com.captainalm.YTDLNetFrontEnd
setEnableButtons(true);
break;
case BWArg.Go:
setEnableButtons(false);
if (theProcess != null)
{
if (!theProcess.HasExited) theProcess.WaitForExit();
theProcess.Close();
}
var theTarget = "";
var extraArgs = "";
this.Invoke(new Action(() =>
{
theTarget = textBoxEntry.Text;
extraArgs = textBoxExtraArgs.Text;
textBoxEntry.Text = "";
textBoxOutput.AppendText("Downloading: " + theTarget + Environment.NewLine);
if (!extraArgs.Equals("")) {
textBoxOutput.AppendText("Extra Arguments: " + extraArgs + Environment.NewLine);
}
}));
theProcess = YTDL.executeApplication(theTarget);
theProcess = YTDL.executeApplication(theTarget, extraArgs);
if (theProcess != null)
{
theProcess.BeginOutputReadLine();
theProcess.BeginErrorReadLine();
theProcess.OutputDataReceived += theProcess_OutputDataReceived;
theProcess.ErrorDataReceived += theProcess_ErrorDataReceived;
theProcess.Exited += theProcess_Exited;
theProcess.EnableRaisingEvents = true;
theProcess.WaitForExit();
theProcess.addOuputReceiver(new OutputReceiverTextbox(textBoxOutput));
theProcess.addErrorReceiver(new OutputReaderPrefixed(textBoxOutput, "Error: "));
try
{
theProcess.start();
}
catch (Exception ex)
{
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText("Could not start python, is python and the downloader installed?" + Environment.NewLine
+ ex.GetType().FullName + " : " + ex.Message + Environment.NewLine);
}));
}
}
else
{
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText("Could not start python, is python and the downloader installed?" + Environment.NewLine);
}));
}
setEnableButtons(true);
@ -141,43 +178,8 @@ namespace com.captainalm.YTDLNetFrontEnd
}
}
void theProcess_Exited(object sender, EventArgs e)
private enum BWArg
{
try
{
theProcess.CancelOutputRead();
}
catch (InvalidOperationException ex)
{
}
try
{
theProcess.CancelErrorRead();
}
catch (InvalidOperationException ex)
{
}
}
void theProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null || e.Data.Equals("")) return;
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText("Error: " + e.Data + Environment.NewLine);
}));
}
void theProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null || e.Data.Equals("")) return;
this.Invoke(new Action(() =>
{
textBoxOutput.AppendText(e.Data + Environment.NewLine);
}));
}
private enum BWArg {
Install,
Go
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyVersion("0.4.0.1")]
[assembly: AssemblyFileVersion("0.4.0.1")]

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using com.captainalm.YTDLNetFrontEnd.util;
namespace com.captainalm.YTDLNetFrontEnd
{
@ -12,7 +13,7 @@ namespace com.captainalm.YTDLNetFrontEnd
{
private static string[] pathLocs = Environment.GetEnvironmentVariable("PATH").Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
public static Process executeApplication(string target)
public static MonitorableProcess executeApplication(string target, string extra)
{
var packageName = "";
switch (getInstalled())
@ -27,12 +28,12 @@ namespace com.captainalm.YTDLNetFrontEnd
return null;
}
var pyProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m " + packageName + " --hls-prefer-native " +
((getInstalled() == ApplicationType.YT_DLP) ? "-N 16 " : "") + "\"" + target + "\"")
((getInstalled() == ApplicationType.YT_DLP) ? "-N 16 " : "") + "\"" + target + "\"" + (extra.Equals("") ? "" : " " + extra))
{ UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 };
return Process.Start(pyProSet);
return new MonitorableProcess(pyProSet);
}
public static string executeInstall(ApplicationType package, bool update)
public static MonitorableProcess executeInstall(ApplicationType package, bool update)
{
var packageName = "";
switch (package)
@ -44,17 +45,11 @@ namespace com.captainalm.YTDLNetFrontEnd
packageName = "yt-dlp";
break;
default:
return "Invalid Package Name";
return null;
}
var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip install " + packageName + ((update) ? " --upgrade" : ""))
{ UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 };
using (var pipPro = Process.Start(pipProSet))
{
var rpote = new ReadProcessOutputToEnd(pipPro);
pipPro.WaitForExit();
if (!rpote.getError().Equals("")) return rpote.getError();
return rpote.getOutput();
}
return new MonitorableProcess(pipProSet);
}
private static ApplicationType installed;
@ -66,11 +61,13 @@ namespace com.captainalm.YTDLNetFrontEnd
{
var pipProSet = new ProcessStartInfo(findExecutableInPath("python"), "-m pip freeze")
{ UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 };
using (var pipPro = Process.Start(pipProSet))
using (var pipPro = new MonitorableProcess(pipProSet))
{
var rpote = new ReadProcessOutputToEnd(pipPro);
pipPro.WaitForExit();
var theList = rpote.getOutput();
var outputReader = new OutputReceiverReader();
pipPro.addOuputReceiver(outputReader);
pipPro.start();
pipPro.waitForExit();
var theList = outputReader.getData();
if (theList.Contains("yt-dlp")) installed = ApplicationType.YT_DLP;
else if (theList.Contains("youtube-dl")) installed = ApplicationType.YoutubeDL;
else installed = ApplicationType.Unavailable;
@ -103,68 +100,4 @@ namespace com.captainalm.YTDLNetFrontEnd
YoutubeDL = 1,
YT_DLP = 2
}
class ReadProcessOutputToEnd : IDisposable
{
Process theProcess;
string tout = "";
string terr = "";
public ReadProcessOutputToEnd(Process processIn)
{
theProcess = processIn;
theProcess.BeginOutputReadLine();
theProcess.BeginErrorReadLine();
theProcess.OutputDataReceived += theProcess_OutputDataReceived;
theProcess.ErrorDataReceived += theProcess_ErrorDataReceived;
theProcess.Exited += theProcess_Exited;
theProcess.EnableRaisingEvents = true;
}
void theProcess_Exited(object sender, EventArgs e)
{
try
{
theProcess.CancelOutputRead();
}
catch (InvalidOperationException ex)
{
}
try
{
theProcess.CancelErrorRead();
}
catch (InvalidOperationException ex)
{
}
}
void theProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null || e.Data.Equals("")) return;
terr += e.Data + Environment.NewLine;
}
void theProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null || e.Data.Equals("")) return;
tout += e.Data + Environment.NewLine;
}
public string getOutput()
{
return tout;
}
public string getError()
{
return terr;
}
public void Dispose()
{
theProcess.Close();
}
}
}

View File

@ -55,6 +55,9 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="util\IProcessOuputReceiver.cs" />
<Compile Include="util\MonitorableProcess.cs" />
<Compile Include="util\OutputReceivers.cs" />
<Compile Include="YTDL.cs" />
<EmbeddedResource Include="Main.resx">
<DependentUpon>Main.cs</DependentUpon>

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace com.captainalm.YTDLNetFrontEnd.util
{
public interface IProcessOuputReceiver
{
void receiveData(string dataIn);
}
}

View File

@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace com.captainalm.YTDLNetFrontEnd.util
{
public class MonitorableProcess : IDisposable
{
protected ProcessStartInfo startInfo;
protected Process representation;
protected List<IProcessOuputReceiver> outputReceivers = new List<IProcessOuputReceiver>();
protected List<IProcessOuputReceiver> errorReceivers = new List<IProcessOuputReceiver>();
protected bool closed = false;
private object inputSLock = new object();
private object outputSLock = new object();
private object processSLock = new object();
public MonitorableProcess(ProcessStartInfo startInfo, bool redirectInput = false)
{
this.startInfo = startInfo;
this.startInfo.UseShellExecute = false;
this.startInfo.RedirectStandardInput = redirectInput;
}
public void addOuputReceiver(IProcessOuputReceiver processOuput)
{
lock (outputSLock)
{
lock (processSLock)
{
if (representation == null) startInfo.RedirectStandardOutput = true;
}
outputReceivers.Add(processOuput);
}
}
public void removeOutputReceiver(IProcessOuputReceiver processOuput)
{
lock (outputSLock)
{
outputReceivers.Remove(processOuput);
}
}
public void addErrorReceiver(IProcessOuputReceiver processError)
{
lock (outputSLock)
{
lock (processSLock)
{
if (representation == null) startInfo.RedirectStandardError = true;
}
errorReceivers.Add(processError);
}
}
public void removeErrorReceiver(IProcessOuputReceiver processError)
{
lock (outputSLock)
{
errorReceivers.Remove(processError);
}
}
public void writeData(string inputData)
{
lock (processSLock)
{
if (!startInfo.RedirectStandardInput || representation == null || closed || representation.HasExited) return;
lock (inputSLock)
{
representation.StandardInput.Write(inputData);
}
}
}
public Process start()
{
if (closed) return null;
lock (processSLock)
{
representation = Process.Start(startInfo);
if (startInfo.RedirectStandardOutput)
{
representation.BeginOutputReadLine();
representation.OutputDataReceived += representation_OutputDataReceived;
}
if (startInfo.RedirectStandardError)
{
representation.BeginErrorReadLine();
representation.ErrorDataReceived += representation_ErrorDataReceived;
}
representation.Exited += representation_Exited;
representation.EnableRaisingEvents = true;
if (startInfo.RedirectStandardInput) representation.StandardInput.AutoFlush = true;
}
return representation;
}
void representation_Exited(object sender, EventArgs e)
{
try
{
representation.CancelOutputRead();
}
catch (InvalidOperationException ex)
{
}
try
{
representation.CancelErrorRead();
}
catch (InvalidOperationException ex)
{
}
}
protected void representation_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
foreach (var c in outputReceivers) if (e.Data != null && !e.Data.Equals("")) lock (outputSLock) c.receiveData(e.Data);
}
void representation_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
foreach (var c in errorReceivers) if (e.Data != null && !e.Data.Equals("")) lock (outputSLock) c.receiveData(e.Data);
}
public void waitForExit()
{
if (representation == null || closed) return;
if (!representation.HasExited) representation.WaitForExit();
}
void IDisposable.Dispose()
{
close();
}
public void close()
{
if (representation == null || closed) return;
closed = true;
waitForExit();
if (representation != null && startInfo.RedirectStandardInput) representation.StandardInput.Close();
representation.Close();
}
public void kill()
{
if (representation != null && !closed && !representation.HasExited) representation.Kill();
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Windows.Forms;
namespace com.captainalm.YTDLNetFrontEnd.util
{
public class OutputReceiverReader : IProcessOuputReceiver
{
private string theOutput = "";
public void receiveData(string dataIn)
{
theOutput += dataIn;
}
public string getData()
{
return theOutput.TrimEnd(Environment.NewLine.ToCharArray());
}
}
public class OutputReceiverTextbox : IProcessOuputReceiver
{
protected TextBox theOutputTextbox;
public OutputReceiverTextbox(TextBox tboxIn)
{
theOutputTextbox = tboxIn;
}
public virtual void receiveData(string dataIn)
{
if (theOutputTextbox.InvokeRequired)
{
theOutputTextbox.Invoke(new Action(() =>
{
theOutputTextbox.AppendText(dataIn + Environment.NewLine);
}));
}
}
}
public class OutputReaderPrefixed : OutputReceiverTextbox
{
protected string prefix;
public OutputReaderPrefixed(TextBox tboxIn, string prefix)
: base(tboxIn)
{
this.prefix = prefix;
}
public override void receiveData(string dataIn)
{
base.receiveData(prefix + dataIn);
}
}
}