|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace PocketMineInstaller |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + |
| 12 | + static string windowsPhpURL = "https://jenkins.pmmp.io/job/PHP-7.2-Aggregate/lastBuild/artifact/PHP-7.2-Windows-x64.zip"; |
| 13 | + |
| 14 | + static void Main(string[] args) |
| 15 | + { |
| 16 | + Console.ForegroundColor = ConsoleColor.White; |
| 17 | + Console.Out.WriteLine("-> PocketMine console installator by CzechPMDevs"); |
| 18 | + Console.Out.WriteLine("-> Type 'help' to display installator commands."); |
| 19 | + Console.Out.WriteLine(""); |
| 20 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 21 | + |
| 22 | + Console.Title = "PocketMine-MP installer by CzechPMDevs"; |
| 23 | + |
| 24 | + string output = ""; |
| 25 | + while(null != (output = Console.ReadLine())) { |
| 26 | + HandleCommand(output); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static void HandleCommand(string command) |
| 31 | + { |
| 32 | + string[] args = command.Split(' '); |
| 33 | + int argsCount = args.Count(); |
| 34 | + if(argsCount < 1) { |
| 35 | + Console.Out.WriteLine("Type 'help' to display PocketMine installer commands."); |
| 36 | + Console.Out.WriteLine(argsCount); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + switch(args[0]) { |
| 41 | + case "help": |
| 42 | + Console.Out.WriteLine("--- PocketMine Installator commands ---"); |
| 43 | + Console.Out.WriteLine("help : Displays help pages"); |
| 44 | + Console.Out.WriteLine("stop : Stops installer"); |
| 45 | + Console.Out.WriteLine("install : Installs PocketMine server"); |
| 46 | + Console.Out.WriteLine("start : Starts PocketMine server"); |
| 47 | + Console.Out.WriteLine("remove : Removes PocketMine server"); |
| 48 | + Console.Out.WriteLine("list : Displays list of available servers"); |
| 49 | + break; |
| 50 | + case "stop": |
| 51 | + Console.Out.WriteLine("Installer stopped!"); |
| 52 | + Environment.Exit(0); |
| 53 | + break; |
| 54 | + case "install": |
| 55 | + if(argsCount < 2) { |
| 56 | + Console.Out.WriteLine("Use 'install <serverName>'"); |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + string name = ""; |
| 61 | + string path = getDataPath() + (name = string.Join(" ", args.Skip(1).ToArray())); |
| 62 | + Install(name, path); |
| 63 | + break; |
| 64 | + case "start": |
| 65 | + if (argsCount < 2) |
| 66 | + { |
| 67 | + Console.Out.WriteLine("Use 'start <serverName>'"); |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + Start(args[1]); |
| 72 | + break; |
| 73 | + case "list": |
| 74 | + string[] dirs = System.IO.Directory.GetDirectories(getDataPath(), "*", System.IO.SearchOption.TopDirectoryOnly); |
| 75 | + string[] server = { }; |
| 76 | + foreach(string dir in dirs) |
| 77 | + { |
| 78 | + System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(dir); |
| 79 | + if (info.Name != "bin") |
| 80 | + { |
| 81 | + server = server.Concat(new string[] { info.Name }).ToArray(); |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + Console.Out.WriteLine("Available servers: " + string.Join(", ", server)); |
| 87 | + |
| 88 | + break; |
| 89 | + case "remove": |
| 90 | + if (argsCount < 2) |
| 91 | + { |
| 92 | + Console.Out.WriteLine("Use 'start <serverName>'"); |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + Remove(args[1]); |
| 97 | + break; |
| 98 | + default: |
| 99 | + Console.Out.WriteLine("Type 'help' to display PocketMine installer commands."); |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + static void Remove(string name) |
| 105 | + { |
| 106 | + if (!System.IO.Directory.Exists(getDataPath() + name)) |
| 107 | + { |
| 108 | + Console.Out.WriteLine("Server " + name + " was not found!"); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + try |
| 113 | + { |
| 114 | + System.IO.Directory.Delete(getDataPath() + name, true); |
| 115 | + |
| 116 | + Console.ForegroundColor = ConsoleColor.White; |
| 117 | + Console.BackgroundColor = ConsoleColor.DarkGreen; |
| 118 | + Console.Out.WriteLine("Server "+name+" removed!"); |
| 119 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 120 | + Console.BackgroundColor = ConsoleColor.Black; |
| 121 | + |
| 122 | + } |
| 123 | + catch(Exception e) |
| 124 | + { |
| 125 | + Console.BackgroundColor = ConsoleColor.Red; |
| 126 | + Console.Out.WriteLine(e.Message); |
| 127 | + Console.BackgroundColor = ConsoleColor.Black; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + static void Start(string name) |
| 135 | + { |
| 136 | + if(!System.IO.Directory.Exists(getDataPath() + name)) |
| 137 | + { |
| 138 | + Console.Out.WriteLine("Server " + name + " was not found!"); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + ProcessStartInfo cmd = new ProcessStartInfo(); |
| 143 | + cmd.FileName = getDataPath() + name + "/start.cmd"; |
| 144 | + |
| 145 | + Process process = new Process(); |
| 146 | + process.StartInfo = cmd; |
| 147 | + process.Start(); |
| 148 | + process.WaitForExit(); |
| 149 | + |
| 150 | + Console.ForegroundColor = ConsoleColor.White; |
| 151 | + Console.BackgroundColor = ConsoleColor.DarkGreen; |
| 152 | + Console.Out.WriteLine("Server '"+ name +"' started!"); |
| 153 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 154 | + Console.BackgroundColor = ConsoleColor.Black; |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + static void Install(string name, string path) |
| 159 | + { |
| 160 | + if(System.IO.Directory.Exists(path)) { |
| 161 | + Console.Out.WriteLine("Server " + name + " is already installed!"); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + InitDirectories(); |
| 166 | + InstallPhp(); |
| 167 | + |
| 168 | + Console.ForegroundColor = ConsoleColor.White; |
| 169 | + Console.WriteLine("Installing into directory '" + path + "'"); |
| 170 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 171 | + |
| 172 | + Console.Out.WriteLine("Creating directory for the server..."); |
| 173 | + System.IO.Directory.CreateDirectory(path); |
| 174 | + |
| 175 | + Console.Out.WriteLine("Downloading latest PocketMine-MP.phar from github..."); |
| 176 | + WebClient webClient = new WebClient(); |
| 177 | + webClient.DownloadFile("https://jenkins.pmmp.io/job/PocketMine-MP/lastSuccessfulBuild/artifact/PocketMine-MP.phar", path + "/PocketMine-MP.phar"); |
| 178 | + Console.Out.WriteLine("PocketMine-MP downloaded!"); |
| 179 | + |
| 180 | + Console.Out.WriteLine("Generating starting script..."); |
| 181 | + System.IO.File.WriteAllText(path + "/start.cmd", "@echo off\r\n" + |
| 182 | + "CD " + path + "/\r\n" + |
| 183 | + "TITLE PocketMine-MP server software for Minecraft: Pocket Edition\r\n" + |
| 184 | + "REM pause on exitcode != 0 so the user can see what went wrong\r\n" + |
| 185 | + getDataPath() + "bin/php/php.exe " + path + "/PocketMine-MP.phar"); |
| 186 | + |
| 187 | + Console.ForegroundColor = ConsoleColor.White; |
| 188 | + Console.BackgroundColor = ConsoleColor.DarkGreen; |
| 189 | + Console.Out.WriteLine("Server " + name + " installed!"); |
| 190 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 191 | + Console.BackgroundColor = ConsoleColor.Black; |
| 192 | + } |
| 193 | + |
| 194 | + static void InitDirectories() |
| 195 | + { |
| 196 | + if (!System.IO.Directory.Exists(getDataPath())) { |
| 197 | + System.IO.Directory.CreateDirectory(getDataPath()); |
| 198 | + Console.Out.WriteLine("Created directory '" + getDataPath() + "'!"); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + static void InstallPhp() |
| 203 | + { |
| 204 | + if(System.IO.Directory.Exists(getDataPath() + "bin")) { |
| 205 | + return; |
| 206 | + } |
| 207 | + |
| 208 | + Console.ForegroundColor = ConsoleColor.White; |
| 209 | + Console.Out.WriteLine("PHP not found, installing php ..."); |
| 210 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 211 | + Console.Out.WriteLine("Downloading php.zip ..."); |
| 212 | + |
| 213 | + WebClient webClient = new WebClient(); |
| 214 | + webClient.DownloadFile(windowsPhpURL, getDataPath() + "php.zip"); |
| 215 | + Console.Out.WriteLine("PHP downloaded!"); |
| 216 | + |
| 217 | + Console.Out.WriteLine("Extracting php..."); |
| 218 | + ZipFile.ExtractToDirectory(getDataPath() + "php.zip", getDataPath()); |
| 219 | + Console.Out.WriteLine("PHP extracted!"); |
| 220 | + |
| 221 | + Console.Out.WriteLine("Removing unuseful files..."); |
| 222 | + if(System.IO.File.Exists(getDataPath() + "vc_redist.x64.exe")) { |
| 223 | + System.IO.File.Delete(getDataPath() + "vc_redist.x64.exe"); |
| 224 | + } |
| 225 | + if (System.IO.File.Exists(getDataPath() + "vc_redist.x86.exe")) { |
| 226 | + System.IO.File.Delete(getDataPath() + "vc_redist.x86.exe"); |
| 227 | + } |
| 228 | + if (System.IO.File.Exists(getDataPath() + "php.zip")) { |
| 229 | + System.IO.File.Delete(getDataPath() + "php.zip"); |
| 230 | + } |
| 231 | + |
| 232 | + Console.ForegroundColor = ConsoleColor.White; |
| 233 | + Console.BackgroundColor = ConsoleColor.DarkGreen; |
| 234 | + Console.Out.WriteLine("PocketMine php installed!"); |
| 235 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 236 | + Console.BackgroundColor = ConsoleColor.Black; |
| 237 | + } |
| 238 | + |
| 239 | + static string getDataPath() |
| 240 | + { |
| 241 | + return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/PocketMine/"; |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments