internal override void Read(ResourceReader reader)
        {
            int propertyCount = reader.ReadInt32();

            for (int i = 0; i < propertyCount; i++)
            {
                var type = ( UserPropertyValueType )reader.ReadInt32();
                var name = reader.ReadStringWithHash(Version);
                var size = reader.ReadInt32();

                UserProperty property;
                switch (type)
                {
                case UserPropertyValueType.Int:
                    property = new UserIntProperty(name, reader.ReadInt32());
                    break;

                case UserPropertyValueType.Float:
                    property = new UserFloatProperty(name, reader.ReadSingle());
                    break;

                case UserPropertyValueType.Bool:
                    property = new UserBoolProperty(name, reader.ReadBoolean());
                    break;

                case UserPropertyValueType.String:
                    property = new UserStringProperty(name, reader.ReadString(size - 1));
                    break;

                case UserPropertyValueType.ByteVector3:
                    property = new UserByteVector3Property(name, reader.ReadByteVector3());
                    break;

                case UserPropertyValueType.ByteVector4:
                    property = new UserByteVector4Property(name, reader.ReadByteVector4());
                    break;

                case UserPropertyValueType.Vector3:
                    property = new UserVector3Property(name, reader.ReadVector3());
                    break;

                case UserPropertyValueType.Vector4:
                    property = new UserVector4Property(name, reader.ReadVector4());
                    break;

                case UserPropertyValueType.ByteArray:
                    property = new UserByteArrayProperty(name, reader.ReadBytes(size));
                    break;

                default:
                    throw new InvalidDataException($"Unknown node property type: {type}");
                }

                Add(property);
            }
        }
Ejemplo n.º 2
0
        private static void ConvertAssimpMetadataToProperties(Ai.Metadata metadata, Node node)
        {
            foreach (var metadataEntry in metadata)
            {
                UserProperty property = null;

                // Skip some garbage entries
                if (metadataEntry.Key == "IsNull" ||
                    metadataEntry.Key == "InheritType" ||
                    metadataEntry.Key == "DefaultAttributeIndex" ||
                    metadataEntry.Key == "UserProperties" ||  // dupe of UDP3DSMAX
                    metadataEntry.Key == "MaxHandle")
                {
                    continue;
                }

                if (metadataEntry.Key == "UDP3DSMAX")
                {
                    var properties = (( string )metadataEntry.Value.Data)
                                     .Split(new[] { "&cr;&lf;", "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                    if (properties.Length == 0)
                    {
                        continue;
                    }

                    foreach (var propertyString in properties)
                    {
                        // Parse property string
                        KeyValuePair <string, string> kvp;
                        if (propertyString.Contains('='))
                        {
                            var split = propertyString.Split('=');
                            kvp = new KeyValuePair <string, string>(split[0].TrimEnd(), split[1].TrimStart());
                        }
                        else
                        {
                            var split = propertyString.Split(' ');
                            kvp = new KeyValuePair <string, string>(split[0], split.Length > 1 ? split[1] : null);
                        }

                        // Parse value
                        if (kvp.Value == null)
                        {
                            // Assume flag bool
                            property = new UserBoolProperty(kvp.Key, true);
                        }
                        else if (kvp.Value.StartsWith("[") && kvp.Value.EndsWith("]"))
                        {
                            // Array/Vector
                            var arrayContents = kvp.Value.Substring(1, kvp.Value.Length - 2);
                            var arrayValues   = arrayContents.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            var arrayFloatValues = new List <float>();
                            foreach (var arrayValue in arrayValues)
                            {
                                if (!float.TryParse(arrayValue, out var arrayFloatValue))
                                {
                                    throw new Exception($"Failed to parse array user property value as float: {arrayValue}");
                                }

                                arrayFloatValues.Add(arrayFloatValue);
                            }

                            if (arrayFloatValues.Count == 3)
                            {
                                property = new UserVector3Property(kvp.Key, new Vector3(arrayFloatValues[0], arrayFloatValues[1], arrayFloatValues[2]));
                            }
                            else if (arrayFloatValues.Count == 4)
                            {
                                property = new UserVector4Property(kvp.Key, new Vector4(arrayFloatValues[0], arrayFloatValues[1], arrayFloatValues[2], arrayFloatValues[3]));
                            }
                            else
                            {
                                var arrayByteValues = arrayFloatValues.Cast <byte>();
                                property = new UserByteArrayProperty(kvp.Key, arrayByteValues.ToArray());
                            }
                        }
                        else if (int.TryParse(kvp.Value, out int intValue))
                        {
                            property = new UserIntProperty(kvp.Key, intValue);
                        }
                        else if (float.TryParse(kvp.Value, out float floatValue))
                        {
                            property = new UserFloatProperty(kvp.Key, floatValue);
                        }
                        else if (bool.TryParse(kvp.Value, out bool boolValue))
                        {
                            property = new UserBoolProperty(kvp.Key, boolValue);
                        }
                        else
                        {
                            property = new UserStringProperty(kvp.Key, kvp.Value);
                        }
                    }
                }
                else
                {
                    switch (metadataEntry.Value.DataType)
                    {
                    case Ai.MetaDataType.Bool:
                        property = new UserBoolProperty(metadataEntry.Key, metadataEntry.Value.DataAs <bool>().Value);
                        break;

                    case Ai.MetaDataType.Int:
                        property = new UserIntProperty(metadataEntry.Key, metadataEntry.Value.DataAs <int>().Value);
                        break;

                    case Ai.MetaDataType.UInt64:
                        property = new UserByteArrayProperty(metadataEntry.Key, BitConverter.GetBytes(metadataEntry.Value.DataAs <ulong>().Value));
                        break;

                    case Ai.MetaDataType.Float:
                        property = new UserFloatProperty(metadataEntry.Key, metadataEntry.Value.DataAs <float>().Value);
                        break;

                    case Ai.MetaDataType.String:
                        property = new UserStringProperty(metadataEntry.Key, ( string )metadataEntry.Value.Data);
                        break;

                    case Ai.MetaDataType.Vector3D:
                        var data = metadataEntry.Value.DataAs <Ai.Vector3D>().Value;
                        property = new UserVector3Property(metadataEntry.Key, new Vector3(data.X, data.Y, data.Z));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                if (property == null)
                {
                    throw new Exception("Property shouldn't be null");
                }

                node.Properties.Add(property.Name, property);
            }
        }