Removed some LINQ
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
using PashaBibko.Pacore.DevTools;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace PashaBibko.Pacore
|
namespace PashaBibko.Pacore
|
||||||
@@ -13,53 +14,58 @@ namespace PashaBibko.Pacore
|
|||||||
|
|
||||||
public static ReadOnlyCollection<Type> GetAttributesOf<T>()
|
public static ReadOnlyCollection<Type> GetAttributesOf<T>()
|
||||||
{
|
{
|
||||||
return AttributeCache.TryGetValue(typeof(T), out List<Type> classes) ?
|
return AttributeCache.TryGetValue(typeof(T), out List<Type> classes)
|
||||||
classes.AsReadOnly() :
|
? classes.AsReadOnly()
|
||||||
throw new ArgumentException($"Attribute [{nameof(T)}] is not used by any classes");
|
: throw new ArgumentException($"Attribute [{nameof(T)}] is not used by any classes");
|
||||||
}
|
}
|
||||||
|
|
||||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
||||||
private static void ScanAllAssemblies()
|
private static void ScanAllAssemblies()
|
||||||
{
|
{
|
||||||
/* Fetches all the class types in all loaded assemblies */
|
using (CodeProfiler.Start("Assembly scan"))
|
||||||
Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[]
|
{
|
||||||
.SelectMany(assembly =>
|
/* Fetches all the class types in all loaded assemblies */
|
||||||
{
|
Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[]
|
||||||
try
|
.SelectMany(assembly =>
|
||||||
{
|
{
|
||||||
return assembly.GetTypes();
|
try
|
||||||
|
{
|
||||||
|
return assembly.GetTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (ReflectionTypeLoadException err)
|
||||||
|
{
|
||||||
|
return err.Types.Where(t => t != null);
|
||||||
|
}
|
||||||
|
}) // IEnumerable<Type>
|
||||||
|
.Where(type => type.IsClass && !type.IsAbstract) // IEnumerable<Type>
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
/* Allocates space for the cache */
|
||||||
|
AttributeCache = classes // Type[]
|
||||||
|
.Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable<Type>
|
||||||
|
.ToDictionary
|
||||||
|
(
|
||||||
|
keySelector: t => t,
|
||||||
|
elementSelector: _ => new List<Type>()
|
||||||
|
); // Dictionary<Type, List<Type>>
|
||||||
|
|
||||||
|
/* Finds which attributes are attached to what classes */
|
||||||
|
HashSet<Type> seen = new();
|
||||||
|
foreach (Type current in classes)
|
||||||
|
{
|
||||||
|
/* Tracks the seen attributes */
|
||||||
|
seen.Clear();
|
||||||
|
foreach (object attr in current.GetCustomAttributes(inherit: true))
|
||||||
|
{
|
||||||
|
seen.Add(attr.GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (ReflectionTypeLoadException err)
|
/* Adds the class type to each attribute in the dictionary */
|
||||||
|
foreach (Type type in seen)
|
||||||
{
|
{
|
||||||
return err.Types.Where(t => t != null);
|
AttributeCache[type].Add(current);
|
||||||
}
|
}
|
||||||
}) // IEnumerable<Type>
|
|
||||||
.Where(type => type.IsClass && !type.IsAbstract) // IEnumerable<Type>
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
/* Allocates space for the cache */
|
|
||||||
AttributeCache = classes // Type[]
|
|
||||||
.Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable<Type>
|
|
||||||
.ToDictionary
|
|
||||||
(
|
|
||||||
keySelector: t => t,
|
|
||||||
elementSelector: _ => new List<Type>()
|
|
||||||
); // Dictionary<Type, List<Type>>
|
|
||||||
|
|
||||||
/* Finds which attributes are attached to what classes */
|
|
||||||
foreach (Type current in classes)
|
|
||||||
{
|
|
||||||
/* Finds all the attributes of the current class */
|
|
||||||
Type[] attached = current // Type
|
|
||||||
.GetCustomAttributes(inherit: true) // object[]
|
|
||||||
.Select(attribute => attribute.GetType()) // IEnumerable<Type>
|
|
||||||
.Distinct().ToArray();
|
|
||||||
|
|
||||||
/* Adds the class type to each attribute in the dictionary */
|
|
||||||
foreach (Type attribute in attached)
|
|
||||||
{
|
|
||||||
AttributeCache[attribute].Add(current);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,20 +14,6 @@ using UnityEngine;
|
|||||||
|
|
||||||
private void OnTestChange() => LogTestValue();
|
private void OnTestChange() => LogTestValue();
|
||||||
|
|
||||||
private void Update()
|
|
||||||
{
|
|
||||||
using (CodeProfiler.Start("Test Snippet"))
|
|
||||||
{
|
|
||||||
SpinWait sw = new();
|
|
||||||
int count = Random.Range(1, 50);
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
sw.SpinOnce();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[InspectorCallable(nameof(LogSpawnableType))]
|
[InspectorCallable(nameof(LogSpawnableType))]
|
||||||
public void LogSpawnableType()
|
public void LogSpawnableType()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user