Removed some LINQ

This commit is contained in:
2026-01-24 21:47:18 +00:00
parent 434b902814
commit e905c1f7c5
2 changed files with 43 additions and 51 deletions

View File

@@ -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);
} }
} }
} }

View File

@@ -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()
{ {