ValueListManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. #if UNITY_EDITOR
  5. [ExecuteInEditMode()]
  6. #endif
  7. public class ValueListManager:MonoBehaviour {
  8. // Static properties
  9. private static Dictionary<string, ValueListManager> instances;
  10. // Parameters
  11. public string key;
  12. public ValueListWithQualifiers[] valueListsWithQualifiers;
  13. // Properties
  14. private List<ValueList> valueLists; // Valid value lists, already filtered and ordered
  15. // TODO: add tester interface. You can enter an ID, and it shows the selected value, and where it's coming from. Also add a simulator?
  16. // TODO: add a "comparer", so two language xmls can be compared and the missing ids can be listed.
  17. // TODO: create a better inspector? http://answers.unity3d.com/questions/26207/how-can-i-recreate-the-array-inspector-element-for.html
  18. // ================================================================================================================
  19. // MAIN EVENT INTERFACE -------------------------------------------------------------------------------------------
  20. void Awake() {
  21. // Load all value lists, creating a pre-filtered list
  22. valueLists = new List<ValueList>();
  23. for (var i = 0; i < valueListsWithQualifiers.Length; i++) {
  24. // Filter
  25. if (doQualifiersApply(valueListsWithQualifiers[i])) {
  26. // Is valid, load
  27. var valueList = new ValueList("values_" + key + "_" + i);
  28. valueList.SetFromJSON(File.ReadAllText(valueListsWithQualifiers[i].fileName));
  29. valueLists.Add(valueList);
  30. }
  31. }
  32. // Add to the list of instances that can be retrieved later
  33. ValueListManager.addInstance(this);
  34. Debug.Log("Value lists read; current language is " + ValueListManager.GetInstance().GetString("generic.language"));
  35. }
  36. // ================================================================================================================
  37. // STATIC INTERFACE -----------------------------------------------------------------------------------------------
  38. static ValueListManager() {
  39. instances = new Dictionary<string, ValueListManager>();
  40. }
  41. private static void addInstance(ValueListManager instance) {
  42. if (instances.ContainsKey(instance.key)) instances.Remove(instance.key);
  43. instances.Add(instance.key, instance);
  44. }
  45. public static ValueListManager GetInstance(string key = "") {
  46. if (instances.ContainsKey(key)) return instances[key];
  47. Debug.LogError("Error! Tried reading a ValueListManager with key [" + key + "] that doesn't exist.");
  48. return null;
  49. }
  50. // ================================================================================================================
  51. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  52. public string GetString(string keyPath) {
  53. return GetValue<string>(keyPath);
  54. }
  55. public T GetValue<T>(string keyPath) {
  56. List<ValueList> lists = getValidValueLists();
  57. if (lists != null) {
  58. for (int i = 0; i < lists.Count; i++) {
  59. if (lists[i].HasKey(keyPath)) return lists[i].GetValue<T>(keyPath);
  60. }
  61. }
  62. Debug.LogWarning("Trying reading object of value [" + keyPath + "] that was not found on value lists.");
  63. return default(T);
  64. }
  65. // ================================================================================================================
  66. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  67. private ValueList getFirstValidValueList() {
  68. for (var i = 0; i < valueListsWithQualifiers.Length; i++) {
  69. if (doQualifiersApply(valueListsWithQualifiers[i])) {
  70. return valueLists[i];
  71. }
  72. }
  73. return null;
  74. }
  75. private List<ValueList> getValidValueLists() {
  76. // Create a list of valueLists that qualify given their selectors
  77. // TODO: filter by other qualifiers that are not platform- or device-specific?
  78. return valueLists;
  79. }
  80. private bool doQualifiersApply(ValueListWithQualifiers valueListWithQualifiers) {
  81. // Checks whether a value list's qualifiers are valid
  82. if (!valueListWithQualifiers.enabled) return false;
  83. bool isValid = true;
  84. // Check language
  85. if (isValid && valueListWithQualifiers.languageFilterEnabled) {
  86. isValid = valueListWithQualifiers.language == Application.systemLanguage;
  87. }
  88. // Check OS
  89. if (isValid && valueListWithQualifiers.platformFilterEnabled) {
  90. isValid = valueListWithQualifiers.platform == Application.platform;
  91. }
  92. return isValid;
  93. }
  94. }
  95. [System.Serializable]
  96. public class ValueListWithQualifiers {
  97. public string fileName;
  98. public bool enabled;
  99. public bool languageFilterEnabled;
  100. public SystemLanguage language;
  101. public bool platformFilterEnabled;
  102. public RuntimePlatform platform;
  103. /*
  104. * http://developer.android.com/guide/topics/resources/providing-resources.html
  105. * Qualifiers:
  106. * . Country (mcc, mnc)
  107. * . Language/locale (en, en-rUS) ("r" is to differentiate the region portion)
  108. * . Width/height/smallest width
  109. * . Layout direction
  110. * . Screen size (small, normal, large, xlarge)
  111. * . Aspect ratio (long, not long)
  112. * . Round or not
  113. * . Screen orientation
  114. * . UI mode (car, desktop, appliance, etc)
  115. * . Screen pixel density (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi)
  116. * . Touchscreen type (notouch, finger)
  117. * . Keyboard availability (keysexposed, keyshidden, keyssoft)
  118. */
  119. }