Skip to content

Commit

Permalink
Use TypeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Apr 25, 2024
1 parent ab0f4f1 commit 26f767d
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ public static string GetModifierName(Type type)
static ModifierMenuItems()
{
// Get all Modifier types
var allTypes
= ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => typeof(CinemachineFreeLookModifier.Modifier).IsAssignableFrom(t)
&& !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineFreeLookModifier.Modifier),
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);

s_AllModifiers.Clear();
s_ModifierNames.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ protected virtual void OnEnable()
if (sExtensionTypes == null)
{
// Populate the extension list
List<Type> exts = new List<Type>();
List<string> names = new List<string>();
List<Type> exts = new ();
List<string> names = new ();
exts.Add(null);
names.Add("(select)");
var allExtensions
= ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => typeof(CinemachineExtension).IsAssignableFrom(t)
&& !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allExtensions = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineExtension),
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
foreach (Type t in allExtensions)
{
exts.Add(t);
Expand Down
5 changes: 2 additions & 3 deletions com.unity.cinemachine/Editor/Obsolete/VcamStageEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ static EditorInitialize()
}

// Get all ICinemachineComponents
var allTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies((Type t) =>
typeof(CinemachineComponentBase).IsAssignableFrom(t) && !t.IsAbstract &&
t.GetCustomAttribute<CameraPipelineAttribute>() != null); // we allow obsolete attributes here
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineComponentBase),
(t) => !t.IsAbstract && t.GetCustomAttribute<CameraPipelineAttribute>() != null); // we allow obsolete attributes here

foreach (var t in allTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,8 @@ void AssetFieldWithCreateButton(
{
// Collect all the eligible asset types
Type type = EmbeddedAssetType(property);
if (mAssetTypes == null)
mAssetTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => type.IsAssignableFrom(t) && !t.IsAbstract
&& t.GetCustomAttribute<ObsoleteAttribute>() == null).ToArray();
mAssetTypes ??= ReflectionHelpers.GetTypesDerivedFrom(type,
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null).ToArray();

float iconSize = r.height + 4;
r.width -= iconSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ public static int GetTypeIndex(string typeName)
static AutoDollyMenuItems()
{
// Get all eligible types
var allTypes
= ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => typeof(SplineAutoDolly.ISplineAutoDolly).IsAssignableFrom(t)
&& !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(typeof(SplineAutoDolly.ISplineAutoDolly),
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);

s_AllItems.Clear();
s_AllItems.Add(null);
Expand Down
14 changes: 6 additions & 8 deletions com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ static PipelineStageMenu()
}

// Get all CinemachineComponentBase
var allTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies((Type t) =>
typeof(CinemachineComponentBase).IsAssignableFrom(t) && !t.IsAbstract
&& t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineComponentBase),
(t) => !t.IsAbstract
&& t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);

var iter = allTypes.GetEnumerator();
while (iter.MoveNext())
Expand All @@ -367,10 +367,8 @@ static PipelineStageMenu()
s_ExtensionNames = new List<string>();
s_ExtensionTypes.Add(null);
s_ExtensionNames.Add("(select)");
var allExtensions
= ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => typeof(CinemachineExtension).IsAssignableFrom(t)
&& !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allExtensions = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineExtension),
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var iter2 = allExtensions.GetEnumerator();
while (iter2.MoveNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ public static void AddInputControllerHelp(
{
if (s_AllAxisControllerTypes == null)
{
var allTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => typeof(IInputAxisController).IsAssignableFrom(t) && !t.IsAbstract
&& typeof(MonoBehaviour).IsAssignableFrom(t)
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(typeof(IInputAxisController),
(t) => !t.IsAbstract && typeof(MonoBehaviour).IsAssignableFrom(t)
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
s_AllAxisControllerTypes = new();
var iter = allTypes.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ public static VisualElement AssetSelectorWithPresets<T>(
static List<Type> GetAssetTypes(Type baseType)
{
// GML todo: optimize with TypeCache
var allTypes = ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => baseType.IsAssignableFrom(t) && !t.IsAbstract
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
var allTypes = ReflectionHelpers.GetTypesDerivedFrom(baseType,
(t) => !t.IsAbstract && t.GetCustomAttribute<ObsoleteAttribute>() == null);
var list = new List<Type>();
var iter = allTypes.GetEnumerator();
while (iter.MoveNext())
Expand Down
5 changes: 2 additions & 3 deletions com.unity.cinemachine/Editor/Utility/InspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,8 @@ public static string GetAssignableBehaviourNames(Type inputType)
return "(none)";
if (!s_AssignableTypes.ContainsKey(inputType))
{
var allSources = ReflectionHelpers.GetTypesInAllDependentAssemblies(
(Type t) => inputType.IsAssignableFrom(t) && !t.IsAbstract
&& typeof(MonoBehaviour).IsAssignableFrom(t)
var allSources = ReflectionHelpers.GetTypesDerivedFrom(inputType,
(t) => !t.IsAbstract && typeof(MonoBehaviour).IsAssignableFrom(t)
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
var s = string.Empty;
var iter = allSources.GetEnumerator();
Expand Down
80 changes: 11 additions & 69 deletions com.unity.cinemachine/Editor/Utility/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Unity.Cinemachine
Expand Down Expand Up @@ -31,82 +32,23 @@ public static void CopyFields(
}
}

/// <summary>Search the assembly for all types that match a predicate</summary>
/// <param name="assembly">The assembly to search</param>
/// <param name="predicate">The type to look for</param>
/// <returns>A list of types found in the assembly that inherit from the predicate</returns>
public static IEnumerable<Type> GetTypesInAssembly(
Assembly assembly, Predicate<Type> predicate)
/// <summary>Search all assemblies for all types that match a predicate</summary>
/// <param name="type">The type or interface to look for</param>
/// <param name="predicate">Additional conditions to test</param>
/// <returns>A list of types found that inherit from the type and satisfy the predicate.</returns>
public static IEnumerable<Type> GetTypesDerivedFrom(Type type, Predicate<Type> predicate)
{
var list = new List<Type>();
if (assembly != null)
var iter = TypeCache.GetTypesDerivedFrom(type).GetEnumerator();
while (iter.MoveNext())
{
try
{
var allTypes = assembly.GetTypes();
if (allTypes != null)
{
for (int i = 0; i < allTypes.Length; ++i)
{
var t = allTypes[i];
if (t != null && predicate(t))
list.Add(t);
}
}
}
catch (Exception) {} // Can't load the types in this assembly
var t = iter.Current;
if (t != null && predicate(t))
list.Add(t);
}
return list;
}

/// <summary>Get a type from a name</summary>
/// <param name="typeName">The name of the type to search for</param>
/// <returns>The type matching the name, or null if not found</returns>
public static Type GetTypeInAllDependentAssemblies(string typeName)
{
var iter = GetTypesInAllDependentAssemblies(t => t.Name == typeName).GetEnumerator();
if (iter.MoveNext())
return iter.Current;
return null;
}

/// <summary>Search all assemblies for all types that match a predicate</summary>
/// <param name="predicate">The type to look for</param>
/// <returns>A list of types found in the assembly that inherit from the predicate</returns>
public static IEnumerable<Type> GetTypesInAllDependentAssemblies(Predicate<Type> predicate)
{
List<Type> foundTypes = new(100);
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var definedIn = typeof(CinemachineComponentBase).Assembly.GetName().Name;
for (int i = 0; i < assemblies.Length; ++i)
{
var assembly = assemblies[i];
if (assembly.GlobalAssemblyCache)
continue;

// Note that we have to call GetName().Name. Just GetName() will not work.
bool skip = assembly.GetName().Name != definedIn;
if (skip)
{
var referencedAssemblies = assembly.GetReferencedAssemblies();
for (int j = 0; skip && j < referencedAssemblies.Length; ++j)
if (referencedAssemblies[j].Name == definedIn)
skip = false;
}
if (skip)
continue;

try
{
var iter = GetTypesInAssembly(assembly, predicate).GetEnumerator();
while (iter.MoveNext())
foundTypes.Add(iter.Current);
}
catch (Exception) {} // Just skip uncooperative assemblies
}
return foundTypes;
}

/// <summary>Cheater extension to access internal field of an object</summary>
/// <typeparam name="T">The field type</typeparam>
/// <param name="type">The type of the field</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ public override void SetUp()
m_CmCamera = CreateGameObject("CinemachineCamera", typeof(CinemachineCamera)).GetComponent<CinemachineCamera>();
m_CmCamera.Priority.Value = 100;

s_AllCinemachineComponents = ReflectionHelpers.GetTypesInAllDependentAssemblies((Type t) =>
typeof(CinemachineComponentBase).IsAssignableFrom(t) && !t.IsAbstract
&& t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
s_AllCinemachineComponents = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineComponentBase),
(t) => !t.IsAbstract && t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() == null);
}

[TearDown]
Expand Down
8 changes: 4 additions & 4 deletions com.unity.cinemachine/Tests/Editor/UpgradeCm2ToCm3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ static IEnumerable ConvertTestCases
{
get
{
s_AllCinemachineComponents = ReflectionHelpers.GetTypesInAllDependentAssemblies((Type t) =>
typeof(CinemachineComponentBase).IsAssignableFrom(t) && !t.IsAbstract
&& t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() != null);
s_AllCinemachineComponents = ReflectionHelpers.GetTypesDerivedFrom(typeof(CinemachineComponentBase),
(t) => !t.IsAbstract
&& t.GetCustomAttribute<CameraPipelineAttribute>() != null
&& t.GetCustomAttribute<ObsoleteAttribute>() != null);
foreach (var cmComponent in s_AllCinemachineComponents)
yield return new TestCaseData(cmComponent).SetName(cmComponent.Name).Returns(null);
}
Expand Down

0 comments on commit 26f767d

Please sign in to comment.