ShareX/ShareX.HelpersLib/CLI/ExternalCLIManager.cs

130 lines
4.2 KiB
C#
Raw Normal View History

2014-05-09 11:08:30 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2014-05-09 11:08:30 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
{
public abstract class ExternalCLIManager : IDisposable
{
2014-05-09 11:08:30 +12:00
public event DataReceivedEventHandler OutputDataReceived;
public event DataReceivedEventHandler ErrorDataReceived;
2019-11-17 14:03:54 +13:00
public bool IsProcessRunning { get; private set; }
2015-09-15 19:00:38 +12:00
protected Process process;
2014-05-09 13:16:43 +12:00
public virtual int Open(string path, string args = null)
{
2014-05-09 13:16:43 +12:00
if (File.Exists(path))
2014-05-09 03:17:42 +12:00
{
2015-06-03 23:32:34 +12:00
using (process = new Process())
{
2018-12-07 07:51:41 +13:00
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = path,
WorkingDirectory = Path.GetDirectoryName(path),
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
2014-05-09 01:28:46 +12:00
2015-06-03 23:32:34 +12:00
process.EnableRaisingEvents = true;
if (psi.RedirectStandardOutput) process.OutputDataReceived += cli_OutputDataReceived;
if (psi.RedirectStandardError) process.ErrorDataReceived += cli_ErrorDataReceived;
process.StartInfo = psi;
2018-12-07 07:51:41 +13:00
DebugHelper.WriteLine($"CLI: \"{psi.FileName}\" {psi.Arguments}");
2015-06-03 23:32:34 +12:00
process.Start();
2018-12-07 07:51:41 +13:00
2015-06-03 23:32:34 +12:00
if (psi.RedirectStandardOutput) process.BeginOutputReadLine();
if (psi.RedirectStandardError) process.BeginErrorReadLine();
try
{
2019-11-17 14:03:54 +13:00
IsProcessRunning = true;
process.WaitForExit();
}
finally
{
2019-11-17 14:03:54 +13:00
IsProcessRunning = false;
}
2015-06-03 23:32:34 +12:00
return process.ExitCode;
}
2014-05-09 11:08:30 +12:00
}
2014-05-09 03:17:42 +12:00
2014-05-09 11:08:30 +12:00
return -1;
}
2014-05-09 11:08:30 +12:00
private void cli_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
2021-06-10 10:14:01 +12:00
OutputDataReceived?.Invoke(sender, e);
}
}
2014-05-09 11:08:30 +12:00
private void cli_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
2014-05-09 11:08:30 +12:00
if (e.Data != null)
{
2021-06-10 10:14:01 +12:00
ErrorDataReceived?.Invoke(sender, e);
}
}
2014-05-09 11:08:30 +12:00
public void WriteInput(string input)
{
2019-11-17 14:03:54 +13:00
if (IsProcessRunning && process != null && process.StartInfo != null && process.StartInfo.RedirectStandardInput)
2014-05-09 11:08:30 +12:00
{
process.StandardInput.WriteLine(input);
}
}
2014-05-09 11:08:30 +12:00
public virtual void Close()
{
2019-11-17 14:03:54 +13:00
if (IsProcessRunning && process != null)
{
2014-05-09 11:08:30 +12:00
process.CloseMainWindow();
}
}
public void Dispose()
{
2014-05-09 11:08:30 +12:00
if (process != null)
{
2014-05-09 11:08:30 +12:00
process.Dispose();
}
}
}
}