1+ using System . IO ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using System . Text ;
5+ using UnityEngine ;
6+ using UnityEditor ;
7+ using UnityEditor . Callbacks ;
8+ using UnityEditor . iOS . Xcode ;
9+
10+ public static class PostBuildTrigger
11+ {
12+ enum Position { Begin , End } ;
13+
14+ private static string rn = "\n " ;
15+
16+ private static string PATH_PROJECT_FILE = "Unity-iPhone.xcodeproj/project.pbxproj" ;
17+ private static string PATH_AUTH = "/Classes/UnityAppController.mm" ;
18+ private static string SIGNATURE_AUTH =
19+ "- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation" ;
20+ private static string CODE_AUTH = rn +
21+ "if([HockeyAppUnity handleOpenURL:url sourceApplication:sourceApplication annotation:annotation]){" + rn +
22+ "return YES;" + rn +
23+ "}" + rn ;
24+
25+ private static string PATH_UI_LOADED = "/Classes/UI/UnityAppController+ViewHandling.mm" ;
26+ private static string SIGNATURE_UI_LOADED =
27+ "- (void)showGameUI" ;
28+ private static string CODE_UI_LOADED = rn +
29+ "[HockeyAppUnity sendViewLoadedMessageToUnity];" + rn ;
30+
31+ private static string CODE_LIB_IMPORT =
32+ "#import \" HockeyAppUnity.h\" " + rn ;
33+
34+ [ PostProcessBuild ( 100 ) ]
35+ public static void OnPostProcessBuild ( BuildTarget target , string path )
36+ {
37+
38+ if ( target == BuildTarget . iOS )
39+ {
40+ // Get target for Xcode project
41+ string projPath = PBXProject . GetPBXProjectPath ( path ) ;
42+ PBXProject proj = new PBXProject ( ) ;
43+ proj . ReadFromString ( File . ReadAllText ( projPath ) ) ;
44+
45+ string targetName = PBXProject . GetUnityTargetName ( ) ;
46+ string projectTarget = proj . TargetGuidByName ( targetName ) ;
47+
48+ // Add dependencies
49+ proj . AddFrameworkToProject ( projectTarget , "AssetsLibrary.framework" , false ) ;
50+ proj . AddFrameworkToProject ( projectTarget , "CoreText.framework" , false ) ;
51+ proj . AddFrameworkToProject ( projectTarget , "MobileCoreServices.framework" , false ) ;
52+ proj . AddFrameworkToProject ( projectTarget , "QuickLook.framework" , false ) ;
53+ proj . AddFrameworkToProject ( projectTarget , "Security.framework" , false ) ;
54+
55+ File . WriteAllText ( projPath , proj . WriteToString ( ) ) ;
56+
57+ InsertAuthCodeIntoControllerClass ( path ) ;
58+ InsertUILoadedCallbackIntoControllerClass ( path ) ;
59+ }
60+ }
61+
62+ private static void InsertAuthCodeIntoControllerClass ( string projectPath ) {
63+ string filepath = projectPath + PATH_AUTH ;
64+ string [ ] methodSignatures = { SIGNATURE_AUTH } ;
65+ string [ ] valuesToAppend = { CODE_AUTH } ;
66+ Position [ ] positionsInMethod = new Position [ ] { Position . Begin } ;
67+
68+ InsertCodeIntoClass ( filepath , methodSignatures , valuesToAppend , positionsInMethod ) ;
69+ }
70+
71+ private static void InsertUILoadedCallbackIntoControllerClass ( string projectPath ) {
72+ string filepath = projectPath + PATH_UI_LOADED ;
73+ string [ ] methodSignatures = { SIGNATURE_UI_LOADED } ;
74+ string [ ] valuesToAppend = { CODE_UI_LOADED } ;
75+ Position [ ] positionsInMethod = new Position [ ] { Position . End } ;
76+
77+ InsertCodeIntoClass ( filepath , methodSignatures , valuesToAppend , positionsInMethod ) ;
78+ }
79+
80+ private static void InsertCodeIntoClass ( string filepath , string [ ] methodSignatures , string [ ] valuesToAppend , Position [ ] positionsInMethod ) {
81+ if ( ! File . Exists ( filepath ) ) {
82+ return ;
83+ }
84+
85+ string fileContent = File . ReadAllText ( filepath ) ;
86+ List < int > ignoredIndices = new List < int > ( ) ;
87+
88+ for ( int i = 0 ; i < valuesToAppend . Length ; i ++ ) {
89+ string val = valuesToAppend [ i ] ;
90+
91+ if ( fileContent . Contains ( val ) ) {
92+ ignoredIndices . Add ( i ) ;
93+ }
94+ }
95+
96+ string [ ] fileLines = File . ReadAllLines ( filepath ) ;
97+ List < string > newContents = new List < string > ( ) ;
98+ bool found = false ;
99+ int foundIndex = - 1 ;
100+
101+ newContents . Add ( CODE_LIB_IMPORT ) ;
102+ foreach ( string line in fileLines ) {
103+ if ( line . Trim ( ) . Contains ( CODE_LIB_IMPORT . Trim ( ) ) ) {
104+ continue ;
105+ }
106+
107+ newContents . Add ( line + rn ) ;
108+ for ( int j = 0 ; j < methodSignatures . Length ; j ++ ) {
109+ if ( ( line . Trim ( ) . Equals ( methodSignatures [ j ] ) ) && ! ignoredIndices . Contains ( j ) ) {
110+ foundIndex = j ;
111+ found = true ;
112+ }
113+ }
114+
115+ if ( found ) {
116+ if ( ( positionsInMethod [ foundIndex ] == Position . Begin ) && line . Trim ( ) . Equals ( "{" ) ) {
117+ newContents . Add ( valuesToAppend [ foundIndex ] + rn ) ;
118+ found = false ;
119+ } else if ( ( positionsInMethod [ foundIndex ] == Position . End ) && line . Trim ( ) . Equals ( "}" ) ) {
120+ newContents = newContents . GetRange ( 0 , newContents . Count - 3 ) ;
121+ newContents . Add ( valuesToAppend [ foundIndex ] + rn + "}" + rn ) ;
122+ found = false ;
123+ }
124+ }
125+ }
126+ string output = string . Join ( "" , newContents . ToArray ( ) ) ;
127+ File . WriteAllText ( filepath , output ) ;
128+ }
129+ }
0 commit comments