PackPlatformiOSBuild.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEditor;
  6. #if UNITY_IOS
  7. using UnityEditor.iOS.Xcode;
  8. #endif
  9. namespace Pack
  10. {
  11. public partial class PackPlatformiOS
  12. {
  13. protected override string GetBuildOutputPath()
  14. {
  15. // iOS 的XCode项目路径最好不能有中文,否则易出错
  16. string path = Application.dataPath + PackConstant.BuildAppPath + "/" + GetBuildTarget() + "/" + channelUniqueId;
  17. path = Path.GetFullPath(path).Replace('\\', '/');
  18. return path + "/XCodeProj";
  19. }
  20. public override void BuildClear()
  21. {
  22. base.BuildClear();
  23. DeleteDirectoryAssets(Path.GetFullPath(Application.dataPath + PackConstant.PluginiOSDestPath), true);
  24. DeleteDirectoryAssets(Path.GetFullPath(Application.dataPath + PackConstant.SDKBridgeDestPath), true);
  25. }
  26. protected override void ChangePlayerSettings()
  27. {
  28. base.ChangePlayerSettings();
  29. PlayerSettings.bundleVersion = gameVersionCode.ToString(true);
  30. PlayerSettings.iOS.buildNumber = gameVersionCode;
  31. }
  32. protected override void ChangePlatformSpecial()
  33. {
  34. base.ChangePlatformSpecial();
  35. EditorUtility.DisplayProgressBar("平台特殊设置", "开始平台特殊设置", 0);
  36. if (plugins != null && plugins.Count > 0)
  37. {
  38. CopyUnityAssets(Application.dataPath + PackConstant.PluginiOSSrcPath
  39. , plugins
  40. , Application.dataPath + PackConstant.PluginiOSDestPath
  41. , Application.dataPath + PackConstant.PluginiOSSrcMetaPath
  42. , Application.dataPath + PackConstant.PluginiOSDestMetaPath);
  43. }
  44. EditorUtility.DisplayProgressBar("平台特殊设置", "平台特殊设置", 0.5f);
  45. if (sdkBridges != null && sdkBridges.Count > 0)
  46. {
  47. CopyUnityAssets(Application.dataPath + PackConstant.SDKBridgeSrcPath
  48. , sdkBridges
  49. , Application.dataPath + PackConstant.SDKBridgeDestPath
  50. , Application.dataPath + PackConstant.SDKBridgeSrcMetaPath
  51. , Application.dataPath + PackConstant.SDKBridgeDestMetaPath);
  52. }
  53. EditorUtility.DisplayProgressBar("平台特殊设置", "完成平台特殊设置", 1);
  54. }
  55. protected override BuildOptions ChangeBuildOptionsBefore(BuildOptions buildOptions)
  56. {
  57. buildOptions = base.ChangeBuildOptionsBefore(buildOptions);
  58. return buildOptions;
  59. }
  60. private void CopymmFile(string[] fs,string topath)
  61. {
  62. string path = Application.dataPath + PackConstant.BuildSharedResourcePath + "/Plugins/Special/iOS/";
  63. string outPath = GetBuildOutputPath();
  64. foreach (var item in fs)
  65. {
  66. File.Copy(path + item, Path.Combine(outPath,topath+ "/"+item), true);
  67. }
  68. }
  69. protected override void BuildAppCompleted(BuildOptions buildOptions)
  70. {
  71. base.BuildAppCompleted(buildOptions);
  72. string outPath = GetBuildOutputPath();
  73. // iOS 的XCode项目路径最好不能有中文,否则易出错
  74. // 所以设置一个txt文件来标注项目名字
  75. string dirPath = Path.GetDirectoryName(outPath);
  76. if (!Directory.Exists(dirPath))
  77. {
  78. Directory.CreateDirectory(dirPath);
  79. }
  80. string[] files = Directory.GetFiles(dirPath, ".txt");
  81. bool exist = false;
  82. string packFileName = GetPackFileName();
  83. foreach (string file in files)
  84. {
  85. if (Path.GetFileNameWithoutExtension(file) == packFileName)
  86. {
  87. exist = true;
  88. }
  89. else
  90. {
  91. File.Delete(file);
  92. }
  93. }
  94. if (!exist)
  95. {
  96. File.Create(Path.Combine(dirPath, packFileName + ".txt"));
  97. }
  98. // 为了过审核,需要删除OpenUrl
  99. //File.Copy(Application.dataPath + PackConstant.BuildSharedResourcePath + "/Plugins/Special/iOS/libiPhone-lib.a", Path.Combine(outPath, "Libraries/libiPhone-lib.a"), true);
  100. //CopymmFile(new string[] { "LuLuConnector.h", "LuLuConnector.mm" }, "Classes");
  101. ChangeXCodeProject(buildOptions);
  102. }
  103. #if UNITY_IOS
  104. private void AddFrameworkToProject(PBXProject pro, string target, string[] libs, bool weak)
  105. {
  106. foreach (var item in libs)
  107. {
  108. pro.AddFrameworkToProject(target,item,weak);
  109. }
  110. }
  111. #endif
  112. protected void ChangeXCodeProject(BuildOptions buildOptions)
  113. {
  114. #if UNITY_IOS
  115. EditorUtility.DisplayProgressBar("修改XCodeProject", "开始修改XCodeProject", 0);
  116. string outPath = GetBuildOutputPath();
  117. PBXProject pbxProject = new PBXProject();
  118. string pbxprojPath = PBXProject.GetPBXProjectPath(outPath);
  119. pbxProject.ReadFromFile(pbxprojPath);
  120. string targetGuid = pbxProject.GetUnityMainTargetGuid();//pbxProject.TargetGuidByName("Unity-iPhone");
  121. string unityFmwkGUid = pbxProject.GetUnityFrameworkTargetGuid();
  122. Debug.Log($"=================== targetGuid = {targetGuid} ===============");
  123. //pbxProject.AddCopyFilesBuildPhase(targetGuid, "SSBundle.bundle", Application.dataPath + "/Plugins/iOS", "");
  124. pbxProject.SetBuildProperty(targetGuid, "DEBUG_INFORMATION_FORMAT", "dwarf-with-dsym");
  125. pbxProject.SetBuildProperty(targetGuid, "GCC_GENERATE_DEBUGGING_SYMBOLS", "YES");
  126. pbxProject.SetBuildProperty(targetGuid, "COPY_PHASE_STRIP", "NO");
  127. pbxProject.SetBuildProperty(targetGuid, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
  128. pbxProject.SetBuildProperty(targetGuid, "GCC_ENABLE_CPP_EXCEPTIONS", "YES");
  129. pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
  130. pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC -lz");
  131. pbxProject.AddBuildProperty(unityFmwkGUid, "OTHER_LDFLAGS", "-ObjC");
  132. pbxProject.SetBuildProperty(targetGuid, "SWIFT_VERSION", "5.0");
  133. //pbxProject.AddBuildProperty(targetGuid, "Library Search Path", "$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)");
  134. //pbxProject.AddBuildProperty(targetGuid, "Library Search Path", "$(SDKROOT)/usr/lib/swift");
  135. //pbxProject.AddBuildProperty(unityFmwkGUid, "LIBRARY_SEARCH_PATHS", "$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)");
  136. //pbxProject.AddBuildProperty(targetGuid, "LIBRARY_SEARCH_PATHS", "$(SDKROOT)/usr/lib/swift");
  137. //pbxProject.AddBuildProperty(unityFmwkGUid, "LIBRARY_SEARCH_PATHS", "$(SDKROOT)/usr/lib/swift");
  138. //pbxProject.AddBuildProperty(targetGuid, "Runpath Search Path", "/usr/lib/swift");
  139. //pbxProject.AddBuildProperty(targetGuid, "RUNPATH_SEARCH_PATHS", "/usr/lib/swift");
  140. //AdSupport.framework AuthenticationServices.framework, Accelerate.framework
  141. AddFrameworkToProject(pbxProject, targetGuid,new string[] { "libz.dylib" , "libc++.dylib" , "AdSupport.framework" , "AuthenticationServices.framework", "Accelerate.framework" },false);
  142. AddFrameworkToProject(pbxProject, unityFmwkGUid, new string[] { "Accelerate.framework" },false);
  143. if (plugins.Contains("Bugly"))
  144. {
  145. pbxProject.AddFrameworkToProject(targetGuid, "libz.dylib", false);
  146. pbxProject.AddFrameworkToProject(targetGuid, "libc++.dylib", false);
  147. }
  148. if (plugins.Contains("Lebian"))
  149. {
  150. if ((buildOptions & BuildOptions.AcceptExternalModificationsToPlayer) == 0)
  151. {
  152. //pbxProject.AddShellScriptBuildPhase(targetGuid, "LB Script", "/bin/sh", "echo $(date \"+%Y%m%d%H%M%S\")$(tr -dc \"a-z\" < /dev/urandom | head -c 2) > $TARGET_BUILD_DIR/$PRODUCT_NAME.app/LBTimestamp");
  153. }
  154. }
  155. pbxProject.WriteToFile(pbxprojPath);
  156. EditorUtility.DisplayProgressBar("修改XCodeProject", "修改project.pbxproj完成", 0.25f);
  157. string plistPath = Path.Combine(outPath, "Info.plist");
  158. PlistDocument plistDocument = new PlistDocument();
  159. plistDocument.ReadFromFile(plistPath);
  160. //if (plugins.Contains("Lebian"))
  161. //{
  162. // int appId;
  163. // if (!ExpressionEvaluator.Evaluate(leBian_MainChId, out appId))
  164. // {
  165. // appId = 0;
  166. // }
  167. // plistDocument.root.SetInteger("LEBIAN_APPID", appId);
  168. // plistDocument.root.SetString("LEBIAN_SECID", leBian_SECID);
  169. // plistDocument.root.SetString("LEBIAN_META", leBian_ClientChId);
  170. // plistDocument.root.SetInteger("LEBIAN_VERCODE", (int)(uint)resVersionCode);
  171. //}
  172. //if (!string.IsNullOrEmpty(plugins.Find((x)=>x.StartsWith("YOUYI"))))
  173. //{
  174. // plistDocument.root.SetString("YouYi_GameId", "1023");
  175. // plistDocument.root.SetString("YouYi_AppKey", "3db62ef3789f3752a4a6d1b7ffc6f922");
  176. // plistDocument.root.SetString("YouYi_VestId", "");
  177. // plistDocument.root.SetString("NSUserTrackingUsageDescription", "获取设备信息用以精准推送您喜欢的内容");
  178. //}
  179. //plistDocument.root.SetString("FacebookUrlSchemeSuffix", "3db62ef3789f3752a4a6d1b7ffc6f922");
  180. /* FacebookAppID:facebook 后台应用参数
  181. * FacebookUrlSchemeSuffix 应用参数后缀,后缀仅可使用字母字符。(可不设置,当多应用共用一个 appid 时需添加,可避免 FB 登录成功后返回到其他应用)
  182. * FacebookDisplayNam:facebook 后台配置的应用名称{游戏名称};
  183. * FacebookClientToken:在 facebook 后台应用设置->高级->客户端口令查看;
  184. * LSApplicationQueriesSchemes:Facebook 相关白名单。*/
  185. //plistDocument.root.SetString("FacebookAppID", "214372257886989");
  186. //plistDocument.root.SetString("FacebookDisplayNam", "初心者大冒險");
  187. //plistDocument.root.SetString("FacebookClientToken", "eb90a0c5b6ab59dc16d63f2e78a34f39");
  188. //PlistElementArray plArr = plistDocument.root.CreateArray("LSApplicationQueriesSchemes");
  189. ////fbapi
  190. ////fb-messenger-api
  191. ////fbauth2
  192. ////fbshareextension
  193. ////fb-messenger-share-api
  194. //plArr.AddString("fbapi");
  195. //plArr.AddString("fb-messenger-api");
  196. //plArr.AddString("fbauth2");
  197. //plArr.AddString("fbshareextension");
  198. //plArr.AddString("fb-messenger-share-api");
  199. plistDocument.WriteToFile(plistPath);
  200. EditorUtility.DisplayProgressBar("修改XCodeProject", "修改info.list完成", 0.5f);
  201. if ((buildOptions & BuildOptions.AcceptExternalModificationsToPlayer) == 0)
  202. {
  203. string deviceSettingsMMPath = Path.Combine(outPath, "Classes/Unity/DeviceSettings.mm");
  204. ObjCScript script = new ObjCScript(deviceSettingsMMPath);
  205. script.AddImport("WentingSDK.h");
  206. script.ReplaceInLine("_VendorID = AllocCString([[UIDevice currentDevice].identifierForVendor UUIDString]);",
  207. "_VendorID = AllocCString([WentingSDK GetUUID]);");
  208. script.Save();
  209. //if (plugins.Contains("Lebian"))
  210. //{
  211. // string unityAppControllerMMPath = Path.Combine(outPath, "Classes/UnityAppController.mm");
  212. // script = new ObjCScript(unityAppControllerMMPath);
  213. // script.AddImport("LBSDK/LBInit.h", true);
  214. // int idx = script.FindLine("::printf(\"-> applicationDidFinishLaunching()\\n\");");
  215. // script.InsertLine(idx, idx + 1);
  216. // script.InsertLine(idx, idx + 2, "// 请务必在方法的最前面调用该接口,否则很容易出问题");
  217. // script.InsertLine(idx, idx + 3, "if ([[LBInit sharedInstance] LBSDKShouldInitWithLaunchOptions:launchOptions]) {");
  218. // script.InsertLine(idx, idx + 4, "\treturn YES;");
  219. // script.InsertLine(idx, idx + 5, "}");
  220. // script.InsertLine(idx, idx + 6);
  221. // script.Save();
  222. //}
  223. int idx = 0;
  224. string unityAppControllerMMPath = "";
  225. if (plugins.Contains("WentingLicense"))
  226. {
  227. unityAppControllerMMPath = Path.Combine(outPath, "Classes/UnityAppController.mm");
  228. script = new ObjCScript(unityAppControllerMMPath);
  229. script.AddImport("WentingLicense.h", false);
  230. idx = script.FindLine("::printf(\"-> applicationDidFinishLaunching()\\n\");");
  231. script.InsertLine(idx, idx + 1);
  232. script.InsertLine(idx, idx + 2, "if ([[WentingLicense sharedInstance] CheckAgreeWithLaunchOptions:launchOptions]) {");
  233. script.InsertLine(idx, idx + 3, "\treturn YES;");
  234. script.InsertLine(idx, idx + 4, "}");
  235. script.InsertLine(idx, idx + 5);
  236. script.Save();
  237. }
  238. unityAppControllerMMPath = Path.Combine(outPath, "Classes/UnityAppController.mm");
  239. script = new ObjCScript(unityAppControllerMMPath);
  240. //SMPCQuickSDK/SMPCQuickSDK.h
  241. //"QuickSDK_ios.h"
  242. script.AddImport("SMPCQuickSDK/SMPCQuickSDK.h", true);
  243. script.AddImport("QuickSDK_ios.h", false);
  244. // / 注骨事件监听[[QuickSok ios shareInstance] addNotifications];
  245. /// 初始化
  246. //SMPCQuickSDKInitConfigure* cfg m[[sMpcQuicksDKInitconfigure alloc] init];
  247. // cfg.productkey e e"09633196";
  248. // cfg.productCode 50840817638746911281319234382938":
  249. //int error m ismpcouicksok defaultInstance] initwithconfia: cf application:application didFinishLaunchinaWithoptions:launchoptions
  250. //if (error!8){
  251. // NSLog(@"不能启动初始化:hd", error);
  252. idx = script.FindLine("::printf(\"-> applicationDidFinishLaunching()\\n\");");
  253. script.InsertLine(idx, idx + 1);
  254. script.InsertLine(idx, idx + 2,"//注册事件监听");
  255. script.InsertLine(idx, idx + 3, "[[QuickSDK_ios shareInstance] addNotifications];");
  256. script.InsertLine(idx, idx + 4, "//初始化");
  257. script.InsertLine(idx, idx + 5, "SMPCQuickSDKInitConfigure *cfg = [[SMPCQuickSDKInitConfigure alloc] init];");
  258. script.InsertLine(idx, idx + 6, "cfg.productKey = @\"60700384\";");
  259. script.InsertLine(idx, idx + 7, "cfg.productCode = @\"84854058398037796099970471354112\"; ");
  260. script.InsertLine(idx, idx + 8, "int error = [[SMPCQuickSDK defaultInstance] intWithConfig:cfg application:application didFinishLaunchingWithOptions:launchOptions];");
  261. script.InsertLine(idx, idx + 9,"if (error != 0){");
  262. script.InsertLine(idx, idx + 10,"NsLog(@\"不能启动初始化:%d\",error);");
  263. script.InsertLine(idx, idx + 11,"}");
  264. script.Save();
  265. //if (!string.IsNullOrEmpty(plugins.Find((x) => x.StartsWith("YOUYI"))))
  266. //{
  267. // string unityAppControllerMMPath = Path.Combine(outPath, "Classes/UnityAppController.mm");
  268. // script = new ObjCScript(unityAppControllerMMPath);
  269. // script.AddImport("YWBeginSDK.h", false);
  270. // int idx = script.FindLine("::printf(\"-> applicationWillEnterForeground()\\n\");");
  271. // script.InsertLine(idx, idx + 1);
  272. // script.InsertLine(idx, idx + 2, "[[YWBeginSDK standardDeafaults] ywInterfaceApplicationWillEnterForeground];");
  273. // script.InsertLine(idx, idx + 3);
  274. // script.Save();
  275. //}
  276. }
  277. EditorUtility.DisplayProgressBar("修改XCodeProject", "完成修改XCodeProject", 1);
  278. EditorUtility.ClearProgressBar();
  279. #endif
  280. }
  281. protected class ObjCScript
  282. {
  283. private string m_FilePath;
  284. private List<string> m_Lines;
  285. private int importIdx = -1;
  286. private int includeIdx = -1;
  287. public ObjCScript(string filePath)
  288. {
  289. m_FilePath = filePath;
  290. if (!File.Exists(filePath))
  291. {
  292. PackLog.LogError(filePath + " is not Exist");
  293. return;
  294. }
  295. m_Lines = new List<string>(File.ReadAllLines(filePath));
  296. for (int i = 0, iMax = m_Lines.Count; i < iMax; i++)
  297. {
  298. string line = m_Lines[i].Trim();
  299. if (line.StartsWith("#import "))
  300. {
  301. importIdx = i;
  302. }
  303. if (line.StartsWith("#include "))
  304. {
  305. includeIdx = i;
  306. }
  307. }
  308. }
  309. public void AddImport(string name, bool isframework = false)
  310. {
  311. if (m_Lines == null)
  312. {
  313. PackLog.LogError(m_FilePath + " is not Valid Objc Script");
  314. return;
  315. }
  316. int idx = -1;
  317. if (importIdx >= 0 && importIdx < m_Lines.Count)
  318. {
  319. idx = importIdx;
  320. }
  321. if (idx == -1)
  322. {
  323. if (includeIdx >= 0 && includeIdx < m_Lines.Count)
  324. {
  325. idx = includeIdx;
  326. }
  327. }
  328. if (idx == -1)
  329. {
  330. PackLog.LogError(m_FilePath + " is not Valid Objc Script");
  331. return;
  332. }
  333. if (isframework)
  334. {
  335. idx = idx + 1;
  336. m_Lines.Insert(idx, "#import <" + name + ">");
  337. }
  338. else
  339. {
  340. idx = idx + 1;
  341. m_Lines.Insert(idx, "#import \"" + name + "\"");
  342. }
  343. }
  344. public int FindLine(string str)
  345. {
  346. if (m_Lines == null)
  347. {
  348. PackLog.LogError(m_FilePath + " is not Valid Objc Script");
  349. return -1;
  350. }
  351. return m_Lines.FindIndex((x) => x.Contains(str));
  352. }
  353. public void InsertLine(int alginIdx, int insertIdx, string str = "")
  354. {
  355. if (m_Lines == null)
  356. {
  357. PackLog.LogError(m_FilePath + " is not Valid Objc Script");
  358. return;
  359. }
  360. if (alginIdx < 0 || alginIdx >= m_Lines.Count)
  361. {
  362. PackLog.LogError(" alginIdx is not Valid Value " + alginIdx);
  363. return;
  364. }
  365. if (insertIdx < 0 || insertIdx >= m_Lines.Count)
  366. {
  367. PackLog.LogError(" insertIdx is not Valid Value " + insertIdx);
  368. return;
  369. }
  370. if (string.IsNullOrEmpty(str))
  371. {
  372. m_Lines.Insert(insertIdx, "");
  373. return;
  374. }
  375. string line = m_Lines[alginIdx];
  376. int len1 = line.Length;
  377. int len2 = len1 - line.TrimStart().Length;
  378. string startStr = line.Substring(0, len2);
  379. m_Lines.Insert(insertIdx, startStr + str);
  380. return;
  381. }
  382. public void ReplaceInLine(string oldStr, string newStr)
  383. {
  384. if (m_Lines == null)
  385. {
  386. PackLog.LogError(m_FilePath + " is not Valid Objc Script");
  387. return;
  388. }
  389. for (int i = 0, iMax = m_Lines.Count; i < iMax; i++)
  390. {
  391. string line = m_Lines[i];
  392. if (line.Contains(oldStr))
  393. {
  394. m_Lines[i] = line.Replace(oldStr, newStr);
  395. }
  396. }
  397. }
  398. public void Save()
  399. {
  400. if (m_Lines == null) return;
  401. File.WriteAllLines(m_FilePath, m_Lines);
  402. }
  403. public override string ToString()
  404. {
  405. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  406. for (int i = 0, iMax = m_Lines.Count; i < iMax; i++)
  407. {
  408. sb.AppendLine(m_Lines[i]);
  409. }
  410. return sb.ToString();
  411. }
  412. }
  413. }
  414. }