UpgradeManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using Object = UnityEngine.Object;
  8. namespace I2.Loc
  9. {
  10. [InitializeOnLoad]
  11. public class UpgradeManager
  12. {
  13. static bool mAlreadyCheckedPlugins;
  14. static UpgradeManager()
  15. {
  16. EditorApplication.update += AutoCheckPlugins;
  17. }
  18. public static void AutoCheckPlugins()
  19. {
  20. CheckPlugins ();
  21. }
  22. public static void CheckPlugins( bool bForce = false )
  23. {
  24. EditorApplication.update -= AutoCheckPlugins;
  25. if (mAlreadyCheckedPlugins && !bForce)
  26. return;
  27. mAlreadyCheckedPlugins = true;
  28. EnablePlugins(bForce);
  29. CreateLanguageSources();
  30. //CreateScriptLocalization();
  31. }
  32. const string EditorPrefs_AutoEnablePlugins = "I2Loc AutoEnablePlugins";
  33. [MenuItem( "Tools/I2 Localization/Enable Plugins/Force Detection", false, 0 )]
  34. public static void ForceCheckPlugins()
  35. {
  36. CheckPlugins( true );
  37. }
  38. [MenuItem( "Tools/I2 Localization/Enable Plugins/Enable Auto Detection", false, 1 )]
  39. public static void EnableAutoCheckPlugins()
  40. {
  41. EditorPrefs.SetBool(EditorPrefs_AutoEnablePlugins, true);
  42. }
  43. [MenuItem( "Tools/I2 Localization/Enable Plugins/Enable Auto Detection", true)]
  44. public static bool ValidEnableAutoCheckPlugins()
  45. {
  46. return !EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  47. }
  48. [MenuItem( "Tools/I2 Localization/Enable Plugins/Disable Auto Detection", false, 2 )]
  49. public static void DisableAutoCheckPlugins()
  50. {
  51. EditorPrefs.SetBool(EditorPrefs_AutoEnablePlugins, false);
  52. }
  53. [MenuItem( "Tools/I2 Localization/Enable Plugins/Disable Auto Detection", true)]
  54. public static bool ValidDisableAutoCheckPlugins()
  55. {
  56. return EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  57. }
  58. [MenuItem("Tools/I2 Localization/Toggle Highlight Localized", false, 17)]
  59. public static void ToogleH()
  60. {
  61. LocalizationManager.HighlightLocalizedTargets = !LocalizationManager.HighlightLocalizedTargets;
  62. LocalizationManager.LocalizeAll(true);
  63. }
  64. [MenuItem("Tools/I2 Localization/Create Temp")]
  65. public static void CreateTemp()
  66. {
  67. LanguageSourceData source = LocalizationManager.Sources[0];
  68. for (int i = 0; i < 1000; ++i)
  69. source.AddTerm("Term " + i, eTermType.Text, false);
  70. source.UpdateDictionary(true);
  71. }
  72. public static void EnablePlugins( bool bForce = false )
  73. {
  74. if (!bForce)
  75. {
  76. bool AutoEnablePlugins = EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  77. if (!AutoEnablePlugins)
  78. return;
  79. }
  80. //var tar = System.Enum.GetValues(typeof(BuildTargetGroup));
  81. foreach (BuildTargetGroup target in Enum.GetValues(typeof(BuildTargetGroup)))
  82. if (target!=BuildTargetGroup.Unknown && !target.HasAttributeOfType<ObsoleteAttribute>())
  83. {
  84. #if UNITY_5_6
  85. if (target == BuildTargetGroup.Switch) continue; // some releases of 5.6 defined BuildTargetGroup.Switch but didn't handled it correctly
  86. #endif
  87. EnablePluginsOnPlatform( target );
  88. }
  89. // Force these one (iPhone has the same # than iOS and iPhone is deprecated, so iOS was been skipped)
  90. EnablePluginsOnPlatform(BuildTargetGroup.iOS);
  91. }
  92. static void EnablePluginsOnPlatform( BuildTargetGroup Platform )
  93. {
  94. string Settings = PlayerSettings.GetScriptingDefineSymbolsForGroup(Platform );
  95. bool HasChanged = false;
  96. List<string> symbols = new List<string>( Settings.Split(';'));
  97. HasChanged |= UpdateSettings("NGUI", "NGUIDebug", "", ref symbols);
  98. HasChanged |= UpdateSettings("DFGUI", "dfPanel", "", ref symbols);
  99. HasChanged |= UpdateSettings("TK2D", "tk2dTextMesh", "", ref symbols);
  100. HasChanged |= UpdateSettings( "TextMeshPro", "TMPro.TMP_FontAsset", "TextMeshPro", ref symbols );
  101. HasChanged |= UpdateSettings( "SVG", "SVGImporter.SVGAsset", "", ref symbols );
  102. if (HasChanged)
  103. {
  104. try
  105. {
  106. Settings = string.Empty;
  107. for (int i=0,imax=symbols.Count; i<imax; ++i)
  108. {
  109. if (i>0) Settings += ";";
  110. Settings += symbols[i];
  111. }
  112. PlayerSettings.SetScriptingDefineSymbolsForGroup(Platform, Settings );
  113. }
  114. catch (Exception)
  115. {
  116. }
  117. }
  118. }
  119. static bool UpdateSettings( string mPlugin, string mType, string AssemblyType, ref List<string> symbols)
  120. {
  121. try
  122. {
  123. bool hasPluginClass = false;
  124. if (!string.IsNullOrEmpty( AssemblyType ))
  125. {
  126. var rtype = AppDomain.CurrentDomain.GetAssemblies()
  127. .Where( assembly => assembly.FullName.Contains(AssemblyType) )
  128. .Select( assembly => assembly.GetType( mType, false ) )
  129. .Where( t => t!=null )
  130. .FirstOrDefault();
  131. if (rtype != null)
  132. hasPluginClass = true;
  133. }
  134. if (!hasPluginClass)
  135. hasPluginClass = typeof( Localize ).Assembly.GetType( mType, false )!=null;
  136. bool hasPluginDef = symbols.IndexOf(mPlugin)>=0;
  137. if (hasPluginClass != hasPluginDef)
  138. {
  139. if (hasPluginClass) symbols.Add(mPlugin);
  140. else symbols.Remove(mPlugin);
  141. return true;
  142. }
  143. }
  144. catch(Exception)
  145. {
  146. }
  147. return false;
  148. }
  149. //[MenuItem( "Tools/I2 Localization/Create I2Languages", false, 1)]
  150. public static void CreateLanguageSources()
  151. {
  152. if (LocalizationManager.GlobalSources==null || LocalizationManager.GlobalSources.Length==0)
  153. return;
  154. Object GlobalSource = Resources.Load(LocalizationManager.GlobalSources[0]);
  155. LanguageSourceData sourceData = null;
  156. string sourcePath = null;
  157. if (GlobalSource != null)
  158. {
  159. if (GlobalSource is GameObject)
  160. {
  161. // I2Languages was a prefab before 2018.3, it should be converted to an ScriptableObject
  162. sourcePath = AssetDatabase.GetAssetPath(GlobalSource);
  163. LanguageSource langSourceObj = (GlobalSource as GameObject).GetComponent<LanguageSource>();
  164. sourceData = langSourceObj.mSource;
  165. }
  166. else
  167. {
  168. return;
  169. }
  170. }
  171. LanguageSourceAsset asset = ScriptableObject.CreateInstance<LanguageSourceAsset>();
  172. if (sourceData != null)
  173. {
  174. asset.mSource = sourceData;
  175. AssetDatabase.DeleteAsset(sourcePath);
  176. }
  177. if (string.IsNullOrEmpty(sourcePath))
  178. {
  179. //string PluginPath = GetI2LocalizationPath();
  180. string ResourcesFolder = "Assets/Resources";//PluginPath.Substring(0, PluginPath.Length-"/Localization".Length) + "/Resources";
  181. string fullresFolder = Application.dataPath + ResourcesFolder.Replace("Assets", "");
  182. if (!Directory.Exists(fullresFolder))
  183. Directory.CreateDirectory(fullresFolder);
  184. sourcePath = ResourcesFolder + "/" + LocalizationManager.GlobalSources[0] + ".asset";
  185. }
  186. else
  187. {
  188. sourcePath = sourcePath.Replace(".prefab", ".asset");
  189. }
  190. AssetDatabase.CreateAsset(asset, sourcePath);
  191. AssetDatabase.SaveAssets();
  192. AssetDatabase.Refresh();
  193. }
  194. [MenuItem("Tools/I2 Localization/Help", false, 30)]
  195. [MenuItem("Help/I2 Localization")]
  196. public static void MainHelp()
  197. {
  198. Application.OpenURL(LocalizeInspector.HelpURL_Documentation);
  199. }
  200. [MenuItem("Tools/I2 Localization/Open I2Languages.asset", false, 0)]
  201. public static void OpenGlobalSource()
  202. {
  203. CreateLanguageSources();
  204. LanguageSourceAsset GO = Resources.Load<LanguageSourceAsset>(LocalizationManager.GlobalSources[0]);
  205. if (GO == null)
  206. Debug.Log("Unable to find Global Language at Assets/Resources/" + LocalizationManager.GlobalSources[0] + ".asset");
  207. Selection.activeObject = GO;
  208. }
  209. /*static void CreateScriptLocalization()
  210. {
  211. string[] assets = AssetDatabase.FindAssets("ScriptLocalization");
  212. if (assets.Length>0)
  213. return;
  214. string ScriptsFolder = "Assets";
  215. string ScriptText = LocalizationEditor.mScriptLocalizationHeader + " }\n}";
  216. System.IO.File.WriteAllText(ScriptsFolder + "/ScriptLocalization.cs", ScriptText);
  217. AssetDatabase.SaveAssets();
  218. AssetDatabase.Refresh();
  219. }*/
  220. public static string GetI2LocalizationPath()
  221. {
  222. string[] assets = AssetDatabase.FindAssets("LocalizationManager");
  223. if (assets.Length==0)
  224. return string.Empty;
  225. string PluginPath = AssetDatabase.GUIDToAssetPath(assets[0]);
  226. PluginPath = PluginPath.Substring(0, PluginPath.Length - "/Scripts/LocalizationManager.cs".Length);
  227. return PluginPath;
  228. }
  229. public static string GetI2Path()
  230. {
  231. string pluginPath = GetI2LocalizationPath();
  232. return pluginPath.Substring(0, pluginPath.Length-"/Localization".Length);
  233. }
  234. public static string GetI2CommonResourcesPath()
  235. {
  236. string I2Path = GetI2Path();
  237. return I2Path + "/Resources";
  238. }
  239. }
  240. public static class UpgradeManagerHelper
  241. {
  242. public static bool HasAttributeOfType<T>(this Enum enumVal) where T:Attribute
  243. {
  244. var type = enumVal.GetType();
  245. var memInfo = type.GetMember(enumVal.ToString());
  246. var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
  247. return attributes.Length > 0;
  248. }
  249. }
  250. }