XCProject.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. // TODO: Aggiungere controllo se file già presente
  306. PBXFileReference fileReference = GetFile( System.IO.Path.GetFileName( filePath ) );
  307. if( fileReference != null ) {
  308. // UnityEngine.Debug.Log( "File già presente." );
  309. return null;
  310. }
  311. fileReference = new PBXFileReference( filePath, (TreeEnum)System.Enum.Parse( typeof(TreeEnum), tree ) );
  312. parent.AddChild( fileReference );
  313. fileReferences.Add( fileReference );
  314. results.Add( fileReference.guid, fileReference );
  315. //Create a build file for reference
  316. if( !string.IsNullOrEmpty( fileReference.buildPhase ) && createBuildFiles ) {
  317. // PBXDictionary<PBXBuildPhase> currentPhase = GetBuildPhase( fileReference.buildPhase );
  318. PBXBuildFile buildFile;
  319. switch( fileReference.buildPhase ) {
  320. case "PBXFrameworksBuildPhase":
  321. foreach( KeyValuePair<string, PBXFrameworksBuildPhase> currentObject in frameworkBuildPhases ) {
  322. buildFile = new PBXBuildFile( fileReference, weak );
  323. buildFiles.Add( buildFile );
  324. currentObject.Value.AddBuildFile( buildFile );
  325. }
  326. if ( !string.IsNullOrEmpty( absPath ) && File.Exists(absPath) && tree.CompareTo( "SOURCE_ROOT" ) == 0 ) {
  327. // UnityEngine.Debug.LogError(absPath);
  328. string libraryPath = Path.Combine( "$(SRCROOT)", Path.GetDirectoryName( filePath ) );
  329. this.AddLibrarySearchPaths( new PBXList(libraryPath) );
  330. }
  331. else if (!string.IsNullOrEmpty( absPath ) && Directory.Exists(absPath) && absPath.EndsWith(".framework") && tree.CompareTo("GROUP") == 0) { // Annt: Add framework search path for FacebookSDK
  332. string frameworkPath = Path.Combine( "$(SRCROOT)", Path.GetDirectoryName( filePath ) );
  333. this.AddFrameworkSearchPaths(new PBXList(frameworkPath));
  334. }
  335. break;
  336. case "PBXResourcesBuildPhase":
  337. foreach( KeyValuePair<string, PBXResourcesBuildPhase> currentObject in resourcesBuildPhases ) {
  338. buildFile = new PBXBuildFile( fileReference, weak );
  339. buildFiles.Add( buildFile );
  340. currentObject.Value.AddBuildFile( buildFile );
  341. }
  342. break;
  343. case "PBXShellScriptBuildPhase":
  344. foreach( KeyValuePair<string, PBXShellScriptBuildPhase> currentObject in shellScriptBuildPhases ) {
  345. buildFile = new PBXBuildFile( fileReference, weak );
  346. buildFiles.Add( buildFile );
  347. currentObject.Value.AddBuildFile( buildFile );
  348. }
  349. break;
  350. case "PBXSourcesBuildPhase":
  351. foreach( KeyValuePair<string, PBXSourcesBuildPhase> currentObject in sourcesBuildPhases ) {
  352. buildFile = new PBXBuildFile( fileReference, weak );
  353. buildFiles.Add( buildFile );
  354. currentObject.Value.AddBuildFile( buildFile );
  355. }
  356. break;
  357. case "PBXCopyFilesBuildPhase":
  358. foreach( KeyValuePair<string, PBXCopyFilesBuildPhase> currentObject in copyBuildPhases ) {
  359. buildFile = new PBXBuildFile( fileReference, weak );
  360. buildFiles.Add( buildFile );
  361. currentObject.Value.AddBuildFile( buildFile );
  362. }
  363. break;
  364. case null:
  365. UnityEngine.Debug.LogWarning( "fase non supportata null" );
  366. break;
  367. default:
  368. UnityEngine.Debug.LogWarning( "fase non supportata def" );
  369. return null;
  370. }
  371. }
  372. return results;
  373. }
  374. public bool AddFolder( string folderPath, PBXGroup parent = null, string[] exclude = null, bool recursive = true, bool createBuildFile = true )
  375. {
  376. if( !Directory.Exists( folderPath ) )
  377. return false;
  378. DirectoryInfo sourceDirectoryInfo = new DirectoryInfo( folderPath );
  379. if( exclude == null )
  380. exclude = new string[] {};
  381. string regexExclude = string.Format( @"{0}", string.Join( "|", exclude ) );
  382. // PBXDictionary results = new PBXDictionary();
  383. if( parent == null )
  384. parent = rootGroup;
  385. // Create group
  386. PBXGroup newGroup = GetGroup( sourceDirectoryInfo.Name, null /*relative path*/, parent );
  387. // groups.Add( newGroup );
  388. foreach( string directory in Directory.GetDirectories( folderPath ) )
  389. {
  390. if( Regex.IsMatch( directory, regexExclude ) ) {
  391. continue;
  392. }
  393. // special_folders = ['.bundle', '.framework', '.xcodeproj']
  394. // UnityEngine.Debug.Log( "DIR: " + directory );
  395. if( directory.EndsWith( ".bundle" ) ) {
  396. // Treath it like a file and copy even if not recursive
  397. //UnityEngine.Debug.LogWarning( "This is a special folder: " + directory );
  398. AddFile( directory, newGroup, "SOURCE_ROOT", createBuildFile );
  399. // UnityEngine.Debug.Log( "fatto" );
  400. continue;
  401. }
  402. if( recursive ) {
  403. // UnityEngine.Debug.Log( "recursive" );
  404. AddFolder( directory, newGroup, exclude, recursive, createBuildFile );
  405. }
  406. }
  407. // Adding files.
  408. foreach( string file in Directory.GetFiles( folderPath ) ) {
  409. if( Regex.IsMatch( file, regexExclude ) ) {
  410. continue;
  411. }
  412. // UnityEngine.Debug.Log( "--> " + file + ", " + newGroup );
  413. AddFile( file, newGroup, "SOURCE_ROOT", createBuildFile );
  414. }
  415. modified = true;
  416. return modified;
  417. }
  418. #endregion
  419. #region Getters
  420. public PBXFileReference GetFile( string name )
  421. {
  422. if( string.IsNullOrEmpty( name ) ) {
  423. return null;
  424. }
  425. foreach( KeyValuePair<string, PBXFileReference> current in fileReferences ) {
  426. if( !string.IsNullOrEmpty( current.Value.name ) && current.Value.name.CompareTo( name ) == 0 ) {
  427. return current.Value;
  428. }
  429. }
  430. return null;
  431. }
  432. public PBXGroup GetGroup( string name, string path = null, PBXGroup parent = null )
  433. {
  434. // UnityEngine.Debug.Log( "GetGroup: " + name + ", " + path + ", " + parent );
  435. if( string.IsNullOrEmpty( name ) )
  436. return null;
  437. if( parent == null )
  438. parent = rootGroup;
  439. foreach( KeyValuePair<string, PBXGroup> current in groups ) {
  440. // UnityEngine.Debug.Log( "current: " + current.Value.guid + ", " + current.Value.name + ", " + current.Value.path + " - " + parent.HasChild( current.Key ) );
  441. if( string.IsNullOrEmpty( current.Value.name ) ) {
  442. if( current.Value.path.CompareTo( name ) == 0 && parent.HasChild( current.Key ) ) {
  443. return current.Value;
  444. }
  445. }
  446. else if( current.Value.name.CompareTo( name ) == 0 && parent.HasChild( current.Key ) ) {
  447. return current.Value;
  448. }
  449. }
  450. PBXGroup result = new PBXGroup( name, path );
  451. groups.Add( result );
  452. parent.AddChild( result );
  453. modified = true;
  454. return result;
  455. }
  456. #endregion
  457. #region Mods
  458. public void ApplyMod( string pbxmod )
  459. {
  460. XCMod mod = new XCMod( pbxmod );
  461. ApplyMod( mod );
  462. }
  463. public void ApplyMod( XCMod mod )
  464. {
  465. PBXGroup modGroup = this.GetGroup( mod.group );
  466. // UnityEngine.Debug.Log( "Adding libraries..." );
  467. // PBXGroup librariesGroup = this.GetGroup( "Libraries" );
  468. foreach( XCModFile libRef in mod.libs ) {
  469. string completeLibPath = System.IO.Path.Combine( "usr/lib", libRef.filePath );
  470. this.AddFile( completeLibPath, modGroup, "SDKROOT", true, libRef.isWeak );
  471. }
  472. // UnityEngine.Debug.Log( "Adding frameworks..." );
  473. PBXGroup frameworkGroup = this.GetGroup( "Frameworks" );
  474. foreach( string framework in mod.frameworks ) {
  475. string[] filename = framework.Split( ':' );
  476. bool isWeak = ( filename.Length > 1 ) ? true : false;
  477. string completePath = System.IO.Path.Combine( "System/Library/Frameworks", filename[0] );
  478. this.AddFile( completePath, frameworkGroup, "SDKROOT", true, isWeak );
  479. }
  480. // UnityEngine.Debug.Log( "Adding files..." );
  481. // foreach( string filePath in mod.files ) {
  482. // string absoluteFilePath = System.IO.Path.Combine( mod.path, filePath );
  483. //
  484. //
  485. // if( filePath.EndsWith(".framework") )
  486. // this.AddFile( absoluteFilePath, frameworkGroup, "GROUP", true, false);
  487. // else
  488. // this.AddFile( absoluteFilePath, modGroup );
  489. // }
  490. // UnityEngine.Debug.Log( "Adding folders..." );
  491. foreach( string folderPath in mod.folders ) {
  492. string absoluteFolderPath = System.IO.Path.Combine( mod.path, folderPath );
  493. this.AddFolder( absoluteFolderPath, modGroup, (string[])mod.excludes.ToArray( typeof(string) ) );
  494. }
  495. // UnityEngine.Debug.Log( "Adding headerpaths..." );
  496. foreach( string headerpath in mod.headerpaths ) {
  497. string absoluteHeaderPath = System.IO.Path.Combine( mod.path, headerpath );
  498. this.AddHeaderSearchPaths( absoluteHeaderPath );
  499. }
  500. // UnityEngine.Debug.Log( "Adding librarypaths..." );
  501. foreach( string headerpath in mod.librarypaths ) {
  502. string absoluteLibraryPath = System.IO.Path.Combine( mod.path, headerpath );
  503. this.AddLibrarySearchPaths( absoluteLibraryPath );
  504. }
  505. // UnityEngine.Debug.Log ("Adding Zip...");
  506. foreach (string zipPath in mod.zipPaths) {
  507. string zipFileName = zipPath;
  508. string sdkName = zipFileName.Remove(zipFileName.LastIndexOf("."));
  509. string unzipLoc = mod.path + "/" + zipFileName;
  510. string dirpath = this.projectRootPath;
  511. ZipHelper zipTool = new ZipHelper();
  512. zipTool.UnzipWithPath (unzipLoc, dirpath);
  513. //删除多余解压文件
  514. DirectoryInfo di = new DirectoryInfo(dirpath + "/__MACOSX");
  515. di.Delete(true);
  516. if (sdkName == "ShareSDK")
  517. {
  518. //根据Editor所勾选执行删除工作
  519. DeleteUnnecessaryFile();
  520. }
  521. string absoluteFolderPath = System.IO.Path.Combine( this.projectRootPath, sdkName + "/" );
  522. // this.AddFolder( absoluteFolderPath, modGroup, (string[])mod.excludes.ToArray( typeof(string) ) );
  523. //第二个参数传null,能够在xcode项目再减少一层文件夹
  524. this.AddFolder( absoluteFolderPath, null, (string[])mod.excludes.ToArray( typeof(string) ) );
  525. }
  526. // UnityEngine.Debug.Log( "Adding files..." );
  527. foreach( string filePath in mod.files ) {
  528. //原版为根据.projmods中的files来添加
  529. // string absoluteFilePath = System.IO.Path.Combine( mod.path, filePath );
  530. //对于ShareSDK,由于解压的文件夹已经移动至Xcode根目录,所以要根据根目录路径进行库路径添加
  531. string absoluteFilePath = System.IO.Path.Combine(this.projectRootPath, filePath );
  532. if( filePath.EndsWith(".framework") )
  533. this.AddFile( absoluteFilePath, frameworkGroup, "GROUP", true, false);
  534. else
  535. this.AddFile( absoluteFilePath, modGroup );
  536. }
  537. // UnityEngine.Debug.Log( "Configure build settings..." );
  538. Hashtable buildSettings = mod.buildSettings;
  539. if( buildSettings.ContainsKey("OTHER_LDFLAGS") )
  540. {
  541. // UnityEngine.Debug.Log( " Adding other linker flags..." );
  542. ArrayList otherLinkerFlags = (ArrayList) buildSettings["OTHER_LDFLAGS"];
  543. foreach( string linker in otherLinkerFlags )
  544. {
  545. string _linker = linker;
  546. if( !_linker.StartsWith("-") )
  547. _linker = "-" + _linker;
  548. this.AddOtherLDFlags( _linker );
  549. }
  550. }
  551. if( buildSettings.ContainsKey("GCC_ENABLE_CPP_EXCEPTIONS") )
  552. {
  553. // UnityEngine.Debug.Log( " GCC_ENABLE_CPP_EXCEPTIONS = " + buildSettings["GCC_ENABLE_CPP_EXCEPTIONS"] );
  554. this.GccEnableCppExceptions( (string) buildSettings["GCC_ENABLE_CPP_EXCEPTIONS"] );
  555. }
  556. if( buildSettings.ContainsKey("GCC_ENABLE_OBJC_EXCEPTIONS") )
  557. {
  558. // UnityEngine.Debug.Log( " GCC_ENABLE_OBJC_EXCEPTIONS = " + buildSettings["GCC_ENABLE_OBJC_EXCEPTIONS"] );
  559. this.GccEnableObjCExceptions( (string) buildSettings["GCC_ENABLE_OBJC_EXCEPTIONS"] );
  560. }
  561. this.Consolidate();
  562. }
  563. #endregion
  564. #region Savings
  565. public void Consolidate()
  566. {
  567. PBXDictionary consolidated = new PBXDictionary();
  568. consolidated.Append<PBXBuildFile>( this.buildFiles );
  569. consolidated.Append<PBXGroup>( this.groups );
  570. consolidated.Append<PBXFileReference>( this.fileReferences );
  571. // consolidated.Append<PBXProject>( this.project );
  572. consolidated.Append<PBXNativeTarget>( this.nativeTargets );
  573. consolidated.Append<PBXFrameworksBuildPhase>( this.frameworkBuildPhases );
  574. consolidated.Append<PBXResourcesBuildPhase>( this.resourcesBuildPhases );
  575. consolidated.Append<PBXShellScriptBuildPhase>( this.shellScriptBuildPhases );
  576. consolidated.Append<PBXSourcesBuildPhase>( this.sourcesBuildPhases );
  577. consolidated.Append<PBXCopyFilesBuildPhase>( this.copyBuildPhases );
  578. consolidated.Append<XCBuildConfiguration>( this.buildConfigurations );
  579. consolidated.Append<XCConfigurationList>( this.configurationLists );
  580. consolidated.Add( project.guid, project.data );
  581. _objects = consolidated;
  582. consolidated = null;
  583. }
  584. public void Backup()
  585. {
  586. string backupPath = Path.Combine( this.filePath, "project.backup.pbxproj" );
  587. // Delete previous backup file
  588. if( File.Exists( backupPath ) )
  589. File.Delete( backupPath );
  590. // Backup original pbxproj file first
  591. File.Copy( System.IO.Path.Combine( this.filePath, "project.pbxproj" ), backupPath );
  592. }
  593. /// <summary>
  594. /// Saves a project after editing.
  595. /// </summary>
  596. public void Save()
  597. {
  598. PBXDictionary result = new PBXDictionary();
  599. result.Add( "archiveVersion", 1 );
  600. result.Add( "classes", new PBXDictionary() );
  601. result.Add( "objectVersion", 45 );
  602. Consolidate();
  603. result.Add( "objects", _objects );
  604. result.Add( "rootObject", _rootObjectKey );
  605. Backup();
  606. // Parse result object directly into file
  607. PBXParser parser = new PBXParser();
  608. StreamWriter saveFile = File.CreateText( System.IO.Path.Combine( this.filePath, "project.pbxproj" ) );
  609. saveFile.Write( parser.Encode( result, false ) );
  610. saveFile.Close();
  611. }
  612. /**
  613. * Raw project data.
  614. */
  615. public Dictionary<string, object> objects
  616. {
  617. get
  618. {
  619. return null;
  620. }
  621. }
  622. #endregion
  623. public void Dispose()
  624. {
  625. }
  626. private void DeleteUnnecessaryFile()
  627. {
  628. ChosenPlatforms chosenPlats;
  629. try
  630. {
  631. string binFilePath = Application.dataPath + "/ShareSDKiOSAutoPackage/Editor/SDKPorter/ManagePlatforms/ChosenPlatforms.bin";
  632. BinaryFormatter formatter = new BinaryFormatter();
  633. Stream destream = new FileStream(binFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  634. chosenPlats = (ChosenPlatforms)formatter.Deserialize(destream);
  635. destream.Flush();
  636. destream.Close();
  637. }
  638. catch (Exception)
  639. {
  640. chosenPlats = new ChosenPlatforms ();
  641. }
  642. Type t = chosenPlats.GetType ();
  643. foreach (PropertyInfo platform in t.GetProperties ())
  644. {
  645. Hashtable plat = (Hashtable)platform.GetValue (chosenPlats, null);
  646. if (!(Boolean)plat["chosen"])
  647. {
  648. //删除多余平台相关文件
  649. if (plat ["sdkPath"] != null)
  650. {
  651. DirectoryInfo di = new DirectoryInfo(this.projectRootPath + (string)plat ["sdkPath"]);
  652. if (di.Exists)
  653. {
  654. di.Delete(true);
  655. }
  656. }
  657. if (plat ["connectorPath"] != null)
  658. {
  659. DirectoryInfo di = new DirectoryInfo(this.projectRootPath + (string)plat ["connectorPath"]);
  660. if (di.Exists)
  661. {
  662. di.Delete(true);
  663. }
  664. }
  665. if (plat ["jsPath"] != null)
  666. {
  667. if (File.Exists (this.projectRootPath + (string)plat ["jsPath"]))
  668. {
  669. File.Delete (this.projectRootPath + (string)plat ["jsPath"]);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }
  676. }