@@ -38,9 +38,7 @@ static VisualStudioEditor()
3838 if ( ! UnityInstallation . IsMainUnityEditorProcess )
3939 return ;
4040
41- if ( IsWindows )
42- Discovery . FindVSWhere ( ) ;
43-
41+ Discovery . Initialize ( ) ;
4442 CodeEditor . Register ( new VisualStudioEditor ( ) ) ;
4543
4644 _discoverInstallations = AsyncOperation < IVisualStudioInstallation [ ] > . Run ( DiscoverInstallations ) ;
@@ -193,38 +191,23 @@ bool IsSupportedPath(string path)
193191 return false ;
194192 }
195193
196- private static void CheckCurrentEditorInstallation ( )
194+ public bool OpenProject ( string path , int line , int column )
197195 {
198196 var editorPath = CodeEditor . CurrentEditorInstallation ;
199- try
200- {
201- if ( Discovery . TryDiscoverInstallation ( editorPath , out _ ) )
202- return ;
203- }
204- catch ( IOException )
205- {
206- }
207197
208- Debug . LogWarning ( $ "Visual Studio executable { editorPath } is not found. Please change your settings in Edit > Preferences > External Tools.") ;
209- }
210-
211- public bool OpenProject ( string path , int line , int column )
212- {
213- CheckCurrentEditorInstallation ( ) ;
198+ if ( ! Discovery . TryDiscoverInstallation ( editorPath , out var installation ) ) {
199+ Debug . LogWarning ( $ "Visual Studio executable { editorPath } is not found. Please change your settings in Edit > Preferences > External Tools.") ;
200+ return false ;
201+ }
214202
215203 if ( ! IsSupportedPath ( path ) )
216204 return false ;
217205
218206 if ( ! IsProjectGeneratedFor ( path , out var missingFlag ) )
219207 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.") ;
220208
221- if ( IsOSX )
222- return OpenOSXApp ( path , line , column ) ;
223-
224- if ( IsWindows )
225- return OpenWindowsApp ( path , line ) ;
226-
227- return false ;
209+ var solution = GetOrGenerateSolutionFile ( path ) ;
210+ return installation . Open ( path , line , column , solution ) ;
228211 }
229212
230213 private static string GetProjectGenerationFlagDescription ( ProjectGenerationFlag flag )
@@ -287,114 +270,6 @@ private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missin
287270 return false ;
288271 }
289272
290- private enum COMIntegrationState
291- {
292- Running ,
293- DisplayProgressBar ,
294- ClearProgressBar ,
295- Exited
296- }
297-
298- private bool OpenWindowsApp ( string path , int line )
299- {
300- var progpath = FileUtility . GetPackageAssetFullPath ( "Editor" , "COMIntegration" , "Release" , "COMIntegration.exe" ) ;
301-
302- if ( string . IsNullOrWhiteSpace ( progpath ) )
303- return false ;
304-
305- string absolutePath = "" ;
306- if ( ! string . IsNullOrWhiteSpace ( path ) )
307- {
308- absolutePath = Path . GetFullPath ( path ) ;
309- }
310-
311- // We remove all invalid chars from the solution filename, but we cannot prevent the user from using a specific path for the Unity project
312- // So process the fullpath to make it compatible with VS
313- var solution = GetOrGenerateSolutionFile ( path ) ;
314- if ( ! string . IsNullOrWhiteSpace ( solution ) )
315- {
316- solution = $ "\" { solution } \" ";
317- solution = solution . Replace ( "^" , "^^" ) ;
318- }
319-
320-
321- var psi = ProcessRunner . ProcessStartInfoFor ( progpath , $ "\" { CodeEditor . CurrentEditorInstallation } \" { solution } \" { absolutePath } \" { line } ") ;
322- psi . StandardOutputEncoding = System . Text . Encoding . Unicode ;
323- psi . StandardErrorEncoding = System . Text . Encoding . Unicode ;
324-
325- // inter thread communication
326- var messages = new BlockingCollection < COMIntegrationState > ( ) ;
327-
328- var asyncStart = AsyncOperation < ProcessRunnerResult > . Run (
329- ( ) => ProcessRunner . StartAndWaitForExit ( psi , onOutputReceived : data => OnOutputReceived ( data , messages ) ) ,
330- e => new ProcessRunnerResult { Success = false , Error = e . Message , Output = string . Empty } ,
331- ( ) => messages . Add ( COMIntegrationState . Exited )
332- ) ;
333-
334- MonitorCOMIntegration ( messages ) ;
335-
336- var result = asyncStart . Result ;
337-
338- if ( ! result . Success && ! string . IsNullOrWhiteSpace ( result . Error ) )
339- Debug . LogError ( $ "Error while starting Visual Studio: { result . Error } ") ;
340-
341- return result . Success ;
342- }
343-
344- private static void MonitorCOMIntegration ( BlockingCollection < COMIntegrationState > messages )
345- {
346- var displayingProgress = false ;
347- COMIntegrationState state ;
348-
349- do
350- {
351- state = messages . Take ( ) ;
352- switch ( state )
353- {
354- case COMIntegrationState . ClearProgressBar :
355- EditorUtility . ClearProgressBar ( ) ;
356- displayingProgress = false ;
357- break ;
358- case COMIntegrationState . DisplayProgressBar :
359- EditorUtility . DisplayProgressBar ( "Opening Visual Studio" , "Starting up Visual Studio, this might take some time." , .5f ) ;
360- displayingProgress = true ;
361- break ;
362- }
363- } while ( state != COMIntegrationState . Exited ) ;
364-
365- // Make sure the progress bar is properly cleared in case of COMIntegration failure
366- if ( displayingProgress )
367- EditorUtility . ClearProgressBar ( ) ;
368- }
369-
370- private static readonly COMIntegrationState [ ] ProgressBarCommands = { COMIntegrationState . DisplayProgressBar , COMIntegrationState . ClearProgressBar } ;
371- private static void OnOutputReceived ( string data , BlockingCollection < COMIntegrationState > messages )
372- {
373- if ( data == null )
374- return ;
375-
376- foreach ( var cmd in ProgressBarCommands )
377- {
378- if ( data . IndexOf ( cmd . ToString ( ) , StringComparison . OrdinalIgnoreCase ) >= 0 )
379- messages . Add ( cmd ) ;
380- }
381- }
382-
383- [ DllImport ( "AppleEventIntegration" ) ]
384- static extern bool OpenVisualStudio ( string appPath , string solutionPath , string filePath , int line ) ;
385-
386- bool OpenOSXApp ( string path , int line , int column )
387- {
388- string absolutePath = "" ;
389- if ( ! string . IsNullOrWhiteSpace ( path ) )
390- {
391- absolutePath = Path . GetFullPath ( path ) ;
392- }
393-
394- var solution = GetOrGenerateSolutionFile ( path ) ;
395- return OpenVisualStudio ( CodeEditor . CurrentEditorInstallation , solution , absolutePath , line ) ;
396- }
397-
398273 private string GetOrGenerateSolutionFile ( string path )
399274 {
400275 _generator . Sync ( ) ;
0 commit comments