Beispiel #1
0
        public static void GenerateAOTClasses(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var spoofTypes = defaultSpoofTypes;

            spoofTypes.AddRange(UserTypePrefs.GetPreferedTypesList(typeof(object), false).Where(t => t.IsValueType && !spoofTypes.Contains(t)));
            var types = EditorUtils.GetAssemblyTypes(typeof(object)).Where(t => t.RTGetAttribute <SpoofAOTAttribute>(true) != null).ToList();

            var nClasses = 0;
            var nMethods = 0;

            var sb = new StringBuilder();

            sb.AppendLine("#pragma warning disable 0219, 0168, 0612");
            sb.AppendLine("namespace ParadoxNotion.Internal{");
            sb.AppendLine();
            sb.AppendLine("	//Auto generated classes for AOT support, where using undeclared generic classes with value types is limited. These are not actualy used but rather just declared for the compiler");
            sb.AppendLine("	class AOTDummy{");
            sb.AppendLine();
            sb.AppendLine("		object o = null;");



            foreach (var type in types)
            {
                if (!type.IsAbstract && type.IsGenericTypeDefinition && type.GetGenericArguments().Length == 1)
                {
                    var constrains = type.GetGenericArguments()[0].GetGenericParameterConstraints();
                    if (constrains.Length == 0 || constrains[0].IsValueType)
                    {
                        if (typeof(Delegate).IsAssignableFrom(type))
                        {
                            nClasses++;
                            sb.AppendLine(string.Format("		void {0}()", type.FriendlyName(true).Replace(".", "_").Replace("<T>", "_Delegate")) + "{");
                            foreach (var spoofType in spoofTypes)
                            {
                                var a = type.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">");
                                var b = "_" + type.FriendlyName().Replace("<T>", "_" + spoofType.Name);
                                sb.AppendLine(string.Format("			{0} {1} = null;", a, b));
                            }
                            sb.AppendLine("		}");
                        }
                        else
                        {
                            foreach (var spoofType in spoofTypes)
                            {
                                nClasses++;
                                sb.AppendLine(string.Format("		class {0} : {1}",
                                                            type.FriendlyName(true).Replace(".", "_").Replace("<T>", "_" + spoofType.Name),
                                                            type.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">")) + "{}");
                            }
                        }

                        sb.AppendLine();
                    }
                }
            }

            foreach (var type in types)
            {
                var index = 0;
                foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    if (method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 1)
                    {
                        var constrains = method.GetGenericArguments()[0].GetGenericParameterConstraints();
                        if (constrains.Length == 0 || constrains[0].IsValueType)
                        {
                            index++;

                            var decType = method.DeclaringType;
                            var varName = "_" + decType.Name;
                            sb.AppendLine(string.Format("		void {0}_{1}_{2}()", decType.FullName.Replace(".", "_"), method.Name, index) + " {");
                            if (!method.IsStatic)
                            {
                                sb.AppendLine(string.Format("			{0} {1} = null;", decType.FullName, varName));
                            }

                            foreach (var spoofType in spoofTypes)
                            {
                                nMethods++;
                                var a            = method.IsStatic? decType.FullName : varName;
                                var b            = method.Name;
                                var c            = spoofType.FullName;
                                var paramsString = "";
                                var parameters   = method.GetParameters();
                                for (var i = 0; i < parameters.Length; i++)
                                {
                                    var parameter = parameters[i];
                                    var toString  = parameter.ParameterType.FullName;
                                    if (parameter.ParameterType.IsGenericParameter)
                                    {
                                        toString = spoofType.FullName;
                                    }
                                    if (parameter.ParameterType.IsGenericType)
                                    {
                                        toString = parameter.ParameterType.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">");
                                        toString = toString.Replace("[[T]]", "");
                                    }
                                    paramsString += string.Format("({0})o", toString);
                                    if (i < parameters.Length - 1)
                                    {
                                        paramsString += ", ";
                                    }
                                }
                                var d = paramsString;
                                sb.AppendLine(string.Format("			{0}.{1}<{2}>( {3} );", a, b, c, d));
                            }

                            sb.AppendLine("		}");
                            sb.AppendLine();
                        }
                    }
                }
            }

            //custom stuff
            sb.AppendLine("		void CustomSpoof(){");
            foreach (var spoofType in spoofTypes)
            {
                sb.AppendLine(string.Format("			System.Action<{0}> Action_{1};", spoofType.FullName, spoofType.Name));
                sb.AppendLine(string.Format("			System.Func<{0}> Func_{1};", spoofType.FullName, spoofType.Name));
                sb.AppendLine(string.Format("			System.Collections.Generic.IList<{0}> List_{1};", spoofType.FullName, spoofType.Name));
                sb.AppendLine(string.Format("			System.Collections.Generic.IDictionary<System.String, {0}> Dict_{1};", spoofType.FullName, spoofType.Name));
            }
            sb.AppendLine("		}");

            sb.AppendLine("	}");
            sb.AppendLine("}");
            sb.AppendLine();
            sb.AppendLine(string.Format("//{0} Classes | {1} Methods", nClasses, nMethods));
            sb.AppendLine("#pragma warning restore 0219, 0168, 0612");

            File.WriteAllText(path, sb.ToString());
        }
 //Executes the item's registered delegate
 void ExecuteItemFunc(EditorUtils.MenuItemInfo item)
 {
     if (item.func != null){
         item.func();
     } else {
         item.func2(item.userData);
     }
     base.editorWindow.Close();
 }
Beispiel #3
0
        ///Get icon for type
        public static Texture GetTypeIcon(MemberInfo info, bool fallbackToDefault = true)
        {
            // if (!EditorGUIUtility.isProSkin){ return null; }
            if (info == null)
            {
                return(null);
            }
            var type = info is Type ? info as Type : info.ReflectedType;

            if (type == null)
            {
                return(null);
            }

            Texture texture = null;

            if (typeIcons.TryGetValue(type.FullName, out texture))
            {
                if (texture != null)
                {
                    if (texture.name != DEFAULT_TYPE_ICON_NAME || fallbackToDefault)
                    {
                        return(texture);
                    }
                }
                return(null);
            }

            if (texture == null)
            {
                if (type.IsEnumerableCollection())
                {
                    var elementType = type.GetEnumerableElementType();
                    if (elementType != null)
                    {
                        texture = GetTypeIcon(elementType);
                    }
                }
            }

            if (typeof(UnityEngine.Object).IsAssignableFrom(type))
            {
                texture = AssetPreview.GetMiniTypeThumbnail(type);
                if (texture == null && (typeof(MonoBehaviour).IsAssignableFrom(type) || typeof(ScriptableObject).IsAssignableFrom(type)))
                {
                    texture = EditorGUIUtility.ObjectContent(EditorUtils.MonoScriptFromType(type), null).image;
                    if (texture == null)
                    {
                        texture = Icons.csIcon;
                    }
                }
            }

            if (texture == null)
            {
                texture = Resources.Load <Texture>(IMPLICIT_ICONS_PATH + type.FullName);
            }

            ///Explicit icons not in dark theme
            if (EditorGUIUtility.isProSkin)
            {
                if (texture == null)
                {
                    var iconAtt = type.RTGetAttribute <IconAttribute>(true);
                    if (iconAtt != null)
                    {
                        texture = GetTypeIcon(iconAtt, null);
                    }
                }
            }

            if (texture == null)
            {
                var current = type.BaseType;
                while (current != null)
                {
                    texture = Resources.Load <Texture>(IMPLICIT_ICONS_PATH + current.FullName);
                    current = current.BaseType;
                    if (texture != null)
                    {
                        break;
                    }
                }
            }

            if (texture == null)
            {
                texture = Resources.Load <Texture>(IMPLICIT_ICONS_PATH + DEFAULT_TYPE_ICON_NAME);
            }

            typeIcons[type.FullName] = texture;

            if (texture != null)     //it should not be
            {
                if (texture.name != DEFAULT_TYPE_ICON_NAME || fallbackToDefault)
                {
                    return(texture);
                }
            }
            return(null);
        }
Beispiel #4
0
        //Show stuff
        public override void OnGUI(Rect rect)
        {
            var e = Event.current;

            EditorGUIUtility.SetIconSize(Vector2.zero);
            hoveringIndex = Mathf.Clamp(hoveringIndex, -1, currentNode.children.Count - 1);

            ///MAIN AREA
            mainRect = new Rect(rect.x, rect.y, rect.width, rect.height - helpRectHeight);
            if (EditorGUIUtility.isProSkin)
            {
                GUI.Box(mainRect, "", (GUIStyle)"PreBackground");
            }
            GUILayout.BeginArea(mainRect);

            //HEADER
            GUILayout.Space(2);
            GUILayout.Label(string.Format("<color=#{0}><size=15><b>{1}</b></size></color>", EditorGUIUtility.isProSkin? "dddddd" : "222222", title), headerStyle);

            ///SEARCH
            if (e.keyCode == KeyCode.DownArrow)
            {
                GUIUtility.keyboardControl = 0;
            }
            if (e.keyCode == KeyCode.UpArrow)
            {
                GUIUtility.keyboardControl = 0;
            }
            // GUILayout.Space(2);
            GUILayout.BeginHorizontal();
            GUI.SetNextControlName("SearchToolbar");
            search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField");
            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = string.Empty;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            searchMode      = (SearchMode)EditorGUILayout.EnumPopup(searchMode);
            filterFavorites = EditorGUILayout.ToggleLeft("Filter Favorites", filterFavorites);
            GUILayout.EndHorizontal();

            EditorUtils.BoldSeparator();

            ///BACK
            if (currentNode.parent != null && string.IsNullOrEmpty(search))
            {
                GUILayout.BeginHorizontal("box");
                if (GUILayout.Button(string.Format("<b><size=14>◄ {0}/{1}</size></b>", currentNode.parent.name, currentNode.name), (GUIStyle)"label"))
                {
                    currentNode = currentNode.parent;
                }
                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();
                if (lastRect.Contains(e.mousePosition))
                {
                    GUI.color = hoverColor;
                    GUI.DrawTexture(lastRect, EditorGUIUtility.whiteTexture);
                    GUI.color = Color.white;
                    base.editorWindow.Repaint();
                    hoveringIndex = -1;
                }
            }

            //Go back with right click as well...
            if (e.type == EventType.MouseDown && e.button == 1)
            {
                e.Use();
                if (currentNode.parent != null)
                {
                    currentNode = currentNode.parent;
                }
            }


            ///TREE
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
            GUILayout.BeginVertical();

            if (search != lastSearch)
            {
                hoveringIndex = 0;
                if (!string.IsNullOrEmpty(search) && search.Length >= 2)
                {
                    var searchRootNode = new Node()
                    {
                        name = "Search Root"
                    };
                    var a = search.ToUpper();
                    foreach (var node in leafNodes)
                    {
                        var b     = node.name.ToUpper();
                        var match = false;
                        if (searchMode == SearchMode.Contains)
                        {
                            match = true;
                            var words = a.Split(' ');
                            foreach (var word in words)
                            {
                                if (!b.Contains(word))
                                {
                                    match = false;
                                    break;
                                }
                            }
                        }
                        if (searchMode == SearchMode.StartsWith && b.StartsWith(a))
                        {
                            match = true;
                        }
                        if (match)
                        {
                            searchRootNode.children[node.name] = node;
                        }
                    }
                    currentNode = searchRootNode;
                }
                else
                {
                    currentNode = rootNode;
                }
                lastSearch = search;
            }



            var  i          = 0;
            var  itemAdded  = false;
            Node lastParent = null;

            foreach (var childPair in currentNode.children)
            {
                var node = childPair.Value;

                if (search != null && search.Length >= 2)
                {
                    var currentParent = node.parent;
                    if (currentParent != lastParent)
                    {
                        lastParent = currentParent;
                        GUI.color  = EditorGUIUtility.isProSkin? Color.black : Color.white;
                        GUILayout.BeginHorizontal("box");
                        GUI.color = Color.white;
                        if (GUILayout.Button(currentParent.unfolded? "▼" : "▶", (GUIStyle)"label", GUILayout.Width(16)))
                        {
                            currentParent.unfolded = !currentParent.unfolded;
                        }
                        GUILayout.Label(string.Format("<size=12><b>{0}</b></size>", currentParent.fullPath));
                        GUILayout.EndHorizontal();
                    }

                    if (!node.parent.unfolded)
                    {
                        continue;
                    }
                }

                if (filterFavorites && !node.isFavorite && !node.HasAnyFavoriteChild())
                {
                    continue;
                }

                itemAdded = true;
                var leafItem     = node.item;
                var icon         = leafItem != null? leafItem.content.image : EditorUtils.folderIcon;
                var itemDisabled = leafItem != null && leafItem.func == null && leafItem.func2 == null;

                GUI.color = EditorGUIUtility.isProSkin? Color.white : new Color(0.8f, 0.8f, 0.8f, 1);
                GUILayout.BeginHorizontal("box");
                GUI.color = Color.white;

                //Prefix icon
                GUILayout.Label(icon, GUILayout.Width(32), GUILayout.Height(16));

                GUI.enabled = !itemDisabled;

                //Favorite
                GUI.color = node.isFavorite? Color.white : (node.HasAnyFavoriteChild()? new Color(1, 1, 1, 0.2f) : new Color(0f, 0f, 0f, 0.4f));
                if (GUILayout.Button(EditorUtils.favoriteIcon, GUIStyle.none, GUILayout.Width(16), GUILayout.Height(16)))
                {
                    node.ToggleFavorite();
                }
                GUI.color = Color.white;

                //Content
                var label = node.name;
                if (search != null && search.Length >= 2)                  //simple highlight
                {
                    label = Regex.Replace(label, search, "<b>$&</b>", RegexOptions.IgnoreCase);
                }

                if (GUILayout.Button(string.Format("<size=10>{0}</size>",
                                                   (leafItem == null? string.Format("<b>{0}</b>", label) : label)),
                                     (GUIStyle)"label", GUILayout.Width(0), GUILayout.ExpandWidth(true)))
                {
                    if (leafItem != null)
                    {
                        ExecuteItemFunc(leafItem);
                        break;
                    }
                    else
                    {
                        currentNode   = node;
                        hoveringIndex = 0;
                        break;
                    }
                }

                //Suffix icon
                GUILayout.Label(leafItem != null? "<b>+</b>" : "►", GUILayout.Width(20));

                GUILayout.EndHorizontal();
                var lastRect = GUILayoutUtility.GetLastRect();

                if (lastRect.Contains(e.mousePosition) && e.type == EventType.MouseMove)
                {
                    hoveringIndex = i;
                }

                if (hoveringIndex == i)
                {
                    GUI.color = hoverColor;
                    GUI.DrawTexture(lastRect, EditorGUIUtility.whiteTexture);
                    GUI.color = Color.white;
                    base.editorWindow.Repaint();
                }

                i++;

                GUI.enabled = true;
            }


            if (!itemAdded)
            {
                GUILayout.Label("No results to display with current search and filter combination");
            }

            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
            GUILayout.EndArea();


            GUILayout.FlexibleSpace();


            ///HELP AREA
            var hoveringNode = hoveringIndex >= 0 && currentNode.children.Count > 0? currentNode.children.Values.ToList()[hoveringIndex] : null;
            var helpRect     = new Rect(rect.x + 2, rect.yMax - helpRectHeight + 2, rect.width - 4, helpRectHeight - 2);

            GUI.color = new Color(0, 0, 0, 0.3f);
            GUI.Box(helpRect, "", (GUIStyle)"TextField");
            GUI.color = Color.white;
            GUILayout.BeginArea(helpRect);
            GUILayout.BeginVertical();
            var doc = string.Empty;

            if (hoveringNode != null && hoveringNode.item != null)
            {
                doc = hoveringNode.item.content.tooltip;
                var memberInfo = hoveringNode.item.userData as MemberInfo;
                if (memberInfo != null && string.IsNullOrEmpty(doc))
                {
                    doc = DocsByReflection.GetMemberSummary(memberInfo);
                }
            }
            GUILayout.Label(doc, EditorStyles.wordWrappedLabel);
            GUILayout.EndVertical();
            GUILayout.EndArea();


            //handle the events
            HandeEvents(e);
            if (!init)
            {
                init = true;
                EditorGUI.FocusTextInControl("SearchToolbar");
            }

            EditorGUIUtility.SetIconSize(Vector2.zero);
        }
Beispiel #5
0
 public override object OnGUI(GUIContent content, object instance)
 {
     return(EditorUtils.DrawEditorFieldDirect(content, instance, objectType, context, fieldInfo, content.text, attributes));
 }