@@ -17,7 +17,7 @@ public static class EverythingApi
1717 {
1818 private const int BufferSize = 4096 ;
1919
20- private static SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
20+ private static readonly SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
2121
2222 // cached buffer to remove redundant allocations.
2323 private static readonly StringBuilder buffer = new ( BufferSize ) ;
@@ -34,6 +34,9 @@ public enum StateCode
3434 InvalidCallError
3535 }
3636
37+ const uint EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004u ;
38+ const uint EVERYTHING_REQUEST_RUN_COUNT = 0x00000400u ;
39+
3740 /// <summary>
3841 /// Checks whether the sort option is Fast Sort.
3942 /// </summary>
@@ -78,7 +81,7 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
7881
7982 if ( option . MaxCount < 0 )
8083 throw new ArgumentOutOfRangeException ( nameof ( option . MaxCount ) , option . MaxCount , "MaxCount must be greater than or equal to 0" ) ;
81-
84+
8285 await _semaphore . WaitAsync ( token ) ;
8386
8487
@@ -112,6 +115,17 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
112115
113116 EverythingApiDllImport . Everything_SetSort ( option . SortOption ) ;
114117 EverythingApiDllImport . Everything_SetMatchPath ( option . IsFullPathSearch ) ;
118+
119+ if ( option . SortOption == SortOption . RUN_COUNT_DESCENDING )
120+ {
121+ EverythingApiDllImport . Everything_SetRequestFlags ( EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT ) ;
122+ }
123+ else
124+ {
125+ EverythingApiDllImport . Everything_SetRequestFlags ( EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME ) ;
126+ }
127+
128+
115129
116130 if ( token . IsCancellationRequested ) yield break ;
117131
@@ -132,10 +146,12 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
132146
133147 var result = new SearchResult
134148 {
149+ // todo the types are wrong. Everything expects uint everywhere, but we send int just above/below. how to fix? Is EverythingApiDllImport autogenerated or handmade?
135150 FullPath = buffer . ToString ( ) ,
136151 Type = EverythingApiDllImport . Everything_IsFolderResult ( idx ) ? ResultType . Folder :
137152 EverythingApiDllImport . Everything_IsFileResult ( idx ) ? ResultType . File :
138- ResultType . Volume
153+ ResultType . Volume ,
154+ Score = ( int ) EverythingApiDllImport . Everything_GetResultRunCount ( ( uint ) idx )
139155 } ;
140156
141157 yield return result ;
@@ -172,5 +188,19 @@ private static void CheckAndThrowExceptionOnError()
172188 throw new ArgumentOutOfRangeException ( ) ;
173189 }
174190 }
191+
192+ public static async Task IncrementRunCounterAsync ( string fileOrFolder )
193+ {
194+ await _semaphore . WaitAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
195+ try
196+ {
197+ _ = EverythingApiDllImport . Everything_IncRunCountFromFileName ( fileOrFolder ) ;
198+ }
199+ catch ( Exception )
200+ {
201+ /*ignored*/
202+ }
203+ finally { _semaphore . Release ( ) ; }
204+ }
175205 }
176206}
0 commit comments