|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using SteamInstaller.Extensions; |
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.IO.Compression; |
| 8 | +using System.Net; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Reflection; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace SteamInstaller |
| 15 | +{ |
| 16 | + class Program |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Constants |
| 20 | + /// </summary> |
| 21 | + private const string STEAM_CMD_URL = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"; |
| 22 | + private const string FILENAME = "steamcmd.zip"; |
| 23 | + private const string FOLDERNAME = "SteamCMD"; |
| 24 | + private const string EXE = "steamcmd.exe"; |
| 25 | + private static string test = ""; |
| 26 | + |
| 27 | + private static ILogger logger; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Constructor of SteamInstaller |
| 31 | + /// </summary> |
| 32 | + /// <param name="args"></param> |
| 33 | + static void Main(string[] args) |
| 34 | + { |
| 35 | + Console.WriteLine(Environment.NewLine); |
| 36 | + WriteColor(@"[$$$$$$$$\ $$\ $$\ $$$$$$\ $$\]", ConsoleColor.DarkGreen); |
| 37 | + WriteColor(@"[$$ _____|\__| $$ | $$ __$$\ $$ |]", ConsoleColor.DarkGreen); |
| 38 | + WriteColor(@"[$$ | $$\ $$$$$$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\]", ConsoleColor.DarkGreen); |
| 39 | + WriteColor(@"[$$$$$\ $$ |$$ __$$\ $$ _____|\_$$ _|$$$$$$\ $$ | $$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\]", ConsoleColor.DarkGreen); |
| 40 | + WriteColor(@"[$$ __| $$ |$$ | \__|\$$$$$$\ $$ | \______|$$ | $$ / $$ |$$ / $$ |$$$$$$$$ |$$ | \__|]", ConsoleColor.DarkGreen); |
| 41 | + WriteColor(@"[$$ | $$ |$$ | \____$$\ $$ |$$\ $$ | $$\ $$ | $$ |$$ | $$ |$$ ____|$$ |]", ConsoleColor.DarkGreen); |
| 42 | + WriteColor(@"[$$ | $$ |$$ | $$$$$$$ | \$$$$ | \$$$$$$ |\$$$$$$ |\$$$$$$$ |\$$$$$$$\ $$ |]", ConsoleColor.DarkGreen); |
| 43 | + WriteColor(@"[\__| \__|\__| \_______/ \____/ \______/ \______/ \_______| \_______|\__|]", ConsoleColor.DarkGreen); |
| 44 | + Console.WriteLine(Environment.NewLine); |
| 45 | + |
| 46 | + WriteColor(@"[//--Informationen------------------------------------------------]", ConsoleColor.DarkGreen); |
| 47 | + WriteColor($"[// Title:] {Assembly.GetEntryAssembly().GetName().Name}", ConsoleColor.DarkGreen); |
| 48 | + WriteColor($"[// Version:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}", ConsoleColor.DarkGreen); |
| 49 | + WriteColor($"[// Autor:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright}", ConsoleColor.DarkGreen); |
| 50 | + |
| 51 | + |
| 52 | + var serviceCollection = new ServiceCollection(); |
| 53 | + ConfigureServices(serviceCollection); |
| 54 | + |
| 55 | + var serviceProvider = serviceCollection.BuildServiceProvider(); |
| 56 | + logger = serviceProvider.GetService<ILogger<Program>>(); |
| 57 | + |
| 58 | + if (DownloadFile()) |
| 59 | + { |
| 60 | + Directory.Delete(FOLDERNAME, true); |
| 61 | + |
| 62 | + logger.LogInformation($"Unzip {FILENAME} to {FOLDERNAME}"); |
| 63 | + ZipFile.ExtractToDirectory(FILENAME, FOLDERNAME); |
| 64 | + |
| 65 | + logger.LogInformation($"Execute {EXE}"); |
| 66 | + using (Process compiler = new Process()) |
| 67 | + { |
| 68 | + compiler.StartInfo.FileName = $"{FOLDERNAME}/{EXE}"; |
| 69 | + compiler.StartInfo.Arguments = "+quit"; |
| 70 | + compiler.StartInfo.UseShellExecute = false; |
| 71 | + compiler.StartInfo.CreateNoWindow = true; |
| 72 | + compiler.StartInfo.RedirectStandardOutput = true; |
| 73 | + compiler.OutputDataReceived += Compiler_OutputDataReceived; |
| 74 | + |
| 75 | + compiler.Start(); |
| 76 | + compiler.BeginOutputReadLine(); |
| 77 | + |
| 78 | + compiler.WaitForExit(); |
| 79 | + var bytes = Encoding.ASCII.GetBytes(test); |
| 80 | + Console.WriteLine(Encoding.UTF8.GetString(bytes)); |
| 81 | + |
| 82 | + logger.LogInformation($"{EXE} finished with code {compiler.ExitCode}"); |
| 83 | + logger.LogInformation($"{FOLDERNAME} is successful installed!"); |
| 84 | + |
| 85 | + compiler.Close(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + logger.LogInformation("HELLO GUYS"); |
| 90 | + Console.ReadKey(); |
| 91 | + //_ = DownloadExtension.DownloadAsync(STEAM_CMD_URL, "steamcmd.zip"); |
| 92 | + //Console.WriteLine("Hello World!"); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Write some coloring console messages for the user |
| 97 | + /// https://stackoverflow.com/questions/2743260/is-it-possible-to-write-to-the-console-in-colour-in-net |
| 98 | + /// </summary> |
| 99 | + /// <param name="message">Message to write</param> |
| 100 | + /// <param name="color">ConsoleColor value of the color</param> |
| 101 | + static void WriteColor(string message, ConsoleColor color) |
| 102 | + { |
| 103 | + var pieces = Regex.Split(message, @"(\[[^\]]*\])"); |
| 104 | + |
| 105 | + for (int i = 0; i < pieces.Length; i++) |
| 106 | + { |
| 107 | + string piece = pieces[i]; |
| 108 | + |
| 109 | + if (piece.StartsWith("[") && piece.EndsWith("]")) |
| 110 | + { |
| 111 | + Console.ForegroundColor = color; |
| 112 | + piece = piece.Substring(1, piece.Length - 2); |
| 113 | + } |
| 114 | + |
| 115 | + Console.Write(piece); |
| 116 | + Console.ResetColor(); |
| 117 | + } |
| 118 | + |
| 119 | + Console.WriteLine(); |
| 120 | + } |
| 121 | + |
| 122 | + private async static void Test() |
| 123 | + { |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + //var proc = new Process |
| 130 | + //{ |
| 131 | + // StartInfo = new ProcessStartInfo |
| 132 | + // { |
| 133 | + // FileName = $"{FOLDERNAME}/{EXE}", |
| 134 | + // UseShellExecute = false, |
| 135 | + // RedirectStandardOutput = true, |
| 136 | + // RedirectStandardInput = true, |
| 137 | + |
| 138 | + // CreateNoWindow = true |
| 139 | + // } |
| 140 | + //}; |
| 141 | + |
| 142 | + //proc.Start(); |
| 143 | + |
| 144 | + //while (!proc.StandardOutput.EndOfStream) |
| 145 | + //{ |
| 146 | + // await Task.Delay(5000); |
| 147 | + // //proc.Dispose(); |
| 148 | + //} |
| 149 | + //proc.Dispose(); |
| 150 | + ////proc.WaitForExit(); |
| 151 | + //logger.LogInformation($"{EXE} finished with code {proc.ExitCode}"); |
| 152 | + //logger.LogInformation($"{FOLDERNAME} is successful installed!"); |
| 153 | + //proc.Close(); |
| 154 | + } |
| 155 | + |
| 156 | + private static void Compiler_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 157 | + { |
| 158 | + //Console.WriteLine(e.Data); |
| 159 | + test += e.Data + Environment.NewLine; |
| 160 | + } |
| 161 | + |
| 162 | + private static void ConfigureServices(IServiceCollection services) |
| 163 | + { |
| 164 | + services.AddLogging(configure => configure.AddConsole()); |
| 165 | + } |
| 166 | + |
| 167 | + private static bool DownloadFile() |
| 168 | + { |
| 169 | + logger.LogInformation($"Start download steamcmd from {STEAM_CMD_URL}"); |
| 170 | + try |
| 171 | + { |
| 172 | + var client = new WebClient(); |
| 173 | + client.DownloadFile(STEAM_CMD_URL, FILENAME); |
| 174 | + logger.LogInformation("Download complete!"); |
| 175 | + return true; |
| 176 | + } |
| 177 | + catch (WebException e) |
| 178 | + { |
| 179 | + var response = (HttpWebResponse)e.Response; |
| 180 | + logger.LogError($"Download failed! Webexception ended with status: {response.StatusCode}"); |
| 181 | + logger.LogError(e.Message); |
| 182 | + return false; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments