LocalizationManager_Sources.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  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. public static Func<LanguageSourceData, bool> Callback_AllowSyncFromGoogle = null;
  57. static bool AllowSyncFromGoogle(LanguageSourceData Source)
  58. {
  59. if (Callback_AllowSyncFromGoogle == null)
  60. return true;
  61. return Callback_AllowSyncFromGoogle.Invoke(Source);
  62. }
  63. internal static void AddSource ( LanguageSourceData Source )
  64. {
  65. if (Sources.Contains (Source))
  66. return;
  67. Sources.Add( Source );
  68. if (Source.HasGoogleSpreadsheet() && Source.GoogleUpdateFrequency != LanguageSourceData.eGoogleUpdateFrequency.Never && AllowSyncFromGoogle(Source))
  69. {
  70. #if !UNITY_EDITOR
  71. Source.Import_Google_FromCache();
  72. bool justCheck = false;
  73. #else
  74. bool justCheck=true;
  75. #endif
  76. if (Source.GoogleUpdateDelay > 0)
  77. CoroutineManager.Start( Delayed_Import_Google(Source, Source.GoogleUpdateDelay, justCheck) );
  78. else
  79. Source.Import_Google(false, justCheck);
  80. }
  81. //if (force)
  82. {
  83. for (int i = 0; i < Source.mLanguages.Count; ++i)
  84. Source.mLanguages[i].SetLoaded(true);
  85. }
  86. if (Source.mDictionary.Count==0)
  87. Source.UpdateDictionary(true);
  88. }
  89. static IEnumerator Delayed_Import_Google ( LanguageSourceData source, float delay, bool justCheck )
  90. {
  91. yield return new WaitForSeconds( delay );
  92. if (source != null) // handle cases where the source is already deleted
  93. {
  94. source.Import_Google(false, justCheck);
  95. }
  96. }
  97. internal static void RemoveSource (LanguageSourceData Source )
  98. {
  99. //Debug.Log ("RemoveSource " + Source+" " + Source.GetInstanceID());
  100. Sources.Remove( Source );
  101. }
  102. public static bool IsGlobalSource( string SourceName )
  103. {
  104. return Array.IndexOf(GlobalSources, SourceName)>=0;
  105. }
  106. public static LanguageSourceData GetSourceContaining( string term, bool fallbackToFirst = true )
  107. {
  108. if (!string.IsNullOrEmpty(term))
  109. {
  110. for (int i=0, imax=Sources.Count; i<imax; ++i)
  111. {
  112. if (Sources[i].GetTermData(term) != null)
  113. return Sources[i];
  114. }
  115. }
  116. return fallbackToFirst && Sources.Count>0 ? Sources[0] : null;
  117. }
  118. public static Object FindAsset (string value)
  119. {
  120. for (int i=0, imax=Sources.Count; i<imax; ++i)
  121. {
  122. Object Obj = Sources[i].FindAsset(value);
  123. if (Obj)
  124. return Obj;
  125. }
  126. return null;
  127. }
  128. public static void ApplyDownloadedDataFromGoogle()
  129. {
  130. for (int i = 0, imax = Sources.Count; i < imax; ++i)
  131. {
  132. Sources[i].ApplyDownloadedDataFromGoogle();
  133. }
  134. }
  135. #endregion
  136. }
  137. }