ParameterSerializer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Newtonsoft.Json.Linq;
  4. namespace WingServerCommon.Config.Parameters
  5. {
  6. public class ParameterSerializer
  7. {
  8. /// <summary>
  9. /// Deserialize json file to parameter
  10. /// </summary>
  11. /// <param name="filePath">File path that will be deserialize</param>
  12. /// <returns>Parameter list.</returns>
  13. public static IEnumerable<IParameter> Deserialize(string filePath)
  14. {
  15. var content = File.ReadAllText(filePath);
  16. var configCrypter = new ConfigCrypter();
  17. content = configCrypter.DecryptDES(content);
  18. var childrenList = JToken.Parse(content).Children();
  19. var parameters = new List<IParameter>();
  20. foreach (var child in childrenList)
  21. {
  22. var setionParameters = ParseTokenSetions(child);
  23. parameters.AddRange(setionParameters);
  24. }
  25. return parameters;
  26. }
  27. private static IEnumerable<IParameter> ParseTokenSetions(JToken children)
  28. {
  29. var item = (JProperty)children;
  30. var parsedParameters = ParseToken(item.Name, item.Value.Children()).ToList();
  31. return parsedParameters;
  32. }
  33. private static IEnumerable<IParameter> ParseToken(string sectionName, JEnumerable<JToken> childrenList)
  34. {
  35. var parameters = new List<IParameter>();
  36. foreach (var jToken in childrenList)
  37. {
  38. var item = (JProperty)jToken;
  39. if (item.Value is JArray jArray)
  40. {
  41. //TODO Do we have array parameter?
  42. continue;
  43. }
  44. if (item.Value is JObject jObject)
  45. {
  46. if (IsRangeParameter(jObject))
  47. {
  48. var parameter = ParseRangeParameter(jObject, item.Name);
  49. parameter.Section = sectionName;
  50. parameters.Add(parameter);
  51. continue;
  52. }
  53. if (IsEnumParameter(jObject))
  54. {
  55. var parameter = ParseEnumParameter(item.Name, jObject);
  56. parameter.Section = sectionName;
  57. parameters.Add(parameter);
  58. continue;
  59. }
  60. var parsedParameters = ParseToken(sectionName, jObject.Children()).ToList();
  61. parameters.AddRange(parsedParameters);
  62. }
  63. if (item.Value is JValue jValue)
  64. {
  65. var parameter = ParseBasicParameter(jValue, item.Name);
  66. parameter.Section = sectionName;
  67. parameters.Add(parameter);
  68. }
  69. }
  70. return parameters;
  71. }
  72. private static bool IsEnumParameter(JObject jObject)
  73. {
  74. return jObject.Children().Any(m => ((JProperty)m).Name == "Source");
  75. }
  76. private static bool IsRangeParameter(JObject jObject)
  77. {
  78. return jObject.Children().Any(m => ((JProperty)m).Name == "MaxValue");
  79. }
  80. private static IParameter ParseEnumParameter(string name, JObject jObject)
  81. {
  82. var defaultValue = jObject["DefaultValue"]?.Value<JValue>();
  83. switch (defaultValue?.Type)
  84. {
  85. case JTokenType.String:
  86. return new EnumStringParameter
  87. {
  88. Name = name,
  89. Source = jObject["Source"].Values<string>().ToList(),
  90. Value = defaultValue.Value<string>()
  91. };
  92. //TODO other types
  93. }
  94. throw new ArgumentException($"Invalid enum parameter {name}.");
  95. }
  96. private static IParameter ParseRangeParameter(JObject jObject, string key)
  97. {
  98. var max = jObject["MaxValue"].Value<JValue>();
  99. switch (max.Type)
  100. {
  101. case JTokenType.Integer:
  102. {
  103. if (max.Value<long>() > int.MaxValue)
  104. {
  105. var defaultValue = jObject["DefaultValue"].Value<long>();
  106. var minValue = jObject["MinValue"].Value<long>();
  107. var maxValue = max.Value<long>();
  108. var longRangeValue = new RangeLongParameter
  109. {
  110. Name = key,
  111. Value = defaultValue,
  112. MaxValue = maxValue,
  113. MinValue = minValue
  114. };
  115. }
  116. else
  117. {
  118. var defaultValue = jObject["DefaultValue"].Value<int>();
  119. var minValue = jObject["MinValue"].Value<int>();
  120. var maxValue = max.Value<int>();
  121. var intRangeValue = new RangeIntParameter
  122. {
  123. Name = key,
  124. Value = defaultValue,
  125. MaxValue = maxValue,
  126. MinValue = minValue
  127. };
  128. }
  129. break;
  130. }
  131. }
  132. throw new ArgumentException($"Invalid range parameter with name {key}.");
  133. }
  134. private static IParameter ParseBasicParameter(JValue jValue, string key)
  135. {
  136. if (jValue.Type == JTokenType.Integer)
  137. {
  138. return new IntParameter { Name = key, Value = jValue.Value<int>() };
  139. }
  140. if (jValue.Type == JTokenType.Boolean)
  141. {
  142. return new BoolParameter { Name = key, Value = jValue.Value<bool>() };
  143. }
  144. if (jValue.Type == JTokenType.String)
  145. {
  146. return new StringParameter { Name = key, Value = jValue.Value<string>() };
  147. }
  148. //TODO Implement FloatParameter
  149. //if (jValue.Type == JTokenType.Float)
  150. //{
  151. // return new FloatParameter { Name = key, Value = jValue.Value<double>() };
  152. //}
  153. throw new ArgumentException($"Invalid jValue type {jValue.Type}");
  154. }
  155. }
  156. }