@@ -262,9 +262,8 @@ private async void MetroWindow_Closing(object sender, CancelEventArgs e)
262262 if ( ! ClosingBuffer )
263263 {
264264 // Close directly if no files need to be saved
265- var editors = GetAllEditorElements ( ) ? . ToList ( ) ;
266265
267- if ( editors == null || ( editors != null && ! editors . Any ( x => x . NeedsSave ) ) || Program . OptionsObject . ActionOnClose != ActionOnClose . Prompt )
266+ if ( ! EditorReferences . Any ( ) || ! EditorReferences . Any ( x => x . NeedsSave ) || Program . OptionsObject . ActionOnClose != ActionOnClose . Prompt )
268267 {
269268 ClosingBuffer = true ;
270269 CloseProgram ( true ) ;
@@ -277,7 +276,7 @@ private async void MetroWindow_Closing(object sender, CancelEventArgs e)
277276 // Build list of unsaved files to show
278277 var sb = new StringBuilder ( ) ;
279278
280- foreach ( var editor in editors . Where ( x => x . NeedsSave ) )
279+ foreach ( var editor in EditorReferences . Where ( x => x . NeedsSave ) )
281280 {
282281 sb . AppendLine ( $ " - { editor . Parent . Title . Substring ( 1 ) } ") ;
283282 }
@@ -345,99 +344,98 @@ public bool TryLoadSourceFile(string filePath, out EditorElement outEditor, bool
345344 {
346345 outEditor = null ;
347346 var fileInfo = new FileInfo ( filePath ) ;
348- if ( fileInfo . Exists )
347+
348+ if ( ! fileInfo . Exists )
349+ {
350+ return false ;
351+ }
352+
353+ if ( DirHelper . HasValidTextExtension ( fileInfo ) )
349354 {
350- if ( fileInfo . Extension == ".sp" ||
351- fileInfo . Extension == ".inc" ||
352- fileInfo . Extension == ".txt" ||
353- fileInfo . Extension == ".cfg" ||
354- fileInfo . Extension == ".ini" )
355+ var finalPath = fileInfo . FullName ;
356+ if ( ! DirHelper . CanAccess ( finalPath ) )
355357 {
356- var finalPath = fileInfo . FullName ;
357- if ( ! DirUtils . CanAccess ( finalPath ) )
358- {
359- return false ;
360- }
358+ return false ;
359+ }
361360
362- var editors = GetAllEditorElements ( ) ;
363- if ( editors != null )
361+ if ( EditorReferences . Any ( ) )
362+ {
363+ foreach ( var editor in EditorReferences )
364364 {
365- foreach ( var editor in editors )
365+ if ( editor . FullFilePath == finalPath )
366366 {
367- if ( editor . FullFilePath == finalPath )
367+ if ( SelectMe )
368368 {
369- if ( SelectMe )
370- {
371- editor . Parent . IsSelected = true ;
372- editor . editor . TextArea . Caret . Show ( ) ;
373- EditorToFocus = editor ;
374- SelectDocumentTimer . Start ( ) ;
375- }
376-
377- outEditor = editor ;
378- return true ;
369+ editor . Parent . IsSelected = true ;
370+ editor . editor . TextArea . Caret . Show ( ) ;
371+ EditorToFocus = editor ;
372+ SelectDocumentTimer . Start ( ) ;
379373 }
374+
375+ outEditor = editor ;
376+ return true ;
380377 }
381378 }
379+ }
382380
383- AddEditorElement ( fileInfo , fileInfo . Name , SelectMe , out outEditor ) ;
384- if ( TryOpenIncludes && Program . OptionsObject . Program_OpenCustomIncludes )
381+ AddEditorElement ( fileInfo , fileInfo . Name , SelectMe , out outEditor ) ;
382+ if ( TryOpenIncludes && Program . OptionsObject . Program_OpenCustomIncludes )
383+ {
384+ using var textReader = fileInfo . OpenText ( ) ;
385+ var source = Regex . Replace ( textReader . ReadToEnd ( ) , @"/\*.*?\*/" , string . Empty ,
386+ RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Singleline ) ;
387+ var regex = new Regex ( @"^\s*\#include\s+((\<|"")(?<name>.+?)(\>|""))" ,
388+ RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Multiline ) ;
389+ var mc = regex . Matches ( source ) ;
390+ for ( var i = 0 ; i < mc . Count ; ++ i )
385391 {
386- using var textReader = fileInfo . OpenText ( ) ;
387- var source = Regex . Replace ( textReader . ReadToEnd ( ) , @"/\*.*?\*/" , string . Empty ,
388- RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Singleline ) ;
389- var regex = new Regex ( @"^\s*\#include\s+((\<|"")(?<name>.+?)(\>|""))" ,
390- RegexOptions . Compiled | RegexOptions . ExplicitCapture | RegexOptions . Multiline ) ;
391- var mc = regex . Matches ( source ) ;
392- for ( var i = 0 ; i < mc . Count ; ++ i )
392+ try
393393 {
394- try
394+ var fileName = mc [ i ] . Groups [ "name" ] . Value ;
395+ if ( ! ( fileName . EndsWith ( ".inc" , StringComparison . InvariantCultureIgnoreCase ) ||
396+ fileName . EndsWith ( ".sp" , StringComparison . InvariantCultureIgnoreCase ) ) )
395397 {
396- var fileName = mc [ i ] . Groups [ "name" ] . Value ;
397- if ( ! ( fileName . EndsWith ( ".inc" , StringComparison . InvariantCultureIgnoreCase ) ||
398- fileName . EndsWith ( ".sp" , StringComparison . InvariantCultureIgnoreCase ) ) )
399- {
400- fileName += ".inc" ;
401- }
402-
403- fileName = Path . Combine (
404- fileInfo . DirectoryName ?? throw new NullReferenceException ( ) , fileName ) ;
405- TryLoadSourceFile ( fileName , out _ , false ,
406- Program . OptionsObject . Program_OpenIncludesRecursively ) ;
407- }
408- catch ( Exception )
409- {
410- // ignored
398+ fileName += ".inc" ;
411399 }
400+
401+ fileName = Path . Combine (
402+ fileInfo . DirectoryName ?? throw new NullReferenceException ( ) , fileName ) ;
403+ TryLoadSourceFile ( fileName , out _ , false ,
404+ Program . OptionsObject . Program_OpenIncludesRecursively ) ;
405+ }
406+ catch ( Exception )
407+ {
408+ // ignored
412409 }
413410 }
414411 }
415- else if ( fileInfo . Extension == ".smx" )
412+ }
413+ else if ( DirHelper . IsBinary ( fileInfo ) )
414+ {
415+ if ( DASMReferences . Any ( ) )
416416 {
417- var viewers = GetAllDASMElements ( ) ;
418- if ( viewers != null )
417+ foreach ( var dasmviewer in DASMReferences )
419418 {
420- foreach ( var dasmviewer in viewers )
419+ if ( dasmviewer . FilePath == fileInfo . FullName )
421420 {
422- if ( dasmviewer . FilePath == fileInfo . FullName )
423- {
424- DockingManager . ActiveContent = dasmviewer ;
425- return true ;
426- }
421+ DockingManager . ActiveContent = dasmviewer ;
422+ return true ;
427423 }
428424 }
429- AddDASMElement ( fileInfo ) ;
430- }
431-
432- if ( UseBlendoverEffect )
433- {
434- BlendOverEffect . Begin ( ) ;
435425 }
426+ AddDASMElement ( fileInfo ) ;
427+ }
428+ else
429+ {
430+ return false ;
431+ }
436432
437- return true ;
433+ if ( UseBlendoverEffect )
434+ {
435+ BlendOverEffect . Begin ( ) ;
438436 }
439437
440- return false ;
438+ return true ;
441439 }
442440
443441 /// <summary>
@@ -549,9 +547,8 @@ private void CloseProgram(bool saveAll)
549547 {
550548 // Save all the last open files
551549 var lastOpenFiles = new List < string > ( ) ;
552- var editors = GetAllEditorElements ( ) ? . ToList ( ) ;
553550
554- editors ? . ForEach ( x =>
551+ EditorReferences . ForEach ( x =>
555552 {
556553 if ( File . Exists ( x . FullFilePath ) )
557554 {
@@ -562,11 +559,11 @@ private void CloseProgram(bool saveAll)
562559
563560 if ( saveAll )
564561 {
565- editors ? . ForEach ( x => x . Close ( true ) ) ;
562+ EditorReferences . ForEach ( x => x . Close ( true ) ) ;
566563 }
567564 else
568565 {
569- editors ? . ForEach ( x => x . Close ( false , false ) ) ;
566+ EditorReferences . ForEach ( x => x . Close ( false , false ) ) ;
570567 }
571568
572569 // Kill children process from "Server Start" feature
@@ -601,7 +598,7 @@ public void RestoreMainWindow()
601598 public void EvaluateRTL ( )
602599 {
603600 FlowDirection = Program . IsRTL ? FlowDirection . RightToLeft : FlowDirection . LeftToRight ;
604- GetAllEditorElements ( ) ? . ForEach ( x => x . EvaluateRTL ( ) ) ;
601+ EditorReferences . ForEach ( x => x . EvaluateRTL ( ) ) ;
605602 }
606603 #endregion
607604 }
0 commit comments