@@ -54,19 +54,27 @@ public static string GetPortableDllPath(string asmdefPath)
5454 . FirstOrDefault ( x => Regex . IsMatch ( x , "CSharpCompilerSettings_[0-9a-zA-Z]{32}.dll" ) ) ;
5555 }
5656
57-
5857 private static bool IsInSameDirectory ( string path )
5958 {
6059 if ( string . IsNullOrEmpty ( path ) ) return false ;
6160
62- var dir = Path . GetFullPath ( Path . GetDirectoryName ( path ) ) ;
63- var coreAssemblyLocationDir = Path . GetFullPath ( Path . GetDirectoryName ( typeof ( Core ) . Assembly . Location ) ) ;
61+ var dir = Path . GetFullPath ( Path . GetDirectoryName ( path ) ) . ToLower ( ) ;
62+ var coreAssemblyLocationDir = Path . GetFullPath ( Path . GetDirectoryName ( typeof ( Core ) . Assembly . Location ) ) . ToLower ( ) ;
6463 return dir == coreAssemblyLocationDir ;
6564 }
6665
67- private static string FindAsmdef ( )
66+ private static string FindAsmdef ( string path = null )
6867 {
69- var asmdefPath = Directory . GetFiles ( Path . GetDirectoryName ( typeof ( Core ) . Assembly . Location ) , "*.asmdef" )
68+ if ( string . IsNullOrEmpty ( path ) )
69+ {
70+ var assemblyPath = typeof ( Core ) . Assembly . Location ;
71+ assemblyPath = AssetDatabase . FindAssets ( "t:DefaultAsset " + Path . GetFileNameWithoutExtension ( assemblyPath ) )
72+ . Select ( AssetDatabase . GUIDToAssetPath )
73+ . FirstOrDefault ( x => x . EndsWith ( ".dll" ) ) ;
74+ path = Path . GetDirectoryName ( assemblyPath ) ;
75+ }
76+
77+ var asmdefPath = Directory . GetFiles ( path , "*.asmdef" )
7078 . FirstOrDefault ( x => x . EndsWith ( ".asmdef" ) ) ?? "" ;
7179
7280 return asmdefPath . Replace ( '\\ ' , '/' ) . Replace ( Environment . CurrentDirectory . Replace ( '\\ ' , '/' ) + "/" , "" ) ;
@@ -89,7 +97,8 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
8997 var command = oldCommand . Replace ( EditorApplication . applicationContentsPath . Replace ( '\\ ' , '/' ) , "@APP_CONTENTS@" ) ;
9098 var isDefaultCsc = Regex . IsMatch ( command , "@APP_CONTENTS@/[^ ]*(mcs|csc)" ) ;
9199 var assemblyName = Path . GetFileNameWithoutExtension ( scriptAssembly . Get ( "Filename" ) as string ) ;
92- var originPath = scriptAssembly . Get ( "OriginPath" ) as string ;
100+ var asmdefDir = scriptAssembly . Get ( "OriginPath" ) as string ;
101+ var asmdefPath = string . IsNullOrEmpty ( asmdefDir ) ? "" : FindAsmdef ( asmdefDir ) ;
93102
94103 // csc is not Unity default. It is already modified.
95104 if ( ! isDefaultCsc )
@@ -101,62 +110,72 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
101110 // Kill current process.
102111 compiler . Call ( "Dispose" ) ;
103112
104- var compilerInfo = CompilerInfo . GetInstalledInfo ( setting . CompilerPackage . PackageId ) ;
105-
106- // csc is not installed. Restart current process.
107- if ( ! compilerInfo . IsValid )
108- {
109- Logger . LogWarning ( " <color=#bbbb44><Skipped> C# compiler '{0}' is not installed. Restart compiler process: {1}</color>" , compilerInfo . Path , oldCommand ) ;
110-
111- var currentProgram = tProgram . New ( psi ) ;
112- currentProgram . Call ( "Start" ) ;
113- compiler . Set ( "process" , currentProgram , fiProcess ) ;
114- return ;
115- }
116-
117- // Change exe file path.
113+ // Response file.
118114 var responseFile = Regex . Replace ( psi . Arguments , "^.*@(.+)$" , "$1" ) ;
119- compilerInfo . Setup ( psi , responseFile ) ;
120-
121115 var text = File . ReadAllText ( responseFile ) ;
122116 text = Regex . Replace ( text , "[\r \n ]+" , "\n " ) ;
123117 text = Regex . Replace ( text , "^-" , "/" ) ;
124- text = Regex . Replace ( text , "\n /langversion:[^\n ]+\n " , "\n /langversion:" + setting . LanguageVersion + "\n " ) ;
125118 text = Regex . Replace ( text , "\n /debug\n " , "\n /debug:portable\n " ) ;
126119 text += "\n /preferreduilang:en-US" ;
127120
128- // Nullable.
129- text += "\n /nullable:" + setting . Nullable . ToString ( ) . ToLower ( ) ;
121+ // Compiler
122+ if ( ! setting . UseDefaultCompiler )
123+ {
124+ var compilerInfo = CompilerInfo . GetInstalledInfo ( setting . CompilerPackage . PackageId ) ;
125+
126+ // csc is not installed. Restart current process.
127+ if ( ! compilerInfo . IsValid )
128+ {
129+ Logger . LogWarning ( " <color=#bbbb44><Skipped> C# compiler '{0}' is not installed. Restart compiler process: {1}</color>" , compilerInfo . Path , oldCommand ) ;
130+
131+ var currentProgram = tProgram . New ( psi ) ;
132+ currentProgram . Call ( "Start" ) ;
133+ compiler . Set ( "process" , currentProgram , fiProcess ) ;
134+ return ;
135+ }
136+
137+ // Change exe file path.
138+ compilerInfo . Setup ( psi , responseFile ) ;
139+ text = Regex . Replace ( text , "\n /langversion:[^\n ]+\n " , "\n /langversion:" + setting . LanguageVersion + "\n " ) ;
140+
141+ // Nullable.
142+ text += "\n /nullable:" + setting . Nullable . ToString ( ) . ToLower ( ) ;
143+ }
130144
131145 // Modify scripting define symbols.
132- var defines = Regex . Matches ( text , "^/define:(.*)$" , RegexOptions . Multiline )
133- . Cast < Match > ( )
134- . Select ( x => x . Groups [ 1 ] . Value ) ;
135- text = Regex . Replace ( text , "[\r \n ]+/define:[^\r \n ]+" , "" ) ;
136- var modifiedDefines = Utils . ModifySymbols ( defines , setting . AdditionalSymbols ) ;
137- foreach ( var d in modifiedDefines )
138- text += "\n /define:" + d ;
139-
140- // Setup analyzer.
141- foreach ( var package in setting . AnalyzerPackages )
146+ if ( ! string . IsNullOrEmpty ( setting . AdditionalSymbols ) )
142147 {
143- var analyzerInfo = AnalyzerInfo . GetInstalledInfo ( package . PackageId ) ;
144- foreach ( var dll in analyzerInfo . DllFiles )
145- text += string . Format ( "\n /analyzer:\" {0}\" " , dll ) ;
148+ var defines = Regex . Matches ( text , "^/define:(.*)$" , RegexOptions . Multiline )
149+ . Cast < Match > ( )
150+ . Select ( x => x . Groups [ 1 ] . Value ) ;
151+ text = Regex . Replace ( text , "[\r \n ]+/define:[^\r \n ]+" , "" ) ;
152+ var modifiedDefines = Utils . ModifySymbols ( defines , setting . AdditionalSymbols ) ;
153+ foreach ( var d in modifiedDefines )
154+ text += "\n /define:" + d ;
146155 }
147156
148- // Ruleset.
149- var rulesets = new [ ] { "Assets/Default.ruleset" }
150- . Concat ( string . IsNullOrEmpty ( originPath )
151- ? new [ ] { "Assets/" + assemblyName + ".ruleset" }
152- : Directory . GetFiles ( originPath , "*.ruleset" ) )
153- . Where ( File . Exists ) ;
154-
155- foreach ( var ruleset in rulesets )
156- text += string . Format ( "\n /ruleset:\" {0}\" " , ruleset ) ;
157-
157+ // Analyzer.
158+ var globalSettings = CscSettingsAsset . instance ;
159+ if ( globalSettings . ShouldToRecompileToAnalyze ( asmdefPath ) )
160+ {
161+ // Analyzer dlls.
162+ foreach ( var package in globalSettings . AnalyzerPackages )
163+ {
164+ var analyzerInfo = AnalyzerInfo . GetInstalledInfo ( package . PackageId ) ;
165+ foreach ( var dll in analyzerInfo . DllFiles )
166+ text += string . Format ( "\n /analyzer:\" {0}\" " , dll ) ;
167+ }
158168
169+ // Ruleset.
170+ var rulesets = new [ ] { "Assets/Default.ruleset" }
171+ . Concat ( string . IsNullOrEmpty ( asmdefDir )
172+ ? new [ ] { "Assets/" + assemblyName + ".ruleset" }
173+ : Directory . GetFiles ( asmdefDir , "*.ruleset" ) )
174+ . Where ( File . Exists ) ;
159175
176+ foreach ( var ruleset in rulesets )
177+ text += string . Format ( "\n /ruleset:\" {0}\" " , ruleset ) ;
178+ }
160179
161180 // Replace NewLine and save.
162181 text = Regex . Replace ( text , "\n " , Environment . NewLine ) ;
@@ -188,12 +207,13 @@ private static void OnAssemblyCompilationStarted(string name)
188207 var compilerTasks = tEditorCompilationInterface . Get ( "Instance" ) . Get ( "compilationTask" ) . Get ( "compilerTasks" ) as IDictionary ;
189208 var scriptAssembly = compilerTasks . Keys . Cast < object > ( ) . FirstOrDefault ( x => ( x . Get ( "Filename" ) as string ) == assemblyName + ".dll" ) ;
190209 if ( scriptAssembly == null )
210+ {
211+ Logger . LogWarning ( " <color=#bbbb44><Skipped> scriptAssembly <b>'{0}'</b> is not found.</color>" , assemblyName ) ;
191212 return ;
213+ }
192214
193215 var asmdefDir = scriptAssembly . Get ( "OriginPath" ) as string ;
194- var asmdefPath = string . IsNullOrEmpty ( asmdefDir )
195- ? ""
196- : Directory . GetFiles ( asmdefDir , "*.asmdef" ) . First ( ) . Replace ( '\\ ' , '/' ) . Replace ( Environment . CurrentDirectory . Replace ( '\\ ' , '/' ) + "/" , "" ) ;
216+ var asmdefPath = string . IsNullOrEmpty ( asmdefDir ) ? "" : FindAsmdef ( asmdefDir ) ;
197217 if ( IsGlobal && GetPortableDllPath ( asmdefPath ) != null )
198218 {
199219 Logger . LogWarning ( " <color=#bbbb44><Skipped> Local CSharpCompilerSettings.*.dll for <b>'{0}'</b> is found.</color>" , assemblyName ) ;
@@ -206,10 +226,9 @@ private static void OnAssemblyCompilationStarted(string name)
206226 return ;
207227 }
208228
209- var settings = IsGlobal
210- ? CscSettingsAsset . instance
211- : CscSettingsAsset . GetAtPath ( asmdefPath ) ;
212- if ( ! settings || ! settings . ShouldToRecompile )
229+ var globalSettings = CscSettingsAsset . instance ;
230+ var settings = GetSettings ( ) ;
231+ if ( ! settings . ShouldToRecompile && ! globalSettings . ShouldToRecompileToAnalyze ( asmdefPath ) )
213232 {
214233 Logger . LogWarning ( " <color=#bbbb44><Skipped> Assembly <b>'{0}'</b> does not need to be recompiled.</color>" , assemblyName ) ;
215234 return ;
@@ -276,11 +295,12 @@ static Core()
276295
277296 // Install custom csc before compilation.
278297 var settings = GetSettings ( ) ;
279- if ( ! settings || settings . UseDefaultCompiler ) return ;
280- CompilerInfo . GetInstalledInfo ( settings . CompilerPackage . PackageId ) ;
298+ if ( ! settings . UseDefaultCompiler )
299+ CompilerInfo . GetInstalledInfo ( settings . CompilerPackage . PackageId ) ;
281300
282- foreach ( var package in settings . AnalyzerPackages )
283- AnalyzerInfo . GetInstalledInfo ( package . PackageId ) ;
301+ if ( IsGlobal )
302+ foreach ( var package in settings . AnalyzerPackages . Where ( x => x . IsValid ) )
303+ AnalyzerInfo . GetInstalledInfo ( package . PackageId ) ;
284304
285305 // If Unity 2020.2 or newer, request re-compilation.
286306 var version = Application . unityVersion . Split ( '.' ) ;
0 commit comments