Sfoglia il codice sorgente

实现自动加载git依赖的功能

tanghai 1 anno fa
parent
commit
04f499a298

+ 45 - 0
ET.sln

@@ -55,6 +55,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ET.Hotfix", "Unity\Packages
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ET.Model", "Unity\Packages\cn.etetet.statesync\DotNet~\Model\ET.Model.csproj", "{39CCBBF9-E293-4668-879E-0C5A2577AB71}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "com.hibzz.dependencyresolver", "Unity\com.hibzz.dependencyresolver.csproj", "{1B7EC6EB-8895-C3F9-4D13-9A290F903425}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Coffee.GitDependencyResolver", "Unity\Coffee.GitDependencyResolver.csproj", "{2C967558-08BC-4AC5-8614-44DB93D1B627}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SandolkakosDigital.PackageManagerUtilities.Editor", "Unity\SandolkakosDigital.PackageManagerUtilities.Editor.csproj", "{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -353,6 +359,42 @@ Global
 		{39CCBBF9-E293-4668-879E-0C5A2577AB71}.Release|x64.Build.0 = Release|Any CPU
 		{39CCBBF9-E293-4668-879E-0C5A2577AB71}.Release|x86.ActiveCfg = Release|Any CPU
 		{39CCBBF9-E293-4668-879E-0C5A2577AB71}.Release|x86.Build.0 = Release|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|x64.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|x64.Build.0 = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Debug|x86.Build.0 = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|Any CPU.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|Any CPU.Build.0 = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|x64.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|x64.Build.0 = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|x86.ActiveCfg = Debug|Any CPU
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425}.Release|x86.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|x64.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|x64.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Debug|x86.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|Any CPU.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|Any CPU.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|x64.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|x64.Build.0 = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|x86.ActiveCfg = Debug|Any CPU
+		{2C967558-08BC-4AC5-8614-44DB93D1B627}.Release|x86.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|x64.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|x64.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Debug|x86.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|Any CPU.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|Any CPU.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|x64.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|x64.Build.0 = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|x86.ActiveCfg = Debug|Any CPU
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B}.Release|x86.Build.0 = Debug|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -382,6 +424,9 @@ Global
 		{31A5CA3C-6C77-436A-99AC-3D3D099144B8} = {46008B91-6EC0-448B-8D1A-D8C6D2EEBEF4}
 		{A6D58788-959F-4A1C-A211-F6865B985082} = {46008B91-6EC0-448B-8D1A-D8C6D2EEBEF4}
 		{39CCBBF9-E293-4668-879E-0C5A2577AB71} = {46008B91-6EC0-448B-8D1A-D8C6D2EEBEF4}
+		{1B7EC6EB-8895-C3F9-4D13-9A290F903425} = {914C77C9-212A-4DD0-8D9A-074620E77FAA}
+		{2C967558-08BC-4AC5-8614-44DB93D1B627} = {914C77C9-212A-4DD0-8D9A-074620E77FAA}
+		{D3CE13BC-3E46-01B0-0BD8-C5DD3A6C780B} = {914C77C9-212A-4DD0-8D9A-074620E77FAA}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {EABC01E3-3EB5-47EF-B46E-AAD8BB3585F1}

+ 3 - 0
Unity/Packages/cn.etetet.core/Editor/GitDependencyResolver.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: ce7769db96264d48961e70d63f9d52ab
+timeCreated: 1717601777

+ 189 - 0
Unity/Packages/cn.etetet.core/Editor/GitDependencyResolver/DependencyResolver.cs

@@ -0,0 +1,189 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using ET;
+using MongoDB.Bson.Serialization.Attributes;
+using UnityEditor;
+using UnityEditor.PackageManager;
+using UnityEngine;
+
+using PackageInfo = UnityEditor.PackageManager.PackageInfo;
+using UnityEditor.PackageManager.Requests;
+
+namespace Hibzz.DependencyResolver
+{
+    [BsonIgnoreExtraElements]
+    public class PackageGitDependency
+    {
+        public Dictionary<string, string> gitDependencies;
+    }
+    
+    [InitializeOnLoad]
+    public class DependencyResolver
+    {
+        static AddAndRemoveRequest packageInstallationRequest;
+
+        // called by the attribute [InitializeOnLoad]
+        static DependencyResolver()
+        {
+            Events.registeredPackages += OnPackagesRegistered;
+        }
+
+        // Invoked when the package manager completes registering new packages
+        static void OnPackagesRegistered(PackageRegistrationEventArgs packageRegistrationInfo)
+        {
+            // stores all the dependencies that needs to be installed in this step
+            Dictionary<string, string> dependencies = new ();
+
+            // loop through all of the added packages and get their git
+            // dependencies and add it to the list that contains all the
+            // dependencies that need to be installed
+            foreach (var package in packageRegistrationInfo.added)
+            {
+                // get the dependencies of the added package
+                if (!GetDependencies(package, out PackageGitDependency package_dependencies))
+                {
+                    continue;
+                }
+
+                foreach (var gitDependency in package_dependencies.gitDependencies)
+                {
+                    dependencies[gitDependency.Key] = gitDependency.Value;
+                }
+            }
+
+            // remove any dependencies that's already installed
+            var installed_packages = PackageInfo.GetAllRegisteredPackages().ToList();
+            foreach (string dependency in dependencies.Keys.ToArray())
+            {
+                if (IsInCollection(dependency, installed_packages))
+                {
+                    dependencies.Remove(dependency);
+                }
+            }
+
+            // Install the dependencies
+            InstallDependencies(dependencies);
+        }
+
+        /// <summary>
+        /// Request a list of git dependencies in the package
+        /// </summary>
+        /// <param name="packageInfo">The package to get the git dependencies from</param>
+        /// <param name="dependencies">The retrieved list of git dependencies </param>
+        /// <returns>Was the request successful?</returns>
+        static bool GetDependencies(PackageInfo packageInfo, out PackageGitDependency dependencies)
+        {
+            // Read the contents of the package.json file
+            string package_json_path = $"{packageInfo.resolvedPath}/package.json";
+            string package_json_content = File.ReadAllText(package_json_path);
+
+            PackageGitDependency packageGitDependency = MongoHelper.FromJson<PackageGitDependency>(package_json_content);
+            // if no token with the key git-dependecies is found, failed to get git dependencies
+            if (packageGitDependency.gitDependencies is null || packageGitDependency.gitDependencies.Count == 0)
+            {
+                dependencies = null;
+                return false;
+            }
+
+            // convert the git dependency token to a list of strings...
+            // maybe we should check for errors in this process? what if git-dependency isn't array of string?
+            dependencies = packageGitDependency;
+            return true;
+        }
+
+        /// <summary>
+        /// Is the given dependency url found in the given collection
+        /// </summary>
+        /// <param name="dependency">The url the dependency to check for</param>
+        /// <param name="collection">The collection to look through</param>
+        /// <returns></returns>
+        static bool IsInCollection(string dependency, List<PackageInfo> collection)
+        {
+            // when package collection given is null, it's inferred that the dependency is not in the collection
+            if (collection == null) { return false; }
+
+            // check if any of the installed package has the dependency
+            foreach (var package in collection)
+            {
+                // the package id for a package installed with git is `package_name@package_giturl`
+                // get the repo url by performing some string manipulation on the package id
+                //string repourl = package.packageId.Substring(package.packageId.IndexOf('@') + 1);
+
+                // Found!
+                if (package.name == dependency)
+                {
+                    return true;
+                }
+            }
+
+            // the dependency wasn't found in the package collection
+            return false;
+        }
+
+        /// <summary>
+        /// Install all the given dependencies
+        /// </summary>
+        /// <param name="dependencies">A list of dependencies to install</param>
+        static void InstallDependencies(Dictionary<string, string> dependencies)
+        {
+            // there are no dependencies to install, skip
+            if (dependencies == null || dependencies.Count <= 0)
+            {
+                return;
+            }
+
+            // before installing the packages, make sure that user knows what
+            // the dependencies to install are... additionally, check if the
+            // application is being run on batch mode so that we can skip the
+            // installation dialog
+            if (!Application.isBatchMode &&
+                !EditorUtility.DisplayDialog(
+                    $"Dependency Resolver",
+                    $"The following dependencies are required: \n\n{GetPrintFriendlyName(dependencies.Keys.ToList())}",
+                    "Install Dependencies",
+                    "Cancel"))
+            {
+                // user decided to cancel the installation of the dependencies...
+                return;
+            }
+
+            // the user pressed install, perform the actual installation
+            // (or the application was in batch mode)
+            packageInstallationRequest = Client.AddAndRemove(dependencies.Values.ToArray(), null);
+
+            // show the progress bar till the installation is complete
+            EditorUtility.DisplayProgressBar("Dependency Resolver", "Preparing installation of dependencies...", 0);
+            EditorApplication.update += DisplayProgress;
+        }
+
+        /// <summary>
+        /// Get a print friendly name of all dependencies to show in the dialog box
+        /// </summary>
+        /// <param name="dependencies">The list of dependencies to parse through</param>
+        /// <returns>A print friendly string representing all the dependencies</returns>
+        static string GetPrintFriendlyName(List<string> dependencies)
+        {
+            // ideally, we want the package name, but that requires downloading the package.json and parsing through
+            // it, which is kinda too much... i could ask for the users to give a package name along with the url in
+            // package.json, but again too complicated just for a dialog message... username/repo will do fine for now
+
+            string result = string.Join("\n", dependencies);    // concatenate dependencies on a new line
+
+            return result;
+        }
+
+        /// <summary>
+        /// Shows a progress bar till the AddAndRemoveRequest is completed
+        /// </summary>
+        static void DisplayProgress()
+        {
+            if(packageInstallationRequest.IsCompleted)
+            {
+                EditorUtility.ClearProgressBar();
+                EditorApplication.update -= DisplayProgress;
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Packages/cn.etetet.core/Editor/GitDependencyResolver/DependencyResolver.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: d087a58f7e93a0b449fdce47cfbd701b
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 1
Unity/Packages/cn.etetet.loader/Resources/GlobalConfig.asset

@@ -13,5 +13,5 @@ MonoBehaviour:
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
   CodeMode: 3
-  BuildType: 0
+  BuildType: 1
   SceneName: StateSync

+ 3 - 3
Unity/ProjectSettings/PackageManagerSettings.asset

@@ -13,10 +13,10 @@ MonoBehaviour:
   m_Name: 
   m_EditorClassIdentifier: 
   m_EnablePreReleasePackages: 0
-  m_EnablePackageDependencies: 1
   m_AdvancedSettingsExpanded: 1
   m_ScopedRegistriesSettingsExpanded: 1
   m_SeeAllPackageVersions: 0
+  m_DismissPreviewPackagesInUse: 0
   oneTimeWarningShown: 1
   m_Registries:
   - m_Id: main
@@ -39,6 +39,6 @@ MonoBehaviour:
   m_RegistryInfoDraft:
     m_Modified: 0
     m_ErrorMessage: 
-    m_UserModificationsInstanceId: -828
-    m_OriginalInstanceId: -830
+    m_UserModificationsInstanceId: -838
+    m_OriginalInstanceId: -842
   m_LoadAssets: 0