Beispiel #1
0
        public int GetIntegerByIdentifier(string value)
        {
            var key = value.Substring(IntegerResourceIdentifier.Length);

            if (Integers.ContainsKey(key))
            {
                return(Integers[key]);
            }

            throw new UnknownResourceException("Integer", key);
        }
        /// <summary>
        /// Returns storage content.
        /// </summary>
        /// <param name="type">The type of the storage content.</param>
        /// <param name="name">The name of the storage content.</param>
        public static object GetObject(string type, string name)
        {
            switch (type.ToLower())
            {
            case "pokemon":
                if (Pokemons.ContainsKey(name))
                {
                    return(Pokemons[name]);
                }
                break;

            case "string":
            case "str":
                if (Strings.ContainsKey(name))
                {
                    return(Strings[name]);
                }
                break;

            case "integer":
            case "int":
                if (Integers.ContainsKey(name))
                {
                    return(Integers[name]);
                }
                break;

            case "boolean":
            case "bool":
                if (Booleans.ContainsKey(name))
                {
                    return(Booleans[name]);
                }
                break;

            case "item":
                if (Items.ContainsKey(name))
                {
                    return(Items[name]);
                }
                break;

            case "single":
            case "sng":
                if (Singles.ContainsKey(name))
                {
                    return(Singles[name]);
                }
                break;

            case "double":
            case "dbl":
                if (Doubles.ContainsKey(name))
                {
                    return(Doubles[name]);
                }
                break;
            }

            return(ScriptVersion2.ScriptComparer.DefaultNull);
        }
        /// <summary>
        /// Adds or updates storage content.
        /// </summary>
        /// <param name="type">The type of the storage content.</param>
        /// <param name="name">The name of the storage content.</param>
        /// <param name="newContent">The new storage content.</param>
        public static void SetObject(string type, string name, object newContent)
        {
            switch (type.ToLower())
            {
            case "pokemon":
                if (Pokemons.ContainsKey(name))
                {
                    Pokemons[name] = (BasePokemon)newContent;
                }
                else
                {
                    Pokemons.Add(name, (BasePokemon)newContent);
                }
                break;

            case "string":
            case "str":
                if (Strings.ContainsKey(name))
                {
                    Strings[name] = Convert.ToString(newContent);
                }
                else
                {
                    Strings.Add(name, Convert.ToString(newContent));
                }
                break;

            case "integer":
            case "int":
                if (Integers.ContainsKey(name))
                {
                    Integers[name] = @int(newContent);
                }
                else
                {
                    Integers.Add(name, @int(newContent));
                }
                break;

            case "boolean":
            case "bool":
                if (Booleans.ContainsKey(name))
                {
                    Booleans[name] = Convert.ToBoolean(newContent);
                }
                else
                {
                    Booleans.Add(name, Convert.ToBoolean(newContent));
                }
                break;

            case "item":
                if (Items.ContainsKey(name))
                {
                    Items[name] = (BaseItem)newContent;
                }
                else
                {
                    Items.Add(name, (BaseItem)newContent);
                }
                break;

            case "single":
            case "sng":
                if (Singles.ContainsKey(name))
                {
                    Singles[name] = sng(newContent);
                }
                else
                {
                    Singles.Add(name, sng(newContent));
                }
                break;

            case "double":
            case "dbl":
                if (Doubles.ContainsKey(name))
                {
                    Doubles[name] = dbl(newContent);
                }
                else
                {
                    Doubles.Add(name, dbl(newContent));
                }
                break;
            }
        }
Beispiel #4
0
        public void LoadValues()
        {
            var typesBinder = new MappedTypesBinder {
                KnownTypes =
                {
                    { "String",    typeof(StringResource)    },
                    { "Integer",   typeof(IntegerResource)   },
                    { "Dimension", typeof(DimensionResource) },
                    { "Colour",    typeof(ColourResource)    }
                }
            };

            foreach (var file in ValueFiles)
            {
                var path = Path.Combine(config.Paths.Values, file);
                log.Verbose(Tag, "Deserialising '{0}'", path);

                var content = File.ReadAllText(path);

                var values = JsonConvert.DeserializeObject <Dictionary <string, IValueResource> >(content, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Objects,
                    Binder           = typesBinder,
                    Converters       = new JsonConverter[] {
                        new DimensionConverter(this),
                        new ColourConverter(this),
                        new StringConverter(this),
                        new IntegerConverter(this)
                    }
                });

                values.ForEach(p => {
                    var key      = p.Key;
                    var resource = p.Value;

                    if (resource is DimensionResource)
                    {
                        if (Dimensions.ContainsKey(key))
                        {
                            throw new DuplicateValueNameException("Dimension", key);
                        }

                        Dimensions.Add(key, (resource as DimensionResource).Value);
                    }
                    else if (resource is IntegerResource)
                    {
                        if (Integers.ContainsKey(key))
                        {
                            throw new DuplicateValueNameException("Integer", key);
                        }

                        Integers.Add(key, (resource as IntegerResource).Value);
                    }
                    else if (resource is StringResource)
                    {
                        if (Strings.ContainsKey(key))
                        {
                            throw new DuplicateValueNameException("String", key);
                        }

                        Strings.Add(key, (resource as StringResource).Value);
                    }
                    else if (resource is ColourResource)
                    {
                        if (Colours.ContainsKey(key))
                        {
                            throw new DuplicateValueNameException("Colour", key);
                        }

                        Colours.Add(key, (resource as ColourResource).Value);
                    }
                });
            }
        }