LocalizationManager_Sources.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5. using System.Globalization;
  6. using System.Collections;
  7. namespace I2.Loc
  8. {
  9. public static partial class LocalizationManager
  10. {
  11. #region Variables: Misc
  12. public static List<LanguageSourceData> Sources = new List<LanguageSourceData>();
  13. public static string[] GlobalSources = { "I2Languages" };
  14. #endregion
  15. #region Sources
  16. public static bool UpdateSources()
  17. {
  18. UnregisterDeletededSources();
  19. RegisterSourceInResources();
  20. RegisterSceneSources();
  21. return Sources.Count>0;
  22. }
  23. static void UnregisterDeletededSources()
  24. {
  25. // Delete sources that were part of another scene and not longer available
  26. for (int i=Sources.Count-1; i>=0; --i)
  27. if (Sources[i] == null)
  28. RemoveSource( Sources[i] );
  29. }
  30. static void RegisterSceneSources()
  31. {
  32. LanguageSource[] sceneSources = (LanguageSource[])Resources.FindObjectsOfTypeAll( typeof(LanguageSource) );
  33. foreach (LanguageSource source in sceneSources)
  34. if (!Sources.Contains(source.mSource))
  35. {
  36. if (source.mSource.owner == null)
  37. source.mSource.owner = source;
  38. AddSource( source.mSource );
  39. }
  40. }
  41. static void RegisterSourceInResources()
  42. {
  43. // Find the Source that its on the Resources Folder
  44. foreach (string SourceName in GlobalSources)
  45. {
  46. LanguageSourceAsset sourceAsset = (ResourceManager.pInstance.GetAsset<LanguageSourceAsset>(SourceName));
  47. if (sourceAsset && !Sources.Contains(sourceAsset.mSource))
  48. {
  49. if (!sourceAsset.mSource.mIsGlobalSource)
  50. sourceAsset.mSource.mIsGlobalSource = true;
  51. sourceAsset.mSource.owner = sourceAsset;
  52. AddSource(sourceAsset.mSource);
  53. }
  54. }
  55. }
  56. internal static void AddSource ( LanguageSourceData Source )
  57. {
  58. if (Sources.Contains (Source))
  59. return;
  60. Sources.Add( Source );
  61. if (Source.HasGoogleSpreadsheet() && Source.GoogleUpdateFrequency != LanguageSourceData.eGoogleUpdateFrequency.Never)
  62. {
  63. #if !UNITY_EDITOR
  64. Source.Import_Google_FromCache();
  65. bool justCheck = false;
  66. #else
  67. bool justCheck=true;
  68. #endif
  69. if (Source.GoogleUpdateDelay > 0)
  70. CoroutineManager.Start( Delayed_Import_Google(Source, Source.GoogleUpdateDelay, justCheck) );
  71. else
  72. Source.Import_Google(false, justCheck);
  73. }
  74. //if (force)
  75. {
  76. for (int i = 0; i < Source.mLanguages.Count(); ++i)
  77. Source.mLanguages[i].SetLoaded(true);
  78. }
  79. if (Source.mDictionary.Count==0)
  80. Source.UpdateDictionary(true);
  81. }
  82. static IEnumerator Delayed_Import_Google ( LanguageSourceData source, float delay, bool justCheck )
  83. {
  84. yield return new WaitForSeconds( delay );
  85. if (source != null) // handle cases where the source is already deleted
  86. {
  87. source.Import_Google(false, justCheck);
  88. }
  89. }
  90. internal static void RemoveSource (LanguageSourceData Source )
  91. {
  92. //Debug.Log ("RemoveSource " + Source+" " + Source.GetInstanceID());
  93. Sources.Remove( Source );
  94. }
  95. public static bool IsGlobalSource( string SourceName )
  96. {
  97. return System.Array.IndexOf(GlobalSources, SourceName)>=0;
  98. }
  99. public static LanguageSourceData GetSourceContaining( string term, bool fallbackToFirst = true )
  100. {
  101. if (!string.IsNullOrEmpty(term))
  102. {
  103. for (int i=0, imax=Sources.Count; i<imax; ++i)
  104. {
  105. if (Sources[i].GetTermData(term) != null)
  106. return Sources[i];
  107. }
  108. }
  109. return ((fallbackToFirst && Sources.Count>0) ? Sources[0] : null);
  110. }
  111. public static Object FindAsset (string value)
  112. {
  113. for (int i=0, imax=Sources.Count; i<imax; ++i)
  114. {
  115. Object Obj = Sources[i].FindAsset(value);
  116. if (Obj)
  117. return Obj;
  118. }
  119. return null;
  120. }
  121. public static void ApplyDownloadedDataFromGoogle()
  122. {
  123. for (int i = 0, imax = Sources.Count; i < imax; ++i)
  124. {
  125. Sources[i].ApplyDownloadedDataFromGoogle();
  126. }
  127. }
  128. #endregion
  129. }
  130. }