public static AssetInfo Create(RawAssetInfo rawAssetInfo, Type type, AssetSettingsKind settingsKind)
        {
            if (string.IsNullOrEmpty(rawAssetInfo.guid))
            {
                Debug.LogError("Can't create AssetInfo since guid is invalid!");
                return(null);
            }

            var newAsset = new AssetInfo
            {
                GUID         = rawAssetInfo.guid,
                Path         = rawAssetInfo.path,
                Kind         = rawAssetInfo.kind,
                Type         = type,
                SettingsKind = settingsKind,
                fileInfo     = new FileInfo(rawAssetInfo.path),
                metaFileInfo = new FileInfo(rawAssetInfo.path + ".meta")
            };

            newAsset.UpdateIfNeeded();

            return(newAsset);
        }
Exemple #2
0
        private static bool UpdateMap(AssetsMap map)
        {
            // ----------------------------------------
            // getting all valid assets within project
            // ----------------------------------------
            if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 1 of 4", "Getting all valid assets...", 0))
            {
                Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                return(false);
            }

            var allAssetPaths  = AssetDatabase.GetAllAssetPaths();
            var validNewAssets = new List <RawAssetInfo>(allAssetPaths.Length);

            foreach (var assetPath in allAssetPaths)
            {
                /*if (assetPath.Contains(@"ScriptableObjectScriptWithMissingScript"))
                 * {
                 *      Debug.Log(assetPath);
                 * }*/

                var kind = CSEditorTools.GetAssetKind(assetPath);
                if (kind == AssetKind.Unsupported)
                {
                    continue;
                }

                if (!File.Exists(assetPath))
                {
                    continue;
                }
                if (AssetDatabase.IsValidFolder(assetPath))
                {
                    continue;
                }

                var guid    = AssetDatabase.AssetPathToGUID(assetPath);
                var rawInfo = new RawAssetInfo
                {
                    path = CSPathTools.EnforceSlashes(assetPath),
                    guid = guid,
                    kind = kind,
                };

                validNewAssets.Add(rawInfo);
            }

            // -----------------------------
            // checking existing map assets
            // -----------------------------

            if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 2 of 4", "Checking existing assets in map...", 0))
            {
                Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                return(false);
            }

            var count = map.assets.Count;

#if !UNITY_2020_1_OR_NEWER
            var updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif
            for (var i = count - 1; i > -1; i--)
            {
#if !UNITY_2020_1_OR_NEWER
                if (i % updateStep == 0 && i != 0)
#endif
                {
                    var index = count - i;
                    if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 2 of 4", "Checking existing assets in map..." + index + "/" + count, (float)index / count))
                    {
                        EditorUtility.ClearProgressBar();
                        Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                        return(false);
                    }
                }

                var assetInMap = map.assets[i];
                if (assetInMap.Exists())
                {
                    validNewAssets.RemoveAll(a => a.guid == assetInMap.GUID);
                    assetInMap.UpdateIfNeeded();
                }
                else
                {
                    assetInMap.Clean();
                    map.assets.RemoveAt(i);
                }
            }

            // ------------------------
            // dealing with new assets
            // ------------------------

            if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 3 of 4", "Looking for new assets...", 0))
            {
                Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                return(false);
            }

            count = validNewAssets.Count;
#if !UNITY_2020_1_OR_NEWER
            updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif
            for (var i = 0; i < count; i++)
            {
#if !UNITY_2020_1_OR_NEWER
                if (i % updateStep == 0 && i != 0)
#endif
                {
                    if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 3 of 4",
                                                                   "Looking for new assets..." + (i + 1) + "/" + count, (float)i / count))
                    {
                        Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                        return(false);
                    }
                }

                var rawAssetInfo     = validNewAssets[i];
                var rawAssetInfoPath = rawAssetInfo.path;

                var type = AssetDatabase.GetMainAssetTypeAtPath(rawAssetInfoPath);
                if (type == null)
                {
                    var loadedAsset = AssetDatabase.LoadMainAssetAtPath(rawAssetInfoPath);
                    if (loadedAsset == null)
                    {
                        if (rawAssetInfo.kind != AssetKind.FromPackage)
                        {
                            if (!CSAssetTools.IsAssetScriptableObjectWithMissingScript(rawAssetInfoPath))
                            {
                                Debug.LogWarning(Maintainer.LogPrefix + "Can't retrieve type of the asset:\n" +
                                                 rawAssetInfoPath);
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        type = loadedAsset.GetType();
                    }
                }

                var settingsKind = rawAssetInfo.kind == AssetKind.Settings ? GetSettingsKind(rawAssetInfoPath) : AssetSettingsKind.NotSettings;

                var asset = AssetInfo.Create(rawAssetInfo, type, settingsKind);
                map.assets.Add(asset);
            }

            if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 4 of 4", "Generating links...", 0))
            {
                Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                return(false);
            }

            count = map.assets.Count;

#if !UNITY_2020_1_OR_NEWER
            updateStep = Math.Max(count / ProjectSettings.UpdateProgressStep, 1);
#endif
            for (var i = 0; i < count; i++)
            {
#if !UNITY_2020_1_OR_NEWER
                if (i % updateStep == 0 && i != 0)
#endif
                {
                    if (EditorUtility.DisplayCancelableProgressBar("Updating Assets Map, phase 4 of 4", "Generating links..." + (i + 1) + "/" + count, (float)i / count))
                    {
                        Debug.LogError(Maintainer.LogPrefix + "Assets Map update was canceled by user.");
                        return(false);
                    }
                }

                var asset = map.assets[i];

                if (!asset.needToRebuildReferences)
                {
                    continue;
                }

                var dependencies   = asset.dependenciesGUIDs;
                var referenceInfos = new List <AssetReferenceInfo>(asset.assetReferencesInfo);

                foreach (var mapAsset in map.assets)
                {
                    var referencedAtInfos = new List <ReferencedAtAssetInfo>(mapAsset.referencedAtInfoList);

                    foreach (var dependency in dependencies)
                    {
                        if (mapAsset.GUID != dependency)
                        {
                            continue;
                        }
                        if (mapAsset.Type == asset.Type && asset.Type == CSReflectionTools.fontType)
                        {
                            continue;
                        }

                        var referencedAtInfo = new ReferencedAtAssetInfo()
                        {
                            assetInfo = asset
                        };
                        referencedAtInfos.Add(referencedAtInfo);

                        var referenceInfo = new AssetReferenceInfo()
                        {
                            assetInfo = mapAsset
                        };
                        referenceInfos.Add(referenceInfo);
                    }

                    mapAsset.referencedAtInfoList = referencedAtInfos.ToArray();
                }

                asset.assetReferencesInfo     = referenceInfos.ToArray();
                asset.needToRebuildReferences = false;
            }

            /*Debug.Log("Total assets in map: " + map.assets.Count);
             * foreach (var mapAsset in map.assets)
             * {
             *      //if (!(mapAsset.path.Contains("frag_ab") || mapAsset.path.Contains("frag_ac"))) continue;
             *      if (!mapAsset.Path.Contains("NewAssembly")) continue;
             *
             *      Debug.Log("==================================================\n" + mapAsset.Path + "\n" + mapAsset.Path);
             *      Debug.Log("[REFERENCED BY]");
             *      foreach (var reference in mapAsset.referencedAtInfoList)
             *      {
             *              Debug.Log(reference.assetInfo.Path);
             *      }
             *
             *      Debug.Log("[REFERENCES]");
             *      foreach (var reference in mapAsset.assetReferencesInfo)
             *      {
             *              Debug.Log(reference.assetInfo.Path);
             *      }
             * }*/

            return(true);
        }