コード例 #1
0
ファイル: BoxData.cs プロジェクト: zerodowned/annox
        /// <summary>
        /// Gets a BoxMobile object from a type
        /// </summary>
        /// <param name="t">The type to evaluate</param>
        /// <returns>A BoxMobile object or null if the type can't be constructed properly</returns>
        public static BoxMobile FromType(Type t)
        {
            ConstructorInfo[] ctors = t.GetConstructors();

            bool      constructable = false;
            BoxMobile mobile        = null;

            foreach (ConstructorInfo c in ctors)
            {
                if (BoxUtil.IsConstructable(c))
                {
                    constructable = true;
                    break;
                }
            }

            if (constructable)
            {
                mobile        = new BoxMobile();
                mobile.m_Name = t.Name;

                foreach (ConstructorInfo c in ctors)
                {
                    if (BoxUtil.IsConstructable(c))
                    {
                        ParameterInfo[] pms = c.GetParameters();

                        if (pms.Length == 0)
                        {
                            try
                            {
                                Mobile theMobile = (Mobile)Activator.CreateInstance(t);

                                mobile.m_Art = theMobile.Body.BodyID;
                                mobile.Hue   = theMobile.Hue;

                                if (theMobile != null)
                                {
                                    theMobile.Delete();
                                }
                            }
                            catch
                            {
                                // TODO : Logging?
                                return(null);
                            }
                        }
                        else if (pms.Length == 1 && (pms[0].ParameterType == typeof(string)))
                        {
                            mobile.CanBeNamed = true;
                        }
                    }
                }
            }

            return(mobile);
        }
コード例 #2
0
ファイル: Item.cs プロジェクト: Orion321/unknown-shard
        /// <summary>
        /// Creates a BoxItem definition from a type
        /// </summary>
        /// <param name="type">The type that will be analyzed</param>
        /// <returns>The corresponding BoxItem object, null if the object can't be constructed</returns>
        public static BoxItem FromType(Type type)
        {
            BoxItem item = new BoxItem();

            ConstructorInfo[] ctors = type.GetConstructors();

            item.m_Name = type.Name;

            foreach (ConstructorInfo ctor in ctors)
            {
                if (BoxUtil.IsConstructable(ctor))
                {
                    int NumOfParams = ctor.GetParameters().Length;

                    if (NumOfParams == 0)
                    {
                        item.m_EmptyCtor = true;
                        item.m_Item      = ItemDef.GetItemDef(type);
                    }
                    else if (NumOfParams <= 2)
                    {
                        if (item.m_AdditionalCtors == null)
                        {
                            item.m_AdditionalCtors = new ArrayList();
                        }

                        item.m_AdditionalCtors.Add(ConstructorDef.FromConstructorInfo(ctor));
                    }
                }
            }

            if (item.m_EmptyCtor || item.m_AdditionalCtors != null)
            {
                // If there's no default constructor, try to get the ItemDef from the additional constructors
                if (!item.m_EmptyCtor)
                {
                    // Get the first available def
                    foreach (ConstructorDef cDef in item.m_AdditionalCtors)
                    {
                        if (cDef.DefaultArt != null)
                        {
                            item.m_Item = cDef.DefaultArt.Clone() as ItemDef;
                            break;
                        }
                    }
                }

                return(item);
            }
            else
            {
                return(null);
            }
        }