ProjectDDD/Assets/_Datas/SLShared/SLUnity/Editor/SLFileUtility.cs
2025-06-17 20:47:57 +09:00

206 lines
5.4 KiB (Stored with Git LFS)
C#

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public static class SLFileUtility
{
public static bool DirectoryCopy(string sourceDirName, string destDirName, bool overwrite, bool copySubDirs, bool displayProgress = false, string pattern = "*.*")
{
var sourceDir = new DirectoryInfo(sourceDirName);
if (!sourceDir.Exists)
{
return false;
//throw new DirectoryNotFoundException(
// "Source directory does not exist or could not be found: "
// + sourceDirName);
}
var destDir = new DirectoryInfo(destDirName);
if (!destDir.Exists)
{
destDir = Directory.CreateDirectory(destDirName);
AssetDatabase.Refresh();
}
if (copySubDirs)
{
var dirs = sourceDir.GetDirectories();
for (var i = 0; i < dirs.Length; i++)
{
var subdir = dirs[i];
var temppath = Path.Combine(destDirName, subdir.Name);
if (DirectoryCopy(subdir.FullName, temppath, overwrite, copySubDirs, displayProgress, pattern) == false)
return false;
}
}
var files = sourceDir.GetFiles(pattern);
for (var i = 0; i < files.Length; i++)
{
var file = files[i];
if (displayProgress)
{
if (EditorUtility.DisplayCancelableProgressBar(
sourceDir.Name + " > " + destDir.Name,
file.Name,
i / (float)files.Length))
{
EditorUtility.ClearProgressBar();
return false;
}
}
var temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, overwrite);
}
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
return true;
}
public static void SearchDirectory(string sourceDirName, bool doRecursive, ref List<string> pathList)
{
var dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
var dirs = dir.GetDirectories();
var files = dir.GetFiles();
foreach (var file in files)
{
pathList.Add(file.FullName);
}
if (doRecursive)
{
foreach (var subdir in dirs)
{
pathList.Add(subdir.FullName);
SearchDirectory(subdir.FullName, doRecursive, ref pathList);
}
}
}
public static string FixPath(string path)
{
path = path.Replace('\\', '/');
path = path.Replace("//", "/");
while (path.Length > 0 && path[0] == '/')
{
path = path.Remove(0, 1);
}
return path;
}
public static string FileName(string path)
{
return Path.GetFileNameWithoutExtension(path);
}
public static string FolderPath(string path)
{
return FixPath(Path.GetDirectoryName(path));
}
public static string FolderName(string path)
{
var dirPath = FolderPath(path);
return FixPath(dirPath.Substring(dirPath.LastIndexOf('/') + 1));
}
public static string CombinePaths(params string[] paths)
{
var path = "";
for (var i = 0; i < paths.Length; i++)
{
path = Path.Combine(path, FixPath(paths[i]));
}
return FixPath(path);
}
public static string RelativePath(string path)
{
path = FixPath(path);
if (path.StartsWith("Assets"))
{
return path;
}
if (path.StartsWith(FixPath(Application.dataPath)))
{
return "Assets" + path.Substring(FixPath(Application.dataPath).Length);
}
else
{
return "";
}
}
public static void MakeFolderFromFilePath(string filePath)
{
var paths = FixPath(filePath).Split('/');
var path = "";
for (var i = 0; i < paths.Length - 1; ++i)
{
path += paths[i] + "/";
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
AssetDatabase.Refresh();
}
}
}
public static DateTime GetLastWriteTime(string filePath)
{
var fileWriteTime = File.GetLastWriteTime(filePath);
var metaWriteTime = File.GetLastWriteTime(filePath + ".meta");
var result = DateTime.Compare(fileWriteTime, metaWriteTime);
if (result < 0)
{
// file이 meta보다 빠름.
return metaWriteTime;
}
else
{
return fileWriteTime;
}
}
public static void AttrToNormal(string targetDir)
{
File.SetAttributes(targetDir, FileAttributes.Normal);
var files = Directory.GetFiles(targetDir);
var dirs = Directory.GetDirectories(targetDir);
foreach (var file in files)
{
try
{
File.SetAttributes(file, FileAttributes.Normal);
}
catch
{
}
}
foreach (var dir in dirs)
{
AttrToNormal(dir);
}
}
}