From 5895886c46ede6ca26fa148125a9591dadb9b552 Mon Sep 17 00:00:00 2001 From: crowbarmaster Date: Mon, 4 Oct 2021 00:14:10 -0400 Subject: [PATCH] Code cleanup --- BedrockService/Client/Forms/EditSrv.cs | 4 +-- BedrockService/Client/Forms/MainWindow.cs | 2 +- .../Client/Forms/ManagePacksForms.cs | 27 ++++++++----------- BedrockService/Client/Networking/TCPClient.cs | 2 -- .../Service/BedrockService.Service.csproj | 3 +++ BedrockService/Service/BedrockService.cs | 5 ++-- .../Service/Management/ConfigManager.cs | 4 +-- .../Service/Networking/TCPListener.cs | 5 ++-- BedrockService/Service/Program.cs | 10 +++---- .../Service/Server/BedrockServer.cs | 4 +-- .../PackParser/MinecraftKnownPacksClass.cs | 14 ++++------ .../MinecraftPackManifestJsonClass.cs | 7 +---- .../Server/PackParser/MinecraftPackParser.cs | 12 +++------ 13 files changed, 40 insertions(+), 59 deletions(-) diff --git a/BedrockService/Client/Forms/EditSrv.cs b/BedrockService/Client/Forms/EditSrv.cs index 92704f80..bcd8bf61 100644 --- a/BedrockService/Client/Forms/EditSrv.cs +++ b/BedrockService/Client/Forms/EditSrv.cs @@ -65,8 +65,8 @@ private void SaveBtn_Click(object sender, EventArgs e) { if (DelBackupBtn.Enabled) { - if(dataGrid.SelectedRows.Count < 2) - RollbackFolderName = (string)dataGrid.CurrentRow.Cells[0].Value; + if (dataGrid.SelectedRows.Count < 2) + RollbackFolderName = (string)dataGrid.CurrentRow.Cells[0].Value; DialogResult = DialogResult.OK; Close(); } diff --git a/BedrockService/Client/Forms/MainWindow.cs b/BedrockService/Client/Forms/MainWindow.cs index d2761791..5ca0aa63 100644 --- a/BedrockService/Client/Forms/MainWindow.cs +++ b/BedrockService/Client/Forms/MainWindow.cs @@ -522,7 +522,7 @@ private void BackupManager_Click(object sender, EventArgs e) private void ManPacks_Click(object sender, EventArgs e) { FormManager.GetTCPClient.SendData(NetworkMessageSource.Client, NetworkMessageDestination.Server, (byte)connectedHost.Servers.IndexOf(selectedServer), NetworkMessageTypes.PackList); - while(FormManager.GetTCPClient.RecievedPacks == null) + while (FormManager.GetTCPClient.RecievedPacks == null) { Thread.Sleep(150); } diff --git a/BedrockService/Client/Forms/ManagePacksForms.cs b/BedrockService/Client/Forms/ManagePacksForms.cs index e407ebea..93d78468 100644 --- a/BedrockService/Client/Forms/ManagePacksForms.cs +++ b/BedrockService/Client/Forms/ManagePacksForms.cs @@ -1,19 +1,14 @@ using BedrockService.Client.Management; using BedrockService.Service.Networking; -using BedrockService.Utilities; +using BedrockService.Service.Server.PackParser; using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; using System.IO; -using System.Linq; +using System.IO.Compression; using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; -using BedrockService.Service.Server.PackParser; -using System.IO.Compression; namespace BedrockService.Client.Forms { @@ -28,7 +23,7 @@ public ManagePacksForms(byte serverIndex) //FormManager.GetTCPClient.SendData(File.ReadAllBytes($@"E:\testRB.zip"), NetworkMessageSource.Client, NetworkMessageDestination.Server, ServerIndex, NetworkMessageTypes.PackFile); } - public void PopulateServerPacks (List packList) + public void PopulateServerPacks(List packList) { foreach (MinecraftPackParser pack in packList) foreach (MinecraftPackContainer container in pack.FoundPacks) @@ -42,14 +37,14 @@ private void ListBox_SelectedIndexChanged(object sender, EventArgs e) parsedPacksListBox.SelectedIndex = -1; if (thisBox == parsedPacksListBox) serverListBox.SelectedIndex = -1; - if(thisBox.SelectedIndex != -1) + if (thisBox.SelectedIndex != -1) { MinecraftPackContainer selectedPack = (MinecraftPackContainer)thisBox.SelectedItem; - if(selectedPack.IconBytes != null) - using (MemoryStream ms = new MemoryStream(selectedPack.IconBytes)) - { - selectedPackIcon.Image = Image.FromStream(ms); - } + if (selectedPack.IconBytes != null) + using (MemoryStream ms = new MemoryStream(selectedPack.IconBytes)) + { + selectedPackIcon.Image = Image.FromStream(ms); + } if (selectedPack.JsonManifest != null) textBox1.Text = $"{selectedPack.JsonManifest.header.name}\r\n{selectedPack.JsonManifest.header.description}\r\n{selectedPack.JsonManifest.header.uuid}\r\n{selectedPack.JsonManifest.header.version[0]}"; else @@ -72,7 +67,7 @@ private void removePackBtn_Click(object sender, EventArgs e) private void removeAllPacksBtn_Click(object sender, EventArgs e) { List temp = new List(); - foreach(object item in serverListBox.Items) + foreach (object item in serverListBox.Items) temp.Add((MinecraftPackContainer)item); FormManager.GetTCPClient.SendData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(temp)), NetworkMessageSource.Client, NetworkMessageDestination.Server, ServerIndex, NetworkMessageTypes.RemovePack); serverListBox.Items.Clear(); @@ -116,7 +111,7 @@ private void openFileBtn_Click(object sender, EventArgs e) openFileDialog.Filter = "MC pack file (.MCWORLD, .MCPACK, .MCADDON, .Zip)|*.mcworld;*.mcpack;*.mcaddon;*.zip"; openFileDialog.Title = "Select pack file(s)"; openFileDialog.Multiselect = true; - if(openFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { MinecraftPackParser parser = new MinecraftPackParser(openFileDialog.FileNames); PackExtractDir = parser.PackExtractDirectory; diff --git a/BedrockService/Client/Networking/TCPClient.cs b/BedrockService/Client/Networking/TCPClient.cs index d2d106f3..4f72ff3e 100644 --- a/BedrockService/Client/Networking/TCPClient.cs +++ b/BedrockService/Client/Networking/TCPClient.cs @@ -4,11 +4,9 @@ using BedrockService.Service.Server.Logging; using BedrockService.Service.Server.PackParser; using BedrockService.Utilities; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; diff --git a/BedrockService/Service/BedrockService.Service.csproj b/BedrockService/Service/BedrockService.Service.csproj index 7283f787..2b503dee 100644 --- a/BedrockService/Service/BedrockService.Service.csproj +++ b/BedrockService/Service/BedrockService.Service.csproj @@ -71,9 +71,11 @@ ..\packages\NCrontab.Signed.3.3.2\lib\net35\NCrontab.Signed.dll + False ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + False @@ -102,6 +104,7 @@ ..\packages\Topshelf.4.3.0\lib\net452\Topshelf.dll + False diff --git a/BedrockService/Service/BedrockService.cs b/BedrockService/Service/BedrockService.cs index 977a6015..1ebd79dd 100644 --- a/BedrockService/Service/BedrockService.cs +++ b/BedrockService/Service/BedrockService.cs @@ -1,7 +1,6 @@ using BedrockService.Service.Networking; using BedrockService.Service.Server; using BedrockService.Service.Server.HostInfoClasses; -using BedrockService.Service.Server.PackParser; using NCrontab; using System; using System.Collections.Generic; @@ -318,8 +317,8 @@ await Task.Run(() => else if (File.Exists($@"{Program.ServiceDirectory}\Server\MCSFiles\stock_filelist.ini")) DeleteFilelist(File.ReadAllLines($@"{Program.ServiceDirectory}\Server\MCSFiles\stock_filelist.ini"), server.ServerPath.Value); else - DeleteFilesRecursively(new DirectoryInfo(server.ServerPath.Value)); - + DeleteFilesRecursively(new DirectoryInfo(server.ServerPath.Value)); + while (InstanceProvider.HostInfo.ServerVersion == null || InstanceProvider.HostInfo.ServerVersion == "None") { Thread.Sleep(150); diff --git a/BedrockService/Service/Management/ConfigManager.cs b/BedrockService/Service/Management/ConfigManager.cs index 2407b256..e91674a2 100644 --- a/BedrockService/Service/Management/ConfigManager.cs +++ b/BedrockService/Service/Management/ConfigManager.cs @@ -427,7 +427,7 @@ public bool RollbackToBackup(byte serverIndex, string folderName) ServerInfo server = InstanceProvider.GetServerInfoByIndex(serverIndex); BedrockServer brs = InstanceProvider.GetBedrockServerByIndex(serverIndex); brs.CurrentServerStatus = BedrockServer.ServerStatus.Stopping; - while(brs.CurrentServerStatus != BedrockServer.ServerStatus.Stopped) + while (brs.CurrentServerStatus != BedrockServer.ServerStatus.Stopped) { Thread.Sleep(100); } @@ -462,7 +462,7 @@ private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) foreach (FileInfo file in source.GetFiles()) file.CopyTo(Path.Combine(target.FullName, file.Name), true); } - + private void DeleteFilesRecursively(DirectoryInfo source) { foreach (DirectoryInfo dir in source.GetDirectories()) diff --git a/BedrockService/Service/Networking/TCPListener.cs b/BedrockService/Service/Networking/TCPListener.cs index 37041b5c..2ef1f879 100644 --- a/BedrockService/Service/Networking/TCPListener.cs +++ b/BedrockService/Service/Networking/TCPListener.cs @@ -2,7 +2,6 @@ using BedrockService.Service.Server; using BedrockService.Service.Server.HostInfoClasses; using BedrockService.Service.Server.Logging; -using BedrockService.Service.Server.Management; using BedrockService.Service.Server.PackParser; using BedrockService.Utilities; using Newtonsoft.Json; @@ -202,7 +201,7 @@ private void IncomingListener() MinecraftKnownPacksClass knownPacks = new MinecraftKnownPacksClass($@"{InstanceProvider.GetServerInfoByIndex(serverIndex).ServerPath.Value}\valid_known_packs.json", $@"{Program.ServiceDirectory}\Server\stock_packs.json"); List MCContainer = JsonConvert.DeserializeObject>(data); - foreach(MinecraftPackContainer cont in MCContainer) + foreach (MinecraftPackContainer cont in MCContainer) knownPacks.RemovePackFromServer(InstanceProvider.GetServerInfoByIndex(serverIndex).ServerPath.Value, cont); break; @@ -214,7 +213,7 @@ private void IncomingListener() List list = new List(); foreach (MinecraftKnownPacksClass.KnownPack pack in knownPacks.KnownPacks) list.Add(new MinecraftPackParser($@"{InstanceProvider.GetServerInfoByIndex(serverIndex).ServerPath}\{pack.path.Replace(@"/", @"\")}")); - + SendData(Encoding.UTF8.GetBytes(JArray.FromObject(list).ToString()), NetworkMessageSource.Server, NetworkMessageDestination.Client, NetworkMessageTypes.PackList); break; diff --git a/BedrockService/Service/Program.cs b/BedrockService/Service/Program.cs index e175346b..da5763e0 100644 --- a/BedrockService/Service/Program.cs +++ b/BedrockService/Service/Program.cs @@ -1,11 +1,11 @@ -using System; -using System.Threading; +using BedrockService.Service.Server; +using System; +using System.Diagnostics; using System.IO; using System.Reflection; +using System.Threading; using Topshelf; using Topshelf.Runtime; -using BedrockService.Service.Server; -using System.Diagnostics; namespace BedrockService.Service { @@ -44,7 +44,7 @@ static void Main(string[] args) x.Service(settings => InstanceProvider.BedrockService, s => { s.BeforeStartingService(_ => InstanceProvider.ServiceLogger.AppendLine("Starting service...")); - s.BeforeStoppingService(_ => + s.BeforeStoppingService(_ => { InstanceProvider.ServiceLogger.AppendLine("Stopping service..."); IsExiting = true; diff --git a/BedrockService/Service/Server/BedrockServer.cs b/BedrockService/Service/Server/BedrockServer.cs index 4dad1ca1..14512030 100644 --- a/BedrockService/Service/Server/BedrockServer.cs +++ b/BedrockService/Service/Server/BedrockServer.cs @@ -165,7 +165,7 @@ public void ApplicationWatchdogMonitor() { return; } - + } } @@ -344,7 +344,7 @@ private void StdOutToLog(object sender, DataReceivedEventArgs e) Console.WriteLine($"Player {username} disconnected with XUID: {xuid}"); InstanceProvider.PlayerManager.PlayerDisconnected(xuid, serverInfo); } - if(dataMsg.Contains("Failed to load Vanilla")) + if (dataMsg.Contains("Failed to load Vanilla")) { CurrentServerStatus = ServerStatus.Stopping; while (CurrentServerStatus != ServerStatus.Stopped) diff --git a/BedrockService/Service/Server/PackParser/MinecraftKnownPacksClass.cs b/BedrockService/Service/Server/PackParser/MinecraftKnownPacksClass.cs index 2eddb581..c59a3b7c 100644 --- a/BedrockService/Service/Server/PackParser/MinecraftKnownPacksClass.cs +++ b/BedrockService/Service/Server/PackParser/MinecraftKnownPacksClass.cs @@ -1,11 +1,7 @@ -using System; -using System.IO; +using Newtonsoft.Json.Linq; using System.Collections.Generic; -using Newtonsoft.Json; +using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Newtonsoft.Json.Linq; namespace BedrockService.Service.Server.PackParser { @@ -29,7 +25,7 @@ public override string ToString() } } - public MinecraftKnownPacksClass (string serverFile, string stockFile) + public MinecraftKnownPacksClass(string serverFile, string stockFile) { KnownPacks = ParseJsonArray(serverFile); stockPacks = ParseJsonArray(stockFile); @@ -44,7 +40,7 @@ public MinecraftKnownPacksClass (string serverFile, string stockFile) } } - public void RemovePackFromServer (string serverPath, MinecraftPackContainer pack) + public void RemovePackFromServer(string serverPath, MinecraftPackContainer pack) { if (pack.ManifestType == "WorldPack") Directory.Delete($@"{serverPath}\worlds\{pack.FolderName}", true); @@ -54,7 +50,7 @@ public void RemovePackFromServer (string serverPath, MinecraftPackContainer pack Directory.Delete($@"{serverPath}\resource_packs\{ pack.FolderName}", true); KnownPacks.Remove(KnownPacks.First(p => p.uuid != null || p.uuid == pack.JsonManifest.header.uuid)); List packsToWrite = new List(stockPacks); - if(KnownPacks.Count > 0) + if (KnownPacks.Count > 0) packsToWrite.AddRange(KnownPacks); File.WriteAllText($@"{serverPath}\valid_known_packs.json", JArray.FromObject(packsToWrite).ToString()); } diff --git a/BedrockService/Service/Server/PackParser/MinecraftPackManifestJsonClass.cs b/BedrockService/Service/Server/PackParser/MinecraftPackManifestJsonClass.cs index f970b235..a39270da 100644 --- a/BedrockService/Service/Server/PackParser/MinecraftPackManifestJsonClass.cs +++ b/BedrockService/Service/Server/PackParser/MinecraftPackManifestJsonClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace BedrockService.Service.Server.PackParser { diff --git a/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs b/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs index ce4475a9..a01237ab 100644 --- a/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs +++ b/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs @@ -1,11 +1,7 @@ -using System; +using Newtonsoft.Json; using System.Collections.Generic; using System.IO; using System.IO.Compression; -using Newtonsoft.Json; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace BedrockService.Service.Server.PackParser { @@ -69,9 +65,9 @@ public MinecraftPackParser(string[] files) FileInfo fInfo = new FileInfo(file); string zipFilePath = $@"{PackExtractDirectory.FullName}\{fInfo.Name.Replace(fInfo.Extension, "")}"; ZipFile.ExtractToDirectory(file, zipFilePath); - foreach(FileInfo extractedFile in new DirectoryInfo(zipFilePath).GetFiles()) + foreach (FileInfo extractedFile in new DirectoryInfo(zipFilePath).GetFiles()) { - if(extractedFile.Extension == ".mcpack") + if (extractedFile.Extension == ".mcpack") { Directory.CreateDirectory($@"{zipFilePath}\{extractedFile.Name.Replace(extractedFile.Extension, "")}"); ZipFile.ExtractToDirectory(extractedFile.FullName, $@"{zipFilePath}\{fInfo.Name.Replace(fInfo.Extension, "")}_{extractedFile.Name.Replace(extractedFile.Extension, "")}"); @@ -82,7 +78,7 @@ public MinecraftPackParser(string[] files) ParseDirectory(directory); } - private void ParseDirectory(DirectoryInfo directoryToParse) + private void ParseDirectory(DirectoryInfo directoryToParse) { InstanceProvider.ServiceLogger.AppendLine($"Parsing directory {directoryToParse.Name}"); if (directoryToParse.Exists)