-
-
Notifications
You must be signed in to change notification settings - Fork 4
Assembly improvement #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JonathanMCarter
merged 9 commits into
CarterGames:contribution
from
Gardens-Lies:assembly-improvement
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10e3f60
Fix systemic assembly load when using GetAssemblies()
NoixDeXydre cb8a16b
ProjectAssemblyDefinition's scope is now internal to avoid external a…
NoixDeXydre 7c30992
Rename a variable to be more explicit in AssemblyHelper
NoixDeXydre f7cbb6e
Improve type searching speed with cache
NoixDeXydre dc70835
Fix potential errors or leaks when using loaded assemblies
NoixDeXydre c1caac3
Fix mass instanciation in AssemblyClassDef
NoixDeXydre 7063d5e
Fix Json crash when instanced
NoixDeXydre 0cf1380
Fix TypeInitializationException on initialization
NoixDeXydre 244797e
Make GetLoadedAssemblies only compatible for Unity 6.X
NoixDeXydre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,23 +36,29 @@ public static class AssemblyHelper | |||||
| /* ───────────────────────────────────────────────────────────────────────────────────────────────────────────── | ||||||
| | Fields | ||||||
| ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */ | ||||||
|
|
||||||
| private static Assembly[] audioManagerAssemblies; | ||||||
|
|
||||||
|
|
||||||
| /// <summary> | ||||||
| /// It is intended to use this cache to avoid the use | ||||||
| /// of <see cref="Assembly.GetTypes"/> in each call. | ||||||
| /// </summary> | ||||||
| private static readonly Dictionary<Type, List<Type>> TypeCache = new(); | ||||||
|
|
||||||
| private static Assembly[] _cachedAssemblies; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does not match naming conventions. See: https://carter.games/unity-c-style-guide/#7-fields
Suggested change
|
||||||
|
|
||||||
| /* ───────────────────────────────────────────────────────────────────────────────────────────────────────────── | ||||||
| | Properties | ||||||
| ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */ | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets all the cart assemblies to use when checking in internally only. | ||||||
| /// </summary> | ||||||
| private static IEnumerable<Assembly> AudioManagerAssemblies | ||||||
| private static IEnumerable<Assembly> CachedAssemblies | ||||||
| { | ||||||
| get | ||||||
| { | ||||||
| if (audioManagerAssemblies != null) return audioManagerAssemblies; | ||||||
| audioManagerAssemblies = GetAssemblies(); | ||||||
| return audioManagerAssemblies; | ||||||
| if (_cachedAssemblies != null) return _cachedAssemblies; | ||||||
| _cachedAssemblies = GetAssemblies(); | ||||||
| return _cachedAssemblies; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -82,10 +88,7 @@ private static Assembly[] GetAssemblies() | |||||
| /// <returns>The total in the project.</returns> | ||||||
| public static int CountClassesOfType<T>(bool internalCheckOnly = true) | ||||||
| { | ||||||
| var assemblies = internalCheckOnly ? AudioManagerAssemblies : AppDomain.CurrentDomain.GetAssemblies(); | ||||||
|
|
||||||
| return assemblies.SelectMany(x => x.GetTypes()) | ||||||
| .Count(x => x.IsClass && typeof(T).IsAssignableFrom(x)); | ||||||
| return GetClassesNamesOfType<T>(internalCheckOnly).Count(); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -110,14 +113,11 @@ public static int CountClassesOfType<T>(params Assembly[] assemblies) | |||||
| /// <returns>All the implementations of the entered class.</returns> | ||||||
| public static IEnumerable<T> GetClassesOfType<T>(bool internalCheckOnly = true) | ||||||
| { | ||||||
| var assemblies = internalCheckOnly ? AudioManagerAssemblies : AppDomain.CurrentDomain.GetAssemblies(); | ||||||
|
|
||||||
| return assemblies.SelectMany(x => x.GetTypes()) | ||||||
| .Where(x => x.IsClass && typeof(T).IsAssignableFrom(x) && !x.IsAbstract && x.FullName != typeof(T).FullName) | ||||||
| return GetClassesNamesOfType<T>(internalCheckOnly) | ||||||
| .Select(type => (T)Activator.CreateInstance(type)); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets all the classes of the entered type in the project. | ||||||
| /// </summary> | ||||||
|
|
@@ -126,13 +126,34 @@ public static IEnumerable<T> GetClassesOfType<T>(bool internalCheckOnly = true) | |||||
| /// <returns>All the implementations of the entered class.</returns> | ||||||
| public static IEnumerable<Type> GetClassesNamesOfType<T>(bool internalCheckOnly = true) | ||||||
| { | ||||||
| var assemblies = internalCheckOnly ? AudioManagerAssemblies : AppDomain.CurrentDomain.GetAssemblies(); | ||||||
| Type targetType = typeof(T); | ||||||
|
|
||||||
| return assemblies.SelectMany(x => x.GetTypes()) | ||||||
| .Where(x => x.IsClass && typeof(T).IsAssignableFrom(x) && x.FullName != typeof(T).FullName); | ||||||
| // We don't need to search in the project / assembly if we already know the type. | ||||||
| if (TypeCache.TryGetValue(targetType, out List<Type> cachedTypes)) | ||||||
| return cachedTypes; | ||||||
|
|
||||||
| #if UNITY_6000_0_OR_NEWER | ||||||
| var assemblies = internalCheckOnly ? CachedAssemblies | ||||||
| : UnityEngine.Assemblies.CurrentAssemblies.GetLoadedAssemblies(); | ||||||
| #else | ||||||
| var assemblies = internalCheckOnly ? CachedAssemblies | ||||||
| : AppDomain.CurrentDomain.GetAssemblies(); | ||||||
| #endif | ||||||
|
|
||||||
| // Searching all the implementation of the class | ||||||
| var foundTypes = assemblies | ||||||
| .SelectMany(x => x.GetTypes()) | ||||||
| .Where(x => x.IsClass && !x.IsAbstract && !x.ContainsGenericParameters | ||||||
| && targetType.IsAssignableFrom(x) && x != targetType) | ||||||
| .ToList(); | ||||||
|
|
||||||
| // Caching for later | ||||||
| TypeCache[targetType] = foundTypes; | ||||||
|
|
||||||
| return foundTypes; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets all the classes of the entered type in the project. | ||||||
| /// </summary> | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't compile in the base development version of
2020.3.0