11using GalaSoft . MvvmLight ;
22using System ;
33using System . Collections . Generic ;
4+ using System . Diagnostics ;
45using System . IO ;
56using System . Linq ;
67using System . Text . RegularExpressions ;
78using System . Windows ;
89
910namespace XenkoShaderExplorer
1011{
12+ public enum XenkoSourceDirMode
13+ {
14+ Official ,
15+ Dev
16+ }
17+
1118 public class MainViewModel : ViewModelBase
1219 {
1320 private const string XenkoEnvironmentVariable = "XenkoDir" ;
1421 private const string FallbackBasePath = @"C:\Program Files\Silicon Studio\Xenko\" ;
1522
1623 private string _filterText ;
17- private string _path ;
24+ private IReadOnlyList < string > _path ;
1825
1926 /// <summary>
2027 /// The list of roots of the tree view. This includes all the shaders
@@ -37,10 +44,42 @@ public string FilterText
3744 }
3845 }
3946
47+ internal void Refresh ( )
48+ {
49+ try
50+ {
51+ List < string > basePath = null ;
52+ switch ( XenkoDirMode )
53+ {
54+ case XenkoSourceDirMode . Official :
55+ var userDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
56+ var nugetPackageDir = Path . Combine ( userDir , ".nuget" , "packages" ) ;
57+ var directories = Directory . GetDirectories ( nugetPackageDir ) //package dir
58+ . Where ( dir => Path . GetFileName ( dir ) . StartsWith ( "xenko" , StringComparison . OrdinalIgnoreCase ) ) //xenko folders
59+ . Select ( dir => Directory . GetDirectories ( dir ) . OrderBy ( subdir => subdir , StringComparer . OrdinalIgnoreCase ) . LastOrDefault ( ) ) //latest version
60+ . Where ( dir => ! dir . EndsWith ( "-dev" ) ) ; //exclude local build package
61+ basePath = directories . ToList ( ) ;
62+ break ;
63+ case XenkoSourceDirMode . Dev :
64+ basePath = new List < string > { Environment . GetEnvironmentVariable ( XenkoEnvironmentVariable ) ?? FallbackBasePath } ;
65+ //basePath = System.IO.Path.Combine(basePath, "sources", "engine", "Xenko.Engine", "Rendering");
66+ break ;
67+ default :
68+ break ;
69+ }
70+
71+ Paths = basePath ;
72+ }
73+ catch ( Exception e )
74+ {
75+ MessageBox . Show ( e . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
76+ }
77+ }
78+
4079 /// <summary>
4180 /// Path to the Xenko installation folder.
4281 /// </summary>
43- public string Path
82+ public IReadOnlyList < string > Paths
4483 {
4584 get { return _path ; }
4685 set
@@ -49,10 +88,11 @@ public string Path
4988 {
5089 try
5190 {
52- RootShaders = BuildShaderTree ( ) . ToList ( ) ;
91+ RootShaders = BuildShaderTree ( ) . OrderBy ( s => s . Name , StringComparer . OrdinalIgnoreCase ) . ToList ( ) ;
5392 RaisePropertyChanged ( nameof ( RootShaders ) ) ;
5493 RaisePropertyChanged ( nameof ( AllShaders ) ) ;
5594 UpdateFiltering ( ) ;
95+ ExpandAll ( false ) ;
5696 }
5797 catch ( Exception e )
5898 {
@@ -62,25 +102,11 @@ public string Path
62102 }
63103 }
64104
105+ public XenkoSourceDirMode XenkoDirMode { get ; internal set ; }
106+
65107 public MainViewModel ( )
66108 {
67- try
68- {
69- var userDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
70- var xenkoDir = System . IO . Path . Combine ( userDir , ".nuget" , "packages" , "xenko" ) ;
71- var directories = Directory . GetDirectories ( xenkoDir )
72- . Where ( folder => ! folder . EndsWith ( "-dev" ) ) //exclude local build package
73- . OrderBy ( folder => folder , StringComparer . OrdinalIgnoreCase ) ;
74- var basePath = directories . LastOrDefault ( ) ;
75-
76- _path = basePath ;
77-
78- RootShaders = BuildShaderTree ( ) . ToList ( ) ;
79- }
80- catch ( Exception e )
81- {
82- MessageBox . Show ( e . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
83- }
109+ Refresh ( ) ;
84110 }
85111
86112 private void UpdateFiltering ( )
@@ -116,13 +142,14 @@ private static IEnumerable<Shader> ShadersInPostOrder(Shader shader)
116142
117143 private IEnumerable < Shader > BuildShaderTree ( )
118144 {
119- var files = Directory . GetFiles ( Path , "*.xksl" , SearchOption . AllDirectories ) ;
145+ var files = Paths . SelectMany ( path => Directory . GetFiles ( path , "*.xksl" , SearchOption . AllDirectories ) ) ;
120146 var shaders = new Dictionary < string , Shader > ( ) ;
121147
122148 foreach ( var file in files )
123149 {
124150 var name = System . IO . Path . GetFileNameWithoutExtension ( file ) ;
125- shaders . Add ( name , new Shader { Path = file , Name = name } ) ;
151+ if ( ! shaders . ContainsKey ( name ) )
152+ shaders [ name ] = new Shader { Path = file , Name = name } ;
126153 }
127154
128155 foreach ( var shader in shaders . Values )
@@ -145,7 +172,7 @@ private IEnumerable<Shader> BuildShaderTree()
145172 . Split ( ',' )
146173 . Select ( s => s . Trim ( ) ) ;
147174 System . Diagnostics . Debug . WriteLine ( string . Join ( ", " , baseShaderNames ) ) ;
148- // if (!baseShaderNames.Contains("ShadowMapCasterBase"))
175+ if ( ! baseShaderNames . Contains ( "ShadowMapCasterBase" ) )
149176 { //I have no clue why this shader doesn't exist. >w>
150177
151178 // There are shaders deriving from "ShadowMapCasterBase" which for some reason doesn't exit,
@@ -165,6 +192,8 @@ private IEnumerable<Shader> BuildShaderTree()
165192 yield return shader ;
166193 }
167194 }
195+
196+ Debug . WriteLine ( $ "Found { shaders . Count } shaders") ;
168197 }
169198 }
170199}
0 commit comments