TapCommonCompile.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. #if UNITY_IOS || UNITY_STANDALONE_OSX
  7. using UnityEditor.iOS.Xcode;
  8. #endif
  9. namespace TapTap.Common.Editor
  10. {
  11. #if UNITY_IOS || UNITY_STANDALONE_OSX
  12. public static class TapCommonCompile
  13. {
  14. public static string GetProjPath(string path)
  15. {
  16. return PBXProject.GetPBXProjectPath(path);
  17. }
  18. public static PBXProject ParseProjPath(string path)
  19. {
  20. var proj = new PBXProject();
  21. proj.ReadFromString(File.ReadAllText(path));
  22. return proj;
  23. }
  24. public static string GetUnityFrameworkTarget(PBXProject proj)
  25. {
  26. #if UNITY_2019_3_OR_NEWER
  27. string target = proj.GetUnityFrameworkTargetGuid();
  28. return target;
  29. #endif
  30. var unityPhoneTarget = proj.TargetGuidByName("Unity-iPhone");
  31. return unityPhoneTarget;
  32. }
  33. public static string GetUnityTarget(PBXProject proj)
  34. {
  35. #if UNITY_2019_3_OR_NEWER
  36. string target = proj.GetUnityMainTargetGuid();
  37. return target;
  38. #endif
  39. var unityPhoneTarget = proj.TargetGuidByName("Unity-iPhone");
  40. return unityPhoneTarget;
  41. }
  42. public static bool CheckTarget(string target)
  43. {
  44. return string.IsNullOrEmpty(target);
  45. }
  46. public static bool HandlerIOSSetting(string path, string appDataPath, string resourceName,
  47. string modulePackageName,
  48. string moduleName, string[] bundleNames, string target, string projPath, PBXProject proj)
  49. {
  50. var resourcePath = Path.Combine(path, resourceName);
  51. var parentFolder = Directory.GetParent(appDataPath).FullName;
  52. Debug.Log($"ProjectFolder path:{parentFolder}");
  53. if (Directory.Exists(resourcePath))
  54. {
  55. Directory.Delete(resourcePath, true);
  56. }
  57. Directory.CreateDirectory(resourcePath);
  58. var remotePackagePath =
  59. TapFileHelper.FilterFileByPrefix(parentFolder + "/Library/PackageCache/", $"{modulePackageName}@");
  60. var assetLocalPackagePath = TapFileHelper.FilterFileByPrefix(parentFolder + "/Assets/TapTap/", moduleName);
  61. var localPackagePath = TapFileHelper.FilterFileByPrefix(parentFolder, moduleName);
  62. var tdsResourcePath = "";
  63. if (!string.IsNullOrEmpty(remotePackagePath))
  64. {
  65. tdsResourcePath = remotePackagePath;
  66. }
  67. else if (!string.IsNullOrEmpty(assetLocalPackagePath))
  68. {
  69. tdsResourcePath = assetLocalPackagePath;
  70. }
  71. else if (!string.IsNullOrEmpty(localPackagePath))
  72. {
  73. tdsResourcePath = localPackagePath;
  74. }
  75. if (string.IsNullOrEmpty(tdsResourcePath))
  76. {
  77. Debug.LogError("tdsResourcePath is NUll");
  78. return false;
  79. }
  80. tdsResourcePath = $"{tdsResourcePath}/Plugins/iOS/Resource";
  81. Debug.Log($"Find {moduleName} path:{tdsResourcePath}");
  82. if (!Directory.Exists(tdsResourcePath))
  83. {
  84. Debug.LogError($"Can't Find {bundleNames}");
  85. return false;
  86. }
  87. TapFileHelper.CopyAndReplaceDirectory(tdsResourcePath, resourcePath);
  88. foreach (var name in bundleNames)
  89. {
  90. proj.AddFileToBuild(target,
  91. proj.AddFile(Path.Combine(resourcePath, name), Path.Combine(resourcePath, name),
  92. PBXSourceTree.Source));
  93. }
  94. File.WriteAllText(projPath, proj.WriteToString());
  95. return true;
  96. }
  97. public static bool HandlerPlist(string pathToBuildProject, string infoPlistPath, bool macos = false)
  98. {
  99. // #if UNITY_2020_1_OR_NEWER
  100. // var macosXCodePlistPath =
  101. // $"{pathToBuildProject}/{PlayerSettings.productName}/Info.plist";
  102. // #elif UNITY_2019_1_OR_NEWER
  103. // var macosXCodePlistPath =
  104. // $"{Path.GetDirectoryName(pathToBuildProject)}/{PlayerSettings.productName}/Info.plist";
  105. // #endif
  106. string plistPath;
  107. if (pathToBuildProject.EndsWith(".app"))
  108. {
  109. plistPath = $"{pathToBuildProject}/Contents/Info.plist";
  110. }
  111. else
  112. {
  113. var macosXCodePlistPath =
  114. $"{Path.GetDirectoryName(pathToBuildProject)}/{PlayerSettings.productName}/Info.plist";
  115. if (!File.Exists(macosXCodePlistPath))
  116. {
  117. macosXCodePlistPath = $"{pathToBuildProject}/{PlayerSettings.productName}/Info.plist";
  118. }
  119. plistPath = !macos
  120. ? pathToBuildProject + "/Info.plist"
  121. : macosXCodePlistPath;
  122. }
  123. Debug.Log($"plist path:{plistPath}");
  124. var plist = new PlistDocument();
  125. plist.ReadFromString(File.ReadAllText(plistPath));
  126. var rootDic = plist.root;
  127. var items = new List<string>
  128. {
  129. "tapsdk",
  130. "tapiosdk",
  131. };
  132. if (!(rootDic["LSApplicationQueriesSchemes"] is PlistElementArray plistElementList))
  133. {
  134. plistElementList = rootDic.CreateArray("LSApplicationQueriesSchemes");
  135. }
  136. foreach (var t in items)
  137. {
  138. plistElementList.AddString(t);
  139. }
  140. if (string.IsNullOrEmpty(infoPlistPath)) return false;
  141. var dic = (Dictionary<string, object>) Plist.readPlist(infoPlistPath);
  142. var taptapId = "";
  143. foreach (var item in dic)
  144. {
  145. if (item.Key.Equals("taptap"))
  146. {
  147. var taptapDic = (Dictionary<string, object>) item.Value;
  148. foreach (var taptapItem in taptapDic.Where(taptapItem => taptapItem.Key.Equals("client_id")))
  149. {
  150. taptapId = (string) taptapItem.Value;
  151. }
  152. }
  153. else
  154. {
  155. rootDic.SetString(item.Key, item.Value.ToString());
  156. }
  157. }
  158. //添加url
  159. var dict = plist.root.AsDict();
  160. if (!(dict["CFBundleURLTypes"] is PlistElementArray array))
  161. {
  162. array = dict.CreateArray("CFBundleURLTypes");
  163. }
  164. if (!macos)
  165. {
  166. var dict2 = array.AddDict();
  167. dict2.SetString("CFBundleURLName", "TapTap");
  168. var array2 = dict2.CreateArray("CFBundleURLSchemes");
  169. array2.AddString($"tt{taptapId}");
  170. }
  171. else
  172. {
  173. var dict2 = array.AddDict();
  174. dict2.SetString("CFBundleURLName", "TapWeb");
  175. var array2 = dict2.CreateArray("CFBundleURLSchemes");
  176. array2.AddString($"open-taptap-{taptapId}");
  177. }
  178. Debug.Log("TapSDK change plist Success");
  179. File.WriteAllText(plistPath, plist.WriteToString());
  180. return true;
  181. }
  182. public static string GetValueFromPlist(string infoPlistPath, string key)
  183. {
  184. if (infoPlistPath == null)
  185. {
  186. return null;
  187. }
  188. var dic = (Dictionary<string, object>) Plist.readPlist(infoPlistPath);
  189. return (from item in dic where item.Key.Equals(key) select (string) item.Value).FirstOrDefault();
  190. }
  191. }
  192. #endif
  193. }