|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEditor.Build; |
| 8 | +using UnityEditor.Callbacks; |
| 9 | + |
| 10 | +namespace Improbable.Gdk.Mobile |
| 11 | +{ |
| 12 | + public static class BuildPostProcessXCode |
| 13 | + { |
| 14 | + private static Type pbxType; |
| 15 | + |
| 16 | + [PostProcessBuild] |
| 17 | + public static void OnPostProcessBuild(BuildTarget buildTarget, string path) |
| 18 | + { |
| 19 | + if (buildTarget != BuildTarget.iOS) |
| 20 | + { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 25 | + foreach (var assembly in assemblies) |
| 26 | + { |
| 27 | + var type = assembly.GetTypes().FirstOrDefault(t => t.FullName == "UnityEditor.iOS.Xcode.PBXProject"); |
| 28 | + if (type != null) |
| 29 | + { |
| 30 | + pbxType = type; |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Safety check again API changes |
| 36 | + if (pbxType == null) |
| 37 | + { |
| 38 | + throw new BuildFailedException("Unable to find type UnityEditor.iOS.Xcode.PBXProject"); |
| 39 | + } |
| 40 | + |
| 41 | + var xcodeObject = Activator.CreateInstance(pbxType); |
| 42 | + |
| 43 | + // Instantiate PBXProject and read from xcode project file. |
| 44 | + var projPath = InvokeStaticMethod<string>("GetPBXProjectPath", path); |
| 45 | + var xcodeText = File.ReadAllText(projPath); |
| 46 | + InvokeMethod(xcodeObject, "ReadFromString", xcodeText); |
| 47 | + |
| 48 | + // Get Target GUIDs |
| 49 | + var unityTargetName = InvokeStaticMethod<string>("GetUnityTargetName"); |
| 50 | + var unityTestTargetName = InvokeStaticMethod<string>("GetUnityTestTargetName"); |
| 51 | + var targetGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTargetName); |
| 52 | + var targetTestingGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTestTargetName); |
| 53 | + |
| 54 | + // Enumerate configGUIDs that need library path patching |
| 55 | + var configNames = InvokeMethod<IEnumerable<string>>(xcodeObject, "BuildConfigNames"); |
| 56 | + var configGUIDs = configNames |
| 57 | + .Select(configName => InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetGUID, configName)) |
| 58 | + .Concat(configNames.Select(configName => |
| 59 | + InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetTestingGUID, configName))); |
| 60 | + |
| 61 | + // Update library paths for each config |
| 62 | + foreach (var configGUID in configGUIDs) |
| 63 | + { |
| 64 | + InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID, "LIBRARY_SEARCH_PATHS", |
| 65 | + null, new[] |
| 66 | + { |
| 67 | + "$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm", |
| 68 | + "$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64" |
| 69 | + }); |
| 70 | + |
| 71 | + // Add platform specific paths |
| 72 | + InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID, |
| 73 | + "LIBRARY_SEARCH_PATHS[sdk=iphoneos*]", new[] |
| 74 | + { |
| 75 | + "$(inherited)", |
| 76 | + "$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm" |
| 77 | + }, null); |
| 78 | + |
| 79 | + InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID, |
| 80 | + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]", |
| 81 | + new[] |
| 82 | + { |
| 83 | + "$(inherited)", |
| 84 | + "$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64" |
| 85 | + }, null); |
| 86 | + } |
| 87 | + |
| 88 | + // Save changes to xcode project file |
| 89 | + InvokeMethod(xcodeObject, "WriteToFile", projPath); |
| 90 | + } |
| 91 | + |
| 92 | + private static T InvokeStaticMethod<T>(string methodName, params object[] parameters) |
| 93 | + { |
| 94 | + return (T) pbxType.InvokeMember(methodName, |
| 95 | + BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, parameters); |
| 96 | + } |
| 97 | + |
| 98 | + private static void InvokeStaticMethod(string methodName, params object[] parameters) |
| 99 | + { |
| 100 | + pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, |
| 101 | + null, null, parameters); |
| 102 | + } |
| 103 | + |
| 104 | + private static T InvokeMethod<T>(object target, string methodName, params object[] parameters) |
| 105 | + { |
| 106 | + return (T) pbxType.InvokeMember(methodName, |
| 107 | + BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, target, parameters); |
| 108 | + } |
| 109 | + |
| 110 | + private static void InvokeMethod(object target, string methodName, params object[] parameters) |
| 111 | + { |
| 112 | + pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, |
| 113 | + null, target, parameters); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments