Unity FileTools

作者: 2b75747cf703 | 来源:发表于2017-05-10 17:47 被阅读193次
    using System;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Babybus.Framework.ExtensionMethods;
    
    namespace Babybus.Framework.Extension
    {
        public class FileTools
        {
            private static Type fileUtilType;
    
            static FileTools()
            {
    #if UNITY_EDITOR
                var unityEditor = typeof(Editor).Assembly;
                fileUtilType = unityEditor.GetType("UnityEditor.FileUtil");
    #endif
            }
    
            public static bool AppendTextAfter(string path, string find, string append)
            {
                return fileUtilType.Invoke<bool>("AppendTextAfter", path, find, append);
            }
    
            public static string CombinePaths(params string[] paths)
            {
                return fileUtilType.Invoke<string>("CombinePaths", paths);
            }
    
            public static void CopyDirectory(string source, string target, bool overwrite)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectory", source, target, overwrite);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryFiltered(string source, string target, bool overwrite, string regExExcludeFilter, bool recursive)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryFiltered", source, target, overwrite, regExExcludeFilter, recursive);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryFiltered(string source, string target, bool overwrite, Func<string, bool> includeCallback, bool recursive)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryFiltered", source, target, overwrite, includeCallback, recursive);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursive(string source, string target)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursive", source, target);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursive(string source, string target, bool overwrite)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursive", source, target, overwrite);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursive(string source, string target, bool overwrite, bool ignoreMeta)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursive", source, target, overwrite, ignoreMeta);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursiveFiltered(string source, string target, bool overwrite, string regExExcludeFilter)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursiveFiltered", source, target, overwrite, regExExcludeFilter);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursiveForPostprocess(string source, string target, bool overwrite)
            {
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursiveForPostprocess", source, target, overwrite);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyDirectoryRecursiveIgnoreMeta(string source, string target, bool overwrite)
            {
                if (overwrite && Directory.Exists(target))
                    DeleteFileOrDirectory(target);
    
                try
                {
                    fileUtilType.Invoke("CopyDirectoryRecursiveIgnoreMeta", source, target);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyFileIfExists(string src, string dst, bool overwrite)
            {
                try
                {
                    fileUtilType.Invoke("CopyFileIfExists", src, dst, overwrite);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void CopyFileOrDirectory(string from, string to, bool overwrite = false)
            {
                if (!File.Exists(from) && !Directory.Exists(from))
                {
                    Debug.LogError(string.Format("FileUtil.CopyFileOrDirectory {0} 路径不存在!", from));
                    return;
                }
    
                if (File.Exists(to) || Directory.Exists(to))
                {
                    if (!overwrite)
                        return;
    
                    DeleteFileOrDirectory(to);
                }
    
                var directory = Path.GetDirectoryName(to);
                if (directory != "" && !Directory.Exists(directory))
                    Directory.CreateDirectory(directory);
    
    #if UNITY_EDITOR
                Debug.Log(string.Format("FileUtil.CopyFileOrDirectory {0} {1}", from, to));
                FileUtil.CopyFileOrDirectory(from, to);
    #endif
            }
    
            public static void CopyFileOrDirectoryFollowSymlinks(string from, string to)
            {
                CopyFileOrDirectoryFollowSymlinks(from, to, false);
            }
    
            public static void CopyFileOrDirectoryFollowSymlinks(string from, string to, bool overwrite)
            {
                if (!File.Exists(from) && !Directory.Exists(from))
                {
                    Debug.LogError(string.Format("FileUtil.CopyFileOrDirectoryFollowSymlinks {0} 路径不存在!", from));
                    return;
                }
    
                if (File.Exists(to) || Directory.Exists(to))
                {
                    if (!overwrite)
                        return;
    
                    DeleteFileOrDirectory(to);
                }
    
                var directory = Path.GetDirectoryName(to);
                if (directory != "" && !Directory.Exists(directory))
                    Directory.CreateDirectory(directory);
    
    #if UNITY_EDITOR
                Debug.Log(string.Format("FileUtil.CopyFileOrDirectoryFollowSymlinks {0} {1}", from, to));
                FileUtil.CopyFileOrDirectoryFollowSymlinks(from, to);
    #endif
            }
    
            public static void CreateOrCleanDirectory(string dir)
            {
                if (string.IsNullOrEmpty(dir))
                    return;
    
                fileUtilType.Invoke("CreateOrCleanDirectory", dir);
            }
    
            public static bool DeleteFileOrDirectory(string path)
            {
    #if UNITY_EDITOR
                return FileUtil.DeleteFileOrDirectory(path);
    #endif
    
                return false;
            }
    
            public static string DeleteLastPathNameComponent(string path)
            {
                return fileUtilType.Invoke<string>("DeleteLastPathNameComponent", path);
            }
    
            public static string GetActualPathName(string path)
            {
                return fileUtilType.Invoke<string>("GetActualPathName", path);
            }
    
            public static List<string> GetAllFilesRecursive(string path)
            {
                return fileUtilType.Invoke<List<string>>("GetAllFilesRecursive", path);
            }
    
            public static long GetDirectorySize(string path)
            {
                return fileUtilType.Invoke<long>("GetDirectorySize", path);
            }
    
            public static string GetLastPathNameComponent(string path)
            {
                return fileUtilType.Invoke<string>("GetLastPathNameComponent", path);
            }
    
            public static string GetPathExtension(string path)
            {
                return fileUtilType.Invoke<string>("GetPathExtension", path);
            }
    
            public static string GetPathWithoutExtension(string path)
            {
                return fileUtilType.Invoke<string>("GetPathWithoutExtension", path);
            }
    
            public static string GetProjectRelativePath(string path)
            {
    #if UNITY_EDITOR
                return FileUtil.GetProjectRelativePath(path);
    #endif
                return "";
            }
    
            public static string GetUniqueTempPathInProject()
            {
    #if UNITY_EDITOR
                return FileUtil.GetUniqueTempPathInProject();
    #endif
                return "";
            }
    
            public static void MoveFileIfExists(string src, string dst)
            {
                fileUtilType.Invoke("MoveFileIfExists", src, dst);
            }
    
            public static void MoveFileOrDirectory(string from, string to, bool overwrite = true)
            {
                if (!File.Exists(from) && !Directory.Exists(from))
                {
                    Debug.LogError(string.Format("FileUtil.MoveFileOrDirectory {0} 路径不存在!", from));
                    return;
                }
    
                if (File.Exists(to) || Directory.Exists(to))
                {
                    if (!overwrite)
                        return;
    
                    DeleteFileOrDirectory(to);
                }
    
                var directory = Path.GetDirectoryName(to);
                if (directory != "" && !Directory.Exists(directory))
                    Directory.CreateDirectory(directory);
    
    #if UNITY_EDITOR
                Debug.Log(string.Format("FileUtil.MoveFileOrDirectory {0} {1}", from, to));
                FileUtil.MoveFileOrDirectory(from, to);
    #endif
            }
    
            public static string NiceWinPath(string unityPath)
            {
                return fileUtilType.Invoke<string>("NiceWinPath", unityPath);
            }
    
            public static string RemovePathPrefix(string fullPath, string prefix)
            {
                return fileUtilType.Invoke<string>("RemovePathPrefix", fullPath, prefix);
            }
    
            public static void ReplaceDirectory(string src, string dst)
            {
                if (!Directory.Exists(src))
                {
                    Debug.LogError(string.Format("FileUtil.ReplaceDirectory {0} 路径不存在!", src));
                    return;
                }
    
                CreateOrCleanDirectory(dst);
    
    #if UNITY_EDITOR
                Debug.Log(string.Format("FileUtil.ReplaceDirectory {0} {1}", src, dst));
                FileUtil.ReplaceDirectory(src, dst);
    #endif
            }
    
            public static void ReplaceFile(string src, string dst)
            {
                if (!File.Exists(src))
                {
                    Debug.LogError(string.Format("FileUtil.ReplaceFile {0} 路径不存在!", src));
                    return;
                }
    
    #if UNITY_EDITOR
                Debug.Log(string.Format("FileUtil.ReplaceFile {0} {1}", src, dst));
                FileUtil.ReplaceFile(src, dst);
    #endif
            }
    
            public static void ReplaceText(string path, params string[] input)
            {
                fileUtilType.Invoke("ReplaceText", path, input);
            }
    
            public static bool ReplaceTextRegex(string path, params string[] input)
            {
                return fileUtilType.Invoke<bool>("ReplaceTextRegex", path, input);
            }
    
            public static void UnityDirectoryDelete(string path)
            {
                try
                {
                    fileUtilType.Invoke("UnityDirectoryDelete", path);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void UnityDirectoryDelete(string path, bool recursive)
            {
                try
                {
                    fileUtilType.Invoke("UnityDirectoryDelete", path, recursive);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void UnityDirectoryRemoveReadonlyAttribute(string target_dir)
            {
                try
                {
                    fileUtilType.Invoke("UnityDirectoryRemoveReadonlyAttribute", target_dir);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void UnityFileCopy(string from, string to, bool overwrite)
            {
                try
                {
                    fileUtilType.Invoke("UnityFileCopy", from, to, overwrite);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static void UnityFileCopy(string from, string to)
            {
                try
                {
                    fileUtilType.Invoke("UnityFileCopy", from, to);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
    
            public static string UnityGetDirectoryName(string path)
            {
                return fileUtilType.Invoke<string>("UnityGetDirectoryName", path);
            }
    
            public static string UnityGetFileName(string path)
            {
                return fileUtilType.Invoke<string>("UnityGetFileName", path);
            }
    
            public static string UnityGetFileNameWithoutExtension(string path)
            {
                return fileUtilType.Invoke<string>("UnityGetFileNameWithoutExtension", path);
            }
    
            public static void WalkFilesystemRecursively(string path, Action<string> fileCallback, Func<string, bool> directoryCallback)
            {
                fileUtilType.Invoke("WalkFilesystemRecursively", path, fileCallback, directoryCallback);
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Unity FileTools

        本文链接:https://www.haomeiwen.com/subject/domgtxtx.html