Beispiel #1
0
        public static string GetUniqueName(string dirPath, string fileName, int maxNameLength)
        {
            string ext           = null;
            string name          = null;
            int    maxLength     = maxNameLength - 4;
            string validFileName = fileName;

            if (validFileName.Length > maxLength)
            {
                ext           = Path.GetExtension(validFileName);
                name          = Path.GetFileNameWithoutExtension(validFileName).Substring(0, maxLength - ext.Length);
                validFileName = name + ext;
            }

            dirPath = DirectoryUtils.ToLongPath(dirPath);
            if (!Directory.Exists(dirPath))
            {
                return(validFileName);
            }

            string filePath = ToLongPath(Path.Combine(dirPath, validFileName));

            if (!File.Exists(filePath))
            {
                return(validFileName);
            }

            ext  = ext ?? Path.GetExtension(validFileName);
            name = name ?? Path.GetFileNameWithoutExtension(validFileName);

            string        escapedName = Regex.Escape(name);
            List <string> files       = new List <string>();
            DirectoryInfo dirInfo     = new DirectoryInfo(dirPath);

            /* MRH - this line was causing the error below.
             * DirectoryInfo dirInfo = new DirectoryInfo(DirectoryUtils.ToLongPath(dirPath, true));
             *  - think this fix is okay because dirPath is set to the "ToLongPath()" about 16 code lines above
             *
             *  error: System.ArgumentException: Illegal characters in path.
             *  at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
             *  at System.IO.DirectoryInfo.Init(String path, Boolean checkHost)
             *  at uTinyRipper.FileUtils.GetUniqueName(String dirPath, String fileName, Int32 maxNameLength) in
             *  C:\Users\Maha\source\repos\UtinyRipper\uTinyRipperCore\Utils\FileUtils.cs:line 145
             */
            Regex regex = new Regex($@"(?i)^{escapedName}(_[\d]+)?\.[^\.]+$");

            foreach (FileInfo fileInfo in dirInfo.EnumerateFiles($"{name}_*{ext}"))
            {
                if (regex.IsMatch(fileInfo.Name))
                {
                    files.Add(fileInfo.Name.ToLower());
                }
            }
            if (files.Count == 0)
            {
                return($"{name}_0{ext}");
            }

            string lowName = name.ToLower();

            for (int i = 1; i < int.MaxValue; i++)
            {
                string newName = $"{lowName}_{i}.";
                if (files.All(t => !t.StartsWith(newName, StringComparison.Ordinal)))
                {
                    return($"{name}_{i}{ext}");
                }
            }
            throw new Exception($"Can't generate unique name for file {fileName} in directory {dirPath}");
        }
        public WebGLStructure(FileCollection collection, string rootPath) :
            base(collection)
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                throw new ArgumentNullException(rootPath);
            }
            m_root = new DirectoryInfo(DirectoryUtils.ToLongPath(rootPath));
            if (!m_root.Exists)
            {
                throw new Exception($"Directory '{rootPath}' doesn't exist");
            }

            Dictionary <string, string> files = new Dictionary <string, string>();
            string buildPath = Path.Combine(m_root.FullName, BuildName);

            if (Directory.Exists(buildPath))
            {
                DirectoryInfo buildDirectory = new DirectoryInfo(buildPath);
                foreach (FileInfo file in buildDirectory.EnumerateFiles())
                {
                    if (file.Name.EndsWith(DataWebExtension, StringComparison.Ordinal))
                    {
                        Name = file.Name.Substring(0, file.Name.Length - DataWebExtension.Length);
                        files.Add(Name, file.FullName);
                        break;
                    }
                }
                DataPathes = new string[] { rootPath, buildPath };
            }
            else
            {
                string developmentPath = Path.Combine(m_root.FullName, DevelopmentName);
                if (Directory.Exists(developmentPath))
                {
                    DirectoryInfo buildDirectory = new DirectoryInfo(developmentPath);
                    foreach (FileInfo file in buildDirectory.EnumerateFiles())
                    {
                        if (file.Extension == DataExtension)
                        {
                            Name = file.Name.Substring(0, file.Name.Length - DataExtension.Length);
                            files.Add(Name, file.FullName);
                            break;
                        }
                    }
                    DataPathes = new string[] { rootPath, developmentPath };
                }
                else
                {
                    string releasePath = Path.Combine(m_root.FullName, ReleaseName);
                    if (Directory.Exists(releasePath))
                    {
                        DirectoryInfo buildDirectory = new DirectoryInfo(releasePath);
                        foreach (FileInfo file in buildDirectory.EnumerateFiles())
                        {
                            if (file.Extension == DataGzExtension)
                            {
                                Name = file.Name.Substring(0, file.Name.Length - DataGzExtension.Length);
                                files.Add(Name, file.FullName);
                                break;
                            }
                        }
                        DataPathes = new string[] { rootPath, releasePath };
                    }
                    else
                    {
                        throw new Exception("Build directory hasn't been found");
                    }
                }
            }

            if (files.Count == 0)
            {
                throw new Exception("No files has been found");
            }

            CollectStreamingAssets(m_root, files);
            Files = files;

            Assemblies = new Dictionary <string, string>();
            //m_fileCollection.AssemblyManager.ScriptingBackEnd = ScriptingBackEnd.Il2Cpp;
        }