ConfigItem.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using NTERA.EmuEra.Game.EraEmu.Sub;
  5. namespace NTERA.EmuEra.Game.EraEmu.Config
  6. {
  7. internal abstract class AConfigItem
  8. {
  9. public AConfigItem(ConfigCode code, string text)
  10. {
  11. Code = code;
  12. Name = code.ToString();
  13. Text = text;
  14. }
  15. public static ConfigItem<T> Copy<T>(ConfigItem<T> other)
  16. {
  17. if(other == null)
  18. return null;
  19. ConfigItem<T> ret = new ConfigItem<T>(other.Code, other.Text, other.Value);
  20. ret.Fixed = other.Fixed;
  21. return ret;
  22. }
  23. public abstract void CopyTo(AConfigItem other);
  24. public abstract bool TryParse(string tokens);
  25. public abstract void SetValue<U>(U p);
  26. public abstract U GetValue<U>();
  27. public abstract string ValueToString();
  28. public readonly ConfigCode Code;
  29. public readonly string Name;
  30. public readonly string Text;
  31. public bool Fixed;
  32. }
  33. internal sealed class ConfigItem<T> : AConfigItem
  34. {
  35. public ConfigItem(ConfigCode code,string text, T t):base(code, text)
  36. {
  37. val = t;
  38. }
  39. private T val;
  40. public T Value
  41. {
  42. get => val;
  43. set
  44. {
  45. if(Fixed)
  46. return;
  47. val = value;
  48. }
  49. }
  50. public override void CopyTo(AConfigItem other)
  51. {
  52. ConfigItem<T> item = ((ConfigItem<T>)other);
  53. item.Fixed = false;
  54. item.Value = Value;
  55. item.Fixed = Fixed;
  56. }
  57. public override void SetValue<U>(U p)
  58. {
  59. //if (this is ConfigItem<U>)
  60. ((ConfigItem<U>)(AConfigItem)this).Value = p;
  61. //else
  62. // throw new ExeEE("型が一致しない");
  63. }
  64. public override U GetValue<U>()
  65. {
  66. ////if (this is ConfigItem<U>)
  67. return ((ConfigItem<U>)(AConfigItem)this).Value;
  68. //throw new ExeEE("型が一致しない");
  69. }
  70. public override string ValueToString()
  71. {
  72. if(this is ConfigItem<bool>)
  73. {
  74. //ConfigItem<T>をConfigItem<bool>に直接キャストすることはできない
  75. bool b = ((ConfigItem<bool>)(AConfigItem)this).Value;
  76. if (b)
  77. return "YES";
  78. return "NO";
  79. }
  80. if (this is ConfigItem<Color>)
  81. {
  82. Color c = ((ConfigItem<Color>)(AConfigItem)this).Value;
  83. return string.Format("{0},{1},{2}", c.R, c.G, c.B);
  84. }
  85. return val.ToString();
  86. }
  87. public override string ToString()
  88. {
  89. return Text + ":" + ValueToString();
  90. }
  91. /// ジェネリック化大失敗。なんかうまい方法ないかな~
  92. public override bool TryParse(string param)
  93. {
  94. bool ret = false;
  95. if ((param == null) || (param.Length == 0))
  96. return false;
  97. if(Fixed)
  98. return false;
  99. string str = param.Trim();
  100. if (this is ConfigItem<bool>)
  101. {
  102. bool b = false;
  103. ret = tryStringToBool(str, ref b);
  104. if (ret)//ConfigItem<T>をConfigItem<bool>に直接キャストすることはできない
  105. ((ConfigItem<bool>)(AConfigItem)this).Value = b;
  106. }
  107. else if (this is ConfigItem<Color>)
  108. {
  109. Color c;
  110. ret = tryStringsToColor(str, out c);
  111. if (ret)
  112. ((ConfigItem<Color>)(AConfigItem)this).Value = c;
  113. else
  114. throw new CodeEE("Value can not be recognized as Color specifier");
  115. }
  116. else if (this is ConfigItem<char>)
  117. {
  118. char c;
  119. ret = char.TryParse(str, out c);
  120. if (ret)
  121. ((ConfigItem<char>)(AConfigItem)this).Value = c;
  122. }
  123. else if (this is ConfigItem<Int32>)
  124. {
  125. Int32 i;
  126. ret = Int32.TryParse(str, out i);
  127. if (ret)
  128. ((ConfigItem<Int32>)(AConfigItem)this).Value = i;
  129. else
  130. throw new CodeEE("Numeric config item contains non-numeric characters");
  131. }
  132. else if (this is ConfigItem<Int64>)
  133. {
  134. Int64 i;
  135. ret = Int64.TryParse(str, out i);
  136. if (ret)
  137. ((ConfigItem<Int64>)(AConfigItem)this).Value = i;
  138. else
  139. throw new CodeEE("Numeric config item contains non-numeric characters");
  140. }
  141. else if (this is ConfigItem<List<Int64>>)
  142. {
  143. ((ConfigItem<List<Int64>>)(AConfigItem)this).Value.Clear();
  144. Int64 i;
  145. string[] strs = str.Split('/');
  146. foreach (string st in strs)
  147. {
  148. ret = Int64.TryParse(st.Trim(), out i);
  149. if (ret)
  150. ((ConfigItem<List<Int64>>)(AConfigItem)this).Value.Add(i);
  151. else
  152. {
  153. throw new CodeEE("Numeric config item contains non-numeric characters");
  154. }
  155. }
  156. }
  157. else if (this is ConfigItem<string>)
  158. {
  159. ret = true;
  160. ((ConfigItem<string>)(AConfigItem)this).Value = str;
  161. }
  162. else if (this is ConfigItem<List<string>>)
  163. {
  164. ret = true;
  165. ((ConfigItem<List<string>>)(AConfigItem)this).Value.Add(str);
  166. }
  167. else if (this is ConfigItem<TextDrawingMode>)
  168. {
  169. str = str.ToUpper();
  170. ret = Enum.IsDefined(typeof(TextDrawingMode), str);
  171. if (ret)
  172. {
  173. ((ConfigItem<TextDrawingMode>)(AConfigItem)this).Value
  174. = (TextDrawingMode)Enum.Parse(typeof(TextDrawingMode), str);
  175. }
  176. else
  177. throw new CodeEE("Invalid specification");
  178. }
  179. else if (this is ConfigItem<ReduceArgumentOnLoadFlag>)
  180. {
  181. str = str.ToUpper();
  182. ret = Enum.IsDefined(typeof(ReduceArgumentOnLoadFlag), str);
  183. if (ret)
  184. {
  185. ((ConfigItem<ReduceArgumentOnLoadFlag>)(AConfigItem)this).Value
  186. = (ReduceArgumentOnLoadFlag)Enum.Parse(typeof(ReduceArgumentOnLoadFlag), str);
  187. }
  188. else
  189. throw new CodeEE("Invalid specification");
  190. }
  191. else if (this is ConfigItem<DisplayWarningFlag>)
  192. {
  193. str = str.ToUpper();
  194. ret = Enum.IsDefined(typeof(DisplayWarningFlag), str);
  195. if (ret)
  196. {
  197. ((ConfigItem<DisplayWarningFlag>)(AConfigItem)this).Value
  198. = (DisplayWarningFlag)Enum.Parse(typeof(DisplayWarningFlag), str);
  199. }
  200. else
  201. throw new CodeEE("Invalid specification");
  202. }
  203. else if (this is ConfigItem<UseLanguage>)
  204. {
  205. str = str.ToUpper();
  206. ret = Enum.IsDefined(typeof(UseLanguage), str);
  207. if (ret)
  208. {
  209. ((ConfigItem<UseLanguage>)(AConfigItem)this).Value
  210. = (UseLanguage)Enum.Parse(typeof(UseLanguage), str);
  211. }
  212. else
  213. throw new CodeEE("Invalid specification");
  214. }
  215. else if (this is ConfigItem<TextEditorType>)
  216. {
  217. str = str.ToUpper();
  218. ret = Enum.IsDefined(typeof(TextEditorType), str);
  219. if (ret)
  220. {
  221. ((ConfigItem<TextEditorType>)(AConfigItem)this).Value
  222. = (TextEditorType)Enum.Parse(typeof(TextEditorType), str);
  223. }
  224. else
  225. throw new CodeEE("Invalid specification");
  226. }
  227. //else
  228. // throw new ExeEE("型不明なコンフィグ");
  229. return ret;
  230. }
  231. private bool tryStringToBool(string arg, ref bool p)
  232. {
  233. if (arg == null)
  234. return false;
  235. string str = arg.Trim();
  236. int i = 0;
  237. if (Int32.TryParse(str, out i))
  238. {
  239. p = (i != 0);
  240. return true;
  241. }
  242. if (str.Equals("NO", StringComparison.CurrentCultureIgnoreCase)
  243. || str.Equals("FALSE", StringComparison.CurrentCultureIgnoreCase)
  244. || str.Equals("後", StringComparison.CurrentCultureIgnoreCase))//"単位の位置"用
  245. {
  246. p = false;
  247. return true;
  248. }
  249. if (str.Equals("YES", StringComparison.CurrentCultureIgnoreCase)
  250. || str.Equals("TRUE", StringComparison.CurrentCultureIgnoreCase)
  251. || str.Equals("前", StringComparison.CurrentCultureIgnoreCase))
  252. {
  253. p = true;
  254. return true;
  255. }
  256. throw new CodeEE("Invalid specification");
  257. }
  258. private bool tryStringsToColor(string str, out Color c)
  259. {
  260. string[] tokens = str.Split(',');
  261. c = Color.Black;
  262. int r, g, b;
  263. if (tokens.Length < 3)
  264. return false;
  265. if (!Int32.TryParse(tokens[0].Trim(), out r) || (r < 0) || (r > 255))
  266. return false;
  267. if (!Int32.TryParse(tokens[1].Trim(), out g) || (g < 0) || (g > 255))
  268. return false;
  269. if (!Int32.TryParse(tokens[2].Trim(), out b) || (b < 0) || (b > 255))
  270. return false;
  271. c = Color.FromArgb(r, g, b);
  272. return true;
  273. }
  274. }
  275. }