-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
204 lines (154 loc) · 6.74 KB
/
Main.cs
File metadata and controls
204 lines (154 loc) · 6.74 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
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
197
198
199
200
201
202
203
204
using System.Diagnostics;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Security.Policy;
using System.Net.NetworkInformation;
namespace Breaworlds_PrivateServerManager
{
public partial class Main : Form
{
public static List<ServerData> ServerList = new List<ServerData>();
public Main()
{
InitializeComponent();
Discord.InitRPC();
btn_launch.Enabled = false;
btn_deleteServ.Enabled = false;
btn_copyServer.Enabled = false;
lbl_serverName.Text = "Select a server to start!";
lbl_socialLink.Visible = false;
txt_bio.Text = String.Empty;
LoadJsonData();
}
private void btn_launch_Click(object sender, EventArgs e)
{
int selected = list_Servers.SelectedIndex;
string ip = ServerList[selected].ServerIp;
string port = ServerList[selected].ServerPort;
string name = ServerList[selected].ServerName;
LaunchGame(ip, port, name);
}
private void LoadJsonData()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appFolder = Path.Combine(appDataPath, "BreaworldsPrivateServerManager");
Directory.CreateDirectory(appFolder); // Ensure the directory exists
if (File.Exists(Path.Combine(appFolder, "servers.json")))
{
string jsonContent = File.ReadAllText(Path.Combine(appFolder, "servers.json"));
ServerList = JsonConvert.DeserializeObject<List<ServerData>>(jsonContent);
foreach (ServerData item in ServerList.ToList())
{
list_Servers.Items.Add(item.ServerName);
}
}
}
private void SaveJsonData()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appFolder = Path.Combine(appDataPath, "BreaworldsPrivateServerManager");
Directory.CreateDirectory(appFolder); // Ensure the directory exists
string filepath = Path.Combine(appFolder, "servers.json");
string jsonContent = JsonConvert.SerializeObject(ServerList, Formatting.Indented);
File.WriteAllText(filepath, jsonContent);
}
public void AddServer(ServerData serverData)
{
ServerList.Add(serverData);
list_Servers.Items.Add(serverData.ServerName);
SaveJsonData();
}
public static void LaunchGame(string ServerIp, string ServerPort, string ServerName)
{
Process[] pname = Process.GetProcessesByName("Breaworlds-PrivateServer");
if (pname.Length == 0)
{
string procArgs = ServerIp + " " + ServerPort + " " + ServerName;
Process.Start("./BreaworldsClient/Breaworlds-PrivateServer.exe", procArgs);
Discord.UpdateRPC($"Playing on {ServerName}", "", true);
}
else
{
MessageBox.Show("An instance of Breaworlds is already open. Close it before opening another.");
}
}
private void btn_addServer_Click(object sender, EventArgs e)
{
new AddServer(this).Show();
}
private void btn_createServer_Click(object sender, EventArgs e)
{
new CreateServerData(this).Show();
}
private void list_Servers_SelectedIndexChanged(object sender, EventArgs e)
{
int selected = list_Servers.SelectedIndex;
if (selected == -1) return;
btn_launch.Enabled = true;
btn_deleteServ.Enabled = true;
btn_copyServer.Enabled = true;
lbl_serverName.Text = ServerList[selected].ServerName;
lbl_socialLink.Visible = true;
lbl_socialLink.Text = ServerList[selected].ServerSocialName;
txt_bio.Text = ServerList[selected].ServerBio;
Discord.UpdateRPC($"Selected {lbl_serverName.Text}", "Idle", false);
}
private void lbl_socialLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
int selected = list_Servers.SelectedIndex;
Process.Start(new ProcessStartInfo
{
FileName = $"https://{ServerList[selected].ServerSocialLink}",
UseShellExecute = true
});
}
public static string GetServerCode(ServerData data)
{
string jsonContent = JsonConvert.SerializeObject(data, Formatting.Indented);
string b64 = base64.Base64Encode(jsonContent);
return b64;
}
private void btn_copyServer_Click(object sender, EventArgs e)
{
int selected = list_Servers.SelectedIndex;
string codeData = GetServerCode(ServerList[selected]);
Clipboard.SetText(codeData);
MessageBox.Show("Copied Server Code to Clipboard!");
}
private void btn_deleteServ_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this server?\nYou won't be able to access it unless you add it again.", String.Empty, MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
int selected = list_Servers.SelectedIndex;
list_Servers.SelectedIndex = -1;
btn_launch.Enabled = false;
btn_deleteServ.Enabled = false;
btn_copyServer.Enabled = false;
lbl_serverName.Text = "Select a server to start!";
lbl_socialLink.Visible = false;
txt_bio.Text = String.Empty;
list_Servers.Items.Remove(ServerList[selected].ServerName);
ServerList.Remove(ServerList[selected]);
SaveJsonData();
}
}
private void list_Servers_DoubleClick(object sender, EventArgs e)
{
int selected = list_Servers.SelectedIndex;
if (selected == -1) return;
btn_launch.Enabled = true;
btn_deleteServ.Enabled = true;
btn_copyServer.Enabled = true;
lbl_serverName.Text = ServerList[selected].ServerName;
lbl_socialLink.Visible = true;
lbl_socialLink.Text = ServerList[selected].ServerSocialName;
txt_bio.Text = ServerList[selected].ServerBio;
string ip = ServerList[selected].ServerIp;
string port = ServerList[selected].ServerPort;
string name = ServerList[selected].ServerName;
LaunchGame(ip, port, name);
}
}
}