Esempio n. 1
0
    public static LevelData GetLevelData(Dictionary <string, object> data)
    {
        LevelData levelData = new LevelData();
        Type      type      = typeof(LevelData);

        foreach (string attribute in data.Keys)
        {
            PropertyInfo propertyInfo = type.GetProperty(attribute, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (propertyInfo == null)
            {
                Debug.Log(string.Format("Illegal property in Level : '{0}'", attribute));
                throw new System.Exception(string.Format("Illegal property in Level : '{0}'", attribute));
            }
            object valueOb = data[attribute];
            if (propertyInfo.PropertyType.IsEnum)
            {
                try{
                    System.Convert.ToInt32(valueOb);
                }catch {
                    valueOb = Enum.Parse(propertyInfo.PropertyType, (string)valueOb);
                }
                if (!Enum.IsDefined(propertyInfo.PropertyType, valueOb))
                {
                    Debug.Log(string.Format("Illegal {0} value :{1}", propertyInfo.PropertyType, valueOb));
                    throw new System.ArgumentException(string.Format("Illegal {0} value :{1}", propertyInfo.PropertyType, valueOb));
                }
            }
            if (valueOb.GetType() != propertyInfo.PropertyType)
            {
                valueOb = System.Convert.ChangeType(valueOb, propertyInfo.PropertyType);
            }
            propertyInfo.SetValue(levelData, valueOb, null);
        }
        if (!levelData.CheckFields())
        {
            Debug.Log("Failing to pass fields test");
            throw new System.Exception("Failing to pass fields test");
        }
        return(levelData);
    }