Exemplo n.º 1
0
        public static Type GetCustomNodeEditorFor(Type nodeType)
        {
            if (_allNodes == null)
            {
                _allNodes = ReflectionUtility.GetAllClassesWithAttribute(typeof(CustomNodeEditorAttribute)).ToArray();
            }

            foreach (var node in _allNodes)
            {
                var customEditor = (CustomNodeEditorAttribute)node.GetCustomAttributes(typeof(CustomNodeEditorAttribute), true).FirstOrDefault();
                if (customEditor != null)
                {
                    if (customEditor.type.IsGenericTypeDefinition &&
                        nodeType.IsGenericType &&
                        customEditor.type == nodeType.GetGenericTypeDefinition())
                    {
                        return(node);
                    }

                    if (customEditor.type == nodeType)
                    {
                        return(node);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Fill this data model with a collection reference.
        /// Gets the collection data from the collection and stores it in this serializable model.
        /// </summary>
        public void FromContainer(IInventoryItemContainer container)
        {
            // Serialize based on inventory item serialization model.
            items = new InventoryItemSerializationModel[container.items.Length];
            for (int i = 0; i < container.items.Length; i++)
            {
                var item = container.items[i];
                InventoryItemSerializationModel inst = null;
                if (item != null)
                {
                    var  classes            = ReflectionUtility.GetAllClassesWithAttribute(typeof(SerializationModelAttribute), true);
                    Type serializationModel = typeof(InventoryItemSerializationModel);

                    foreach (var c in classes)
                    {
                        var attrib = (SerializationModelAttribute)c.GetCustomAttributes(typeof(SerializationModelAttribute), true).First();
                        if (c == item.GetType())
                        {
                            DevdogLogger.LogVerbose("Using custom serialization model for " + item.GetType().Name + " - " + attrib.type.Name);
                            serializationModel = attrib.type;
                        }
                    }

                    inst = (InventoryItemSerializationModel)Activator.CreateInstance(serializationModel);
                    inst.FromItem(item);
                }
                else
                {
                    inst = new InventoryItemSerializationModel(null);
                }

                items[i] = inst;
            }
        }
Exemplo n.º 3
0
        static ReflectionDrawerUtility()
        {
            _customDrawerAttributes = new Dictionary <Type, CustomDrawerAttribute>();
            var customDrawerClasses = ReflectionUtility.GetAllClassesWithAttribute(typeof(CustomDrawerAttribute));

            foreach (var c in customDrawerClasses)
            {
                _customDrawerAttributes[c] = (CustomDrawerAttribute)c.GetCustomAttributes(typeof(CustomDrawerAttribute), true).FirstOrDefault();
            }
        }
        static ObjectPickerUtility()
        {
            _allEditorTypes = ReflectionUtility.GetAllClassesWithAttribute(typeof(CustomObjectPickerAttribute), false);
            foreach (var type in _allEditorTypes)
            {
                if (typeof(ObjectPickerBaseEditor).IsAssignableFrom(type) == false)
                {
                    DevdogLogger.LogError("Class " + type.Name + " has the " + typeof(CustomObjectPickerAttribute).Name + " attribute, but doesn't inherit from " + typeof(ObjectPickerBaseEditor).Name);
                }
            }

            _allEditorTypes = _allEditorTypes.OrderByDescending(o => ((CustomObjectPickerAttribute)o.GetCustomAttributes(typeof(CustomObjectPickerAttribute), true).First()).priority).ToArray();
            foreach (var type in _allEditorTypes)
            {
                _allEditorTypeAttributes[type] = (CustomObjectPickerAttribute)type.GetCustomAttributes(typeof(CustomObjectPickerAttribute), true).First();
            }
        }
Exemplo n.º 5
0
        // TODO: Check if this is working...
        public override void UpdateIssue()
        {
            var types = ReflectionUtility.GetAllClassesWithAttribute(typeof(ReplacedByAttribute), true);

            foreach (var currentType in types)
            {
                var newComponentType = (ReplacedByAttribute)currentType.GetCustomAttributes(typeof(ReplacedByAttribute), true).First();
                if (typeof(UnityEngine.Component).IsAssignableFrom(currentType))
                {
                    var components = Resources.FindObjectsOfTypeAll(currentType).Cast <UnityEngine.Component>().ToArray();
                    foreach (var component in components)
                    {
                        try
                        {
                            var tempComponent = component;
                            var tempNewType   = newComponentType;
                            issues.Add(new GameRuleIssue("Deprecated type " + tempComponent.GetType() + " is used", MessageType.Error, new GameRuleAction("Fix (replace)",
                                                                                                                                                          () =>
                            {
                                if (tempComponent != null && tempComponent.gameObject != null)
                                {
                                    var newComponent = tempComponent.gameObject.AddComponent(tempNewType.type);
                                    ReflectionUtility.CopySerializableValues(tempComponent, newComponent);
                                    UnityEngine.Object.DestroyImmediate(tempComponent, true);
                                }
                            }), new GameRuleAction("Select object", () =>
                            {
                                SelectObject(tempComponent);
                            })));
                        }
                        catch (Exception)
                        {
                            // Ignored
                        }

                        UnityEditor.EditorUtility.SetDirty(component);
                    }
                }
            }
        }
        /// <summary>
        /// Fill this data model with a collection reference.
        /// Gets the collection data from the collection and stores it in this serializable model.
        /// </summary>
        /// <param name="collection"></param>
        public void FromCollection(ItemCollectionBase collection)
        {
            currencies = collection.currenciesGroup.lookups.Select(o => new CurrencyDecoratorSerializationModel(o)).ToArray();
            //            items = collection.items.Select(o => new InventoryItemSerializationModel(o.item)).ToArray();


            // Serialize based on inventory item serialization model.
            items = new InventoryItemSerializationModel[collection.items.Length];
            for (int i = 0; i < collection.items.Length; i++)
            {
                var item = collection.items[i];
                InventoryItemSerializationModel inst = null;
                if (item.item != null)
                {
                    var  classes            = ReflectionUtility.GetAllClassesWithAttribute(typeof(SerializationModelAttribute), true);
                    Type serializationModel = typeof(InventoryItemSerializationModel);

                    foreach (var c in classes)
                    {
                        var attrib = (SerializationModelAttribute)c.GetCustomAttributes(typeof(SerializationModelAttribute), true).First();
                        if (c == item.item.GetType())
                        {
                            DevdogLogger.LogVerbose("Using custom serialization model for " + item.item.GetType().Name + " - " + attrib.type.Name);
                            serializationModel = attrib.type;
                        }
                    }

                    inst = (InventoryItemSerializationModel)Activator.CreateInstance(serializationModel);
                    inst.FromItem(item.item);
                }
                else
                {
                    inst = new InventoryItemSerializationModel(item.item);
                }

                items[i] = inst;
            }
        }
Exemplo n.º 7
0
//        public static void SelectPage(string path)
//        {
//            throw new NotImplementedException();
//        }

        public virtual void CreateEditors()
        {
            _editors.Clear();

            var dict = new Dictionary <string, EmptyEditor>();

            var foundEditors = ReflectionUtility.GetAllClassesWithAttribute(typeof(EditorPageAttribute), true).ToList();

            foundEditors.Sort((a, b) =>
            {
                var first  = (EditorPageAttribute)a.GetCustomAttributes(typeof(EditorPageAttribute), true).First();
                var second = (EditorPageAttribute)b.GetCustomAttributes(typeof(EditorPageAttribute), true).First();

                if (first.order > second.order)
                {
                    return(1);
                }
                else if (first.order < second.order)
                {
                    return(-1);
                }

                return(0);
            });

            foreach (var foundEditor in foundEditors)
            {
                var attribute = (EditorPageAttribute)foundEditor.GetCustomAttributes(typeof(EditorPageAttribute), true).First();
                var pathSplit = attribute.path.Split('/');
                if (pathSplit.Length == 1 || pathSplit.Length == 2)
                {
                    if (dict.ContainsKey(pathSplit[0]) == false)
                    {
                        dict[pathSplit[0]] = new EmptyEditor(pathSplit[0], this);
                    }

                    var constructor = foundEditor.GetConstructor(new[] { typeof(EditorWindow) });
                    if (constructor != null)
                    {
                        var inst = (IEditorCrud)constructor.Invoke(new object[] { this });
                        dict[pathSplit[0]].childEditors.Add(inst);
                    }
                    else
                    {
                        Debug.Log("[Editor] Couldn't initialize editor page, constructor needs to take 1 argument (EditorWindow): parent", this);
                    }
                }
                else
                {
                    Debug.Log("[Editor] Couldn't initialize editor with attribute path: " + attribute.path, this);
                }
            }

            _editors.AddRange(dict.Select(o => o.Value));

            _toolbarIndex = 0;
            if (_editors.Count > _toolbarIndex)
            {
                _editors[_toolbarIndex].Focus();
            }
        }