forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchService.cs
More file actions
446 lines (380 loc) · 14.3 KB
/
Copy pathSearchService.cs
File metadata and controls
446 lines (380 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Profiling;
using UnityEngine;
using Attribute = System.Attribute;
using Object = UnityEngine.Object;
namespace UnityEditor.SearchService
{
public enum SearchEngineScope
{
Scene,
Project,
ObjectSelector
}
public interface ISearchContext
{
Guid guid { get; }
SearchEngineScope engineScope { get; }
IEnumerable<Type> requiredTypes { get; }
IEnumerable<string> requiredTypeNames { get; }
}
public interface ISearchEngineBase
{
string name { get; }
void BeginSession(ISearchContext context);
void EndSession(ISearchContext context);
void BeginSearch(ISearchContext context, string query);
void EndSearch(ISearchContext context);
}
public interface ISearchEngine<T> : ISearchEngineBase
{
IEnumerable<T> Search(ISearchContext context, string query, Action<IEnumerable<T>> asyncItemsReceived);
}
public interface IFilterEngine<in T> : ISearchEngineBase
{
bool Filter(ISearchContext context, string query, T objectToFilter);
}
public interface ISelectorEngine : ISearchEngineBase
{
bool SelectObject(ISearchContext context, Action<Object, bool> onObjectSelectorClosed, Action<Object> onObjectSelectedUpdated);
void SetSearchFilter(ISearchContext context, string searchFilter);
}
struct SearchSessionOptions
{
public bool legacyOnly;
public static readonly SearchSessionOptions Default = new SearchSessionOptions();
}
struct SearchSessionOptionsApplicator : IDisposable
{
bool m_Disposed;
ISearchApi m_Api;
SearchSessionOptions m_Options;
string m_CurrentActiveEngine;
public SearchSessionOptionsApplicator(ISearchApi api, SearchSessionOptions options)
{
m_Api = api;
m_Options = options;
m_Disposed = false;
m_CurrentActiveEngine = m_Api?.activeSearchEngineName;
if (m_Options.legacyOnly)
m_Api?.SetActiveSearchEngine(LegacySearchEngineBase.k_Name, false);
}
public void Dispose()
{
if (m_Disposed)
return;
if (m_Options.legacyOnly)
m_Api?.SetActiveSearchEngine(m_CurrentActiveEngine, false);
m_Disposed = true;
}
}
class SearchSessionHandler
{
SearchEngineScope m_EngineScope;
bool m_WasSearching;
protected ISearchApi m_Api;
protected SearchSessionOptions m_Options;
public ISearchContext context { get; private set; }
public SearchSessionHandler(SearchEngineScope engineScope)
{
m_EngineScope = engineScope;
Init();
}
void Init(bool pullEngines = false)
{
if (pullEngines)
PullEngines();
var index = SearchService.searchApis.FindIndex(api => api.engineScope == m_EngineScope);
if (index >= 0)
{
m_Api = SearchService.searchApis[index];
m_Api.activeEngineChanged += OnActiveEngineChanged;
}
}
void PullEngines()
{
// Calling HasEngineOverride will make sure the engine apis are registered.
switch (m_EngineScope)
{
case SearchEngineScope.Project: ProjectSearch.HasEngineOverride();
break;
case SearchEngineScope.Scene: SceneSearch.HasEngineOverride();
break;
case SearchEngineScope.ObjectSelector: ObjectSelectorSearch.HasEngineOverride();
break;
}
}
protected virtual void OnActiveEngineChanged(string newSearchEngineName)
{
EndSession();
}
public void BeginSession(Func<ISearchContext> searchContextCreator, SearchSessionOptions options)
{
if (m_WasSearching)
return;
// Do a lazy initialize if the apis were not available during creation
if (m_Api == null)
{
Init(true);
if (m_Api == null)
throw new NullReferenceException("SearchService Apis were not initialized properly.");
}
m_Options = options;
context = searchContextCreator();
m_WasSearching = true;
using (new SearchSessionOptionsApplicator(m_Api, options))
m_Api?.BeginSession(context);
}
public void BeginSession(Func<ISearchContext> searchContextCreator)
{
BeginSession(searchContextCreator, SearchSessionOptions.Default);
}
public void EndSession()
{
if (!m_WasSearching)
return;
m_WasSearching = false;
using (new SearchSessionOptionsApplicator(m_Api, m_Options))
m_Api?.EndSession(context);
context = null;
m_Options = SearchSessionOptions.Default;
}
public void BeginSearch(string query)
{
using (new SearchSessionOptionsApplicator(m_Api, m_Options))
m_Api?.BeginSearch(query, context);
}
public void EndSearch()
{
using (new SearchSessionOptionsApplicator(m_Api, m_Options))
m_Api?.EndSearch(context);
}
}
interface ISearchApi
{
SearchEngineScope engineScope { get; }
string displayName { get; }
IEnumerable<ISearchEngineBase> engines { get; }
string activeSearchEngineName { get; }
ISearchEngineBase GetActiveSearchEngine();
void SetActiveSearchEngine(string searchEngineName, bool notify = true);
void RegisterEngine(ISearchEngineBase engine);
void UnregisterEngine(ISearchEngineBase engine);
bool HasEngineOverride();
void BeginSession(ISearchContext context);
void EndSession(ISearchContext context);
void BeginSearch(string query, ISearchContext context);
void EndSearch(ISearchContext context);
ISearchEngineBase GetDefaultEngine();
event Action<string> activeEngineChanged;
}
[InitializeOnLoad]
static class SearchService
{
public const string keyPrefix = "searchservice";
public const string activeSearchEnginesPrefKey = keyPrefix + ".activeengines.";
public static List<ISearchApi> searchApis = new List<ISearchApi>();
public enum SyncSearchEvent
{
StartSession,
SyncSearch,
EndSession
}
public static event Action<SyncSearchEvent, string, string> syncSearchChanged;
public static void NotifySyncSearchChanged(SyncSearchEvent evt, string syncViewId, string searchQuery)
{
syncSearchChanged?.Invoke(evt, syncViewId, searchQuery);
}
public static void HandleSearchEvent(EditorWindow window, Event evt, string searchText)
{
OpenSearchHelper.HandleSearchEvent(window, evt, searchText);
}
public static void DrawOpenSearchButton(EditorWindow window, string searchText)
{
OpenSearchHelper.DrawOpenButton(window, searchText);
}
}
class SearchApiBaseImp<TAttribute, TEngine> : ISearchApi
where TAttribute : Attribute
where TEngine : class, ISearchEngineBase
{
int m_SearchPerformanceTrackerHandle;
public SearchEngineScope engineScope { get; }
public string displayName { get; }
List<TEngine> engines { get; } = new List<TEngine>();
IEnumerable<ISearchEngineBase> ISearchApi.engines => engines.Cast<ISearchEngineBase>();
public string activeSearchEngineName { get; private set; }
TEngine m_ActiveSearchEngine;
public TEngine activeSearchEngine
{
get
{
if (m_ActiveSearchEngine == null)
m_ActiveSearchEngine = GetActiveSearchEngine();
return m_ActiveSearchEngine;
}
private set => m_ActiveSearchEngine = value;
}
public event Action<string> activeEngineChanged;
public SearchApiBaseImp(SearchEngineScope engineScope, string displayName)
{
this.engineScope = engineScope;
this.displayName = displayName ?? throw new ArgumentNullException(nameof(displayName));
RegisterAllEngines();
LoadActiveSearchEngine();
SearchService.searchApis.Add(this);
}
public TEngine GetActiveSearchEngine()
{
if (engines.Count == 0)
return null;
var actualActiveEngineName = string.IsNullOrEmpty(activeSearchEngineName) ? GetDefaultEngine().name : activeSearchEngineName;
var activeEngineIndex = engines.FindIndex(engine => engine.name == actualActiveEngineName);
if (activeEngineIndex < 0)
{
var defaultEngine = GetDefaultEngine() ?? engines.First();
SetActiveSearchEngine(defaultEngine.name);
return defaultEngine;
}
return engines[activeEngineIndex];
}
ISearchEngineBase ISearchApi.GetActiveSearchEngine()
{
return GetActiveSearchEngine();
}
public TEngine GetDefaultEngine()
{
if (engines.Count == 0)
return null;
var index = engines.FindIndex(engine => engine is LegacySearchEngineBase);
if (index < 0)
return null;
return engines[index];
}
ISearchEngineBase ISearchApi.GetDefaultEngine()
{
return GetDefaultEngine();
}
public void SetActiveSearchEngine(string searchEngineName, bool notify = true)
{
if (string.Equals(activeSearchEngineName, searchEngineName, StringComparison.Ordinal))
return;
if (notify)
activeEngineChanged?.Invoke(searchEngineName);
activeSearchEngineName = searchEngineName;
EditorPrefs.SetString(SearchService.activeSearchEnginesPrefKey + engineScope, searchEngineName);
activeSearchEngine = null;
}
void ISearchApi.SetActiveSearchEngine(string searchEngineName, bool notify)
{
SetActiveSearchEngine(searchEngineName, notify);
}
public bool HasEngineOverride()
{
return !(activeSearchEngine is LegacySearchEngineBase);
}
public void BeginSession(ISearchContext context)
{
try
{
activeSearchEngine.BeginSession(context);
}
catch (Exception ex)
{
HandleUserException(ex);
}
}
public void EndSession(ISearchContext context)
{
try
{
activeSearchEngine.EndSession(context);
}
catch (Exception ex)
{
HandleUserException(ex);
}
}
public void BeginSearch(string query, ISearchContext context)
{
m_SearchPerformanceTrackerHandle = EditorPerformanceTracker.StartTracker($"Search Engine {activeSearchEngineName} ({displayName})");
try
{
activeSearchEngine.BeginSearch(context, query);
}
catch (Exception ex)
{
HandleUserException(ex);
}
}
public void EndSearch(ISearchContext context)
{
try
{
activeSearchEngine.EndSearch(context);
}
catch (Exception ex)
{
HandleUserException(ex);
}
if (EditorPerformanceTracker.IsTrackerActive(m_SearchPerformanceTrackerHandle))
EditorPerformanceTracker.StopTracker(m_SearchPerformanceTrackerHandle);
}
void LoadActiveSearchEngine()
{
activeSearchEngineName = EditorPrefs.GetString(SearchService.activeSearchEnginesPrefKey + engineScope, GetDefaultEngine().name);
m_ActiveSearchEngine = GetActiveSearchEngine();
}
public void RegisterEngine(TEngine engine)
{
if (engine != null && engines.FindIndex(e => e.name == engine.name) < 0)
engines.Add(engine);
}
public void UnregisterEngine(TEngine engine)
{
if (engine == null)
return;
var index = engines.FindIndex(e => e.name == engine.name);
if (index >= 0)
engines.RemoveAt(index);
}
void ISearchApi.RegisterEngine(ISearchEngineBase engine)
{
RegisterEngine(engine as TEngine);
}
void ISearchApi.UnregisterEngine(ISearchEngineBase engine)
{
UnregisterEngine(engine as TEngine);
}
void RegisterAllEngines()
{
var types = TypeCache.GetTypesWithAttribute<TAttribute>();
var instantiatedEngines = types.Select(type => Activator.CreateInstance(type));
foreach (var instantiatedEngine in instantiatedEngines)
{
if (instantiatedEngine is TEngine typedEngine)
{
RegisterEngine(typedEngine);
}
else
{
Debug.LogFormat(LogType.Warning, LogOption.NoStacktrace, null,
$"Trying to register a search engine with the attribute {typeof(TAttribute)} but it does not implement the interface {typeof(TEngine)}.");
}
}
}
public void HandleUserException(Exception ex)
{
if (activeSearchEngine is LegacySearchEngineBase)
throw ex;
Debug.LogFormat(LogType.Error, LogOption.None, null, $"Exception caught with search engine ({displayName}){activeSearchEngine.name}:\n{ex}");
Debug.LogFormat(LogType.Error, LogOption.None, null, $"Setting {GetDefaultEngine().name} engine as active.");
SetActiveSearchEngine(GetDefaultEngine().name);
}
}
}