66using System ;
77using System . IO ;
88using System . Linq ;
9- using System . Runtime . InteropServices ;
109using System . Runtime . CompilerServices ;
1110using UnityEditor ;
1211using UnityEngine ;
1312using Unity . CodeEditor ;
14- using System . Collections . Concurrent ;
1513
1614[ assembly: InternalsVisibleTo ( "Unity.VisualStudio.EditorTests" ) ]
1715[ assembly: InternalsVisibleTo ( "Unity.VisualStudio.Standalone.EditorTests" ) ]
@@ -31,8 +29,6 @@ public class VisualStudioEditor : IExternalCodeEditor
3129
3230 private static readonly AsyncOperation < IVisualStudioInstallation [ ] > _discoverInstallations ;
3331
34- private readonly IGenerator _generator = new LegacyStyleProjectGeneration ( ) ;
35-
3632 static VisualStudioEditor ( )
3733 {
3834 if ( ! UnityInstallation . IsMainUnityEditorProcess )
@@ -54,7 +50,7 @@ private static IVisualStudioInstallation[] DiscoverInstallations()
5450 }
5551 catch ( Exception ex )
5652 {
57- UnityEngine . Debug . LogError ( $ "Error detecting Visual Studio installations: { ex } ") ;
53+ Debug . LogError ( $ "Error detecting Visual Studio installations: { ex } ") ;
5854 return Array . Empty < IVisualStudioInstallation > ( ) ;
5955 }
6056 }
@@ -65,8 +61,12 @@ private static IVisualStudioInstallation[] DiscoverInstallations()
6561 // keeping it for now given it is public, so we need a major bump to remove it
6662 public void CreateIfDoesntExist ( )
6763 {
68- if ( ! _generator . HasSolutionBeenGenerated ( ) )
69- _generator . Sync ( ) ;
64+ if ( ! TryGetVisualStudioInstallationForPath ( CodeEditor . CurrentEditorInstallation , true , out var installation ) )
65+ return ;
66+
67+ var generator = installation . ProjectGenerator ;
68+ if ( ! generator . HasSolutionBeenGenerated ( ) )
69+ generator . Sync ( ) ;
7070 }
7171
7272 public void Initialize ( string editorInstallationPath )
@@ -94,7 +94,7 @@ internal virtual bool TryGetVisualStudioInstallationForPath(string editorPath, b
9494 public virtual bool TryGetInstallationForPath ( string editorPath , out CodeEditor . Installation installation )
9595 {
9696 var result = TryGetVisualStudioInstallationForPath ( editorPath , searchInstallations : false , out var vsi ) ;
97- installation = vsi == null ? default : vsi . ToCodeEditorInstallation ( ) ;
97+ installation = vsi ? . ToCodeEditorInstallation ( ) ?? default ;
9898 return result ;
9999 }
100100
@@ -128,29 +128,39 @@ public void OnGUI()
128128 EditorGUI . indentLevel -- ;
129129 }
130130
131- void RegenerateProjectFiles ( )
131+ private void RegenerateProjectFiles ( )
132132 {
133- var rect = EditorGUI . IndentedRect ( EditorGUILayout . GetControlRect ( new GUILayoutOption [ ] { } ) ) ;
133+ var rect = EditorGUI . IndentedRect ( EditorGUILayout . GetControlRect ( ) ) ;
134134 rect . width = 252 ;
135135 if ( GUI . Button ( rect , "Regenerate project files" ) )
136136 {
137- _generator . Sync ( ) ;
137+ if ( TryGetVisualStudioInstallationForPath ( CodeEditor . CurrentEditorInstallation , true , out var installation ) )
138+ {
139+ installation . ProjectGenerator . Sync ( ) ;
140+ }
138141 }
139142 }
140143
141- void SettingsButton ( ProjectGenerationFlag preference , string guiMessage , string toolTip )
144+ private void SettingsButton ( ProjectGenerationFlag preference , string guiMessage , string toolTip )
142145 {
143- var prevValue = _generator . AssemblyNameProvider . ProjectGenerationFlag . HasFlag ( preference ) ;
144- var newValue = EditorGUILayout . Toggle ( new GUIContent ( guiMessage , toolTip ) , prevValue ) ;
145- if ( newValue != prevValue )
146+ if ( TryGetVisualStudioInstallationForPath ( CodeEditor . CurrentEditorInstallation , true , out var installation ) )
146147 {
147- _generator . AssemblyNameProvider . ToggleProjectGeneration ( preference ) ;
148+ var generator = installation . ProjectGenerator ;
149+ var prevValue = generator . AssemblyNameProvider . ProjectGenerationFlag . HasFlag ( preference ) ;
150+ var newValue = EditorGUILayout . Toggle ( new GUIContent ( guiMessage , toolTip ) , prevValue ) ;
151+ if ( newValue != prevValue )
152+ {
153+ generator . AssemblyNameProvider . ToggleProjectGeneration ( preference ) ;
154+ }
148155 }
149156 }
150157
151158 public void SyncIfNeeded ( string [ ] addedFiles , string [ ] deletedFiles , string [ ] movedFiles , string [ ] movedFromFiles , string [ ] importedFiles )
152159 {
153- _generator . SyncIfNeeded ( addedFiles . Union ( deletedFiles ) . Union ( movedFiles ) . Union ( movedFromFiles ) , importedFiles ) ;
160+ if ( TryGetVisualStudioInstallationForPath ( CodeEditor . CurrentEditorInstallation , true , out var installation ) )
161+ {
162+ installation . ProjectGenerator . SyncIfNeeded ( addedFiles . Union ( deletedFiles ) . Union ( movedFiles ) . Union ( movedFromFiles ) , importedFiles ) ;
163+ }
154164
155165 foreach ( var file in importedFiles . Where ( a => Path . GetExtension ( a ) == ".pdb" ) )
156166 {
@@ -173,10 +183,13 @@ public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] mo
173183
174184 public void SyncAll ( )
175185 {
176- _generator . Sync ( ) ;
186+ if ( TryGetVisualStudioInstallationForPath ( CodeEditor . CurrentEditorInstallation , true , out var installation ) )
187+ {
188+ installation . ProjectGenerator . Sync ( ) ;
189+ }
177190 }
178191
179- bool IsSupportedPath ( string path )
192+ private static bool IsSupportedPath ( string path , IGenerator generator )
180193 {
181194 // Path is empty with "Open C# Project", as we only want to open the solution without specific files
182195 if ( string . IsNullOrEmpty ( path ) )
@@ -185,10 +198,7 @@ bool IsSupportedPath(string path)
185198 // cs, uxml, uss, shader, compute, cginc, hlsl, glslinc, template are part of Unity builtin extensions
186199 // txt, xml, fnt, cd are -often- par of Unity user extensions
187200 // asdmdef is mandatory included
188- if ( _generator . IsSupportedFile ( path ) )
189- return true ;
190-
191- return false ;
201+ return generator . IsSupportedFile ( path ) ;
192202 }
193203
194204 public bool OpenProject ( string path , int line , int column )
@@ -200,13 +210,14 @@ public bool OpenProject(string path, int line, int column)
200210 return false ;
201211 }
202212
203- if ( ! IsSupportedPath ( path ) )
213+ var generator = installation . ProjectGenerator ;
214+ if ( ! IsSupportedPath ( path , generator ) )
204215 return false ;
205216
206- if ( ! IsProjectGeneratedFor ( path , out var missingFlag ) )
217+ if ( ! IsProjectGeneratedFor ( path , generator , out var missingFlag ) )
207218 Debug . LogWarning ( $ "You are trying to open { path } outside a generated project. This might cause problems with IntelliSense and debugging. To avoid this, you can change your .csproj preferences in Edit > Preferences > External Tools and enable { GetProjectGenerationFlagDescription ( missingFlag ) } generation.") ;
208219
209- var solution = GetOrGenerateSolutionFile ( path ) ;
220+ var solution = GetOrGenerateSolutionFile ( generator ) ;
210221 return installation . Open ( path , line , column , solution ) ;
211222 }
212223
@@ -235,7 +246,7 @@ private static string GetProjectGenerationFlagDescription(ProjectGenerationFlag
235246 }
236247 }
237248
238- private bool IsProjectGeneratedFor ( string path , out ProjectGenerationFlag missingFlag )
249+ private static bool IsProjectGeneratedFor ( string path , IGenerator generator , out ProjectGenerationFlag missingFlag )
239250 {
240251 missingFlag = ProjectGenerationFlag . None ;
241252
@@ -248,9 +259,9 @@ private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missin
248259 return true ;
249260
250261 // Even on windows, the package manager requires relative path + unix style separators for queries
251- var basePath = _generator . ProjectDirectory ;
252- var relativePath = FileUtility
253- . NormalizeWindowsToUnix ( path )
262+ var basePath = generator . ProjectDirectory ;
263+ var relativePath = path
264+ . NormalizeWindowsToUnix ( )
254265 . Replace ( basePath , string . Empty )
255266 . Trim ( FileUtility . UnixSeparator ) ;
256267
@@ -262,18 +273,18 @@ private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missin
262273 if ( ! Enum . TryParse < ProjectGenerationFlag > ( source . ToString ( ) , out var flag ) )
263274 return true ;
264275
265- if ( _generator . AssemblyNameProvider . ProjectGenerationFlag . HasFlag ( flag ) )
276+ if ( generator . AssemblyNameProvider . ProjectGenerationFlag . HasFlag ( flag ) )
266277 return true ;
267278
268279 // Return false if we found a source not flagged for generation
269280 missingFlag = flag ;
270281 return false ;
271282 }
272283
273- private string GetOrGenerateSolutionFile ( string path )
284+ private static string GetOrGenerateSolutionFile ( IGenerator generator )
274285 {
275- _generator . Sync ( ) ;
276- return _generator . SolutionFile ( ) ;
286+ generator . Sync ( ) ;
287+ return generator . SolutionFile ( ) ;
277288 }
278289 }
279290}
0 commit comments