├── Editor.meta ├── Editor ├── BuildHelper.cs ├── BuildHelper.cs.meta ├── RealityStop.LinkMerge.Editor.asmdef └── RealityStop.LinkMerge.Editor.asmdef.meta ├── LICENSE.md ├── LICENSE.md.meta ├── README.md ├── README.md.meta ├── package.json └── package.json.meta /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9f4e1c2642830f34d8b704386ba6e3af 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/BuildHelper.cs: -------------------------------------------------------------------------------- 1 | namespace RealityStop.LinkMerge 2 | { 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Xml.Linq; 8 | using UnityEditor.Build; 9 | using UnityEditor.Build.Reporting; 10 | using UnityEditor.PackageManager; 11 | using UnityEditor.PackageManager.Requests; 12 | using UnityEngine; 13 | 14 | public class PackagesLinkXmlExtractor : IPreprocessBuildWithReport, IPostprocessBuildWithReport 15 | { 16 | public string TemporaryFolder => $"{Application.dataPath}/Temporary-Build/"; 17 | 18 | public static string TemporaryFolderMeta => $"{Application.dataPath}/Temporary-Build.meta"; 19 | 20 | public string LinkFilePath => $"{TemporaryFolder}link.xml"; 21 | 22 | // We execute the script after the others ones 23 | public int callbackOrder => 10; 24 | 25 | public async void OnPreprocessBuild(BuildReport report) 26 | { 27 | // NOTE: We merge a potentially existing previous link.xml file instead of skipping preprocess step 28 | // if (!File.Exists(LinkFilePath)) 29 | await CreateMergedLinkFromPackages(); 30 | } 31 | 32 | public void OnPostprocessBuild(BuildReport report) 33 | { 34 | if(File.Exists(LinkFilePath)) 35 | File.Delete(LinkFilePath); 36 | if (Directory.Exists(TemporaryFolder)) 37 | Directory.Delete(TemporaryFolder, true); 38 | 39 | if (File.Exists(TemporaryFolderMeta)) 40 | File.Delete(TemporaryFolderMeta); 41 | } 42 | 43 | private async Task CreateMergedLinkFromPackages() 44 | { 45 | ListRequest request = Client.List(); 46 | 47 | do { await Task.Yield(); } while (!request.IsCompleted); 48 | 49 | if (request.Status == StatusCode.Success) 50 | { 51 | List xmlPathList = new List(); 52 | foreach (PackageInfo package in request.Result) 53 | { 54 | string path = package.resolvedPath; 55 | xmlPathList.AddRange(Directory.EnumerateFiles(path, "linkmerge.xml", SearchOption.AllDirectories).ToList()); 56 | } 57 | 58 | if (xmlPathList.Count <= 0) 59 | return; 60 | 61 | XDocument[] xmlList = xmlPathList.Select(XDocument.Load).ToArray(); 62 | 63 | XDocument combinedXml = xmlList.First(); 64 | foreach (XDocument xDocument in xmlList.Where(xml => xml != combinedXml)) 65 | { 66 | combinedXml.Root.Add(xDocument.Root.Elements()); 67 | } 68 | 69 | if (!Directory.Exists(TemporaryFolder)) 70 | { 71 | Directory.CreateDirectory(TemporaryFolder); 72 | } 73 | else if(File.Exists(LinkFilePath)) 74 | { 75 | XDocument previousLinksXML = XDocument.Load(LinkFilePath); 76 | combinedXml.Root.Add(previousLinksXML.Root.Elements()); 77 | } 78 | 79 | combinedXml.Save(LinkFilePath); 80 | } 81 | else if (request.Status >= StatusCode.Failure) 82 | { 83 | Debug.LogError(request.Error.message); 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Editor/BuildHelper.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c12770bea2254cc41b88f94667a1ae57 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/RealityStop.LinkMerge.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "RealityStop.LinkMerge.Editor", 3 | "references": [], 4 | "includePlatforms": [ 5 | "Editor" 6 | ], 7 | "excludePlatforms": [], 8 | "allowUnsafeCode": false, 9 | "overrideReferences": false, 10 | "precompiledReferences": [], 11 | "autoReferenced": true, 12 | "defineConstraints": [], 13 | "versionDefines": [], 14 | "noEngineReferences": false 15 | } -------------------------------------------------------------------------------- /Editor/RealityStop.LinkMerge.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 231170938997d7746a4d3e62988f84e8 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 RealityStop 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1f0c0eb9655ab8743a0774427d47ec4e 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Custom Package Link Merger 2 | ========================== 3 | 4 | ## Overview 5 | Unity employs aggressive stripping when compiling for mobile devices and the web 6 | [strip your code](https://docs.unity3d.com/Manual/ManagedCodeStripping.html), which can cause assemblies that are only referenced via reflection to be stripped from the build output. 7 | 8 | Additionally, `link.xml` files are ignored when placed in UPM package assemblies, preventing the easiest solution. 9 | 10 | This repo contains a build script that can be added to your packages that will inject a custom `linkmerge.xml` file from your package into the project temporarily during the build process. In this way, package-based assemblies can signal to Unity that they should not be stripped. 11 | 12 | The script is multi-instance safe, with all `linkmerge.xml` files being merged together into a single link.xml, regardless of how many packages reference the build script. 13 | 14 | 15 | ## Usage 16 | 17 | Add a `linkmerge.xml` file at your package root. 18 | Fill the document with your assemblies 19 | 20 | ```xml 21 | 22 | 23 | 24 | 25 | ``` 26 | 27 | And add the BuildHelper.cs file to your Editor asmdef. **You're good to go!** 28 | 29 | 30 | 31 | Alternatively, if your package is installed via the [openupm](https://openupm.com) registry, you 32 | can add this package as dependency with the following: 33 | 34 | ```json 35 | // package.json 36 | // ... 37 | "dependencies": { 38 | "com.realitystop.linkmerge": "1.0.0" 39 | }, 40 | // ... 41 | ``` 42 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 89c0622cd9ce43ca93701ff9b34d5203 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.realitystop.linkmerge", 3 | "version": "1.1.0", 4 | "displayName": "Custom Package Link Merger", 5 | "description": "A Utility to pull in flagged link.xml files from custom packages.", 6 | "unity": "2018.4", 7 | "keywords": [ 8 | "Packages", 9 | "Link", 10 | "Merge", 11 | "LinkMerge" 12 | ], 13 | "author": { 14 | "name": "Reality.Stop()", 15 | "url": "https://github.com/RealityStop" 16 | } 17 | } -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 411775e880ef3704d833ee1d2fc9d4d5 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | --------------------------------------------------------------------------------