From f8b49427f1819db06ab2a51aad05da7437ea3122 Mon Sep 17 00:00:00 2001 From: Pasha Date: Sun, 25 Jan 2026 13:57:25 +0000 Subject: [PATCH] Moved editor caches to seperate folder --- Assets/Pacore/Editor/Caches.meta | 3 + .../Editor/Caches/InspectorCallableCache.cs | 56 +++++++++++++++++ .../Caches/InspectorCallableCache.cs.meta | 3 + .../Pacore/Editor/Caches/MonoScriptCache.cs | 34 +++++++++++ .../Editor/Caches/MonoScriptCache.cs.meta | 3 + .../Editor/Drawers/MonoBehaviourDrawer.cs | 61 ++----------------- .../Pacore/Editor/Drawers/MonoScriptDrawer.cs | 31 +--------- 7 files changed, 106 insertions(+), 85 deletions(-) create mode 100644 Assets/Pacore/Editor/Caches.meta create mode 100644 Assets/Pacore/Editor/Caches/InspectorCallableCache.cs create mode 100644 Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta create mode 100644 Assets/Pacore/Editor/Caches/MonoScriptCache.cs create mode 100644 Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta diff --git a/Assets/Pacore/Editor/Caches.meta b/Assets/Pacore/Editor/Caches.meta new file mode 100644 index 0000000..97ca613 --- /dev/null +++ b/Assets/Pacore/Editor/Caches.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9eace838af5041fd9d8cb05c8fbf9ef4 +timeCreated: 1769349074 \ No newline at end of file diff --git a/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs new file mode 100644 index 0000000..246dc9b --- /dev/null +++ b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs @@ -0,0 +1,56 @@ +using PashaBibko.Pacore.Attributes; +using System.Collections.Generic; +using System.Reflection; +using System; + +namespace PashaBibko.Pacore.Editor.Caches +{ + public class InspectorCallableCache + { + public struct AttributeInfo + { + public InspectorCallableAttribute Attribute; + public MethodInfo AttachedMethod; + } + + private const BindingFlags BINDING_FLAGS = + BindingFlags.Public | + BindingFlags.NonPublic | + BindingFlags.Instance; + + private static Dictionary Cache { get; } = new(); + + public static AttributeInfo[] GetAllAttributes(Type type) + { + /* Checks the cache for the type */ + if (Cache.TryGetValue(type, out AttributeInfo[] attributes)) + { + return attributes; + } + + /* Finds all the functions with the attribute */ + MethodInfo[] methods = type.GetMethods(BINDING_FLAGS); + List buttons = new(); + + foreach (MethodInfo method in methods) + { + InspectorCallableAttribute attribute = method.GetCustomAttribute(); + if (attribute != null) + { + AttributeInfo info = new() + { + Attribute = attribute, + AttachedMethod = method, + }; + + buttons.Add(info); + } + } + + /* Adds it to the cache before returning */ + AttributeInfo[] array = buttons.ToArray(); + Cache.Add(type, array); + return array; + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta new file mode 100644 index 0000000..4ac10a2 --- /dev/null +++ b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2574e8cf9f6b45dbb4aa22f0e62b23df +timeCreated: 1769349096 \ No newline at end of file diff --git a/Assets/Pacore/Editor/Caches/MonoScriptCache.cs b/Assets/Pacore/Editor/Caches/MonoScriptCache.cs new file mode 100644 index 0000000..777b1c8 --- /dev/null +++ b/Assets/Pacore/Editor/Caches/MonoScriptCache.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; +using System; + +namespace PashaBibko.Pacore.Editor.Caches +{ + public static class MonoScriptCache + { + private static Dictionary Cache { get; } = new(); + + static MonoScriptCache() + { + /* Finds all MonoScripts and adds them to the dictionary by name */ + MonoScript[] scripts = Resources.FindObjectsOfTypeAll(); + foreach (MonoScript script in scripts) + { + Type scriptType = script.GetClass(); + if (scriptType is not null) + { + string name = scriptType.FullName; + Cache.TryAdd(name, script); + } + } + } + + public static MonoScript Get(string name) + { + /* Fetches the value (if there is one) without creating a default val */ + Cache.TryGetValue(name, out MonoScript script); + return script; + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta b/Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta new file mode 100644 index 0000000..dc2f3f2 --- /dev/null +++ b/Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 96c81b83f299432fbf183bc40b69c061 +timeCreated: 1769349335 \ No newline at end of file diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs index d9acade..36bb636 100644 --- a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -1,62 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using PashaBibko.Pacore.Attributes; +using PashaBibko.Pacore.Editor.Caches; using UnityEditor; using UnityEngine; +using System; using Object = UnityEngine.Object; namespace PashaBibko.Pacore.Editor.Drawers { - public static class InspectorCallableAttributeCache - { - public struct AttributeInfo - { - public InspectorCallableAttribute Attribute; - public MethodInfo AttachedMethod; - } - - private const BindingFlags BINDING_FLAGS = - BindingFlags.Public | - BindingFlags.NonPublic | - BindingFlags.Instance; - - private static Dictionary Cache { get; } = new(); - - public static AttributeInfo[] GetAllAttributes(Type type) - { - /* Checks the cache for the type */ - if (Cache.TryGetValue(type, out AttributeInfo[] attributes)) - { - return attributes; - } - - /* Finds all the functions with the attribute */ - MethodInfo[] methods = type.GetMethods(BINDING_FLAGS); - List buttons = new(); - - foreach (MethodInfo method in methods) - { - InspectorCallableAttribute attribute = method.GetCustomAttribute(); - if (attribute != null) - { - AttributeInfo info = new() - { - Attribute = attribute, - AttachedMethod = method, - }; - - buttons.Add(info); - } - } - - /* Adds it to the cache before returning */ - AttributeInfo[] array = buttons.ToArray(); - Cache.Add(type, array); - return array; - } - } - [CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)] public class MonoBehaviourDrawer : UnityEditor.Editor { @@ -69,8 +18,8 @@ namespace PashaBibko.Pacore.Editor.Drawers public static void DrawFunctionButtons(Object target) { Type type = target.GetType(); - InspectorCallableAttributeCache.AttributeInfo[] buttons - = InspectorCallableAttributeCache.GetAllAttributes(type); + InspectorCallableCache.AttributeInfo[] buttons + = InspectorCallableCache.GetAllAttributes(type); if (buttons.Length == 0) { @@ -80,7 +29,7 @@ namespace PashaBibko.Pacore.Editor.Drawers EditorGUILayout.Space(); EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel); - foreach (InspectorCallableAttributeCache.AttributeInfo button in buttons) + foreach (InspectorCallableCache.AttributeInfo button in buttons) { string name = button.Attribute.ButtonName; if (GUILayout.Button(name)) diff --git a/Assets/Pacore/Editor/Drawers/MonoScriptDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoScriptDrawer.cs index 5e68b76..324da35 100644 --- a/Assets/Pacore/Editor/Drawers/MonoScriptDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoScriptDrawer.cs @@ -1,38 +1,11 @@ -using PashaBibko.Pacore.Attributes; -using System.Collections.Generic; +using PashaBibko.Pacore.Editor.Caches; +using PashaBibko.Pacore.Attributes; using UnityEditor; using UnityEngine; using System; namespace PashaBibko.Pacore.Editor.Drawers { - public static class MonoScriptCache - { - private static Dictionary Cache { get; } = new(); - - static MonoScriptCache() - { - /* Finds all MonoScripts and adds them to the dictionary by name */ - MonoScript[] scripts = Resources.FindObjectsOfTypeAll(); - foreach (MonoScript script in scripts) - { - Type scriptType = script.GetClass(); - if (scriptType is not null) - { - string name = scriptType.FullName; - Cache.TryAdd(name, script); - } - } - } - - public static MonoScript Get(string name) - { - /* Fetches the value (if there is one) without creating a default val */ - Cache.TryGetValue(name, out MonoScript script); - return script; - } - } - [CustomPropertyDrawer(typeof(MonoScriptAttribute))] public class MonoScriptDrawer : PropertyDrawer {