Esempio n. 1
0
        public void CanGetBooleanValues()
        {
            string       test = "boolean: true";
            IObjectStore n    = test.ParseYaml();

            Assert.True(n.GetBool("boolean"));

            string falsies = "boolean: false";

            n = falsies.ParseYaml();
            Assert.False(n.GetBool("boolean"));
        }
Esempio n. 2
0
 public Skill(IObjectStore data)
 {
     Name                 = data.GetString("name");
     Ability              = data.GetEnum <AbilityScoreTypes>("ability");
     TrainingRequired     = data.GetBool("trained");
     Description          = data.GetStringOptional("description");
     UseArmorCheckPenalty = data.GetBoolOptional("armor-check-penalty");
 }
Esempio n. 3
0
        public static T Deserialize <T>(this IObjectStore data, T result)
        {
            var typeInfo   = typeof(T);
            var properties = typeInfo.GetProperties();

            foreach (var prop in properties)
            {
                var attribute = prop.GetCustomAttribute <ObjectStoreAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                var    propType      = prop.PropertyType.Name.ToLower();
                object propertyValue = null;
                object defaultValue  = attribute.DefaultValue;
                bool   useDefault    = defaultValue != null;
                string keyName       = attribute.PropertyName;

                switch (propType)
                {
                case "single":
                    if (attribute.Optional)
                    {
                        if (useDefault)
                        {
                            propertyValue = data.GetFloatOptional(keyName, (float)defaultValue);
                        }
                        else
                        {
                            propertyValue = data.GetFloatOptional(keyName);
                        }
                    }
                    else
                    {
                        propertyValue = data.GetFloat(keyName);
                    }
                    break;

                case "int32":
                    if (attribute.Optional)
                    {
                        if (useDefault)
                        {
                            propertyValue = data.GetIntegerOptional(keyName, (int)defaultValue);
                        }
                        else
                        {
                            propertyValue = data.GetIntegerOptional(keyName);
                        }
                    }
                    else
                    {
                        propertyValue = data.GetInteger(keyName);
                    }
                    break;

                case "string":
                    if (attribute.Optional)
                    {
                        if (useDefault)
                        {
                            propertyValue = data.GetStringOptional(keyName, defaultValue.ToString());
                        }
                        else
                        {
                            propertyValue = data.GetStringOptional(keyName);
                        }
                    }
                    else
                    {
                        propertyValue = data.GetString(keyName);
                    }
                    break;

                case "string[]":
                    if (attribute.Optional)
                    {
                        propertyValue = data.GetListOptional(keyName);
                    }
                    else
                    {
                        propertyValue = data.GetList(keyName);
                    }
                    break;

                case "boolean":
                    if (attribute.Optional)
                    {
                        if (useDefault)
                        {
                            propertyValue = data.GetBoolOptional(keyName, (bool)defaultValue);
                        }
                        else
                        {
                            propertyValue = data.GetBoolOptional(keyName);
                        }
                    }
                    else
                    {
                        propertyValue = data.GetBool(keyName);
                    }
                    break;

                case "movementtype":
                    //TODO: Cannot just keep adding types here, but let's see how common it is first
                    propertyValue = data.GetEnum <MovementType>(keyName);
                    break;

                case "cup":
                    propertyValue = DiceStrings.ParseDice(data.GetString(keyName));
                    break;

                case "spelltype":
                    propertyValue = data.GetEnum <Spells.SpellType>(keyName);
                    break;

                case "object[]":
                    var objectList = data.GetObjectList(keyName);
                    var newList    = new List <object>();
                    foreach (var o in objectList)
                    {
                        var instance = o.GetString(SERIALIZED_TYPE_KEY).Instantiate <object>(o);
                        newList.Add(instance);
                    }
                    propertyValue = newList.ToArray();
                    break;

                default:
                    ShortLog.DebugFormat("Attempting to deserialize: {0}", propType);
                    if (prop.PropertyType.IsEnum)
                    {
                        propertyValue = System.Enum.Parse(prop.PropertyType, data.GetString(keyName), true);
                    }
                    else
                    {
                        propertyValue = DeserializeObject(data, keyName, attribute.Optional, prop.PropertyType);
                    }
                    break;
                }
                prop.SetValue(result, propertyValue);
            }

            return(result);
        }