123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using System;
- using UnityEngine;
- using System.Linq;
- using System.Collections.Generic;
- using Object = UnityEngine.Object;
- namespace I2.Loc
- {
- public class BaseSpecializationManager
- {
- public string[] mSpecializations = null;
- public Dictionary<string, string> mSpecializationsFallbacks;
- public virtual void InitializeSpecializations()
- {
- mSpecializations = new string[] { "Any", "PC", "Touch", "Controller", "VR",
- "XBox", "PS4", "OculusVR", "ViveVR", "GearVR", "Android", "IOS" };
- mSpecializationsFallbacks = new Dictionary<string, string>()
- {
- { "XBox", "Controller" }, { "PS4", "Controller" },
- { "OculusVR", "VR" }, { "ViveVR", "VR" }, { "GearVR", "VR" },
- { "Android", "Touch" }, { "IOS", "Touch" }
- };
- }
- public virtual string GetCurrentSpecialization()
- {
- if (mSpecializations == null)
- InitializeSpecializations();
- #if UNITY_ANDROID
- return "Android";
- #elif UNITY_IOS
- return "IOS";
- #elif UNITY_PS4
- return "PS4";
- #elif UNITY_XBOXONE
- return "XBox";
- #elif UNITY_STANDALONE || UNITY_WEBGL
- return "PC";
- #else
- return (Input.touchSupported ? "Touch" : "PC");
- #endif
- }
- public virtual string GetFallbackSpecialization(string specialization)
- {
- if (mSpecializationsFallbacks == null)
- InitializeSpecializations();
- string fallback;
- if (mSpecializationsFallbacks.TryGetValue(specialization, out fallback))
- return fallback;
- else
- return "Any";
- }
- }
- public partial class SpecializationManager : BaseSpecializationManager
- {
- public static SpecializationManager Singleton = new SpecializationManager();
- private SpecializationManager()
- {
- InitializeSpecializations();
- }
- public static string GetSpecializedText(string text, string specialization = null)
- {
- var idxFirst = text.IndexOf("[i2s_");
- if (idxFirst < 0)
- return text;
- if (string.IsNullOrEmpty(specialization))
- specialization = Singleton.GetCurrentSpecialization();
- while (!string.IsNullOrEmpty(specialization) && specialization != "Any")
- {
- var tag = "[i2s_" + specialization + "]";
- int idx = text.IndexOf(tag);
- if (idx < 0)
- {
- specialization = Singleton.GetFallbackSpecialization(specialization);
- continue;
- }
- idx += tag.Length;
- var idxEnd = text.IndexOf("[i2s_", idx);
- if (idxEnd < 0) idxEnd = text.Length;
- return text.Substring(idx, idxEnd - idx);
- }
- return text.Substring(0, idxFirst);
- }
- public static string SetSpecializedText(string text, string newText, string specialization)
- {
- if (string.IsNullOrEmpty(specialization))
- specialization = "Any";
- if ((text==null || !text.Contains("[i2s_")) && specialization=="Any")
- {
- return newText;
- }
- var dict = GetSpecializations(text);
- dict[specialization] = newText;
- return SetSpecializedText(dict);
- }
- public static string SetSpecializedText( Dictionary<string,string> specializations )
- {
- string text;
- if (!specializations.TryGetValue("Any", out text))
- text = string.Empty;
- foreach (var kvp in specializations)
- {
- if (kvp.Key != "Any" && !string.IsNullOrEmpty(kvp.Value))
- text += "[i2s_" + kvp.Key + "]" + kvp.Value;
- }
- return text;
- }
- public static Dictionary<string, string> GetSpecializations(string text, Dictionary<string, string> buffer = null)
- {
- if (buffer == null)
- buffer = new Dictionary<string, string>();
- else
- buffer.Clear();
- if (text==null)
- {
- buffer["Any"] = "";
- return buffer;
- }
- var idxFirst = 0;
- var idxEnd = text.IndexOf("[i2s_");
- if (idxEnd < 0)
- idxEnd=text.Length;
- buffer["Any"] = text.Substring(0, idxEnd);
- idxFirst = idxEnd;
- while (idxFirst<text.Length)
- {
- idxFirst += "[i2s_".Length;
- int idx = text.IndexOf(']', idxFirst);
- if (idx < 0) break;
- var tag = text.Substring(idxFirst, idx - idxFirst);
- idxFirst = idx+1; // ']'
- idxEnd = text.IndexOf("[i2s_", idxFirst);
- if (idxEnd < 0) idxEnd = text.Length;
- var value = text.Substring(idxFirst, idxEnd - idxFirst);
- buffer[tag] = value;
- idxFirst = idxEnd;
- }
- return buffer;
- }
- public static void AppendSpecializations(string text, List<string> list=null)
- {
- if (text == null)
- return;
- if (list == null)
- list = new List<string>();
- if (!list.Contains("Any"))
- list.Add("Any");
- var idxFirst = 0;
- while (idxFirst<text.Length)
- {
- idxFirst = text.IndexOf("[i2s_", idxFirst);
- if (idxFirst < 0)
- break;
- idxFirst += "[i2s_".Length;
- int idx = text.IndexOf(']', idxFirst);
- if (idx < 0)
- break;
- var tag = text.Substring(idxFirst, idx - idxFirst);
- if (!list.Contains(tag))
- list.Add(tag);
- }
- }
- };
- }
|