SpecializationManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. namespace I2.Loc
  4. {
  5. public class SpecializationManager : BaseSpecializationManager
  6. {
  7. private SpecializationManager()
  8. {
  9. this.InitializeSpecializations();
  10. }
  11. public static string GetSpecializedText(string text, string specialization = null)
  12. {
  13. int num = text.IndexOf("[i2s_");
  14. if (num < 0)
  15. {
  16. return text;
  17. }
  18. if (string.IsNullOrEmpty(specialization))
  19. {
  20. specialization = SpecializationManager.Singleton.GetCurrentSpecialization();
  21. }
  22. while (!string.IsNullOrEmpty(specialization) && specialization != "Any")
  23. {
  24. string text2 = "[i2s_" + specialization + "]";
  25. int num2 = text.IndexOf(text2);
  26. if (num2 >= 0)
  27. {
  28. num2 += text2.Length;
  29. int num3 = text.IndexOf("[i2s_", num2);
  30. if (num3 < 0)
  31. {
  32. num3 = text.Length;
  33. }
  34. return text.Substring(num2, num3 - num2);
  35. }
  36. specialization = SpecializationManager.Singleton.GetFallbackSpecialization(specialization);
  37. }
  38. return text.Substring(0, num);
  39. }
  40. public static string SetSpecializedText(string text, string newText, string specialization)
  41. {
  42. if (string.IsNullOrEmpty(specialization))
  43. {
  44. specialization = "Any";
  45. }
  46. if ((text == null || !text.Contains("[i2s_")) && specialization == "Any")
  47. {
  48. return newText;
  49. }
  50. Dictionary<string, string> specializations = SpecializationManager.GetSpecializations(text, null);
  51. specializations[specialization] = newText;
  52. return SpecializationManager.SetSpecializedText(specializations);
  53. }
  54. public static string SetSpecializedText(Dictionary<string, string> specializations)
  55. {
  56. string text;
  57. if (!specializations.TryGetValue("Any", out text))
  58. {
  59. text = string.Empty;
  60. }
  61. foreach (KeyValuePair<string, string> keyValuePair in specializations)
  62. {
  63. if (keyValuePair.Key != "Any" && !string.IsNullOrEmpty(keyValuePair.Value))
  64. {
  65. string text2 = text;
  66. text = string.Concat(new string[]
  67. {
  68. text2,
  69. "[i2s_",
  70. keyValuePair.Key,
  71. "]",
  72. keyValuePair.Value
  73. });
  74. }
  75. }
  76. return text;
  77. }
  78. public static Dictionary<string, string> GetSpecializations(string text, Dictionary<string, string> buffer = null)
  79. {
  80. if (buffer == null)
  81. {
  82. buffer = new Dictionary<string, string>();
  83. }
  84. else
  85. {
  86. buffer.Clear();
  87. }
  88. if (text == null)
  89. {
  90. buffer["Any"] = string.Empty;
  91. return buffer;
  92. }
  93. int num = text.IndexOf("[i2s_");
  94. if (num < 0)
  95. {
  96. num = text.Length;
  97. }
  98. buffer["Any"] = text.Substring(0, num);
  99. for (int i = num; i < text.Length; i = num)
  100. {
  101. i += "[i2s_".Length;
  102. int num2 = text.IndexOf(']', i);
  103. if (num2 < 0)
  104. {
  105. break;
  106. }
  107. string key = text.Substring(i, num2 - i);
  108. i = num2 + 1;
  109. num = text.IndexOf("[i2s_", i);
  110. if (num < 0)
  111. {
  112. num = text.Length;
  113. }
  114. string value = text.Substring(i, num - i);
  115. buffer[key] = value;
  116. }
  117. return buffer;
  118. }
  119. public static void AppendSpecializations(string text, List<string> list = null)
  120. {
  121. if (text == null)
  122. {
  123. return;
  124. }
  125. if (list == null)
  126. {
  127. list = new List<string>();
  128. }
  129. if (!list.Contains("Any"))
  130. {
  131. list.Add("Any");
  132. }
  133. int i = 0;
  134. while (i < text.Length)
  135. {
  136. i = text.IndexOf("[i2s_", i);
  137. if (i < 0)
  138. {
  139. break;
  140. }
  141. i += "[i2s_".Length;
  142. int num = text.IndexOf(']', i);
  143. if (num < 0)
  144. {
  145. break;
  146. }
  147. string item = text.Substring(i, num - i);
  148. if (!list.Contains(item))
  149. {
  150. list.Add(item);
  151. }
  152. }
  153. }
  154. public static SpecializationManager Singleton = new SpecializationManager();
  155. }
  156. }