-
Notifications
You must be signed in to change notification settings - Fork 0
/
SrcdsControl.cs
196 lines (161 loc) · 6.61 KB
/
SrcdsControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.Win32.SafeHandles;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Text;
namespace SrcdsConsoleRedirect
{
/// <summary>
/// Provides a output redirection solution on srcds.exe
/// </summary>
public class SrcdsControl : IDisposable
{
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr CreateEvent(SECURITY_ATTRIBUTES lpEventAttributes, bool bManualReset, bool bInitialState, string? lpName);
private readonly MemoryMappedFile _mappedFile;
private readonly ManualResetEvent _parent, _child;
/// <summary>
/// Console line width (Default: 80)
/// </summary>
public int ConsoleLineWidth { get; init; } = 80;
/// <summary>
/// Read buffer size (Default: 65536)
/// </summary>
public int BufferSize { get; init; } = 65536;
/// <summary>
/// Process of srcds.exe
/// </summary>
public Process? Process { get; private set; }
/// <summary>
/// Provides a output redirection solution on srcds.exe
/// </summary>
public SrcdsControl()
{
_mappedFile = MemoryMappedFile.CreateNew(null, BufferSize, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, HandleInheritability.Inheritable);
_parent = CreateManualResetEvent();
_child = CreateManualResetEvent();
}
/// <summary>
/// Start srcds.exe
/// </summary>
/// <param name="fileName"></param>
/// <param name="arguments"></param>
/// <returns></returns>
public Process Start(string fileName, string arguments)
{
string newArguments = $"-HFILE {_mappedFile.SafeMemoryMappedFileHandle.DangerousGetHandle()} -HPARENT {_parent.SafeWaitHandle.DangerousGetHandle()} -HCHILD {_child.SafeWaitHandle.DangerousGetHandle()} {arguments}";
Process = new()
{
EnableRaisingEvents = true,
StartInfo = new()
{
FileName = fileName,
Arguments = newArguments
}
};
Process.Exited += (sender, e) => _child.Set();
Process.Start();
return Process;
}
/// <summary>
/// Send command to srcds.exe
/// </summary>
/// <param name="data"></param>
/// <returns><see langword="true"/> if success, else <see langword="false"/></returns>
public bool Write(string data)
{
using MemoryMappedViewStream stream = _mappedFile.CreateViewStream();
using BinaryWriter binaryWriter = new(stream);
binaryWriter.Write(0x2);
binaryWriter.Write(data);
WaitHandle.SignalAndWait(_parent, _child);
using MemoryMappedViewStream stream2 = _mappedFile.CreateViewStream();
using BinaryReader binaryReader = new(stream2);
return binaryReader.ReadBoolean();
}
/// <summary>
/// Get srcds.exe screen buffer
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns>Screen buffer</returns>
/// <exception cref="InvalidOperationException"></exception>
public string GetScreenBuffer(int start, int end)
{
SignalAndWait(new int[] { 0x3, start, end });
using MemoryMappedViewStream stream = _mappedFile.CreateViewStream();
using BinaryReader binaryReader = new(stream);
if (!binaryReader.ReadBoolean())
{
throw new InvalidOperationException("Fail to GetScreenBuffer");
}
byte[] bytes = binaryReader.ReadBytes(BufferSize);
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
/// <summary>
/// Get srcds.exe screen buffer size
/// </summary>
/// <returns>Number of lines</returns>
/// <exception cref="InvalidOperationException"></exception>
public int GetScreenBufferSize()
{
SignalAndWait(new int[] { 0x4 });
int[] data = new int[2];
using MemoryMappedViewStream stream = _mappedFile.CreateViewStream();
Marshal.Copy(stream.SafeMemoryMappedViewHandle.DangerousGetHandle(), data, 0, data.Length);
if (data[0] != 1)
{
throw new InvalidOperationException("Fail to GetScreenBufferSize");
}
return data[1];
}
/// <summary>
/// Set srcds.exe screen buffer size
/// </summary>
/// <param name="size"></param>
/// <returns><see langword="true"/> if success, else <see langword="false"/></returns>
public bool SetScreenBufferSize(int size)
{
SignalAndWait(new int[] { 0x5, size });
int[] data = new int[1];
using MemoryMappedViewStream stream = _mappedFile.CreateViewStream();
Marshal.Copy(stream.SafeMemoryMappedViewHandle.DangerousGetHandle(), data, 0, data.Length);
return data[0] == 1;
}
/// <inheritdoc/>
public void Dispose()
{
_mappedFile?.Dispose();
_parent?.Dispose();
_child?.Dispose();
if (Process != null && !Process.HasExited)
{
Process.Kill();
}
GC.SuppressFinalize(this);
}
private static ManualResetEvent CreateManualResetEvent()
{
SECURITY_ATTRIBUTES lpSecurityAttributes = new();
lpSecurityAttributes.nLength = Marshal.SizeOf(lpSecurityAttributes);
lpSecurityAttributes.lpSecurityDescriptor = IntPtr.Zero;
lpSecurityAttributes.bInheritHandle = 1;
ManualResetEvent manualResetEvent = new(false);
manualResetEvent.SetSafeWaitHandle(new SafeWaitHandle(CreateEvent(lpSecurityAttributes, false, false, null), true));
return manualResetEvent;
}
private void SignalAndWait(int[] parameters)
{
using MemoryMappedViewStream stream = _mappedFile.CreateViewStream();
Marshal.Copy(parameters, 0, stream.SafeMemoryMappedViewHandle.DangerousGetHandle(), parameters.Length);
WaitHandle.SignalAndWait(_parent, _child);
}
}
}