Skip to content

Commit

Permalink
Update WindowsPseudoConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Feb 14, 2022
1 parent 2cb0b58 commit 732200f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion SteamCMD.ConPTY/SteamCMD.ConPTY.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/WindowsGSM/SteamCMD.ConPTY</RepositoryUrl>
<PackageTags>steamcmd, conpty, windows-10</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

Expand Down
8 changes: 4 additions & 4 deletions SteamCMD.ConPTY/SteamCMDConPTY.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class SteamCMDConPTY : WindowsPseudoConsole
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public ProcessInfo Start(short width = 120, short height = 30)
public new ProcessInfo Start(short width = 120, short height = 30)
{
string fileName = "steamcmd.exe";
FileName = "steamcmd.exe";

// Download steamcmd.exe if not exists
if (!File.Exists(Path.Combine(base.WorkingDirectory, fileName)))
if (!File.Exists(Path.Combine(base.WorkingDirectory, FileName)))
{
string zipPath = Path.Combine(base.WorkingDirectory, "steamcmd.zip");

Expand All @@ -37,7 +37,7 @@ public ProcessInfo Start(short width = 120, short height = 30)
File.Delete(zipPath);
}

return base.Start(fileName, width, height);
return base.Start(width, height);
}
}
}
70 changes: 60 additions & 10 deletions SteamCMD.ConPTY/WindowsPseudoConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class WindowsPseudoConsole
/// </summary>
public string WorkingDirectory { get; set; } = Directory.GetCurrentDirectory();

/// <summary>
/// Gets or sets the application or document to start.
/// </summary>
public string FileName { get; set; } = string.Empty;

/// <summary>
/// Arguments that pass to the console. Default: <see cref="string.Empty"/>
/// </summary>
Expand All @@ -48,24 +53,26 @@ public class WindowsPseudoConsole
private bool disposed;

/// <summary>
/// Pseudo Console (ConPTY), required files will be created on the working directory.
/// Pseudo Console (ConPTY)
/// </summary>
public WindowsPseudoConsole() { }

/// <summary>
/// Pseudo Console (ConPTY), required files will be created on the working directory.
/// Pseudo Console (ConPTY)
/// </summary>
/// <param name="workingDirectory">Working directory</param>
[Obsolete]
public WindowsPseudoConsole(string workingDirectory)
{
WorkingDirectory = workingDirectory;
}

/// <summary>
/// Pseudo Console (ConPTY), required files will be created on the working directory.
/// Pseudo Console (ConPTY)
/// </summary>
/// <param name="workingDirectory">Working directory</param>
/// <param name="arguments">Arguments that pass to the console</param>
[Obsolete]
public WindowsPseudoConsole(string workingDirectory, string arguments)
{
(WorkingDirectory, Arguments) = (workingDirectory, arguments);
Expand All @@ -74,6 +81,46 @@ public WindowsPseudoConsole(string workingDirectory, string arguments)
/// <summary>
/// Start pseudo console
/// </summary>
public ProcessInfo Start(short width = 120, short height = 30)
{
if (WorkingDirectory == null)
{
throw new Exception("WorkingDirectory is not set");
}

string filePath = Path.Combine(WorkingDirectory, FileName);

if (!File.Exists(filePath))
{
throw new Exception($"File does not exist ({filePath})");
}

// Start pseudo console
terminal = new Terminal();
ProcessInfo processInfo = terminal.Start($"{filePath}{(string.IsNullOrEmpty(Arguments) ? string.Empty : $" {Arguments}")}", width, height);

// Save the inputStream
inputStream = terminal.Input;

// Read pseudo console output in the background
Task.Run(() => ReadConPtyOutput(terminal.Output));

// Wait the pseudo console exit in the background
Task.Run(() =>
{
terminal.WaitForExit();

// Call Exited event with exit code
Exited?.Invoke(this, terminal.TryGetExitCode(out uint exitCode) ? (int)exitCode : -1);
});

return processInfo;
}

/// <summary>
/// Start pseudo console
/// </summary>
[Obsolete]
public ProcessInfo Start(string fileName, short width = 120, short height = 30)
{
if (WorkingDirectory == null)
Expand Down Expand Up @@ -230,17 +277,20 @@ private async Task ReadConPtyOutput(Stream output)
/// <param name="disposing"></param>
protected void Dispose(bool disposing)
{
if (!disposed)
if (disposed)
{
if (disposing)
{

}
return;
}

terminal.Dispose();
disposed = true;

disposed = true;
if (disposing)
{

}

terminal.Dispose();
inputStream?.Dispose();
}

/// <summary>
Expand Down

0 comments on commit 732200f

Please sign in to comment.