SpecializationManager.cs 6.1 KB

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