Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
UnityCoreHapticsPostProcessor.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6#if UNITY_IOS
7using System;
8using System.IO;
9
10using UnityEngine;
11using UnityEngine.Assertions;
12
13using UnityEditor;
14using UnityEditor.Callbacks;
15using UnityEditor.iOS.Xcode;
16
17using SimpleJSON;
18
19public static class UnityCoreHapticsPostProcessor
20{
21 // Set to path that this script is in
22 const string MODULE_MAP_FILENAME = "module.modulemap";
23
24 //modify variable according to package: Unity Asset Store / GitHub
25 private static bool assetStoreBuild = false;
26 private static string pluginRelativePathInXCode;
27
28
29 [PostProcessBuild]
30 public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath)
31 {
32 if (buildTarget == BuildTarget.iOS)
33 {
34 var pbxProjectPath = PBXProject.GetPBXProjectPath(buildPath);
35 var proj = new PBXProject();
36 proj.ReadFromFile(pbxProjectPath);
37
38 // Find the script file
39 var guids = AssetDatabase.FindAssets("t:Script UnityCoreHaptics");
40 if (guids.Length == 0)
41 {
42 Debug.LogError("UnityCoreHaptics script not found");
43 return;
44 }
45 string scriptPath = AssetDatabase.GUIDToAssetPath(guids[0]);
46 Debug.Log("UnityCoreHaptics Path:" + scriptPath);
47
48 // Retrieve the paths of the Assets and PackageCache folders
49 string assetsFolder = "Assets/";
50
51 // Check if the script is in the Assets or PackageCache folder
52 if (scriptPath.StartsWith(assetsFolder))
53 {
54 assetStoreBuild = true;
55 }
56 else
57 {
58 assetStoreBuild = false;
59 }
60
61 string targetGUID = proj.GetUnityFrameworkTargetGuid();
62
63 // Get relative path of the plugin the from Assets folder
64 // Should be something like "UnityCoreHaptics/Plugins/iOS/UnityCoreHaptics/Source"
65 var pluginRelativePathInUnity = GetPluginPathRelativeToAssets();
66
67 // Get relative path of the plugin in XCode
68 string pluginRelativePathInXCode = "";
69 if (assetStoreBuild)
70 {
71 pluginRelativePathInXCode = Path.Combine("Libraries", pluginRelativePathInUnity);
72 }
73 else
74 {
75 pluginRelativePathInXCode = Path.Combine("Libraries", "com.interhaptics.core_sdk", pluginRelativePathInUnity);
76 }
77
78 proj.AddFrameworkToProject(targetGUID, "CoreHaptics.framework", false);
79 proj.SetBuildProperty(targetGUID, "ENABLE_BITCODE", "NO");
80
81 proj.AddBuildProperty(targetGUID, "CLANG_ENABLE_MODULES", "YES");
82 proj.AddBuildProperty(targetGUID, "SWIFT_INCLUDE_PATHS", pluginRelativePathInXCode);
83 proj.AddBuildProperty(targetGUID, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
84
85 WriteModuleToFramework(proj, targetGUID, pbxProjectPath, pluginRelativePathInUnity, pluginRelativePathInXCode);
86 }
87 }
88
89 // Made to check if two paths are the same
90 // Based on this: https://stackoverflow.com/questions/2281531/how-can-i-compare-directory-paths-in-c
91 private static string _normalizePath(string path)
92 {
93 return Path.GetFullPath(new Uri(path).LocalPath)
94 .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
95 .ToUpperInvariant();
96 }
97
98 private static string _getRelativePath(string basePath, string fullPath) {
99 // Base case 1: not found
100 if (fullPath == null || fullPath == "") {
101 return null;
102 }
103 // Base case 2: found
104 if (_normalizePath(fullPath) == _normalizePath(basePath)) {
105 return "";
106 }
107 // Recursive case
108 var dirPath = Path.GetDirectoryName(fullPath);
109 return Path.Combine(_getRelativePath(basePath, dirPath), Path.GetFileName(fullPath));
110 }
111
112 private static string GetPluginPathRelativeToAssets() {
113 string[] files;
114 if (assetStoreBuild)
115 {
116 files = System.IO.Directory.GetFiles(UnityEngine.Application.dataPath, "UnityCoreHapticsProxy.cs", SearchOption.AllDirectories);
117 }
118 else
119 {
120 files = System.IO.Directory.GetFiles(Path.GetFullPath("Packages/com.interhaptics.core_sdk"), "UnityCoreHapticsProxy.cs", SearchOption.AllDirectories);
121 }
122 if (files.Length != 1) {
123 throw new Exception("[UnityCoreHapticsPostProcessor] Error: there should exactly be one file named UnityCoreHapticsProxy.cs");
124 }
125 if (assetStoreBuild)
126 {
127 return Path.GetDirectoryName(_getRelativePath(UnityEngine.Application.dataPath, files[0]));
128 }
129 else
130 {
131 return Path.GetDirectoryName(_getRelativePath(Path.GetFullPath("Packages/com.interhaptics.core_sdk"), files[0]));
132 }
133 }
134
135 private static void WriteModuleToFramework(
136 PBXProject proj,
137 string targetGUID,
138 string pbxProjectPath,
139 string pluginRelativePathInUnity,
140 string pluginRelativePathInXCode
141 )
142 {
143 // Add a module map reference to the XCode project
144 string moduleMapDestRelativePath = Path.Combine(pluginRelativePathInXCode, MODULE_MAP_FILENAME);
145 Debug.Log("[UnityCoreHapticsPostProcessor] Adding properties to XCode framework. Module path : " + moduleMapDestRelativePath);
146 string file_guid = proj.AddFile(moduleMapDestRelativePath, moduleMapDestRelativePath, PBXSourceTree.Source);
147 proj.AddFileToBuild(targetGUID, file_guid);
148 proj.WriteToFile(pbxProjectPath);
149
150 // Copy the module file from Unity to XCode
151 string sourcePath;
152 if (assetStoreBuild)
153 {
154 sourcePath = Path.Combine(UnityEngine.Application.dataPath, pluginRelativePathInUnity, MODULE_MAP_FILENAME);
155 }
156 else
157 {
158 sourcePath = Path.Combine(Path.GetFullPath("Packages/com.interhaptics.core_sdk"), pluginRelativePathInUnity, MODULE_MAP_FILENAME);
159 }
160 string destPath = Path.Combine(Path.GetDirectoryName(pbxProjectPath), "..", moduleMapDestRelativePath);
161 if (!Directory.Exists(Path.GetDirectoryName(destPath)))
162 {
163 Debug.Log("[UnityCoreHapticsPostProcessor] Creating directory " + destPath);
164 Directory.CreateDirectory(Path.GetDirectoryName(destPath));
165 }
166 Debug.Log("[UnityCoreHapticsPostProcessor] Copy module file to project : " + sourcePath + " -> " + destPath);
167 File.Copy(sourcePath, destPath);
168 }
169}
170#endif