Exemplo n.º 1
0
    private void ReadPropertiesObject(JSONObject propsObject, PropertiesObject obj)
    {
        if (propsObject["properties"] != null)
        {
            foreach (JSONNode propNode in propsObject["properties"].AsArray)
            {
                JSONArray propArray   = propNode.AsArray;
                string    name        = propArray[0];
                string    valueString = propArray[1];

                bool     foundProp = false;
                Property prop      = new Property(null, null, null, null, null);
                foreach (Property checkProp in Property.JoinProperties(
                             obj.Properties(), obj.DeprecatedProperties()))
                {
                    if (checkProp.name == name)
                    {
                        prop      = checkProp;
                        foundProp = true;
                        break;
                    }
                }
                if (!foundProp)
                {
                    warnings.Add("Unrecognized property: " + name);
                    continue;
                }

                System.Type propType;
                if (propArray.Count > 2)
                {
                    propType = System.Type.GetType(propArray[2]); // explicit type
                }
                else
                {
                    propType = prop.value.GetType();
                }

                if (propType == typeof(Material))
                {
                    // skip equality check
                    prop.setter(ReadMaterial(propArray[1].AsObject));
                }
                else
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(propType);
                    using (var textReader = new StringReader(valueString))
                    {
                        // skip equality check. important if this is an EntityReference,
                        // since EntityReference.Equals gets the entity which may not exist yet
                        prop.setter(xmlSerializer.Deserialize(textReader));
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    private void ReadPropertiesObject(MessagePackObjectDictionary propsDict, PropertiesObject obj)
    {
        if (propsDict.ContainsKey(FileKeys.PROPOBJ_PROPERTIES))
        {
            foreach (var propObj in propsDict[FileKeys.PROPOBJ_PROPERTIES].AsList())
            {
                var    propList = propObj.AsList();
                string id       = propList[0].AsString();

                bool     foundProp = false;
                Property prop      = new Property(null, null, null, null, null);
                foreach (Property checkProp in Property.JoinProperties(
                             obj.Properties(), obj.DeprecatedProperties()))
                {
                    if (checkProp.id == id)
                    {
                        prop      = checkProp;
                        foundProp = true;
                        break;
                    }
                }
                if (!foundProp)
                {
                    warnings.Add("Unrecognized property: " + id);
                    continue;
                }

                System.Type propType;
                if (propList.Count > 2)
                {
                    propType = System.Type.GetType(propList[2].AsString()); // explicit type
                }
                else
                {
                    propType = prop.value.GetType();
                }

                if (propType == typeof(Material))
                {
                    // skip equality check
                    prop.setter(ReadMaterial(propList[1].AsDictionary(), false, null));
                }
                else if (propType == typeof(Texture2D))
                {
                    Texture2D tex = new Texture2D(2, 2);
                    if (!ImageConversion.LoadImage(tex, propList[1].AsBinary()))
                    {
                        warnings.Add("Error reading texture data");
                    }
                    prop.setter(tex);
                }
                else if (propType == typeof(EmbeddedData))
                {
                    var dataList = propList[1].AsList();
                    var name     = dataList[0].AsString();
                    var type     = (EmbeddedDataType)System.Enum.Parse(typeof(EmbeddedDataType), dataList[1].AsString());
                    var bytes    = dataList[2].AsBinary();
                    prop.setter(new EmbeddedData(name, bytes, type));
                }
                else // not a special type
                {
                    string        valueString   = propList[1].AsString();
                    XmlSerializer xmlSerializer = new XmlSerializer(propType);
                    using (var textReader = new StringReader(valueString))
                    {
                        // skip equality check. important if this is an EntityReference,
                        // since EntityReference.Equals gets the entity which may not exist yet
                        prop.setter(xmlSerializer.Deserialize(textReader));
                    }
                }
            }
        }
    }