
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

namespace Assets.app.Editor.tools.assetCopyer
{
    class UtilFile
    {
        public static void CopyDirectory(string sourceDirectory, string destDirectory)
        {
            //判断源目录和目标目录是否存在，如果不存在，则创建一个目录
            if (!Directory.Exists(sourceDirectory))
            {
                return;
            }
            if (!Directory.Exists(destDirectory))
            {
                Directory.CreateDirectory(destDirectory);
            }
            //拷贝文件
            CopyFile(sourceDirectory, destDirectory);
            //拷贝子目录       
            //获取所有子目录名称
            string[] directionName = Directory.GetDirectories(sourceDirectory);
            foreach (string directionPath in directionName)
            {
                //根据每个子目录名称生成对应的目标子目录名称
                string directionPathTemp = Path.Combine(destDirectory, directionPath.Substring(sourceDirectory.Length + 1));// destDirectory + "\\" + directionPath.Substring(sourceDirectory.Length + 1);
                                                                                                                            //递归下去
                CopyDirectory(directionPath, directionPathTemp);
            }
        }
        public static void CopyFile(string sourceDirectory, string destDirectory)
        {
            //获取所有文件名称
            string[] fileName = Directory.GetFiles(sourceDirectory);
            foreach (string filePath in fileName)
            {
                //根据每个文件名称生成对应的目标文件名称
                string filePathTemp = Path.Combine(destDirectory, filePath.Substring(sourceDirectory.Length + 1));// destDirectory + "\\" + filePath.Substring(sourceDirectory.Length + 1);
                                                                                                                  //若不存在，直接复制文件；若存在，覆盖复制
                if (File.Exists(filePathTemp))
                {
                    File.Copy(filePath, filePathTemp, true);
                }
                else
                {
                    File.Copy(filePath, filePathTemp);
                }
            }
        }
        [MenuItem("Assets/复制文件夹(复制依赖关系)", false, 0)]
        static private void CopyFilesWithDependency()
        {
            string oldFolderPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
            string folderName = Path.GetFileName(oldFolderPath);
            string parentPath = Path.GetDirectoryName(oldFolderPath);
            string newFoldrPath = Path.Combine(parentPath, "copy_" + folderName);
            UtilFile.CopyDirectory(oldFolderPath, newFoldrPath);
            UtilGuids.RegenerateGuids(Path.GetFullPath(newFoldrPath));
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }
    }
}
