-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm1.cs
More file actions
107 lines (98 loc) · 3.81 KB
/
Form1.cs
File metadata and controls
107 lines (98 loc) · 3.81 KB
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
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
namespace MinecraftServerCSharp
{
public partial class mainWindow : Form
{
public mainWindow()
{
this.running = false;
string workingDirectory = Environment.CurrentDirectory;
this.projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
this.server = new();
InitializeComponent();
this.restartButton.Enabled = false;
}
private void startButton_Click(object sender, EventArgs e)
{
if (this.running) {
this.startButton.Text = "Start";
AppendTextBox("=====================================");
AppendTextBox("============ Stoping the Server ============");
AppendTextBox("=====================================");
server.StandardInput.WriteLine("stop");
server.Close();
this.restartButton.Enabled = false;
this.running = false;
}
else if (!this.running) {
this.startButton.Text = "Stop";
this.running = true;
runServer();
this.restartButton.Enabled = true;
}
}
public void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
return;
}
this.consoleLog.AppendText(value + "\n");
}
private void runServer()
{
try
{
this.server.StartInfo.FileName = this.javaHome;
this.server.StartInfo.RedirectStandardInput = true;
this.server.StartInfo.RedirectStandardOutput = true;
this.server.StartInfo.RedirectStandardError = true;
this.server.StartInfo.CreateNoWindow = true;
this.server.StartInfo.WorkingDirectory = this.projectDirectory + "\\Server";
this.server.StartInfo.Arguments = "-Xmx3G -Xms1G -jar server.jar nogui";
this.server.OutputDataReceived += consoleWriter;
this.server.Exited += serverExit;
AppendTextBox("=====================================");
AppendTextBox("============ Starting the Server ============");
AppendTextBox("=====================================");
this.server.Start();
this.server.BeginOutputReadLine();
this.server.BeginErrorReadLine();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e);
}
}
private void consoleWriter(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
AppendTextBox(outLine.Data);
}
}
private void serverExit(object sendingProcess, EventArgs e)
{
AppendTextBox("Server has exited on its own!");
}
private bool running;
private string projectDirectory;
private string javaHome = "C:\\Program Files\\Java\\jdk-17.0.2\\bin\\java.exe";
private Process server;
private void restartButton_Click(object sender, EventArgs e)
{
AppendTextBox("=====================================");
AppendTextBox("=========== Restarting the Server ===========");
AppendTextBox("=====================================");
this.server.StandardInput.WriteLine("stop");
Thread.Sleep(10);
this.server.Close();
this.server = new Process();
runServer();
}
}
}