2
2
using System . Diagnostics ;
3
3
using System . IO ;
4
4
using System . Text ;
5
- using System . Threading . Tasks ;
6
- using MahApps . Metro . Controls . Dialogs ;
7
5
using Microsoft . Win32 ;
8
- using SPCode . UI ;
9
6
using static SPCode . Interop . TranslationProvider ;
10
- using static SPCode . Utils . JavaInstallation ;
11
7
12
8
namespace SPCode . Utils
13
9
{
14
- public class DecompileUtil
10
+ public static class DecompileUtil
15
11
{
16
- public async Task DecompilePlugin ( string filePath = null )
12
+ public static FileInfo GetFile ( )
17
13
{
18
- var java = new JavaInstallation ( ) ;
14
+ string fileToDecompile ;
19
15
20
- // First we check the java version of the user, and act accordingly
21
- ProgressDialogController checkingJavaDialog = null ;
22
- if ( Program . MainWindow != null )
16
+ var ofd = new OpenFileDialog
23
17
{
24
- checkingJavaDialog = await Program . MainWindow . ShowProgressAsync ( Translate ( "JavaInstallCheck" ) + "..." ,
25
- "" , false , Program . MainWindow . MetroDialogOptions ) ;
26
- MainWindow . ProcessUITasks ( ) ;
27
- }
28
- switch ( java . GetJavaStatus ( ) )
18
+ Filter = Constants . DecompileFileFilters ,
19
+ Title = Translate ( "ChDecomp" )
20
+ } ;
21
+ var result = ofd . ShowDialog ( ) ;
22
+ fileToDecompile = result . Value && ! string . IsNullOrWhiteSpace ( ofd . FileName ) ? ofd . FileName : null ;
23
+
24
+ if ( fileToDecompile == null )
29
25
{
30
- case JavaResults . Absent :
31
- {
32
- // If java is not installed, offer to download it
33
- await checkingJavaDialog . CloseAsync ( ) ;
34
- if ( await Program . MainWindow . ShowMessageAsync ( Translate ( "JavaNotFoundTitle" ) ,
35
- Translate ( "JavaNotFoundMessage" ) ,
36
- MessageDialogStyle . AffirmativeAndNegative , Program . MainWindow . MetroDialogOptions ) == MessageDialogResult . Affirmative )
37
- {
38
- await java . InstallJava ( ) ;
39
- }
40
- return ;
41
- }
42
- case JavaResults . Outdated :
43
- {
44
- // If java is outdated, offer to upgrade it
45
- await checkingJavaDialog . CloseAsync ( ) ;
46
- if ( await Program . MainWindow . ShowMessageAsync ( Translate ( "JavaOutdatedTitle" ) ,
47
- Translate ( "JavaOutdatedMessage" ) ,
48
- MessageDialogStyle . AffirmativeAndNegative , Program . MainWindow . MetroDialogOptions ) == MessageDialogResult . Affirmative )
49
- {
50
- await java . InstallJava ( ) ;
51
- }
52
- return ;
53
- }
54
- case JavaResults . Correct :
55
- {
56
- // Move on
57
- await checkingJavaDialog . CloseAsync ( ) ;
58
- break ;
59
- }
26
+ return null ;
60
27
}
28
+ return new FileInfo ( fileToDecompile ) ;
29
+ }
61
30
62
- string fileToDecompile ;
63
- if ( filePath == null )
31
+ public static string GetDecompiledPlugin ( FileInfo file )
32
+ {
33
+ var decompilerPath = Paths . GetLysisDirectory ( ) ;
34
+ var destFile = file . FullName + ".sp" ;
35
+ var standardOutput = new StringBuilder ( ) ;
36
+ using var process = new Process ( ) ;
37
+ var si = new ProcessStartInfo
64
38
{
65
- var ofd = new OpenFileDialog
66
- {
67
- Filter = "Sourcepawn Plugins (*.smx)|*.smx" ,
68
- Title = Translate ( "ChDecomp" )
69
- } ;
70
- var result = ofd . ShowDialog ( ) ;
71
- fileToDecompile = result . Value && ! string . IsNullOrWhiteSpace ( ofd . FileName ) ? ofd . FileName : null ;
72
- }
73
- else
39
+ WorkingDirectory = decompilerPath ,
40
+ UseShellExecute = false ,
41
+ RedirectStandardOutput = true ,
42
+ RedirectStandardError = true ,
43
+ CreateNoWindow = true ,
44
+ FileName = "cmd.exe" ,
45
+ Arguments = $ "/c LysisDecompiler.exe \" { file . FullName } \" ",
46
+ } ;
47
+ process . StartInfo = si ;
48
+
49
+ if ( File . Exists ( destFile ) )
74
50
{
75
- fileToDecompile = filePath ;
51
+ File . Delete ( destFile ) ;
76
52
}
77
53
78
- if ( ! string . IsNullOrWhiteSpace ( fileToDecompile ) )
54
+ try
79
55
{
80
- var fInfo = new FileInfo ( fileToDecompile ) ;
81
- if ( fInfo . Exists )
82
- {
83
- ProgressDialogController task = null ;
84
- if ( Program . MainWindow != null )
85
- {
86
- task = await Program . MainWindow . ShowProgressAsync ( Translate ( "Decompiling" ) + "..." ,
87
- fInfo . FullName , false , Program . MainWindow . MetroDialogOptions ) ;
88
- MainWindow . ProcessUITasks ( ) ;
89
- }
90
-
91
- // Prepare Lysis execution
92
- var destFile = fInfo . FullName + ".sp" ;
93
- var standardOutput = new StringBuilder ( ) ;
94
- using var process = new Process ( ) ;
95
- process . StartInfo . WorkingDirectory = Paths . GetLysisDirectory ( ) ;
96
- process . StartInfo . UseShellExecute = false ;
97
- process . StartInfo . RedirectStandardOutput = true ;
98
- process . StartInfo . CreateNoWindow = true ;
99
- process . StartInfo . FileName = "java" ;
100
-
101
- process . StartInfo . Arguments = $ "-jar lysis-java.jar \" { fInfo . FullName } \" ";
102
-
103
- // Execute Lysis, read and store output
104
- try
105
- {
106
- process . Start ( ) ;
107
- while ( ! process . HasExited )
108
- {
109
- standardOutput . Append ( process . StandardOutput . ReadToEnd ( ) ) ;
110
- }
111
- standardOutput . Append ( process . StandardOutput . ReadToEnd ( ) ) ;
112
- File . WriteAllText ( destFile , standardOutput . ToString ( ) , Encoding . UTF8 ) ;
113
- }
114
- catch ( Exception ex )
115
- {
116
- await Program . MainWindow . ShowMessageAsync ( $ "{ fInfo . Name } { Translate ( "FailedToDecompile" ) } ",
117
- $ "{ ex . Message } ", MessageDialogStyle . Affirmative ,
118
- Program . MainWindow . MetroDialogOptions ) ;
119
- }
120
-
121
- // Load the decompiled file to SPCode
122
- Program . MainWindow . TryLoadSourceFile ( destFile , out _ , true , false , true ) ;
123
- if ( task != null )
124
- {
125
- await task . CloseAsync ( ) ;
126
- }
127
- }
56
+ process . Start ( ) ;
57
+ standardOutput . Append ( process . StandardOutput . ReadToEnd ( ) ) ;
58
+ File . WriteAllText ( destFile , standardOutput . ToString ( ) , Encoding . UTF8 ) ;
128
59
}
60
+ catch ( Exception )
61
+ {
62
+ throw ;
63
+ }
64
+
65
+ return destFile ;
129
66
}
130
67
}
131
68
}
0 commit comments