SpecializationManager.cs 6.1 KB

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