1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00
xNode/Scripts/Editor/NodeEditorUtilities.cs
Thor Kramer Brigsted 215a3475ea Small editor erformance gain. Removed PrettifyCamelCase.
Use ObjectNames.NicifyVariableName instead
2017-11-28 09:38:21 +01:00

89 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace XNodeEditor {
/// <summary> A set of editor-only utilities and extensions for UnityNodeEditorBase </summary>
public static class NodeEditorUtilities {
public static bool GetAttrib<T>(Type classType, out T attribOut) where T : Attribute {
object[] attribs = classType.GetCustomAttributes(typeof(T), false);
return GetAttrib(attribs, out attribOut);
}
public static bool GetAttrib<T>(object[] attribs, out T attribOut) where T : Attribute {
for (int i = 0; i < attribs.Length; i++) {
if (attribs[i].GetType() == typeof(T)) {
attribOut = attribs[i] as T;
return true;
}
}
attribOut = null;
return false;
}
public static bool GetAttrib<T>(Type classType, string fieldName, out T attribOut) where T : Attribute {
object[] attribs = classType.GetField(fieldName).GetCustomAttributes(typeof(T), false);
return GetAttrib(attribs, out attribOut);
}
public static bool HasAttrib<T>(object[] attribs) where T : Attribute {
for (int i = 0; i < attribs.Length; i++) {
if (attribs[i].GetType() == typeof(T)) {
return true;
}
}
return false;
}
/// <summary> Returns true if this can be casted to <see cref="Type"/></summary>
public static bool IsCastableTo(this Type from, Type to) {
if (to.IsAssignableFrom(from)) return true;
var methods = from.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(
m => m.ReturnType == to &&
(m.Name == "op_Implicit" ||
m.Name == "op_Explicit")
);
return methods.Count() > 0;
}
/// <summary> Return a prettiefied type name. </summary>
public static string PrettyName(this Type type) {
if (type == null) return "null";
if (type == typeof(System.Object)) return "object";
if (type == typeof(float)) return "float";
else if (type == typeof(int)) return "int";
else if (type == typeof(long)) return "long";
else if (type == typeof(double)) return "double";
else if (type == typeof(string)) return "string";
else if (type == typeof(bool)) return "bool";
else if (type.IsGenericType) {
string s = "";
Type genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(List<>)) s = "List";
else s = type.GetGenericTypeDefinition().ToString();
Type[] types = type.GetGenericArguments();
string[] stypes = new string[types.Length];
for (int i = 0; i < types.Length; i++) {
stypes[i] = types[i].PrettyName();
}
return s + "<" + string.Join(", ", stypes) + ">";
} else if (type.IsArray) {
string rank = "";
for (int i = 1; i < type.GetArrayRank(); i++) {
rank += ",";
}
Type elementType = type.GetElementType();
if (!elementType.IsArray) return elementType.PrettyName() + "[" + rank + "]";
else {
string s = elementType.PrettyName();
int i = s.IndexOf('[');
return s.Substring(0, i) + "[" + rank + "]" + s.Substring(i);
}
} else return type.ToString();
}
}
}