123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System.Collections.Concurrent;
- using Newtonsoft.Json.Linq;
- namespace WingServerCommon.Config.Parameters
- {
- public class ParameterDictionarySerializer
- {
-
-
-
-
-
-
- public static ConcurrentDictionary<string, IParameter> Deserialize(string str, int type = 0)
- {
- var content = "";
- if (type == 0)
- {
- content = File.ReadAllText(str);
- }
- else
- {
- content = str;
- }
-
- var configCrypter = new ConfigCrypter();
- content = configCrypter.DecryptDES(content);
- var childrenList = JToken.Parse(content).Children();
- var parameters = new ConcurrentDictionary<string, IParameter>();
- foreach (var child in childrenList)
- {
- ParseTokenSetions(child, ref parameters);
- }
- return parameters;
- }
- private static void ParseTokenSetions(JToken children, ref ConcurrentDictionary<string, IParameter> dic)
- {
- var item = (JProperty)children;
- ParseToken(item.Name, item.Value.Children(), ref dic);
- }
- private static void ParseToken(string sectionName, JEnumerable<JToken> childrenList, ref ConcurrentDictionary<string, IParameter> parameters)
- {
- foreach (var jToken in childrenList)
- {
- var item = (JProperty)jToken;
- if (item.Value is JArray jArray)
- {
-
- continue;
- }
- if (item.Value is JObject jObject)
- {
- if (IsRangeParameter(jObject))
- {
- var parameter = ParseRangeParameter(jObject, item.Name);
- parameter.Section = sectionName;
- var rangeKey = parameter.Section + "_" + parameter.Name;
- parameters.TryAdd(rangeKey, parameter);
- continue;
- }
- if (IsEnumParameter(jObject))
- {
- var parameter = ParseEnumParameter(item.Name, jObject);
- parameter.Section = sectionName;
- var enumKey = parameter.Section + "_" + parameter.Name;
- parameters.TryAdd(enumKey, parameter);
- continue;
- }
- ParseToken(sectionName, jObject.Children(), ref parameters);
- }
- if (item.Value is JValue jValue)
- {
- var parameter = ParseBasicParameter(jValue, item.Name);
- parameter.Section = sectionName;
- var valueKey = parameter.Section + "_" + parameter.Name;
- parameters.TryAdd(valueKey, parameter);
- }
- }
- }
- private static bool IsEnumParameter(JObject jObject)
- {
- return jObject.Children().Any(m => ((JProperty)m).Name == "Source");
- }
- private static bool IsRangeParameter(JObject jObject)
- {
- return jObject.Children().Any(m => ((JProperty)m).Name == "MaxValue");
- }
- private static IParameter ParseEnumParameter(string name, JObject jObject)
- {
- var defaultValue = jObject["DefaultValue"]?.Value<JValue>();
- switch (defaultValue?.Type)
- {
- case JTokenType.String:
- return new EnumStringParameter
- {
- Name = name,
- Source = jObject["Source"].Values<string>().ToList(),
- Value = defaultValue.Value<string>()
- };
-
- }
- throw new ArgumentException($"Invalid enum parameter {name}.");
- }
- private static IParameter ParseRangeParameter(JObject jObject, string key)
- {
- var max = jObject["MaxValue"].Value<JValue>();
- switch (max.Type)
- {
- case JTokenType.Integer:
- {
- if (max.Value<long>() > int.MaxValue)
- {
- var defaultValue = jObject["DefaultValue"].Value<long>();
- var minValue = jObject["MinValue"].Value<long>();
- var maxValue = max.Value<long>();
- var longRangeValue = new RangeLongParameter
- {
- Name = key,
- Value = defaultValue,
- MaxValue = maxValue,
- MinValue = minValue
- };
- }
- else
- {
- var defaultValue = jObject["DefaultValue"].Value<int>();
- var minValue = jObject["MinValue"].Value<int>();
- var maxValue = max.Value<int>();
- var intRangeValue = new RangeIntParameter
- {
- Name = key,
- Value = defaultValue,
- MaxValue = maxValue,
- MinValue = minValue
- };
- }
- break;
- }
- }
- throw new ArgumentException($"Invalid range parameter with name {key}.");
- }
- private static IParameter ParseBasicParameter(JValue jValue, string key)
- {
- if (jValue.Type == JTokenType.Integer)
- {
- return new IntParameter { Name = key, Value = jValue.Value<int>() };
- }
- if (jValue.Type == JTokenType.Boolean)
- {
- return new BoolParameter { Name = key, Value = jValue.Value<bool>() };
- }
- if (jValue.Type == JTokenType.String)
- {
- return new StringParameter { Name = key, Value = jValue.Value<string>() };
- }
-
-
-
-
-
- throw new ArgumentException($"Invalid jValue type {jValue.Type}");
- }
- }
- }
|