summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Packages/com.vrchat.vrcsdk3/Editor/UPMImporter/UPMImporter.cs
blob: 61aa68fa0dc0e6532c4a1d44bbf5dcd4f6eaf2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

namespace VRC.Udon.Editor {
    [InitializeOnLoad]
    public class UPMImporter
    {
        // Add packages here to auto-import
        public static string[] requiredPackages =
        {
            #if UNITY_2019_3_OR_NEWER
            "com.unity.cinemachine@2.8.0",
            "com.unity.postprocessing@3.1.1",
            "com.unity.textmeshpro@2.1.6",
            "com.unity.modules.androidjni@1.0.0",
            "com.unity.timeline@1.2.18",
            "com.unity.ugui@1.0.0",
            "com.unity.test-framework@1.1.27",
            "com.unity.package-manager-ui@2.2.0",
            #else
            "com.unity.cinemachine@2.6.1",
            "com.unity.postprocessing@3.0.3",
            "com.unity.textmeshpro@1.5.1",
            #endif
        };
        
        private static ListRequest list;
        
        static UPMImporter()
        {
            list = Client.List();
            EditorApplication.update += Update;
        }

        public static void Update()
        {
            // Exit early if we're still gathering the list
            if (!list.IsCompleted) return;
            
            // Unsubscribe from Update once the list is ready
            EditorApplication.update -= Update;

            var localPackages = list.Result;
            bool importedNewPackage = false;
            foreach (string packageName in requiredPackages)
            {
                if(localPackages.All(p => $"{p.name}@{p.version}" != packageName))
                {
                    Install(packageName);
                    importedNewPackage = true;
                }
            }
            
            // if Unity tried to import SDK3 before required packages, it will have old errors showing.
            //if(importedNewPackage) ClearLog();
        }

        public static bool Install(string id)
        {
            var request = Client.Add(id);
            while (!request.IsCompleted) {};
            if(request.Error != null)Debug.LogError(request.Error.message);
            return request.Error == null;
        }
        
        public static void ClearLog()
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(SceneView));

            System.Type type = assembly.GetType("UnityEditor.LogEntries");
            System.Reflection.MethodInfo method = type.GetMethod("Clear");
            method.Invoke(new object(), null);
        }

    }

}