1414namespace OpenUtau . Classic {
1515 class ExeWavtool : IWavtool {
1616 static object tempBatLock = new object ( ) ;
17- static object tempShLock = new object ( ) ;
1817
1918 readonly StringBuilder sb = new StringBuilder ( ) ;
2019 readonly string filePath ;
2120 readonly string name ;
21+ readonly string winePath ;
22+ readonly bool useWine ;
2223 private Encoding osEncoding ;
2324
2425 public ExeWavtool ( string filePath , string basePath ) {
2526 this . filePath = filePath ;
2627 name = Path . GetRelativePath ( basePath , filePath ) ;
2728 osEncoding = OS . IsWindows ( ) ? Encoding . GetEncoding ( 0 ) : Encoding . UTF8 ;
29+ string ext = Path . GetExtension ( filePath ) . ToLower ( ) ;
30+ winePath = Preferences . Default . WinePath ;
31+ useWine = ! OS . IsWindows ( ) && ! string . IsNullOrEmpty ( winePath ) && ( ext == ".exe" || ext == ".bat" ) ;
2832 }
2933
3034 public float [ ] Concatenate ( List < ResamplerItem > resamplerItems , string tempPath , CancellationTokenSource cancellation ) {
@@ -45,7 +49,7 @@ public float[] Concatenate(List<ResamplerItem> resamplerItems, string tempPath,
4549 lock ( tempBatLock ) {
4650 using ( var stream = File . Open ( batPath , FileMode . Create ) ) {
4751 UTF8Encoding noBomEncoding = new UTF8Encoding ( false ) ;
48- using ( var writer = new StreamWriter ( stream , OS . IsLinux ( ) ? noBomEncoding : osEncoding ) ) {
52+ using ( var writer = new StreamWriter ( stream , OS . IsWindows ( ) ? osEncoding : noBomEncoding ) ) {
4953 WriteSetUp ( writer , resamplerItems , tempPath ) ;
5054 for ( var i = 0 ; i < resamplerItems . Count ; i ++ ) {
5155 WriteItem ( writer , resamplerItems [ i ] , i , resamplerItems . Count ) ;
@@ -54,14 +58,12 @@ public float[] Concatenate(List<ResamplerItem> resamplerItems, string tempPath,
5458 }
5559 }
5660
57- if ( OS . IsLinux ( ) ) {
58- //Because you can't run .bat files directly on linux, we have to create a shell script wrapper
59- string shPath = PrepareSh ( ) ;
60- ProcessRunner . Run ( shPath , "" , Log . Logger , workDir : PathManager . Inst . CachePath , timeoutMs : 5 * 60 * 1000 ) ;
61- }
62- else {
61+ if ( useWine ) {
62+ ProcessRunner . Run ( winePath , batPath , Log . Logger , workDir : PathManager . Inst . CachePath , timeoutMs : 5 * 60 * 1000 ) ;
63+ } else {
6364 ProcessRunner . Run ( batPath , "" , Log . Logger , workDir : PathManager . Inst . CachePath , timeoutMs : 5 * 60 * 1000 ) ;
6465 }
66+
6567 }
6668 if ( string . IsNullOrEmpty ( tempPath ) || File . Exists ( tempPath ) ) {
6769 using ( var wavStream = Core . Format . Wave . OpenFile ( tempPath ) ) {
@@ -71,39 +73,14 @@ public float[] Concatenate(List<ResamplerItem> resamplerItems, string tempPath,
7173 return new float [ 0 ] ;
7274 }
7375
74- string PrepareSh ( ) {
75- string shPath = Path . Join ( PathManager . Inst . CachePath , "temp.sh" ) ;
76- lock ( tempShLock ) {
77- if ( ! File . Exists ( shPath ) ) {
78- using ( FileStream stream = File . Open ( shPath , FileMode . Create ) ) {
79- //Making a new encoding here that does not have a byte order mark
80- //The byte order mark at the front of an shell script causes an exec format error
81- UTF8Encoding noBomEncoding = new UTF8Encoding ( false ) ;
82- using ( StreamWriter writer = new StreamWriter ( stream , noBomEncoding ) ) {
83- WriteSh ( writer ) ;
84- }
85- }
86- int mode = ( 7 << 6 ) | ( 5 << 3 ) | 5 ;
87- chmod ( shPath , mode ) ;
88- }
89- }
90- return shPath ;
91- }
92-
93- void WriteSh ( StreamWriter writer ) {
94- string batPath = Path . Combine ( PathManager . Inst . CachePath , "temp.bat" ) ;
95- writer . WriteLine ( "#!/bin/bash" ) ;
96- writer . WriteLine ( "LANG=\" ja_JP.UTF8\" wine \" " + batPath + "\" \" ${@,-1}\" " ) ;
97- }
98-
9976 void PrepareHelper ( ) {
10077 string tempHelper = Path . Join ( PathManager . Inst . CachePath , "temp_helper.bat" ) ;
10178 lock ( Renderers . GetCacheLock ( tempHelper ) ) {
10279 if ( ! File . Exists ( tempHelper ) ) {
10380 using ( var stream = File . Open ( tempHelper , FileMode . Create ) ) {
10481 //BOM also causes problems when running .bat files through wine
10582 UTF8Encoding noBomEncoding = new UTF8Encoding ( false ) ;
106- using ( var writer = new StreamWriter ( stream , OS . IsLinux ( ) ? noBomEncoding : osEncoding ) ) {
83+ using ( var writer = new StreamWriter ( stream , OS . IsWindows ( ) ? osEncoding : noBomEncoding ) ) {
10784 WriteHelper ( writer ) ;
10885 }
10986 }
@@ -127,8 +104,7 @@ void WriteSetUp(StreamWriter writer, List<ResamplerItem> resamplerItems, string
127104 writer . WriteLine ( $ "@set tempo={ resamplerItems [ 0 ] . tempo } ") ;
128105 writer . WriteLine ( $ "@set samples={ 44100 } ") ;
129106 writer . WriteLine ( $ "@set oto={ ConvertIfNeeded ( PathManager . Inst . CachePath ) } ") ;
130- string toolPath = OS . IsLinux ( ) ? ResolveResamplerExePathLinux ( filePath ) : filePath ;
131- writer . WriteLine ( $ "@set tool={ ConvertIfNeeded ( toolPath ) } ") ;
107+ writer . WriteLine ( $ "@set tool={ ConvertIfNeeded ( filePath ) } ") ;
132108 string tempFile = Path . GetRelativePath ( PathManager . Inst . CachePath , tempPath ) ;
133109 writer . WriteLine ( $ "@set output={ ConvertIfNeeded ( tempFile ) } ") ;
134110 writer . WriteLine ( "@set helper=temp_helper.bat" ) ;
@@ -143,8 +119,7 @@ void WriteSetUp(StreamWriter writer, List<ResamplerItem> resamplerItems, string
143119 }
144120
145121 void WriteItem ( StreamWriter writer , ResamplerItem item , int index , int total ) {
146- string resampPath = OS . IsLinux ( ) ? ResolveResamplerExePathLinux ( item . resampler . FilePath ) : item . resampler . FilePath ;
147- writer . WriteLine ( $ "@set resamp={ ConvertIfNeeded ( resampPath ) } ") ;
122+ writer . WriteLine ( $ "@set resamp={ ConvertIfNeeded ( item . resampler . FilePath ) } ") ;
148123 writer . WriteLine ( $ "@set params={ item . volume } { item . modulation } !{ item . tempo : G999} { Base64 . Base64EncodeInt12 ( item . pitches ) } ") ;
149124 // fixed the commandline vulnerabilities that also exists in og utau
150125 writer . WriteLine ( $ "@set flag=\" { EscapeFlags ( item . GetFlagsString ( ) ) } \" ") ;
@@ -221,7 +196,7 @@ void WriteTearDown(StreamWriter writer) {
221196 }
222197
223198 string ConvertIfNeeded ( string path ) {
224- if ( OS . IsLinux ( ) ) return ConvertToWindowsPath ( path ) ;
199+ if ( ! OS . IsWindows ( ) ) return ConvertToWindowsPath ( path ) ;
225200 else return path ;
226201 }
227202
@@ -243,46 +218,6 @@ string ConvertToWindowsPath (string linuxPath) {
243218 return windowsPath ;
244219 }
245220
246- //Parse the wrapper shell script created by the user during the resampler install process on linux for the path
247- //to the resampler's exe file. Should work for most paths and files that people would make, but there may be edge cases.
248- //Intended only for use on linux
249- string ResolveResamplerExePathLinux ( string wrapperPath ) {
250- using ( FileStream stream = File . Open ( wrapperPath , FileMode . Open ) ) {
251- using ( StreamReader reader = new StreamReader ( stream ) ) {
252- string line ;
253- int start = - 1 ;
254- int end = - 1 ;
255- for ( line = reader . ReadLine ( ) ; line != null ; line = reader . ReadLine ( ) ) {
256- if ( line [ 0 ] == '#' ) continue ; //ignore comments in the file
257- start = line . IndexOf ( "wine " ) + 5 ;
258- if ( start == - 1 ) continue ;
259-
260- end = - 1 ;
261- if ( line [ start ] == '"' ) { //if path is enclosed by quotation marks
262- start ++ ;
263- end = line . IndexOf ( '"' , start + 1 ) ;
264- }
265- else { //if path is not enclosed by quotation marks (potential for "\ " in string)
266- int lastChecked = start ;
267- do {
268- end = line . IndexOf ( ' ' , lastChecked ) ;
269- if ( line [ end - 1 ] != '\\ ' ) break ;
270-
271- lastChecked = end + 1 ;
272- } while ( end != - 1 ) ;
273- }
274- if ( end != - 1 ) break ;
275- }
276- if ( line == null )
277- throw new InvalidDataException ( "Shell script wrapper for exe resampler is empty" ) ;
278- else if ( start == - 1 || end == - 1 )
279- throw new InvalidDataException ( "Could not find path to .exe resampler in shell script wrapper" ) ;
280- else
281- return line . Substring ( start , end - start ) ;
282- }
283- }
284- }
285-
286221 [ DllImport ( "libc" , SetLastError = true ) ]
287222 private static extern int chmod ( string pathname , int mode ) ;
288223
0 commit comments