Exemplo n.º 1
0
        //===============
        // Primary
        //===============
        public static FileData[] FindAll(string name, bool showWarnings = true, bool firstOnly = false)
        {
            name = name.Replace("\\", "/");
            var time = FileManager.GetTime();

            if (name == "" && showWarnings)
            {
                Debug.LogWarning("[FileManager] No path given for search.");
                return(null);
            }
            string searchKey = name.ToLower();

            if (FileManager.cache.ContainsKey(searchKey))
            {
                return(FileManager.cache[searchKey]);
            }
            if (name.StartsWith("!"))
            {
                name = name.ReplaceFirst("!", "");
            }
            if (name.ContainsAny("<", ">", "?", ":", "|"))
            {
                if (name[1] != ':')
                {
                    if (FileManager.debug)
                    {
                        Debug.LogWarning("[FileManager] Path has invalid characters -- " + name);
                    }
                    return(new FileData[0]);
                }
            }
            if (!name.Contains(".") && name.EndsWith("*"))
            {
                name = name + ".*";
            }
            if (name.Contains("*"))
            {
                firstOnly = false;
            }
            else if (name.Contains(":"))
            {
                firstOnly = true;
            }
            string fileName = name.GetFileName();
            string path     = name.GetDirectory();
            string type     = name.GetFileExtension().ToLower();
            var    results  = new List <FileData>();
            var    types    = new List <string>();
            var    allTypes = FileManager.filesByType.Keys;

            if (type.IsEmpty() || type == "*")
            {
                types = allTypes.ToList();
            }
            else if (type.StartsWith("*"))
            {
                types.AddRange(allTypes.Where(x => x.EndsWith(type.Remove("*"), true)));
            }
            else if (type.EndsWith("*"))
            {
                types.AddRange(allTypes.Where(x => x.StartsWith(type.Remove("*"), true)));
            }
            else if (FileManager.filesByType.ContainsKey(type))
            {
                types.Add(type);
            }
            foreach (var typeName in types)
            {
                FileManager.SearchType(fileName, typeName, path, firstOnly, ref results);
            }
            if (results.Count == 0)
            {
                foreach (var item in FileManager.folders)
                {
                    FileData folder     = item.Value;
                    string   folderPath = item.Key;
                    if (folderPath.Matches(name, true))
                    {
                        results.Add(folder);
                    }
                }
            }
            if (results.Count == 0 && showWarnings)
            {
                Debug.LogWarning("[FileManager] Path [" + name + "] could not be found.");
            }
            FileManager.cache[searchKey] = results.ToArray();
            if (FileManager.clock)
            {
                Debug.Log("[FileManager] : Find [" + name + "] complete (" + results.Count + ") -- " + (FileManager.GetTime() - time) + " seconds.");
            }
            return(results.ToArray());
        }