Ejemplo n.º 1
0
        public override ShapeBase CreateShape(string classname, string name, PropertyEntryCollection properties)
        {
            Type t = GetTypeByName(classname, typeof(ShapeBase), ThrowExceptions);

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

            string moreFailInfo = "";

            try
            {
                object[] args = new object[1] {
                    name
                };                              // always use the "name" constructor
                ShapeBase newObject = Activator.CreateInstance(t, args) as ShapeBase;
                if (newObject == null)
                {
                    if (ThrowExceptions)
                    {
                        throw new Exception("Failed to create instance of class '" + classname + "'");
                    }
                    return(null);
                }

                RecursiveParseProperties(newObject, properties);
                return(newObject);
            }
            catch (Exception ex)
            {
                EditorManager.DumpException(ex, false);
                moreFailInfo = "\nDetailed: ";
                if (ex.InnerException != null)
                {
                    moreFailInfo += ex.InnerException.ToString();
                }
                else
                {
                    moreFailInfo += ex.Message;
                }
            }

            if (ThrowExceptions)
            {
                throw new Exception("Failed to create instance of class '" + classname + "'" + moreFailInfo);
            }

            return(null);
        }
        public override ShapeBase CreateShape(string classname, string name, PropertyEntryCollection properties)
        {
            Type t = GetTypeByName(classname,typeof(ShapeBase),ThrowExceptions);
              if (t == null)
            return null;

              string moreFailInfo = "";
              try
              {
            object[] args = new object[1] { name }; // always use the "name" constructor
            ShapeBase newObject = Activator.CreateInstance(t, args) as ShapeBase;
            if (newObject == null)
            {
              if (ThrowExceptions)
            throw new Exception("Failed to create instance of class '" + classname + "'");
              return null;
            }

            RecursiveParseProperties(newObject, properties);
            return newObject;
              }
              catch (Exception ex)
              {
            EditorManager.DumpException(ex, false);
            moreFailInfo = "\nDetailed: ";
            if (ex.InnerException != null)
              moreFailInfo += ex.InnerException.ToString();
            else
              moreFailInfo += ex.Message;
              }

              if (ThrowExceptions)
            throw new Exception("Failed to create instance of class '" + classname + "'" + moreFailInfo);

              return null;
        }
        public override void RecursiveParseProperties(object target, PropertyEntryCollection properties)
        {
            Type t = target.GetType();
              int iSuccess = 0;
              if (properties != null)
              {
            // match up properties from passed dictionary
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo prop in props)
            {
              if (!prop.CanWrite)
            continue;
              PropertyEntry entry;
              if (!properties.TryGetValue(prop.Name, out entry))
            continue;
              if (entry == null || (!entry.HasSubProperties && string.IsNullOrEmpty(entry.ValueString)))
            continue;

              iSuccess++;

              // convert string value to object
              object newValue = SerializationHelper.GetObjectFromStringData(prop.PropertyType, entry.ValueString, ",");

              if (newValue != null)
              {
            if ((newValue is string) && !string.IsNullOrEmpty(FilenameResolvePath))
            {
              object[] isFilename = prop.GetCustomAttributes(typeof(PrefabResolveFilenameAttribute), false);
              if (isFilename != null && isFilename.Length > 0)
              {
                //ensure that only backslashes are in the string. Otherwise could have mixed back and frontslashes in the string
                newValue = ((string)newValue).Replace("/", @"\");
                newValue = Path.Combine(FilenameResolvePath, (string)newValue);
                newValue = FileHelper.ResolveFilename((string)newValue);
              }
            }

            prop.SetValue(target, newValue, null);
              }

              // assume we have a class here:
              if (entry.SubProperties != null)
              {
            // is the property there already? otherwise create a new instance...
            object childObj = prop.GetValue(target, null);
            if (childObj == null)
            {
              throw new Exception("Prefab xml reading error: Property value is not set for '" + prop.Name + "'");
            }
            // parse the sub-property:
            RecursiveParseProperties(childObj, entry.SubProperties);
            continue; // everything OK
              }

              if (ThrowExceptions && SerializationHelper.LastException != null)
            throw SerializationHelper.LastException;
            }

            //////////////////////////////
            // The following check is for debug purposes only:
            // The number of successfully set properties should match the size of the passed property list.
            if (iSuccess != properties.Count)
            {
              string missingProperties = "";

              // At this point it is not very easy to find out *which* one was missing, so loop the other way around:
              string[] sourceKeys = properties.AllKeys;
              foreach (string key in sourceKeys)
              {
            bool bFound = false;
            bool bExact = false;
            foreach (PropertyInfo prop in props)
              if (prop.CanWrite && string.Compare(prop.Name, key, true) == 0)
              {
                bExact = string.Compare(prop.Name, key, false) == 0;
                bFound = true;
                break;
              }
            if (!bFound || !bExact)
            {
              if (!bFound)
                missingProperties += "\n" + key + " : missing";
              else if (!bExact)
                missingProperties += "\n" + key + " : Casing (warning)";
            }
              }

              Log.Error("The definition contains some invalid properties:\n" + missingProperties);
            }
            //////////////////////////////////
              }
        }
Ejemplo n.º 4
0
        public override void RecursiveParseProperties(object target, PropertyEntryCollection properties)
        {
            Type t        = target.GetType();
            int  iSuccess = 0;

            if (properties != null)
            {
                // match up properties from passed dictionary
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    if (!prop.CanWrite)
                    {
                        continue;
                    }
                    PropertyEntry entry;
                    if (!properties.TryGetValue(prop.Name, out entry))
                    {
                        continue;
                    }
                    if (entry == null || (!entry.HasSubProperties && string.IsNullOrEmpty(entry.ValueString)))
                    {
                        continue;
                    }

                    iSuccess++;

                    // convert string value to object
                    object newValue = SerializationHelper.GetObjectFromStringData(prop.PropertyType, entry.ValueString, ",");

                    if (newValue != null)
                    {
                        if ((newValue is string) && !string.IsNullOrEmpty(FilenameResolvePath))
                        {
                            object[] isFilename = prop.GetCustomAttributes(typeof(PrefabResolveFilenameAttribute), false);
                            if (isFilename != null && isFilename.Length > 0)
                            {
                                //ensure that only backslashes are in the string. Otherwise could have mixed back and frontslashes in the string
                                newValue = ((string)newValue).Replace("/", @"\");
                                newValue = Path.Combine(FilenameResolvePath, (string)newValue);
                                newValue = FileHelper.ResolveFilename((string)newValue);
                            }
                        }

                        prop.SetValue(target, newValue, null);
                    }

                    // assume we have a class here:
                    if (entry.SubProperties != null)
                    {
                        // is the property there already? otherwise create a new instance...
                        object childObj = prop.GetValue(target, null);
                        if (childObj == null)
                        {
                            throw new Exception("Prefab xml reading error: Property value is not set for '" + prop.Name + "'");
                        }
                        // parse the sub-property:
                        RecursiveParseProperties(childObj, entry.SubProperties);
                        continue; // everything OK
                    }

                    if (ThrowExceptions && SerializationHelper.LastException != null)
                    {
                        throw SerializationHelper.LastException;
                    }
                }

                //////////////////////////////
                // The following check is for debug purposes only:
                // The number of successfully set properties should match the size of the passed property list.
                if (iSuccess != properties.Count)
                {
                    string missingProperties = "";

                    // At this point it is not very easy to find out *which* one was missing, so loop the other way around:
                    string[] sourceKeys = properties.AllKeys;
                    foreach (string key in sourceKeys)
                    {
                        bool bFound = false;
                        bool bExact = false;
                        foreach (PropertyInfo prop in props)
                        {
                            if (prop.CanWrite && string.Compare(prop.Name, key, true) == 0)
                            {
                                bExact = string.Compare(prop.Name, key, false) == 0;
                                bFound = true;
                                break;
                            }
                        }
                        if (!bFound || !bExact)
                        {
                            if (!bFound)
                            {
                                missingProperties += "\n" + key + " : missing";
                            }
                            else if (!bExact)
                            {
                                missingProperties += "\n" + key + " : Casing (warning)";
                            }
                        }
                    }
                    EditorManager.EditorConsole.PrintError("The definition contains some invalid properties:\n" + missingProperties);
                }
                //////////////////////////////////
            }
        }
Ejemplo n.º 5
0
        public override void RecursiveParseProperties(object target, PropertyEntryCollection properties)
        {
            Type t = target.GetType();
              int iSuccess = 0;
              if (properties != null)
              {
            // match up properties from passed dictionary
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo prop in props)
            {
              if (!prop.CanWrite)
            continue;
              PropertyEntry entry;
              if (!properties.TryGetValue(prop.Name, out entry))
            continue;
              if (entry == null || (!entry.HasSubProperties && string.IsNullOrEmpty(entry.ValueString)))
            continue;

              iSuccess++;

              // special treatment for array
              if (prop.PropertyType.IsArray && entry.HasSubProperties)
              {
            List<object> array = new List<object>();
            Dictionary<string, PropertyEntry>.ValueCollection elements = entry.SubProperties.Values;
            foreach (PropertyEntry arrayElemEntry in elements)
            {
              Type elemType = prop.PropertyType.GetElementType();
              // Get TypeByName is not necessary here as we already have the type
              // and resolving by name can lead to wrong types when the type name is not unique (as in Vector3F).
              // this.GetTypeByName(arrayElemEntry.ValueString, typeof(object), ThrowExceptions);
              object arrayElem = Activator.CreateInstance(elemType);
              if (arrayElem != null)
              {
                RecursiveParseProperties(arrayElem, arrayElemEntry.SubProperties);
              }
              // fill up:
              for (int i = array.Count; i < arrayElemEntry.ArrayElementIndex; i++)
                array.Add(null);
              array.Add(arrayElem);
            }

            // set the array
            object[] args = new object[] { array.Count };
            object targetArrayObj = Activator.CreateInstance(prop.PropertyType, args);
            {
              Array targetArrayArr = (Array)targetArrayObj;
              for (int i = 0; i < array.Count; ++i)
              {
                targetArrayArr.SetValue(array[i], i);
              }
            }

            prop.SetValue(target, targetArrayObj, null);

            continue;
              }

              // convert string value to object
              object newValue = SerializationHelper.GetObjectFromStringData(prop.PropertyType, entry.ValueString, ",");

              if (newValue != null)
              {
            if ((newValue is string) && !string.IsNullOrEmpty(FilenameResolvePath))
            {
              object[] isFilename = prop.GetCustomAttributes(typeof(PrefabResolveFilenameAttribute), false);
              if (isFilename != null && isFilename.Length > 0)
              {
                //ensure that only backslashes are in the string. Otherwise could have mixed back and forward slashes in the string
                newValue = ((string)newValue).Replace("/", @"\");
                newValue = Path.Combine(FilenameResolvePath, (string)newValue);
                newValue = FileHelper.ResolveFilename((string)newValue);
              }
            }

            prop.SetValue(target, newValue, null);
              }

              // assume we have a class here:
              if (entry.SubProperties != null)
              {
            // is the property there already? otherwise create a new instance...
            object childObj = prop.GetValue(target, null);
            if (childObj == null)
            {
              throw new Exception("Prefab xml reading error: Property value is not set for '" + prop.Name + "'");
            }
            // parse the sub-property:
            RecursiveParseProperties(childObj, entry.SubProperties);
            continue; // everything OK
              }

              if (ThrowExceptions && SerializationHelper.LastException != null)
            throw SerializationHelper.LastException;
            }

            //////////////////////////////
            // The following check is for debug purposes only:
            // The number of successfully set properties should match the size of the passed property list.
            if (iSuccess != properties.Count)
            {
              string missingProperties = "";

              // At this point it is not very easy to find out *which* one was missing, so loop the other way around:
              string[] sourceKeys = properties.AllKeys;
              foreach (string key in sourceKeys)
              {
            bool bFound = false;
            bool bExact = false;
            foreach (PropertyInfo prop in props)
              if (prop.CanWrite && string.Compare(prop.Name, key, true) == 0)
              {
                bExact = string.Compare(prop.Name, key, false) == 0;
                bFound = true;
                break;
              }
            if (!bFound || !bExact)
            {
              if (!bFound)
                missingProperties += "\n" + key + " : missing";
              else if (!bExact)
                missingProperties += "\n" + key + " : Casing (warning)";
            }
              }

              Log.Error("The definition contains some invalid properties:\n" + missingProperties);
            }
            //////////////////////////////////
              }
        }
Ejemplo n.º 6
0
 public PropertyEntryCollectionWrapper(PropertyEntryCollection propertyEntryCollection)
 {
     this.propertyEntryCollection = propertyEntryCollection;
     this.customProperties        = new Collection <PropertyReferenceProperty>();
 }
Ejemplo n.º 7
0
        public override void RecursiveParseProperties(object target, PropertyEntryCollection properties)
        {
            Type t        = target.GetType();
            int  iSuccess = 0;

            if (properties != null)
            {
                // match up properties from passed dictionary
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    if (!prop.CanWrite)
                    {
                        continue;
                    }
                    PropertyEntry entry;
                    if (!properties.TryGetValue(prop.Name, out entry))
                    {
                        continue;
                    }
                    if (entry == null || (!entry.HasSubProperties && string.IsNullOrEmpty(entry.ValueString)))
                    {
                        continue;
                    }

                    iSuccess++;

                    // special treatment for array
                    if (prop.PropertyType.IsArray && entry.HasSubProperties)
                    {
                        List <object> array = new List <object>();
                        Dictionary <string, PropertyEntry> .ValueCollection elements = entry.SubProperties.Values;
                        foreach (PropertyEntry arrayElemEntry in elements)
                        {
                            Type elemType = prop.PropertyType.GetElementType();
                            // Get TypeByName is not necessary here as we already have the type
                            // and resolving by name can lead to wrong types when the type name is not unique (as in Vector3F).
                            // this.GetTypeByName(arrayElemEntry.ValueString, typeof(object), ThrowExceptions);
                            object arrayElem = Activator.CreateInstance(elemType);
                            if (arrayElem != null)
                            {
                                RecursiveParseProperties(arrayElem, arrayElemEntry.SubProperties);
                            }
                            // fill up:
                            for (int i = array.Count; i < arrayElemEntry.ArrayElementIndex; i++)
                            {
                                array.Add(null);
                            }
                            array.Add(arrayElem);
                        }

                        // set the array
                        object[] args           = new object[] { array.Count };
                        object   targetArrayObj = Activator.CreateInstance(prop.PropertyType, args);
                        {
                            Array targetArrayArr = (Array)targetArrayObj;
                            for (int i = 0; i < array.Count; ++i)
                            {
                                targetArrayArr.SetValue(array[i], i);
                            }
                        }

                        prop.SetValue(target, targetArrayObj, null);

                        continue;
                    }

                    // convert string value to object
                    object newValue = SerializationHelper.GetObjectFromStringData(prop.PropertyType, entry.ValueString, ",");

                    if (newValue != null)
                    {
                        if ((newValue is string) && !string.IsNullOrEmpty(FilenameResolvePath))
                        {
                            object[] isFilename = prop.GetCustomAttributes(typeof(PrefabResolveFilenameAttribute), false);
                            if (isFilename != null && isFilename.Length > 0)
                            {
                                //ensure that only backslashes are in the string. Otherwise could have mixed back and forward slashes in the string
                                newValue = ((string)newValue).Replace("/", @"\");
                                newValue = Path.Combine(FilenameResolvePath, (string)newValue);
                                newValue = FileHelper.ResolveFilename((string)newValue);
                            }
                        }

                        prop.SetValue(target, newValue, null);
                    }

                    // assume we have a class here:
                    if (entry.SubProperties != null)
                    {
                        // is the property there already? otherwise create a new instance...
                        object childObj = prop.GetValue(target, null);
                        if (childObj == null)
                        {
                            throw new Exception("Prefab xml reading error: Property value is not set for '" + prop.Name + "'");
                        }
                        // parse the sub-property:
                        RecursiveParseProperties(childObj, entry.SubProperties);
                        continue; // everything OK
                    }

                    if (ThrowExceptions && SerializationHelper.LastException != null)
                    {
                        throw SerializationHelper.LastException;
                    }
                }

                //////////////////////////////
                // The following check is for debug purposes only:
                // The number of successfully set properties should match the size of the passed property list.
                if (iSuccess != properties.Count)
                {
                    string missingProperties = "";

                    // At this point it is not very easy to find out *which* one was missing, so loop the other way around:
                    string[] sourceKeys = properties.AllKeys;
                    foreach (string key in sourceKeys)
                    {
                        bool bFound = false;
                        bool bExact = false;
                        foreach (PropertyInfo prop in props)
                        {
                            if (prop.CanWrite && string.Compare(prop.Name, key, true) == 0)
                            {
                                bExact = string.Compare(prop.Name, key, false) == 0;
                                bFound = true;
                                break;
                            }
                        }
                        if (!bFound || !bExact)
                        {
                            if (!bFound)
                            {
                                missingProperties += "\n" + key + " : missing";
                            }
                            else if (!bExact)
                            {
                                missingProperties += "\n" + key + " : Casing (warning)";
                            }
                        }
                    }

                    Log.Error("The definition contains some invalid properties:\n" + missingProperties);
                }
                //////////////////////////////////
            }
        }