123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System.Collections.Generic;
- using System.Linq;
- using Newtonsoft.Json.Linq;
- namespace WingServerCommon.Config.Parameters
- {
- public class ParameterSerializer
- {
- /// <summary>
- /// Deserialize json file to parameter
- /// </summary>
- /// <param name="filePath">File path that will be deserialize</param>
- /// <returns>Parameter list.</returns>
- public static IEnumerable<IParameter> Deserialize(string filePath)
- {
- var content = File.ReadAllText(filePath);
- var configCrypter = new ConfigCrypter();
- content = configCrypter.DecryptDES(content);
- var childrenList = JToken.Parse(content).Children();
- var parameters = new List<IParameter>();
- foreach (var child in childrenList)
- {
- var setionParameters = ParseTokenSetions(child);
- parameters.AddRange(setionParameters);
- }
- return parameters;
- }
- private static IEnumerable<IParameter> ParseTokenSetions(JToken children)
- {
- var item = (JProperty)children;
- var parsedParameters = ParseToken(item.Name, item.Value.Children()).ToList();
- return parsedParameters;
- }
- private static IEnumerable<IParameter> ParseToken(string sectionName, JEnumerable<JToken> childrenList)
- {
- var parameters = new List<IParameter>();
- foreach (var jToken in childrenList)
- {
- var item = (JProperty)jToken;
- if (item.Value is JArray jArray)
- {
- //TODO Do we have array parameter?
- continue;
- }
- if (item.Value is JObject jObject)
- {
- if (IsRangeParameter(jObject))
- {
- var parameter = ParseRangeParameter(jObject, item.Name);
- parameter.Section = sectionName;
- parameters.Add(parameter);
- continue;
- }
- if (IsEnumParameter(jObject))
- {
- var parameter = ParseEnumParameter(item.Name, jObject);
- parameter.Section = sectionName;
- parameters.Add(parameter);
- continue;
- }
- var parsedParameters = ParseToken(sectionName, jObject.Children()).ToList();
- parameters.AddRange(parsedParameters);
- }
- if (item.Value is JValue jValue)
- {
- var parameter = ParseBasicParameter(jValue, item.Name);
- parameter.Section = sectionName;
- parameters.Add(parameter);
- }
- }
- return parameters;
- }
- 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>()
- };
- //TODO other types
- }
- 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>() };
- }
- //TODO Implement FloatParameter
- //if (jValue.Type == JTokenType.Float)
- //{
- // return new FloatParameter { Name = key, Value = jValue.Value<double>() };
- //}
- throw new ArgumentException($"Invalid jValue type {jValue.Type}");
- }
- }
- }
|