77using ICSharpCode . NRefactory . Visitors ;
88using System . IO ;
99using System . Collections . Generic ;
10+ using System . Linq ;
1011using System . Text . RegularExpressions ;
12+ using JetBrains . Annotations ;
1113using UnityEditor . Modules ;
14+ using UnityEditor . Scripting . ScriptCompilation ;
15+ using UnityEngine ;
1216
1317namespace UnityEditor . Scripting . Compilers
1418{
@@ -52,22 +56,58 @@ public override bool CompilerRequiresAdditionalReferences()
5256 return true ;
5357 }
5458
55- public override string GetNamespace ( string fileName , string definedSymbols )
59+ public string GetNamespaceNewRuntime ( string filePath , string definedSymbols )
5660 {
57- using ( var parser = ParserFactory . CreateParser ( ICSharpCode . NRefactory . SupportedLanguage . CSharp , ReadAndConverteNewLines ( fileName ) ) )
61+ var definedSymbolSplit = definedSymbols . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
62+ string [ ] defines = null ;
63+ var responseFilePath = Path . Combine ( "Assets" , MonoCSharpCompiler . ReponseFilename ) ;
64+ try
65+ {
66+ var responseFileData = ScriptCompilerBase . ParseResponseFileFromFile ( responseFilePath ) ;
67+ defines = new string [ responseFileData . Defines . Length + definedSymbolSplit . Length ] ;
68+ Array . Copy ( definedSymbolSplit , defines , definedSymbolSplit . Length ) ;
69+ Array . Copy ( responseFileData . Defines , 0 , defines , definedSymbolSplit . Length , responseFileData . Defines . Length ) ;
70+ }
71+ catch ( Exception e )
72+ {
73+ Debug . LogException ( e ) ;
74+ }
75+
76+ var uniqueSymbols = new HashSet < string > ( defines ?? definedSymbolSplit ) ;
77+ return CSharpNamespaceParser . GetNamespace ( ReadAndConverteNewLines ( filePath ) . ReadToEnd ( ) , Path . GetFileNameWithoutExtension ( filePath ) , uniqueSymbols . ToArray ( ) ) ;
78+ }
79+
80+ public string GetNamespaceOldRuntime ( string filePath , string definedSymbols )
81+ {
82+ var definedSymbolSplit = definedSymbols . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
83+ string [ ] defines = null ;
84+ var responseFilePath = Path . Combine ( "Assets" , MonoCSharpCompiler . ReponseFilename ) ;
85+ try
86+ {
87+ var responseFileData = ScriptCompilerBase . ParseResponseFileFromFile ( responseFilePath ) ;
88+ defines = new string [ responseFileData . Defines . Length + definedSymbolSplit . Length ] ;
89+ Array . Copy ( definedSymbolSplit , defines , definedSymbolSplit . Length ) ;
90+ Array . Copy ( responseFileData . Defines , 0 , defines , definedSymbolSplit . Length , responseFileData . Defines . Length ) ;
91+ }
92+ catch ( Exception e )
93+ {
94+ Debug . LogException ( e ) ;
95+ }
96+
97+ var uniqueSymbols = new HashSet < string > ( defines ?? definedSymbolSplit ) ;
98+ using ( var parser = ParserFactory . CreateParser ( ICSharpCode . NRefactory . SupportedLanguage . CSharp , ReadAndConverteNewLines ( filePath ) ) )
5899 {
59- var uniqueSymbols = new HashSet < string > ( definedSymbols . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ) ;
60100 foreach ( var symbol in uniqueSymbols )
61101 {
62102 parser . Lexer . ConditionalCompilationSymbols . Add ( symbol , string . Empty ) ;
63103 }
64- parser . Lexer . EvaluateConditionalCompilation = true ;
65104
105+ parser . Lexer . EvaluateConditionalCompilation = true ;
66106 parser . Parse ( ) ;
67107 try
68108 {
69109 var visitor = new NamespaceVisitor ( ) ;
70- VisitorData data = new VisitorData ( ) { TargetClassName = Path . GetFileNameWithoutExtension ( fileName ) } ;
110+ var data = new VisitorData { TargetClassName = Path . GetFileNameWithoutExtension ( filePath ) } ;
71111 parser . CompilationUnit . AcceptVisitor ( visitor , data ) ;
72112 return string . IsNullOrEmpty ( data . DiscoveredNamespace ) ? string . Empty : data . DiscoveredNamespace ;
73113 }
@@ -79,12 +119,24 @@ public override string GetNamespace(string fileName, string definedSymbols)
79119 return string . Empty ;
80120 }
81121
122+ public override string GetNamespace ( string filePath , string definedSymbols )
123+ {
124+ if ( EditorApplication . scriptingRuntimeVersion == ScriptingRuntimeVersion . Latest )
125+ {
126+ return GetNamespaceNewRuntime ( filePath , definedSymbols ) ;
127+ }
128+ else
129+ {
130+ return GetNamespaceOldRuntime ( filePath , definedSymbols ) ;
131+ }
132+ }
133+
82134 // TODO: Revisit this code and switch to version 5.5.1 (or Roslyn if possible) when Editor switches to newer runtime version (on going work expected
83135 // to finish around 2017.2 or 2017.3 release.
84136 //
85137 // This is a workaround for a bug in version 3.2.1 of NRefactory in which it fails to parse sources with a combination of LF / #if / #else
86138 // Version 5.5.1 is confirmed to not have this bug but we can't use it since it requires a newer runtime/c# version;
87- private static StringReader ReadAndConverteNewLines ( string filePath )
139+ static StringReader ReadAndConverteNewLines ( string filePath )
88140 {
89141 var text = File . ReadAllText ( filePath ) ;
90142
@@ -105,27 +157,25 @@ public VisitorData()
105157 public Stack < string > CurrentNamespaces ;
106158 public string DiscoveredNamespace ;
107159 }
108-
109160 class NamespaceVisitor : AbstractAstVisitor
110161 {
111162 public override object VisitNamespaceDeclaration ( ICSharpCode . NRefactory . Ast . NamespaceDeclaration namespaceDeclaration , object data )
112163 {
113- VisitorData visitorData = ( VisitorData ) data ;
164+ var visitorData = ( VisitorData ) data ;
114165 visitorData . CurrentNamespaces . Push ( namespaceDeclaration . Name ) ;
115166 // Visit children (E.g. TypeDcelarion objects)
116167 namespaceDeclaration . AcceptChildren ( this , visitorData ) ;
117168 visitorData . CurrentNamespaces . Pop ( ) ;
118-
119169 return null ;
120170 }
121171
122172 public override object VisitTypeDeclaration ( ICSharpCode . NRefactory . Ast . TypeDeclaration typeDeclaration , object data )
123173 {
124- VisitorData visitorData = ( VisitorData ) data ;
174+ var visitorData = ( VisitorData ) data ;
125175 if ( typeDeclaration . Name == visitorData . TargetClassName )
126176 {
127- string fullNamespace = string . Empty ;
128- foreach ( string ns in visitorData . CurrentNamespaces )
177+ var fullNamespace = string . Empty ;
178+ foreach ( var ns in visitorData . CurrentNamespaces )
129179 {
130180 if ( fullNamespace == string . Empty )
131181 fullNamespace = ns ;
0 commit comments