Skip to content

Commit 92d1d07

Browse files
committed
Refactor type search logic into TypeCandiateService
Replaces TypeSearch with TypeCandiateService and TypeSearchService for improved modularity and extensibility. Updates SubclassSelectorDrawer to use the new service. Removes obsolete TypeSearch and related files.
1 parent a786d39 commit 92d1d07

7 files changed

Lines changed: 78 additions & 155 deletions

File tree

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
150150
var state = new AdvancedDropdownState();
151151

152152
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
153-
var popup = new AdvancedTypePopup(
154-
TypeSearch.GetTypes(baseType),
153+
var types = TypeSearchService.TypeCandiateService.GetDisplayableTypes(baseType);
154+
var popup = new AdvancedTypePopup(
155+
types,
155156
k_MaxTypePopupLineCount,
156157
state
157158
);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace MackySoft.SerializeReferenceExtensions.Editor
6+
{
7+
public sealed class TypeCandiateService
8+
{
9+
10+
private readonly ITypeCandiateProvider typeCandiateProvider;
11+
private readonly IIntrinsicTypePolicy intrinsicTypePolicy;
12+
private readonly ITypeCompatibilityPolicy typeCompatibilityPolicy;
13+
14+
private readonly Dictionary<Type, Type[]> typeCache = new Dictionary<Type, Type[]>();
15+
16+
public TypeCandiateService (ITypeCandiateProvider typeCandiateProvider, IIntrinsicTypePolicy intrinsicTypePolicy, ITypeCompatibilityPolicy typeCompatibilityPolicy)
17+
{
18+
this.typeCandiateProvider = typeCandiateProvider ?? throw new ArgumentNullException(nameof(typeCandiateProvider));
19+
this.intrinsicTypePolicy = intrinsicTypePolicy ?? throw new ArgumentNullException(nameof(intrinsicTypePolicy));
20+
this.typeCompatibilityPolicy = typeCompatibilityPolicy ?? throw new ArgumentNullException(nameof(typeCompatibilityPolicy));
21+
}
22+
23+
public IReadOnlyList<Type> GetDisplayableTypes (Type baseType)
24+
{
25+
if (baseType == null)
26+
{
27+
throw new ArgumentNullException(nameof(baseType));
28+
}
29+
if (typeCache.TryGetValue(baseType, out Type[] cachedTypes))
30+
{
31+
return cachedTypes;
32+
}
33+
34+
var candiateTypes = typeCandiateProvider.GetTypeCandidates(baseType);
35+
var result = candiateTypes
36+
.Where(intrinsicTypePolicy.IsAllowed)
37+
.Where(t => typeCompatibilityPolicy.IsCompatible(baseType, t))
38+
.Distinct()
39+
.ToArray();
40+
41+
typeCache.Add(baseType, result);
42+
return result;
43+
}
44+
}
45+
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeCandiateService.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch.cs

Lines changed: 0 additions & 142 deletions
This file was deleted.

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MackySoft.SerializeReferenceExtensions.Editor
2+
{
3+
public static class TypeSearchService
4+
{
5+
6+
public static readonly IIntrinsicTypePolicy IntrinsicTypePolicy;
7+
public static readonly ITypeCompatibilityPolicy TypeCompatibilityPolicy;
8+
public static readonly ITypeCandiateProvider TypeCandiateProvider;
9+
public static readonly TypeCandiateService TypeCandiateService;
10+
11+
static TypeSearchService ()
12+
{
13+
IntrinsicTypePolicy = DefaultIntrinsicTypePolicy.Instance;
14+
15+
#if UNITY_2023_2_OR_NEWER
16+
TypeCompatibilityPolicy = Unity_2023_2_OrNewer_GenericVarianceTypeCompatibilityPolicy.Instance;
17+
TypeCandiateProvider = Unity_2023_2_OrNewer_TypeCandiateProvider.Instance;
18+
#else
19+
TypeCompatibilityPolicy = DefaultTypeCompatibilityPolicy.Instance;
20+
TypeCandiateProvider = DefaultTypeCandiateProvider.Instance;
21+
#endif
22+
23+
TypeCandiateService = new TypeCandiateService(TypeCandiateProvider, IntrinsicTypePolicy, TypeCompatibilityPolicy);
24+
}
25+
}
26+
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearchService.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)