i write c# plugin this is my codeCode:"Items": { "explosive.timed": { "info": 1, "min": 2, "max": 1000 }, "explosives": { "info": 1, "min": 2, "max": 1000 } }
Code:public class ItemInfo { public ItemInfo(int info, int min, int max) { this.info = info; this.min = min; this.max = max; } public int info; public int min; public int max; } Dictionary<string, ItemInfo> InfoItem = new Dictionary<string, ItemInfo>(); if (Config["Items"] == null) { var items = new Dictionary<string, ItemInfo>() { // shortname {"explosive.timed", new ItemInfo(1, 2, 1000)}, {"explosives", new ItemInfo(1, 2, 1000)} }; Config["Items"] = items; }
how can i read this from config file
Read this config? (C#)
Discussion in 'Rust Development' started by WX-78, Dec 21, 2015.
-
Thank you.. but i can't solve my problem is entry key should have own vars, i can save but i can't read i try to cast them to type it's not work anymore
-
This is how my plugin Kill Feed gets the values inside the configuration file on startup:
Note: It's slightly different from the implementation i use in my plugin.Code:/// <summary> /// Responsible for getting a value from the configuration file. /// </summary> /// <typeparam name="T"> The type that should be returned.</typeparam> /// <param name="defaultValue"> The defaultValue that will be returned if the actual value cannot be found.</param> /// <param name="firstKey"> The first key in a hierarchical structure to get the value.</param> /// <param name="secondKey"> The second key in a hierarchical structure to get the value.</param> /// <param name="thirdKey"> The third key in a hierarchical structure to get the value.</param> /// <returns> Returns either the defaultValue or the value of the configuration file associated with the provided keys.</returns> T GetConfig<T>(T defaultValue, string firstKey, string secondKey = null, string thirdKey = null) { try { object value; // get the value associated with the provided keys if (thirdKey != null) { value = Config[firstKey, secondKey, thirdKey]; } else if (secondKey != null) { value = Config[firstKey, secondKey]; } else { value = Config[firstKey]; } // if the value is a dictionary, add the key/value pairs to a dictionary and return it // this particular implementation only handles dictionarys with string key/value pairs if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>)) // checks if the value is a dictionary { IDictionary valueDictionary = (IDictionary)value; Dictionary<string, string> values = new Dictionary<string, string>(); foreach (object key in valueDictionary.Keys) { if (valueDictionary[key] is string && !string.IsNullOrEmpty((string)valueDictionary[key])) { values.Add((string)key, (string)valueDictionary[key]); } } return (T)Convert.ChangeType(values, typeof(T)); } // if the value is a list, add the list elements to a list and return it // this particular implementation only handles lists with char elements else if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>)) // checks if the value is a list { IList valueList = (IList)value; List<char> values = new List<char>(); foreach (object obj in valueList) { if (obj is string) { char result; if (char.TryParse((string)obj, out result)) { values.Add(result); } } } return (T)Convert.ChangeType(values, typeof(T)); } // handles every other type else { return (T)Convert.ChangeType(value, typeof(T)); } } catch (Exception) { return defaultValue; } } -
thank you very much, i will try later..
[DOUBLEPOST=1450729173,1450706199][/DOUBLEPOST]can u show me example please? i'm so confuse. -
Imagine this configuration file:
In order to get the value of "bar", which is 12, you need to use both keys. The first key is "foo" and the second key is "bar". To get the value you simply write the following, using the GetConfig() function I provided above:Code:{ "foo": { "bar": 12, } }
The first parameter, i.e. 10, denotes the default value that will be provided instead of the actual value if a proper value cannot be found inside the configuration file.Code:void Example() { int bar = GetConfig(10, "foo", "bar"); // do something with bar }Last edited by a moderator: Dec 21, 2015
