PageRenderTime 69ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/NekoKun.Core/ObjProvider/FormatProvider.cs

https://bitbucket.org/nekokun/nekokun
C# | 163 lines | 137 code | 22 blank | 4 comment | 27 complexity | 3b24e1edf72f0ea58648cc6a256e5958 MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace NekoKun
  5. {
  6. public class FormatProvider
  7. {
  8. protected Dictionary<String, String> aliases;
  9. protected List<System.Reflection.MethodInfo> methods;
  10. protected List<Enum> enums;
  11. protected List<Dictionary<string, string>> enumLocaleInfo;
  12. public FormatProvider(Dictionary<string, object> node)
  13. {
  14. aliases = new Dictionary<string, string>();
  15. enums = new List<Enum>();
  16. enumLocaleInfo = new List<Dictionary<string, string>>();
  17. methods = new List<System.Reflection.MethodInfo>();
  18. // ??????????????
  19. // ????????????
  20. // ???? ???????
  21. // ??
  22. var aliasesa = node["Aliases"] as System.Xml.XmlNodeList;
  23. foreach (System.Xml.XmlNode alias in aliasesa)
  24. {
  25. if (alias.Name != "Alias") continue;
  26. aliases.Add(
  27. alias.Attributes["ID"].Value,
  28. alias.FirstChild.Value
  29. );
  30. }
  31. }
  32. public string ParseFormat(string format, List<object> parameters)
  33. {
  34. if (format.IndexOf("{") < 0)
  35. return format;
  36. if (parameters == null)
  37. parameters = new List<object>();
  38. return System.Text.RegularExpressions.Regex.Replace(
  39. format,
  40. @"\{
  41. (
  42. (\d*) \|
  43. )?
  44. (
  45. (assembly \| (\d+) (\|.*?)? ) |
  46. (enum \| (\d+) )
  47. )?
  48. \}
  49. |
  50. \{
  51. (\d*)
  52. \}",
  53. delegate(System.Text.RegularExpressions.Match match)
  54. {
  55. object parm = null;
  56. format.ToString();
  57. if (match.Groups[2].Success || match.Groups[9].Success)
  58. {
  59. int index = Int32.Parse(match.Groups[2].Success ? match.Groups[2].Value : match.Groups[9].Value);
  60. if (index >= 0 && index < parameters.Count)
  61. parm = parameters[index];
  62. }
  63. if (match.Groups[9].Success && parm != null)
  64. return parm.ToString();
  65. if (match.Groups[8].Success)
  66. return ParseFormat(this.enumLocaleInfo[Int32.Parse(match.Groups[8].Value)][parm.ToString()], parameters);
  67. if (match.Groups[5].Success)
  68. {
  69. string[] para;
  70. if (match.Groups[6].Length == 0)
  71. para = null;
  72. else
  73. para = match.Groups[6].Value.Substring(1).Split(Char.Parse("|"));
  74. if (parm == null)
  75. parm = parameters.ToArray();
  76. return this.methods[Int32.Parse(match.Groups[5].Value)].Invoke(null, new object[] { parm, para }) as string;
  77. }
  78. return match.Value;
  79. },
  80. System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
  81. );
  82. }
  83. public string PrepareFormat(string format)
  84. {
  85. return System.Text.RegularExpressions.Regex.Replace(
  86. format,
  87. @"\{(\d*\|)?([^0-9/].*?)\}",
  88. delegate(System.Text.RegularExpressions.Match match){
  89. if (match.Value == "{hide}")
  90. return match.Value;
  91. string processed = match.Groups[2].Value;
  92. if (match.Groups[1].Success && processed.StartsWith("enum|"))
  93. {
  94. var parts = processed.Split(new string[] {"|"}, 3, StringSplitOptions.None);
  95. var enu = (ProjectManager.Components[parts[1]] as EnumProvider).Enums[parts[2]];
  96. if (!enums.Contains(enu))
  97. {
  98. enums.Add(enu);
  99. var newenu = new Dictionary<string, string>();
  100. enumLocaleInfo.Add(newenu);
  101. foreach (var item in enu.GetKeys())
  102. {
  103. newenu.Add(item, PrepareFormat(enu[item]));
  104. }
  105. }
  106. return "{" + match.Groups[1].Value + "enum|" + enums.IndexOf(enu).ToString() + "}";
  107. }
  108. else
  109. foreach (string item in aliases.Keys)
  110. {
  111. if (processed.StartsWith(item))
  112. {
  113. processed = aliases[item] + processed.Substring(item.Length);
  114. break;
  115. }
  116. }
  117. System.Text.RegularExpressions.Match ma = System.Text.RegularExpressions.Regex.Match(
  118. processed,
  119. @"^(.*?)\|(.*?)\|(.*?)\|(.*?)(\|.*)?$"
  120. );
  121. if (ma.Success)
  122. {
  123. if ((ma.Groups[1].Value) == "assembly")
  124. {
  125. System.Reflection.Assembly assembly =
  126. (ProjectManager.Components[
  127. ma.Groups[2].Value
  128. ] as IAssemblyProvider).GetAssembly();
  129. var formatMethod = assembly
  130. .GetType(ma.Groups[3].Value, true, true)
  131. .GetMethod(ma.Groups[4].Value);
  132. methods.Add(formatMethod);
  133. processed = "assembly|" + (methods.Count - 1).ToString() + ma.Groups[5].Value;
  134. }
  135. }
  136. return "{" + match.Groups[1].Value + processed + "}";
  137. }
  138. );
  139. }
  140. }
  141. }