XCProject.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System;
  7. using System.Text.RegularExpressions;
  8. using System.Diagnostics;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Reflection;
  11. namespace cn.sharesdk.unity3d.sdkporter
  12. {
  13. public partial class XCProject : System.IDisposable
  14. {
  15. // private string _filePath;
  16. private PBXDictionary _datastore;
  17. public PBXDictionary _objects;
  18. private PBXDictionary _configurations;
  19. private PBXGroup _rootGroup;
  20. private string _defaultConfigurationName;
  21. private string _rootObjectKey;
  22. public string projectRootPath { get; private set; }
  23. private FileInfo projectFileInfo;
  24. public string filePath { get; private set; }
  25. private string sourcePathRoot;
  26. private bool modified = false;
  27. #region Data
  28. // Objects
  29. private PBXDictionary<PBXBuildFile> _buildFiles;
  30. private PBXDictionary<PBXGroup> _groups;
  31. private PBXDictionary<PBXFileReference> _fileReferences;
  32. private PBXDictionary<PBXNativeTarget> _nativeTargets;
  33. private PBXDictionary<PBXFrameworksBuildPhase> _frameworkBuildPhases;
  34. private PBXDictionary<PBXResourcesBuildPhase> _resourcesBuildPhases;
  35. private PBXDictionary<PBXShellScriptBuildPhase> _shellScriptBuildPhases;
  36. private PBXDictionary<PBXSourcesBuildPhase> _sourcesBuildPhases;
  37. private PBXDictionary<PBXCopyFilesBuildPhase> _copyBuildPhases;
  38. private PBXDictionary<XCBuildConfiguration> _buildConfigurations;
  39. private PBXDictionary<XCConfigurationList> _configurationLists;
  40. private PBXProject _project;
  41. #endregion
  42. #region Constructor
  43. public XCProject()
  44. {
  45. }
  46. public XCProject( string filePath ) : this()
  47. {
  48. if( !System.IO.Directory.Exists( filePath ) ) {
  49. UnityEngine.Debug.LogWarning( "Path does not exists." );
  50. return;
  51. }
  52. if( filePath.EndsWith( ".xcodeproj" ) ) {
  53. // UnityEngine.Debug.Log( "Opening project " + filePath );
  54. this.projectRootPath = Path.GetDirectoryName( filePath );
  55. this.filePath = filePath;
  56. } else {
  57. // UnityEngine.Debug.Log( "Looking for xcodeproj files in " + filePath );
  58. string[] projects = System.IO.Directory.GetDirectories( filePath, "*.xcodeproj" );
  59. if( projects.Length == 0 ) {
  60. UnityEngine.Debug.LogWarning( "Error: missing xcodeproj file" );
  61. return;
  62. }
  63. this.projectRootPath = filePath;
  64. this.filePath = projects[ 0 ];
  65. }
  66. // Convert to absolute
  67. this.projectRootPath = Path.GetFullPath(this.projectRootPath);
  68. projectFileInfo = new FileInfo( Path.Combine( this.filePath, "project.pbxproj" ) );
  69. StreamReader sr = projectFileInfo.OpenText();
  70. string contents = sr.ReadToEnd();
  71. sr.Close();
  72. PBXParser parser = new PBXParser();
  73. _datastore = parser.Decode( contents );
  74. if( _datastore == null ) {
  75. throw new System.Exception( "Project file not found at file path " + filePath );
  76. }
  77. if( !_datastore.ContainsKey( "objects" ) ) {
  78. UnityEngine.Debug.Log( "Errore " + _datastore.Count );
  79. return;
  80. }
  81. _objects = (PBXDictionary)_datastore["objects"];
  82. modified = false;
  83. _rootObjectKey = (string)_datastore["rootObject"];
  84. if( !string.IsNullOrEmpty( _rootObjectKey ) ) {
  85. // _rootObject = (PBXDictionary)_objects[ _rootObjectKey ];
  86. _project = new PBXProject( _rootObjectKey, (PBXDictionary)_objects[ _rootObjectKey ] );
  87. // _rootGroup = (PBXDictionary)_objects[ (string)_rootObject[ "mainGroup" ] ];
  88. _rootGroup = new PBXGroup( _rootObjectKey, (PBXDictionary)_objects[ _project.mainGroupID ] );
  89. }
  90. else {
  91. UnityEngine.Debug.LogWarning( "error: project has no root object" );
  92. _project = null;
  93. _rootGroup = null;
  94. }
  95. }
  96. #endregion
  97. #region Properties
  98. public PBXProject project {
  99. get {
  100. return _project;
  101. }
  102. }
  103. public PBXGroup rootGroup {
  104. get {
  105. return _rootGroup;
  106. }
  107. }
  108. public PBXDictionary<PBXBuildFile> buildFiles {
  109. get {
  110. if( _buildFiles == null ) {
  111. _buildFiles = new PBXDictionary<PBXBuildFile>( _objects );
  112. }
  113. return _buildFiles;
  114. }
  115. }
  116. public PBXDictionary<PBXGroup> groups {
  117. get {
  118. if( _groups == null ) {
  119. _groups = new PBXDictionary<PBXGroup>( _objects );
  120. }
  121. return _groups;
  122. }
  123. }
  124. public PBXDictionary<PBXFileReference> fileReferences {
  125. get {
  126. if( _fileReferences == null ) {
  127. _fileReferences = new PBXDictionary<PBXFileReference>( _objects );
  128. }
  129. return _fileReferences;
  130. }
  131. }
  132. public PBXDictionary<PBXNativeTarget> nativeTargets {
  133. get {
  134. if( _nativeTargets == null ) {
  135. _nativeTargets = new PBXDictionary<PBXNativeTarget>( _objects );
  136. }
  137. return _nativeTargets;
  138. }
  139. }
  140. public PBXDictionary<XCBuildConfiguration> buildConfigurations {
  141. get {
  142. if( _buildConfigurations == null ) {
  143. _buildConfigurations = new PBXDictionary<XCBuildConfiguration>( _objects );
  144. }
  145. return _buildConfigurations;
  146. }
  147. }
  148. public PBXDictionary<XCConfigurationList> configurationLists {
  149. get {
  150. if( _configurationLists == null ) {
  151. _configurationLists = new PBXDictionary<XCConfigurationList>( _objects );
  152. }
  153. return _configurationLists;
  154. }
  155. }
  156. public PBXDictionary<PBXFrameworksBuildPhase> frameworkBuildPhases {
  157. get {
  158. if( _frameworkBuildPhases == null ) {
  159. _frameworkBuildPhases = new PBXDictionary<PBXFrameworksBuildPhase>( _objects );
  160. }
  161. return _frameworkBuildPhases;
  162. }
  163. }
  164. public PBXDictionary<PBXResourcesBuildPhase> resourcesBuildPhases {
  165. get {
  166. if( _resourcesBuildPhases == null ) {
  167. _resourcesBuildPhases = new PBXDictionary<PBXResourcesBuildPhase>( _objects );
  168. }
  169. return _resourcesBuildPhases;
  170. }
  171. }
  172. public PBXDictionary<PBXShellScriptBuildPhase> shellScriptBuildPhases {
  173. get {
  174. if( _shellScriptBuildPhases == null ) {
  175. _shellScriptBuildPhases = new PBXDictionary<PBXShellScriptBuildPhase>( _objects );
  176. }
  177. return _shellScriptBuildPhases;
  178. }
  179. }
  180. public PBXDictionary<PBXSourcesBuildPhase> sourcesBuildPhases {
  181. get {
  182. if( _sourcesBuildPhases == null ) {
  183. _sourcesBuildPhases = new PBXDictionary<PBXSourcesBuildPhase>( _objects );
  184. }
  185. return _sourcesBuildPhases;
  186. }
  187. }
  188. public PBXDictionary<PBXCopyFilesBuildPhase> copyBuildPhases {
  189. get {
  190. if( _copyBuildPhases == null ) {
  191. _copyBuildPhases = new PBXDictionary<PBXCopyFilesBuildPhase>( _objects );
  192. }
  193. return _copyBuildPhases;
  194. }
  195. }
  196. #endregion
  197. #region PBXMOD
  198. public bool AddOtherCFlags( string flag )
  199. {
  200. return AddOtherCFlags( new PBXList( flag ) );
  201. }
  202. public bool AddOtherCFlags( PBXList flags )
  203. {
  204. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  205. buildConfig.Value.AddOtherCFlags( flags );
  206. }
  207. modified = true;
  208. return modified;
  209. }
  210. public bool AddOtherLDFlags( string flag )
  211. {
  212. return AddOtherLDFlags( new PBXList( flag ) );
  213. }
  214. public bool AddOtherLDFlags( PBXList flags )
  215. {
  216. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  217. buildConfig.Value.AddOtherLDFlags( flags );
  218. }
  219. modified = true;
  220. return modified;
  221. }
  222. public bool GccEnableCppExceptions (string value)
  223. {
  224. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  225. buildConfig.Value.GccEnableCppExceptions( value );
  226. }
  227. modified = true;
  228. return modified;
  229. }
  230. public bool GccEnableObjCExceptions (string value)
  231. {
  232. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  233. buildConfig.Value.GccEnableObjCExceptions( value );
  234. }
  235. modified = true;
  236. return modified;
  237. }
  238. public bool AddHeaderSearchPaths( string path )
  239. {
  240. return AddHeaderSearchPaths( new PBXList( path ) );
  241. }
  242. public bool AddHeaderSearchPaths( PBXList paths )
  243. {
  244. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  245. // UnityEngine.Debug.Log( "ADDING HEADER PATH: " + paths + " to " + buildConfig.Key );
  246. buildConfig.Value.AddHeaderSearchPaths( paths );
  247. }
  248. modified = true;
  249. return modified;
  250. }
  251. public bool AddLibrarySearchPaths( string path )
  252. {
  253. return AddLibrarySearchPaths( new PBXList( path ) );
  254. }
  255. public bool AddLibrarySearchPaths( PBXList paths )
  256. {
  257. foreach( KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations ) {
  258. buildConfig.Value.AddLibrarySearchPaths( paths );
  259. }
  260. modified = true;
  261. return modified;
  262. }
  263. public bool AddFrameworkSearchPaths(string path)
  264. {
  265. return AddFrameworkSearchPaths(new PBXList(path));
  266. }
  267. public bool AddFrameworkSearchPaths(PBXList paths)
  268. {
  269. foreach (KeyValuePair<string, XCBuildConfiguration> buildConfig in buildConfigurations)
  270. {
  271. buildConfig.Value.AddFrameworkSearchPaths(paths);
  272. }
  273. modified = true;
  274. return modified;
  275. }
  276. public object GetObject( string guid )
  277. {
  278. return _objects[guid];
  279. }
  280. public PBXDictionary AddFile( string filePath, PBXGroup parent = null, string tree = "SOURCE_ROOT", bool createBuildFiles = true, bool weak = false )
  281. {
  282. PBXDictionary results = new PBXDictionary();
  283. string absPath = string.Empty;
  284. if( Path.IsPathRooted( filePath ) ) {
  285. absPath = filePath;
  286. // UnityEngine.Debug.Log( "Is rooted: " + absPath );
  287. }
  288. else if( tree.CompareTo( "SDKROOT" ) != 0) {
  289. absPath = Path.Combine( Application.dataPath.Replace("Assets", ""), filePath );
  290. // UnityEngine.Debug.Log( "RElative: " + absPath );
  291. }
  292. if( !( File.Exists( absPath ) || Directory.Exists( absPath ) ) && tree.CompareTo( "SDKROOT" ) != 0 ) {
  293. // UnityEngine.Debug.Log( "Missing file: " + absPath + " > " + filePath );
  294. return results;
  295. }
  296. else if( tree.CompareTo( "SOURCE_ROOT" ) == 0 || tree.CompareTo( "GROUP" ) == 0 ) {
  297. System.Uri fileURI = new System.Uri( absPath );
  298. System.Uri rootURI = new System.Uri( ( projectRootPath + "/." ) );
  299. filePath = rootURI.MakeRelativeUri( fileURI ).ToString();
  300. }
  301. // UnityEngine.Debug.Log( "Add file result path: " + filePath );
  302. if( parent == null ) {
  303. parent = _rootGroup;
  304. }
  305. PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( filePath ) );
  306. if( fileReference != null ) {
  307. // UnityEngine.Debug.Log( "File già presente." );
  308. return null;
  309. }
  310. fileReference = new PBXFileReference( filePath, (TreeEnum)System.Enum.Parse( typeof(TreeEnum), tree ) );
  311. parent.AddChild( fileReference );
  312. fileReferences.Add( fileReference );
  313. results.Add( fileReference.guid, fileReference );
  314. //Create a build file for reference
  315. if( !string.IsNullOrEmpty( fileReference.buildPhase ) && createBuildFiles ) {
  316. // PBXDictionary<PBXBuildPhase> currentPhase = GetBuildPhase( fileReference.buildPhase );
  317. PBXBuildFile buildFile;
  318. switch( fileReference.buildPhase ) {
  319. case "PBXFrameworksBuildPhase":
  320. foreach( KeyValuePair<string, PBXFrameworksBuildPhase> currentObject in frameworkBuildPhases ) {
  321. buildFile = new PBXBuildFile( fileReference, weak );
  322. buildFiles.Add( buildFile );
  323. currentObject.Value.AddBuildFile( buildFile );
  324. }
  325. if ( !string.IsNullOrEmpty( absPath ) && File.Exists(absPath) && tree.CompareTo( "SOURCE_ROOT" ) == 0 ) {
  326. // UnityEngine.Debug.LogError(absPath);
  327. string libraryPath = Path.Combine( "$(SRCROOT)", Path.GetDirectoryName( filePath ) );
  328. this.AddLibrarySearchPaths( new PBXList(libraryPath) );
  329. }
  330. else if (!string.IsNullOrEmpty( absPath ) && Directory.Exists(absPath) && absPath.EndsWith(".framework") && tree.CompareTo("GROUP") == 0) { // Annt: Add framework search path for FacebookSDK
  331. string frameworkPath = Path.Combine( "$(SRCROOT)", Path.GetDirectoryName( filePath ) );
  332. this.AddFrameworkSearchPaths(new PBXList(frameworkPath));
  333. }
  334. break;
  335. case "PBXResourcesBuildPhase":
  336. foreach( KeyValuePair<string, PBXResourcesBuildPhase> currentObject in resourcesBuildPhases ) {
  337. buildFile = new PBXBuildFile( fileReference, weak );
  338. buildFiles.Add( buildFile );
  339. currentObject.Value.AddBuildFile( buildFile );
  340. }
  341. break;
  342. case "PBXShellScriptBuildPhase":
  343. foreach( KeyValuePair<string, PBXShellScriptBuildPhase> currentObject in shellScriptBuildPhases ) {
  344. buildFile = new PBXBuildFile( fileReference, weak );
  345. buildFiles.Add( buildFile );
  346. currentObject.Value.AddBuildFile( buildFile );
  347. }
  348. break;
  349. case "PBXSourcesBuildPhase":
  350. foreach( KeyValuePair<string, PBXSourcesBuildPhase> currentObject in sourcesBuildPhases ) {
  351. buildFile = new PBXBuildFile( fileReference, weak );
  352. buildFiles.Add( buildFile );
  353. currentObject.Value.AddBuildFile( buildFile );
  354. }
  355. break;
  356. case "PBXCopyFilesBuildPhase":
  357. foreach( KeyValuePair<string, PBXCopyFilesBuildPhase> currentObject in copyBuildPhases ) {
  358. buildFile = new PBXBuildFile( fileReference, weak );
  359. buildFiles.Add( buildFile );
  360. currentObject.Value.AddBuildFile( buildFile );
  361. }
  362. break;
  363. case null:
  364. UnityEngine.Debug.LogWarning( "fase non supportata null" );
  365. break;
  366. default:
  367. UnityEngine.Debug.LogWarning( "fase non supportata def" );
  368. return null;
  369. }
  370. }
  371. return results;
  372. }
  373. public bool AddFolder( string folderPath, PBXGroup parent = null, string[] exclude = null, bool recursive = true, bool createBuildFile = true )
  374. {
  375. if( !Directory.Exists( folderPath ) )
  376. return false;
  377. DirectoryInfo sourceDirectoryInfo = new DirectoryInfo( folderPath );
  378. if( exclude == null )
  379. exclude = new string[] {};
  380. string regexExclude = string.Format( @"{0}", string.Join( "|", exclude ) );
  381. // PBXDictionary results = new PBXDictionary();
  382. if( parent == null )
  383. parent = rootGroup;
  384. // Create group
  385. PBXGroup newGroup = GetGroup( sourceDirectoryInfo.Name, null /*relative path*/, parent );
  386. // groups.Add( newGroup );
  387. foreach( string directory in Directory.GetDirectories( folderPath ) )
  388. {
  389. if( Regex.IsMatch( directory, regexExclude ) ) {
  390. continue;
  391. }
  392. // special_folders = ['.bundle', '.framework', '.xcodeproj']
  393. // UnityEngine.Debug.Log( "DIR: " + directory );
  394. if( directory.EndsWith( ".bundle" ) ) {
  395. // Treath it like a file and copy even if not recursive
  396. //UnityEngine.Debug.LogWarning( "This is a special folder: " + directory );
  397. AddFile( directory, newGroup, "SOURCE_ROOT", createBuildFile );
  398. // UnityEngine.Debug.Log( "fatto" );
  399. continue;
  400. }
  401. if( recursive ) {
  402. // UnityEngine.Debug.Log( "recursive" );
  403. AddFolder( directory, newGroup, exclude, recursive, createBuildFile );
  404. }
  405. }
  406. // Adding files.
  407. foreach( string file in Directory.GetFiles( folderPath ) ) {
  408. if( Regex.IsMatch( file, regexExclude ) ) {
  409. continue;
  410. }
  411. // UnityEngine.Debug.Log( "--> " + file + ", " + newGroup );
  412. AddFile( file, newGroup, "SOURCE_ROOT", createBuildFile );
  413. }
  414. modified = true;
  415. return modified;
  416. }
  417. #endregion
  418. #region Getters
  419. public PBXFileReference GetFile( string name )
  420. {
  421. if( string.IsNullOrEmpty( name ) ) {
  422. return null;
  423. }
  424. foreach( KeyValuePair<string, PBXFileReference> current in fileReferences ) {
  425. if( !string.IsNullOrEmpty( current.Value.name ) && current.Value.name.CompareTo( name ) == 0 ) {
  426. return current.Value;
  427. }
  428. }
  429. return null;
  430. }
  431. public PBXGroup GetGroup( string name, string path = null, PBXGroup parent = null )
  432. {
  433. // UnityEngine.Debug.Log( "GetGroup: " + name + ", " + path + ", " + parent );
  434. if( string.IsNullOrEmpty( name ) )
  435. return null;
  436. if( parent == null )
  437. parent = rootGroup;
  438. foreach( KeyValuePair<string, PBXGroup> current in groups ) {
  439. // UnityEngine.Debug.Log( "current: " + current.Value.guid + ", " + current.Value.name + ", " + current.Value.path + " - " + parent.HasChild( current.Key ) );
  440. if( string.IsNullOrEmpty( current.Value.name ) ) {
  441. if( current.Value.path.CompareTo( name ) == 0 && parent.HasChild( current.Key ) ) {
  442. return current.Value;
  443. }
  444. }
  445. else if( current.Value.name.CompareTo( name ) == 0 && parent.HasChild( current.Key ) ) {
  446. return current.Value;
  447. }
  448. }
  449. PBXGroup result = new PBXGroup( name, path );
  450. groups.Add( result );
  451. parent.AddChild( result );
  452. modified = true;
  453. return result;
  454. }
  455. #endregion
  456. #region Mods
  457. public void ApplyMod( string pbxmod )
  458. {
  459. XCMod mod = new XCMod( pbxmod );
  460. ApplyMod( mod );
  461. }
  462. public void ApplyMod( XCMod mod )
  463. {
  464. PBXGroup modGroup = this.GetGroup( mod.group );
  465. // UnityEngine.Debug.Log( "Adding libraries..." );
  466. // PBXGroup librariesGroup = this.GetGroup( "Libraries" );
  467. foreach( XCModFile libRef in mod.libs ) {
  468. string completeLibPath = System.IO.Path.Combine( "usr/lib", libRef.filePath );
  469. this.AddFile( completeLibPath, modGroup, "SDKROOT", true, libRef.isWeak );
  470. }
  471. // UnityEngine.Debug.Log( "Adding frameworks..." );
  472. PBXGroup frameworkGroup = this.GetGroup( "Frameworks" );
  473. foreach( string framework in mod.frameworks ) {
  474. string[] filename = framework.Split( ':' );
  475. bool isWeak = ( filename.Length > 1 ) ? true : false;
  476. string completePath = System.IO.Path.Combine( "System/Library/Frameworks", filename[0] );
  477. this.AddFile( completePath, frameworkGroup, "SDKROOT", true, isWeak );
  478. }
  479. // UnityEngine.Debug.Log( "Adding files..." );
  480. // foreach( string filePath in mod.files ) {
  481. // string absoluteFilePath = System.IO.Path.Combine( mod.path, filePath );
  482. //
  483. //
  484. // if( filePath.EndsWith(".framework") )
  485. // this.AddFile( absoluteFilePath, frameworkGroup, "GROUP", true, false);
  486. // else
  487. // this.AddFile( absoluteFilePath, modGroup );
  488. // }
  489. // UnityEngine.Debug.Log( "Adding folders..." );
  490. foreach( string folderPath in mod.folders ) {
  491. string absoluteFolderPath = System.IO.Path.Combine( mod.path, folderPath );
  492. this.AddFolder( absoluteFolderPath, modGroup, (string[])mod.excludes.ToArray( typeof(string) ) );
  493. }
  494. // UnityEngine.Debug.Log( "Adding headerpaths..." );
  495. foreach( string headerpath in mod.headerpaths ) {
  496. string absoluteHeaderPath = System.IO.Path.Combine( mod.path, headerpath );
  497. this.AddHeaderSearchPaths( absoluteHeaderPath );
  498. }
  499. // UnityEngine.Debug.Log( "Adding librarypaths..." );
  500. foreach( string headerpath in mod.librarypaths ) {
  501. string absoluteLibraryPath = System.IO.Path.Combine( mod.path, headerpath );
  502. this.AddLibrarySearchPaths( absoluteLibraryPath );
  503. }
  504. // UnityEngine.Debug.Log ("Adding Zip...");
  505. foreach (string zipPath in mod.zipPaths) {
  506. string zipFileName = zipPath;
  507. string sdkName = zipFileName.Remove(zipFileName.LastIndexOf("."));
  508. string unzipLoc = mod.path + "/" + zipFileName;
  509. string dirpath = this.projectRootPath;
  510. ZipHelper zipTool = new ZipHelper();
  511. zipTool.UnzipWithPath (unzipLoc, dirpath);
  512. //删除多余解压文件
  513. DirectoryInfo di = new DirectoryInfo(dirpath + "/__MACOSX");
  514. di.Delete(true);
  515. if (sdkName == "SDK")
  516. {
  517. //根据Editor所勾选执行删除工作
  518. DeleteUnnecessaryFile();
  519. }
  520. string absoluteFolderPath = System.IO.Path.Combine( this.projectRootPath, sdkName + "/" );
  521. // this.AddFolder( absoluteFolderPath, modGroup, (string[])mod.excludes.ToArray( typeof(string) ) );
  522. //第二个参数传null,能够在xcode项目再减少一层文件夹
  523. this.AddFolder( absoluteFolderPath, null, (string[])mod.excludes.ToArray( typeof(string) ) );
  524. }
  525. // UnityEngine.Debug.Log( "Adding files..." );
  526. foreach( string filePath in mod.files ) {
  527. //原版为根据.projmods中的files来添加
  528. // string absoluteFilePath = System.IO.Path.Combine( mod.path, filePath );
  529. //对于ShareSDK,由于解压的文件夹已经移动至Xcode根目录,所以要根据根目录路径进行库路径添加
  530. string absoluteFilePath = System.IO.Path.Combine(this.projectRootPath, filePath );
  531. if( filePath.EndsWith(".framework") )
  532. this.AddFile( absoluteFilePath, frameworkGroup, "GROUP", true, false);
  533. else
  534. this.AddFile( absoluteFilePath, modGroup );
  535. }
  536. // UnityEngine.Debug.Log( "Configure build settings..." );
  537. Hashtable buildSettings = mod.buildSettings;
  538. if( buildSettings.ContainsKey("OTHER_LDFLAGS") )
  539. {
  540. // UnityEngine.Debug.Log( " Adding other linker flags..." );
  541. ArrayList otherLinkerFlags = (ArrayList) buildSettings["OTHER_LDFLAGS"];
  542. foreach( string linker in otherLinkerFlags )
  543. {
  544. string _linker = linker;
  545. if( !_linker.StartsWith("-") )
  546. _linker = "-" + _linker;
  547. this.AddOtherLDFlags( _linker );
  548. }
  549. }
  550. if( buildSettings.ContainsKey("GCC_ENABLE_CPP_EXCEPTIONS") )
  551. {
  552. // UnityEngine.Debug.Log( " GCC_ENABLE_CPP_EXCEPTIONS = " + buildSettings["GCC_ENABLE_CPP_EXCEPTIONS"] );
  553. this.GccEnableCppExceptions( (string) buildSettings["GCC_ENABLE_CPP_EXCEPTIONS"] );
  554. }
  555. if( buildSettings.ContainsKey("GCC_ENABLE_OBJC_EXCEPTIONS") )
  556. {
  557. // UnityEngine.Debug.Log( " GCC_ENABLE_OBJC_EXCEPTIONS = " + buildSettings["GCC_ENABLE_OBJC_EXCEPTIONS"] );
  558. this.GccEnableObjCExceptions( (string) buildSettings["GCC_ENABLE_OBJC_EXCEPTIONS"] );
  559. }
  560. this.Consolidate();
  561. }
  562. #endregion
  563. #region Savings
  564. public void Consolidate()
  565. {
  566. PBXDictionary consolidated = new PBXDictionary();
  567. consolidated.Append<PBXBuildFile>( this.buildFiles );
  568. consolidated.Append<PBXGroup>( this.groups );
  569. consolidated.Append<PBXFileReference>( this.fileReferences );
  570. // consolidated.Append<PBXProject>( this.project );
  571. consolidated.Append<PBXNativeTarget>( this.nativeTargets );
  572. consolidated.Append<PBXFrameworksBuildPhase>( this.frameworkBuildPhases );
  573. consolidated.Append<PBXResourcesBuildPhase>( this.resourcesBuildPhases );
  574. consolidated.Append<PBXShellScriptBuildPhase>( this.shellScriptBuildPhases );
  575. consolidated.Append<PBXSourcesBuildPhase>( this.sourcesBuildPhases );
  576. consolidated.Append<PBXCopyFilesBuildPhase>( this.copyBuildPhases );
  577. consolidated.Append<XCBuildConfiguration>( this.buildConfigurations );
  578. consolidated.Append<XCConfigurationList>( this.configurationLists );
  579. consolidated.Add( project.guid, project.data );
  580. _objects = consolidated;
  581. consolidated = null;
  582. }
  583. public void Backup()
  584. {
  585. string backupPath = Path.Combine( this.filePath, "project.backup.pbxproj" );
  586. // Delete previous backup file
  587. if( File.Exists( backupPath ) )
  588. File.Delete( backupPath );
  589. // Backup original pbxproj file first
  590. File.Copy( System.IO.Path.Combine( this.filePath, "project.pbxproj" ), backupPath );
  591. }
  592. /// <summary>
  593. /// Saves a project after editing.
  594. /// </summary>
  595. public void Save()
  596. {
  597. PBXDictionary result = new PBXDictionary();
  598. result.Add( "archiveVersion", 1 );
  599. result.Add( "classes", new PBXDictionary() );
  600. result.Add( "objectVersion", 45 );
  601. Consolidate();
  602. result.Add( "objects", _objects );
  603. result.Add( "rootObject", _rootObjectKey );
  604. Backup();
  605. // Parse result object directly into file
  606. PBXParser parser = new PBXParser();
  607. StreamWriter saveFile = File.CreateText( System.IO.Path.Combine( this.filePath, "project.pbxproj" ) );
  608. saveFile.Write( parser.Encode( result, false ) );
  609. saveFile.Close();
  610. }
  611. /**
  612. * Raw project data.
  613. */
  614. public Dictionary<string, object> objects
  615. {
  616. get
  617. {
  618. return null;
  619. }
  620. }
  621. #endregion
  622. public void Dispose()
  623. {
  624. }
  625. private void DeleteUnnecessaryFile()
  626. {
  627. ChosenPlatforms chosenPlats;
  628. try
  629. {
  630. string binFilePath = Application.dataPath + "/ShareSDKiOSAutoPackage/Editor/SDKPorter/ManagePlatforms/ChosenPlatforms.bin";
  631. BinaryFormatter formatter = new BinaryFormatter();
  632. Stream destream = new FileStream(binFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  633. chosenPlats = (ChosenPlatforms)formatter.Deserialize(destream);
  634. destream.Flush();
  635. destream.Close();
  636. }
  637. catch (Exception)
  638. {
  639. chosenPlats = new ChosenPlatforms ();
  640. }
  641. Type t = chosenPlats.GetType ();
  642. foreach (PropertyInfo platform in t.GetProperties ())
  643. {
  644. Hashtable plat = (Hashtable)platform.GetValue (chosenPlats, null);
  645. if (!(Boolean)plat["chosen"])
  646. {
  647. //删除多余平台相关文件
  648. if (plat ["sdkPath"] != null)
  649. {
  650. DirectoryInfo di = new DirectoryInfo(this.projectRootPath + (string)plat ["sdkPath"]);
  651. if (di.Exists)
  652. {
  653. di.Delete(true);
  654. }
  655. }
  656. if (plat ["connectorPath"] != null)
  657. {
  658. DirectoryInfo di = new DirectoryInfo(this.projectRootPath + (string)plat ["connectorPath"]);
  659. if (di.Exists)
  660. {
  661. di.Delete(true);
  662. }
  663. }
  664. if (plat ["jsPath"] != null)
  665. {
  666. if (File.Exists (this.projectRootPath + (string)plat ["jsPath"]))
  667. {
  668. File.Delete (this.projectRootPath + (string)plat ["jsPath"]);
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }