public void Add(ResourceDefinition definition)
        {
            var folder = GetFolderForPath(definition.Path);

            if (definition.Name == folder.Name)
            {
                TSLog.LogWarning(LogCategory.Compile, Strings.Warning_ResourceNameCannotBeSameAsParent,
                                 AssetDatabase.LoadAssetAtPath(definition.FullPath, typeof(Object)));

                definition = new ResourceDefinition("_" + definition.Name, definition.Path, definition.FullPath,
                                                    definition.Type);
            }

            foreach (var p in folder.Resources)
            {
                if (p.Name == definition.Name)
                {
                    TSLog.LogError(LogCategory.Compile,
                                   string.Format("Name for resource at [{0}] conflicts with another resource.", definition.FullPath),
                                   AssetDatabase.LoadAssetAtPath(definition.FullPath, typeof(Object)));

                    TSLog.LogError(LogCategory.Compile, string.Format("  \\_ Other Resource: {0}", p.FullPath),
                                   AssetDatabase.LoadAssetAtPath(p.FullPath, typeof(Object)));

                    return;
                }
            }

            folder.Resources.Add(definition);
        }
 private static void Init()
 {
     try
     {
         _sortingLayerNamesProperty = typeof(InternalEditorUtility).GetProperty("sortingLayerNames",
                                                                                BindingFlags.Static | BindingFlags.NonPublic);
         _sortingLayerUniqueIdsProperty = typeof(InternalEditorUtility).GetProperty("sortingLayerUniqueIDs",
                                                                                    BindingFlags.Static | BindingFlags.NonPublic);
     }
     catch (Exception e)
     {
         TSLog.LogError(LogCategory.Info,
                        "Error accessing sorting layers. See TypeSafe.log for more information.");
         TSLog.LogError(LogCategory.Trace, e.ToString());
     }
 }
        private static void LoadCache()
        {
            if (_typeCache != null)
            {
                return;
            }

            TSLog.Log(LogCategory.Trace, "[AssetTypeCache] Loading Asset Type Cache");

            _typeCache = new Dictionary <string, Type>();

            var cache = TypeCache.Instance.Contents;

            if (cache == null)
            {
                return;
            }

            for (var i = 0; i < cache.Length; i++)
            {
                var type = Type.GetType(cache[i].Type, false);

                if (type == null)
                {
                    TSLog.LogWarning(LogCategory.Trace,
                                     string.Format("[AssetTypeCache] Type from cache was not found (path: {0}, type: {1})", cache[i].Path, cache[i].Type));
                    continue;
                }

                if (_typeCache.ContainsKey(cache[i].Path))
                {
                    TSLog.LogError(LogCategory.Trace, string.Format("[AssetTypeCache] Duplicate path in type cache ({0})", cache[i].Path));
                    continue;
                }

                _typeCache.Add(cache[i].Path, type);
            }
        }
        public static bool GetSafeNameAndVerifyNotDuplicate(string name, CodeTypeDeclaration targetType,
                                                            out string finalName, bool checkReserved = true)
        {
            try
            {
                finalName = GetSafeName(name, false, checkReserved);

                if (IsDuplicate(finalName, targetType))
                {
                    TSLog.LogError(LogCategory.Compile, string.Format("Name {0} conflicts with existing member.", finalName));
                    return(false);
                }
            }
            catch (Exception e)
            {
                TSLog.LogError(LogCategory.Trace, e.ToString());
                TSLog.LogError(LogCategory.Compile, string.Format("Failed to create a C# safe-name for `{0}`", name));
                finalName = null;
                return(false);
            }

            return(true);
        }
        public IEnumerable <ResourceDefinition> Scan()
        {
            var paths =
                AssetDatabase.GetAllAssetPaths()
                .Where(p => Path.HasExtension(p) && PathUtility.IsAssetResource(p))
                .ToList();

            TotalAssets = paths.Count;

            foreach (var path in paths)
            {
                var name         = Path.GetFileNameWithoutExtension(path);
                var relativePath = GetResourceRelativePath(path);

                var inBlacklist = PathUtility.IsInBlacklist(path);
                var inWhitelist = PathUtility.IsInWhitelist(path);

                var skip = inBlacklist || (!inWhitelist && Settings.Instance.EnableWhitelist);

                Type type   = null;
                var  source = UnityUtility.TypeSource.None;

                if (!skip)
                {
                    if (!UnityUtility.GetAssetType(path, out type, out source))
                    {
                        TSLog.LogError(LogCategory.Scanner, string.Format("Failed finding type for asset at path {0}", path));
                        continue;
                    }
                }

                var isEditorType = !skip && UnityUtility.IsEditorType(type);
                var isPrivate    = !skip && (type.IsNotPublic && !UnityUtility.IsUserAssemblyType(type));

                TSLog.Log(LogCategory.Scanner,
                          string.Format("Resource [Name: {0}, RelativePath: {1}, FullPath: {2}, Type: {3}, isEditorType: {4}, typeSource: {5}, inBlacklist: {6}, inWhitelist: {7}, isPrivate: {8}]", name, relativePath, path, type != null ? type.FullName : null,
                                        isEditorType, source, inBlacklist, inWhitelist, isPrivate));

                if (isPrivate)
                {
                    TSLog.Log(LogCategory.Scanner, string.Format("Skipping {0} (isPrivate=true)", relativePath));
                    continue;
                }

                // Skip resources that are of an editor-only type
                if (isEditorType)
                {
                    TSLog.Log(LogCategory.Scanner, string.Format("Skipping {0} (isEditorType=true)", relativePath));
                    continue;
                }

                // Skip resources that are of an editor-only type
                if (inBlacklist)
                {
                    TSLog.Log(LogCategory.Scanner, string.Format("Skipping {0} (inBlacklist=true)", relativePath));
                    continue;
                }

                // Skip resources that are not in the white list.
                if (Settings.Instance.EnableWhitelist && !inWhitelist)
                {
                    TSLog.Log(LogCategory.Scanner, string.Format("Skipping {0} (inWhitelist = false)", relativePath));
                    continue;
                }

                if (type == typeof(Texture2D))
                {
                    var importer = AssetImporter.GetAtPath(path) as TextureImporter;

                    if (importer != null && importer.textureType == TextureImporterType.Sprite)
                    {
                        TSLog.Log(LogCategory.Scanner, "Overriding type -> Sprite (importer.textureType == TextureImporterType.Sprite)");
                        type = typeof(Sprite);
                    }
                }

                var resourceDefinition = new ResourceDefinition(name,
                                                                relativePath, path, type);

                yield return(resourceDefinition);
            }
        }