-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSteamCMD.cs
160 lines (156 loc) · 5.26 KB
/
SteamCMD.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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization;
using System.ServiceProcess;
using System.Runtime.InteropServices;
using System.IO;
using System.Globalization;
using System.Net;
namespace ValheimServerWarden
{
public class SteamCMD
{
private Process process;
public event DataReceivedEventHandler OutputDataReceived
{
add
{
this.process.OutputDataReceived += value;
}
remove
{
this.process.OutputDataReceived -= value;
}
}
public event DataReceivedEventHandler ErrorDataReceived
{
add
{
this.process.ErrorDataReceived += value;
}
remove
{
this.process.ErrorDataReceived -= value;
}
}
private string LogName
{
get
{
return "steamcmd.log";
}
}
public string Username { get; set; }
public bool Log { get; set; }
public SteamCMD(string executablePath) : this(executablePath, false) { }
public SteamCMD(string executablePath, bool log)
{
Log = log;
Username = "anonymous";
this.process = new Process();
this.process.StartInfo.FileName = executablePath;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.StartInfo.RedirectStandardError = true;
this.process.StartInfo.RedirectStandardInput = true;
this.process.EnableRaisingEvents = true;
this.process.OutputDataReceived += Process_OutputDataReceived;
this.process.Exited += Process_Exited;
}
public SteamCMD() : this(@"steamcmd\steamcmd.exe", false) {
if (!File.Exists(process.StartInfo.FileName))
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip", "steamcmd.zip");
}
if (!Directory.Exists(new FileInfo(process.StartInfo.FileName).Directory.FullName))
{
Directory.CreateDirectory(new FileInfo(process.StartInfo.FileName).Directory.FullName);
}
System.IO.Compression.ZipFile.ExtractToDirectory("steamcmd.zip", "steamcmd/");
File.Delete("steamcmd.zip");
}
}
public SteamCMD(bool log) : this(@"steamcmd\steamcmd.exe", log) { }
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
string msg = e.Data;
if (msg == null) return;
//Debug.WriteLine(msg);
if (this.Log)
{
try
{
StreamWriter writer = System.IO.File.AppendText(LogName);
writer.WriteLine(msg);
writer.Close();
}
catch (Exception)
{
//not being able to clear the log is not a major problem
}
}
//Monitor for incorrect password attempts
Regex rx = new Regex(@"Peer (\d+) has wrong password", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match match = rx.Match(msg);
if (match.Success)
{
//match.Groups[1].ToString()
return;
}
}
public void Login()
{
new Thread(() =>
{
if (this.Log)
{
System.IO.File.WriteAllText(LogName, "");
}
Thread.CurrentThread.IsBackground = true;
this.process.Refresh();
this.process.Start();
this.process.BeginOutputReadLine();
this.process.StandardInput.WriteLine($"login {Username}");
this.process.WaitForExit();
}).Start();
}
public void Execute(string command)
{
this.process.StandardInput.WriteLine(command);
}
public void Quit()
{
//add code to exit
Execute("quit");
}
public void Install(string installpath, string appid)
{
Execute($"force_install_dir {installpath}");
Execute($"app_update {appid}");
}
public string Install(string appid)
{
string path = new FileInfo(process.StartInfo.FileName).DirectoryName + "\\" + appid + "\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Install(path, appid);
return path;
}
private void Process_Exited(object sender, EventArgs e)
{
this.process.CancelOutputRead();
}
}
}