44using System . IO ;
55using System . IO . Compression ;
66using System . Linq ;
7+ using System . Reflection ;
78using System . Runtime . InteropServices ;
89using System . Security . Cryptography ;
910using System . Text ;
@@ -14,10 +15,6 @@ namespace SharpLoader
1415{
1516 public static class Program
1617 {
17- /* Limitations:
18- * supports only c# 5.0
19- */
20-
2118 /* Exit codes:
2219 * 0 - default
2320 * 1 - data file not found
@@ -38,18 +35,19 @@ public static class Program
3835 private const int SW_SHOW = 5 ;
3936
4037 public const string Author = "Zaczero" ;
41- public const string Version = "2.0. 1" ;
38+ public const string Version = "2.1" ;
4239
4340 private const int ReadBufferSize = ushort . MaxValue ;
4441
4542 private const string ConfigFileName = "SharpLoader.ini" ;
4643 private static readonly string MyPath = Process . GetCurrentProcess ( ) . MainModule . FileName ;
4744 private static readonly string MyDirectory = Path . GetDirectoryName ( MyPath ) ;
48- public static string ConfigPath = Path . Combine ( MyDirectory , ConfigFileName ) ;
45+ public static string ConfigPath ;
4946
5047 public static List < string > DragDropPaths ;
5148 public static int Seed = - 1 ;
5249 public static string Hash ;
50+ public static string SaveDir ;
5351
5452 private static MainForm _form ;
5553 private static bool _outToConsole ;
@@ -69,7 +67,7 @@ public static void Main(string[] args)
6967 for ( var i = 0 ; i < args . Length ; i ++ )
7068 {
7169 // Argument is path
72- if ( args [ i ] != MyPath && ( File . Exists ( args [ i ] ) || Directory . Exists ( args [ i ] ) ) )
70+ if ( ( File . Exists ( args [ i ] ) || Directory . Exists ( args [ i ] ) ) && Path . GetExtension ( args [ i ] ) != ".exe" )
7371 {
7472 // Directory
7573 if ( File . GetAttributes ( args [ i ] ) . HasFlag ( FileAttributes . Directory ) )
@@ -94,6 +92,12 @@ public static void Main(string[] args)
9492 // Normal argument
9593 else
9694 {
95+ if ( args [ i ] == "-cmd" )
96+ {
97+ cmdMode = true ;
98+ continue ;
99+ }
100+
97101 // Multiple arguments
98102 if ( i + 1 < args . Length )
99103 {
@@ -105,14 +109,12 @@ public static void Main(string[] args)
105109 {
106110 throw new Exception ( $ "invalid seed value: { args [ i + 1 ] } ") ;
107111 }
112+ i ++ ;
108113 }
109- }
110- // Single argument
111- else
112- {
113- if ( args [ i ] == "-cmd" )
114+ else if ( args [ i ] == "-path" )
114115 {
115- cmdMode = true ;
116+ SaveDir = args [ i + 1 ] ;
117+ i ++ ;
116118 }
117119 }
118120 }
@@ -124,6 +126,18 @@ public static void Main(string[] args)
124126 Seed = new Random ( Environment . TickCount ) . Next ( 0 , int . MaxValue ) ;
125127 }
126128
129+ if ( string . IsNullOrEmpty ( SaveDir ) )
130+ {
131+ ConfigPath = Path . Combine ( MyDirectory , ConfigFileName ) ;
132+ SaveDir = MyDirectory ;
133+ }
134+ else
135+ {
136+ ConfigPath = Path . Combine ( SaveDir , ConfigFileName ) ;
137+ }
138+
139+ DumpToAppData ( ) ;
140+
127141 // Show UI
128142 if ( ! cmdMode )
129143 {
@@ -161,6 +175,55 @@ public static void Main(string[] args)
161175 }
162176 }
163177
178+ public static void DumpToAppData ( )
179+ {
180+ var appDataPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "SharpLoader" ) ;
181+ var exePath = Path . Combine ( appDataPath , "SharpLoader.exe" ) ;
182+ var thisPath = Process . GetCurrentProcess ( ) . MainModule . FileName ;
183+
184+ if ( thisPath != exePath )
185+ {
186+ if ( ! Directory . Exists ( appDataPath ) )
187+ {
188+ Directory . CreateDirectory ( appDataPath ) ;
189+ }
190+
191+ // Run the newest file
192+ File . Copy ( thisPath , exePath , true ) ;
193+
194+ var argsString = string . Empty ;
195+ foreach ( var arg in Environment . GetCommandLineArgs ( ) )
196+ {
197+ if ( File . Exists ( arg ) )
198+ {
199+ argsString += $ "\" { arg } \" ";
200+ }
201+ else
202+ {
203+ argsString += $ "{ arg } ";
204+ }
205+ }
206+ argsString = argsString . TrimEnd ( ' ' ) ;
207+
208+ Process . Start ( exePath , $ "{ argsString } -path \" { SaveDir } \" ") ;
209+ Environment . Exit ( 0 ) ;
210+ }
211+
212+ var binPath = Path . Combine ( appDataPath , "bin" ) ;
213+ var binZipPath = Path . Combine ( appDataPath , "bin.zip" ) ;
214+
215+ if ( ! Directory . Exists ( binPath ) )
216+ {
217+ if ( ! File . Exists ( binZipPath ) )
218+ {
219+ WriteResourceToFile ( "SharpLoader.bin.zip" , binZipPath ) ;
220+ }
221+
222+ ZipFile . ExtractToDirectory ( binZipPath , binPath ) ;
223+ File . Delete ( binZipPath ) ;
224+ }
225+ }
226+
164227 public static int Compile ( )
165228 {
166229 var randomizer = new SourceRandomizer ( Seed ) ;
@@ -239,6 +302,8 @@ public static int Compile()
239302 return 2 ;
240303 }
241304
305+ outputName = Path . Combine ( SaveDir , outputName ) ;
306+
242307 // Read sources
243308 var userSourceFiles = new List < string > ( ) ;
244309
@@ -410,8 +475,10 @@ public static int Compile()
410475 return 4 ;
411476 }
412477
478+ int . TryParse ( "lol" , out int parseeed ) ;
479+
413480 Console . ForegroundColor = ConsoleColor . Green ;
414- Out ( $ "-=: Done [{ outputName } ] { ( _outToConsole ? "(press any key to exit)" : string . Empty ) } ") ;
481+ Out ( $ "-=: Done [{ Path . GetFileName ( outputName ) } ] { ( _outToConsole ? "(press any key to exit)" : string . Empty ) } ") ;
415482
416483 var sourceBytes = new List < byte > ( ) ;
417484 foreach ( var s in compileSourceFiles )
@@ -482,6 +549,17 @@ public static void CleanTemp()
482549 }
483550 }
484551
552+ private static void WriteResourceToFile ( string resourceName , string fileName )
553+ {
554+ using ( var resource = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) )
555+ {
556+ using ( var file = new FileStream ( fileName , FileMode . Create , FileAccess . Write ) )
557+ {
558+ resource . CopyTo ( file ) ;
559+ }
560+ }
561+ }
562+
485563 private static IEnumerable < string > GetFilesFromDirectory ( string directory )
486564 {
487565 var dir = new DirectoryInfo ( directory ) ;
0 commit comments