Пример #1
0
        private void Initialize(string buildDir, string groupKey, string className, string instanceData, string version,
                                List <AssetReference> assets, PrefabCreateDescription createDescription)
        {
            m_groupKey             = groupKey;
            m_builderClass         = className;
            m_instanceData         = instanceData;
            m_prefabBuilderVersion = version;
            m_buildDir             = buildDir;

            m_usedAssets = new List <UsedAsset> ();
            assets.ForEach(a => m_usedAssets.Add(new UsedAsset(a.importFrom)));
            createDescription.additionalAssetPaths.ForEach(path => m_usedAssets.Add(new UsedAsset(path)));

            var hash1 = MD5.Create();

            assets.ForEach(a => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a.importFrom)));
            createDescription.additionalAssetPaths.ForEach(path => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(path)));
            m_usedAssetsHash = hash1.ToString();
        }
Пример #2
0
        private void ValidatePrefabBuilder(
            Model.NodeData node,
            BuildTarget target,
            IEnumerable <PerformGraph.AssetGroups> incoming,
            Action folderDoesntExist,
            Action noBuilderData,
            Action failedToCreateBuilder,
            Action <string> canNotCreatePrefab,
            Action <AssetReference> canNotImportAsset
            )
        {
            var outputDir = PrepareOutputDirectory(target, node);

            if (!Directory.Exists(outputDir))
            {
                folderDoesntExist();
            }

            var builder = m_instance.Get <IPrefabBuilder>(target);

            if (null == builder)
            {
                failedToCreateBuilder();
            }

            if (m_createDescription == null)
            {
                m_createDescription = new PrefabCreateDescription();
            }

            try
            {
                builder.OnValidate();
            }
            catch (Exception e)
            {
                throw new NodeException(e.Message, "See reason for detail.", node);
            }

            if (null != builder && null != incoming)
            {
                foreach (var ag in incoming)
                {
                    foreach (var key in ag.assetGroups.Keys)
                    {
                        var assets = ag.assetGroups[key];
                        if (assets.Any())
                        {
                            bool isAllGoodAssets = true;
                            foreach (var a in assets)
                            {
                                if (string.IsNullOrEmpty(a.importFrom))
                                {
                                    canNotImportAsset(a);
                                    isAllGoodAssets = false;
                                }
                            }
                            if (isAllGoodAssets)
                            {
                                // do not call LoadAllAssets() unless all assets have importFrom
                                var al        = ag.assetGroups[key];
                                var allAssets = LoadAllAssets(al);

                                try
                                {
                                    m_createDescription.Reset();
                                    if (!builder.CanCreatePrefab(key, allAssets, ref m_createDescription))
                                    {
                                        canNotCreatePrefab(key);
                                    }
                                }
                                catch (Exception e)
                                {
                                    throw new NodeException(e.Message, "See reason for detail.", node);
                                }

                                UnloadAllAssets(al);
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        public override void Prepare(BuildTarget target,
                                     Model.NodeData node,
                                     IEnumerable <PerformGraph.AssetGroups> incoming,
                                     IEnumerable <Model.ConnectionData> connectionsToOutput,
                                     PerformGraph.Output Output)
        {
            ValidatePrefabBuilder(node, target, incoming,
                                  () => throw new NodeException("Output directory not found.", "Create output directory or set a valid directory path.", node),
                                  () => throw new NodeException("PrefabBuilder is not configured.", "Configure PrefabBuilder from inspector.", node),
                                  () => throw new NodeException("Failed to create PrefabBuilder from settings.", "Fix settings from inspector.", node),
                                  (string groupKey) => throw new NodeException(
                                      $"Can not create prefab with incoming assets for group {groupKey}.", "Fix group input assets for selected PrefabBuilder.", node),
                                  (AssetReference badAsset) => throw new NodeException(
                                      $"Can not import incoming asset {badAsset.fileNameAndExtension}.", "", node));

            if (incoming == null)
            {
                return;
            }

            var prefabOutputDir = PrepareOutputDirectory(target, node);

            var builder = m_instance.Get <IPrefabBuilder>(target);

            UnityEngine.Assertions.Assert.IsNotNull(builder);

            Dictionary <string, List <AssetReference> > output = null;

            if (Output != null)
            {
                output = new Dictionary <string, List <AssetReference> >();
            }

            var aggregatedGroups = new Dictionary <string, List <AssetReference> >();

            foreach (var ag in incoming)
            {
                foreach (var key in ag.assetGroups.Keys)
                {
                    if (!aggregatedGroups.ContainsKey(key))
                    {
                        aggregatedGroups[key] = new List <AssetReference>();
                    }
                    aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable());
                }
            }

            foreach (var key in aggregatedGroups.Keys)
            {
                var assets    = aggregatedGroups[key];
                var threshold = PrefabBuilderUtility.GetPrefabBuilderAssetThreshold(m_instance.ClassName);
                if (threshold < assets.Count)
                {
                    var guiName = PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName);
                    throw new NodeException(
                              string.Format("Too many assets passed to {0} for group:{1}. {2}'s threshold is set to {4}", guiName, key, guiName, threshold),
                              string.Format("Limit number of assets in a group to {4}", threshold), node);
                }

                List <UnityEngine.Object> allAssets = LoadAllAssets(assets);
                bool canCreatePrefab;

                if (m_createDescription == null)
                {
                    m_createDescription = new PrefabCreateDescription();
                }

                try
                {
                    m_createDescription.Reset();
                    canCreatePrefab = builder.CanCreatePrefab(key, allAssets, ref m_createDescription);
                }
                catch (Exception e)
                {
                    throw new NodeException(e.Message, "See reason for detail.", node);
                }

                if (output != null && canCreatePrefab)
                {
                    output[key] = new List <AssetReference> ()
                    {
                        AssetReferenceDatabase.GetPrefabReference(FileUtility.PathCombine(prefabOutputDir, m_createDescription.prefabName + ".prefab"))
                    };
                }
                UnloadAllAssets(assets);
            }

            if (Output != null)
            {
                var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                          null : connectionsToOutput.First();
                Output(dst, output);
            }
        }
Пример #4
0
        public static bool DoesPrefabNeedRebuilding(string buildPath, PrefabBuilder builder, Model.NodeData node, BuildTarget target,
                                                    string groupKey, List <AssetReference> assets, PrefabCreateDescription createDescription)
        {
            var buildInfo = GetPrefabBuildInfo(builder, node, target, groupKey);

            // need rebuilding if no buildInfo found
            if (buildInfo == null)
            {
                return(true);
            }

            // need rebuilding if build path is changed
            if (buildInfo.m_buildDir != buildPath)
            {
                return(true);
            }

            // need rebuilding if given builder is changed
            if (buildInfo.m_builderClass != builder.Builder.ClassName)
            {
                return(true);
            }

            // need rebuilding if given builder is changed
            if (buildInfo.m_instanceData != builder.Builder[target])
            {
                return(true);
            }

            var builderVersion = PrefabBuilderUtility.GetPrefabBuilderVersion(builder.Builder.ClassName);

            // need rebuilding if given builder version is changed
            if (buildInfo.m_prefabBuilderVersion != builderVersion)
            {
                return(true);
            }

            // need rebuilding if given groupKey changed
            if (buildInfo.m_groupKey != groupKey)
            {
                return(true);
            }

            var hash1 = MD5.Create();

            assets.ForEach(a => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a.importFrom)));
            createDescription.additionalAssetPaths.ForEach(path => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(path)));
            if (buildInfo.m_usedAssetsHash != hash1.ToString())
            {
                return(true);
            }

            // If any asset is modified from last time, then need rebuilding
            foreach (var usedAsset in buildInfo.m_usedAssets)
            {
                if (usedAsset.IsAssetModifiedFromLastTime)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #5
0
        public static void SavePrefabBuildInfo(string buildPath, PrefabBuilder builder, Model.NodeData node, BuildTarget target,
                                               string groupKey, List <AssetReference> assets, PrefabCreateDescription description)
        {
            var prefabCacheDir = FileUtility.EnsureCacheDirExists(target, node, PrefabBuilder.kCacheDirName);
            var buildInfoPath  = FileUtility.PathCombine(prefabCacheDir, groupKey + ".asset");

            var version = PrefabBuilderUtility.GetPrefabBuilderVersion(builder.Builder.ClassName);

            var buildInfo = CreateInstance <PrefabBuildInfo>();

            buildInfo.Initialize(buildPath, groupKey, builder.Builder.ClassName, builder.Builder[target], version, assets, description);

            AssetDatabase.CreateAsset(buildInfo, buildInfoPath);
        }