1- using Microsoft . Extensions . DependencyInjection ;
2- using Microsoft . Extensions . Logging ;
3- using SteamInstaller . Extensions ;
4- using System ;
1+ using System ;
52using System . Diagnostics ;
63using System . IO ;
74using System . IO . Compression ;
85using System . Net ;
9- using System . Net . Http ;
106using System . Reflection ;
117using System . Text ;
12- using System . Threading . Tasks ;
8+ using System . Text . RegularExpressions ;
139
1410namespace SteamInstaller
1511{
@@ -22,9 +18,8 @@ class Program
2218 private const string FILENAME = "steamcmd.zip" ;
2319 private const string FOLDERNAME = "SteamCMD" ;
2420 private const string EXE = "steamcmd.exe" ;
25- private static string test = "" ;
2621
27- private static ILogger logger ;
22+ private static string DataStream = "" ;
2823
2924 /// <summary>
3025 /// Constructor of SteamInstaller
@@ -47,49 +42,46 @@ static void Main(string[] args)
4742 WriteColor ( $ "[// Title:] { Assembly . GetEntryAssembly ( ) . GetName ( ) . Name } ", ConsoleColor . DarkGreen ) ;
4843 WriteColor ( $ "[// Version:] { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } ", ConsoleColor . DarkGreen ) ;
4944 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 ( ) )
45+ WriteColor ( @"[//--Settings-----------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
46+ WriteColor ( $ "[// Install SteamCMD:] { Directory . GetCurrentDirectory ( ) } ", ConsoleColor . DarkGreen ) ;
47+ WriteColor ( @"[//--Download-----------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
48+ var download = DownloadFile ( ) ;
49+ if ( ! download )
5950 {
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 ) ) ;
51+ WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
52+ Console . ReadKey ( ) ;
53+ return ;
54+ }
55+ WriteColor ( @"[//--Install------------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
56+ WriteColor ( $ "[//] Download folder { FOLDERNAME } is exists...", ConsoleColor . DarkGreen ) ;
57+ Directory . Delete ( FOLDERNAME , true ) ;
8158
82- logger . LogInformation ( $ "{ EXE } finished with code { compiler . ExitCode } " ) ;
83- logger . LogInformation ( $ " { FOLDERNAME } is successful installed!" ) ;
59+ WriteColor ( $ "[//] Unzip { FILENAME } to { FOLDERNAME } ..." , ConsoleColor . DarkGreen ) ;
60+ ZipFile . ExtractToDirectory ( FILENAME , FOLDERNAME ) ;
8461
85- compiler . Close ( ) ;
86- }
62+ WriteColor ( $ "[//] Execute { EXE } ...", ConsoleColor . DarkGreen ) ;
63+ WriteColor ( @"[//--SteamCMD Output----------------------------------------------]" , ConsoleColor . DarkGreen ) ;
64+ using ( Process compiler = new Process ( ) )
65+ {
66+ compiler . StartInfo . FileName = $ "{ FOLDERNAME } /{ EXE } ";
67+ compiler . StartInfo . Arguments = "+quit" ;
68+ compiler . StartInfo . UseShellExecute = false ;
69+ compiler . StartInfo . RedirectStandardOutput = true ;
70+ compiler . OutputDataReceived += Compiler_OutputDataReceived ;
71+
72+ compiler . Start ( ) ;
73+ compiler . BeginOutputReadLine ( ) ;
74+
75+ compiler . WaitForExit ( ) ;
76+ //var bytes = Encoding.ASCII.GetBytes(DataStream);
77+ //Console.WriteLine(Encoding.UTF8.GetString(bytes));
78+
79+ WriteColor ( $ "[//] Application { EXE } finished with code { compiler . ExitCode } ", ConsoleColor . DarkGreen ) ;
80+ WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
81+ compiler . Close ( ) ;
8782 }
8883
89- logger . LogInformation ( "HELLO GUYS" ) ;
9084 Console . ReadKey ( ) ;
91- //_ = DownloadExtension.DownloadAsync(STEAM_CMD_URL, "steamcmd.zip");
92- //Console.WriteLine("Hello World!");
9385 }
9486
9587 /// <summary>
@@ -119,66 +111,31 @@ static void WriteColor(string message, ConsoleColor color)
119111 Console . WriteLine ( ) ;
120112 }
121113
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-
156114 private static void Compiler_OutputDataReceived ( object sender , DataReceivedEventArgs e )
157115 {
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 ( ) ) ;
116+ WriteColor ( $ "[//] { e . Data } ", ConsoleColor . Cyan ) ;
117+ //DataStream += e.Data + Environment.NewLine;
165118 }
166119
120+ /// <summary>
121+ /// Download the SteamCMD file
122+ /// </summary>
123+ /// <returns>Return true if the file is successful downloaded</returns>
167124 private static bool DownloadFile ( )
168125 {
169- logger . LogInformation ( $ "Start download steamcmd from { STEAM_CMD_URL } " ) ;
126+ WriteColor ( $ "[//] Start download steamcmd from { STEAM_CMD_URL } ..." , ConsoleColor . DarkGreen ) ;
170127 try
171128 {
172129 var client = new WebClient ( ) ;
173130 client . DownloadFile ( STEAM_CMD_URL , FILENAME ) ;
174- logger . LogInformation ( " Download complete!") ;
131+ WriteColor ( $ "[//] Download complete!", ConsoleColor . DarkGreen ) ;
175132 return true ;
176133 }
177134 catch ( WebException e )
178135 {
179136 var response = ( HttpWebResponse ) e . Response ;
180- logger . LogError ( $ "Download failed! Webexception ended with status: { response . StatusCode } ") ;
181- logger . LogError ( e . Message ) ;
137+ WriteColor ( $ "[// Error:] Download failed! Webexception ended with status: { response . StatusCode } ", ConsoleColor . DarkRed ) ;
138+ WriteColor ( $ "[// Error:] { e . Message } " , ConsoleColor . DarkRed ) ;
182139 return false ;
183140 }
184141 }
0 commit comments