Added Pacore
This commit is contained in:
58
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs
Normal file
58
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Caches
|
||||
{
|
||||
public static 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<Type, AttributeInfo[]> Cache { get; } = new();
|
||||
|
||||
public static AttributeInfo[] GetAllAttributesOfType(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<AttributeInfo> buttons = new();
|
||||
|
||||
foreach (MethodInfo method in methods)
|
||||
{
|
||||
InspectorCallableAttribute attribute
|
||||
= method.GetCustomAttribute<InspectorCallableAttribute>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta
Normal file
11
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95392750cbc11ce4b9b9e6717c9e6588
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Assets/Pacore/Editor/Caches/MonoScriptCache.cs
Normal file
34
Assets/Pacore/Editor/Caches/MonoScriptCache.cs
Normal file
@@ -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<string, MonoScript> Cache { get; } = new();
|
||||
|
||||
static MonoScriptCache()
|
||||
{
|
||||
/* Finds all MonoScripts and adds them to the dictionary by name */
|
||||
MonoScript[] scripts = Resources.FindObjectsOfTypeAll<MonoScript>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta
Normal file
11
Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 668c2ebd0824f1d4c9b324be7142823c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user