From 739dd53e02639293cad606f5944a99821689532e Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Tue, 25 Mar 2025 17:34:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BAdata=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E8=BF=9B=E8=A1=8C=E7=BB=9F=E4=B8=80=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Editor/LowercaseFileRenamer.cs | 145 ++++++++++++++++++ .../Editor/LowercaseFileRenamer.cs.meta | 11 ++ Assets/Scripts/Editor/VirtualFPostProcess.cs | 82 +++++----- 3 files changed, 203 insertions(+), 35 deletions(-) create mode 100644 Assets/Scripts/Editor/LowercaseFileRenamer.cs create mode 100644 Assets/Scripts/Editor/LowercaseFileRenamer.cs.meta diff --git a/Assets/Scripts/Editor/LowercaseFileRenamer.cs b/Assets/Scripts/Editor/LowercaseFileRenamer.cs new file mode 100644 index 00000000..2333e5a6 --- /dev/null +++ b/Assets/Scripts/Editor/LowercaseFileRenamer.cs @@ -0,0 +1,145 @@ +#if UNITY_EDITOR +using UnityEditor; +using UnityEngine; +using System.IO; +using System; + +public class WindowsLowercaseRenamer : EditorWindow +{ + [MenuItem("Assets/Windowsȫ")] + private static void SafeRenameAll() + { + string dataPath = Global.dataPath; + + // Ŀ¼ + ProcessRootDirectory(dataPath); + + // Ŀ¼ļ + ProcessDirectoriesRecursive(dataPath); + ProcessFilesRecursive(dataPath); + + AssetDatabase.Refresh(); + Debug.Log("ȫɣ"); + } + // Ŀ¼ + private static void ProcessRootDirectory(string originalPath) + { + try + { + DirectoryInfo rootInfo = new DirectoryInfo(originalPath); + string targetRootName = rootInfo.Name.ToLowerInvariant(); + + // Ŀ¼ƲϹ淶 + if (rootInfo.Name != targetRootName) + { + string parentPath = rootInfo.Parent.FullName; + string newRootPath = Path.Combine(parentPath, targetRootName); + + // ⴦Ŀ¼ҪȨ + if (Directory.Exists(newRootPath)) + { + string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString()); + Directory.Move(originalPath, tempPath); + Directory.Move(tempPath, newRootPath); + } + else + { + Directory.Move(originalPath, newRootPath); + } + + Debug.Log($"Ŀ¼ɹ: {originalPath} {newRootPath}"); + + // ȫ·Ҫ + Global.dataPath = newRootPath; // ʵGlobalʵֿҪ + } + } + catch (Exception ex) + { + Debug.LogError($"Ŀ¼ʧ: {ex.Message}"); + } + } + private static void ProcessDirectoriesRecursive(string path) + { + foreach (var dirPath in Directory.GetDirectories(path)) + { + // ݹ鴦Ŀ¼ + ProcessDirectoriesRecursive(dirPath); + + DirectoryInfo dirInfo = new DirectoryInfo(dirPath); + string targetName = dirInfo.Name.ToLowerInvariant(); + + // ʵĿ¼Ϲ淶ʱ + if (dirInfo.Name != targetName) + { + string parentPath = dirInfo.Parent.FullName; + string newPath = Path.Combine(parentPath, targetName); + + try + { + // ⴦WindowsҪʱ + //if (Directory.Exists(newPath)) + //{ + // string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString()); + // Directory.Move(dirPath, tempPath); + // Directory.Move(tempPath, newPath); + //} + //else + //{ + Directory.Move(dirPath, newPath); + //} + + Debug.Log($"Ŀ¼ɹ: {dirPath} {newPath}"); + } + catch (Exception ex) + { + Debug.LogError($"Ŀ¼ʧ: {dirPath}\n{ex.Message}"); + } + } + } + } + + private static void ProcessFilesRecursive(string path) + { + foreach (var filePath in Directory.GetFiles(path)) + { + FileInfo fileInfo = new FileInfo(filePath); + string targetName = Path.GetFileNameWithoutExtension(fileInfo.Name).ToLowerInvariant(); + string targetExt = fileInfo.Extension.ToLowerInvariant(); + string newFileName = $"{targetName}{targetExt}"; + + // Ƚʵļ + if (fileInfo.Name != newFileName) + { + string newPath = Path.Combine(fileInfo.DirectoryName, newFileName); + + try + { + // Windows⴦ĿڵСдͬ + if (File.Exists(newPath)) + { + string tempPath = Path.Combine(fileInfo.DirectoryName, Guid.NewGuid().ToString() + fileInfo.Extension); + File.Move(filePath, tempPath); + File.Move(tempPath, newPath); + } + else + { + File.Move(filePath, newPath); + } + + Debug.Log($"ļɹ: {filePath} {newPath}"); + } + catch (Exception ex) + { + Debug.LogError($"ļʧ: {filePath}\n{ex.Message}"); + } + } + } + + // ݹĿ¼ + foreach (var dirPath in Directory.GetDirectories(path)) + { + ProcessFilesRecursive(dirPath); + } + } +} +#endif \ No newline at end of file diff --git a/Assets/Scripts/Editor/LowercaseFileRenamer.cs.meta b/Assets/Scripts/Editor/LowercaseFileRenamer.cs.meta new file mode 100644 index 00000000..852097e7 --- /dev/null +++ b/Assets/Scripts/Editor/LowercaseFileRenamer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3fc1a350612ff074eb9880438ed8d526 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Editor/VirtualFPostProcess.cs b/Assets/Scripts/Editor/VirtualFPostProcess.cs index a89bdefc..41b0b061 100644 --- a/Assets/Scripts/Editor/VirtualFPostProcess.cs +++ b/Assets/Scripts/Editor/VirtualFPostProcess.cs @@ -7,60 +7,72 @@ using UnityEngine; public class VirtualFPostProcess : IPostprocessBuildWithReport { - // صȼԽԽִУ public int callbackOrder => 0; - // ڴɺִ public void OnPostprocessBuild(BuildReport report) { - // ȡĿĿ¼· string projectPath = Application.dataPath; - - // ȡDataļе· string dataFolderPath = Path.Combine(projectPath, "../Data"); + string buildOutputPath = GetValidBuildPath(report); - // ȡĿ¼ - string buildOutputPath = report.summary.outputPath; - if (buildOutputPath.Contains(".exe")) - { - var paths = buildOutputPath.Split('/'); - buildOutputPath = buildOutputPath.Replace(paths[paths.Length - 1], ""); - } - // DataļǷ if (Directory.Exists(dataFolderPath)) { - // DataļиƵĿĿ¼ - string targetDataPath = Path.Combine(buildOutputPath, "Data"); - CopyDirectory(dataFolderPath, targetDataPath); - Debug.Log($"Data folder copied to build output directory: {targetDataPath}"); + // ĿĿ¼ǿСд + string targetDataPath = Path.Combine(buildOutputPath, "data"); + CopyDirectoryWithLowerCaseNames(dataFolderPath, targetDataPath); + Debug.Log($"Ŀ¼ѸƵ: {targetDataPath}"); } else { - Debug.LogWarning("Data folder not found at: " + dataFolderPath); + Debug.LogWarning("δҵĿ¼: " + dataFolderPath); } } - // ݹ鸴ļеĸļļ - private void CopyDirectory(string sourceDir, string destinationDir) + private string GetValidBuildPath(BuildReport report) { - // ȷĿĿ¼ - if (!Directory.Exists(destinationDir)) + string path = report.summary.outputPath; + if (path.Contains(".exe")) { - Directory.CreateDirectory(destinationDir); + return Path.GetDirectoryName(path); + } + return path; + } + + private void CopyDirectoryWithLowerCaseNames(string sourceDir, string targetDir) + { + // СдĿĿ¼ + var lowerTargetDir = ConvertToLowerPath(targetDir); + Directory.CreateDirectory(lowerTargetDir); + + // ļСдת + foreach (var file in Directory.GetFiles(sourceDir)) + { + string fileName = Path.GetFileName(file); + string lowerName = ConvertToLowerPath(fileName); + File.Copy(file, Path.Combine(lowerTargetDir, lowerName), true); } - // ԴĿ¼еļ - foreach (string file in Directory.GetFiles(sourceDir)) + // ݹ鴦Ŀ¼Сдת + foreach (var dir in Directory.GetDirectories(sourceDir)) { - string destFile = Path.Combine(destinationDir, Path.GetFileName(file)); - File.Copy(file, destFile, true); // ļ - } - - // ԴĿ¼еļ - foreach (string subDir in Directory.GetDirectories(sourceDir)) - { - string destSubDir = Path.Combine(destinationDir, Path.GetFileName(subDir)); - CopyDirectory(subDir, destSubDir); // ݹ鸴ļ + string dirName = Path.GetFileName(dir); + string lowerDirName = ConvertToLowerPath(dirName); + CopyDirectoryWithLowerCaseNames(dir, Path.Combine(lowerTargetDir, lowerDirName)); } } -} + + // ӢĻ·תСд + private string ConvertToLowerPath(string input) + { + char[] chars = input.ToCharArray(); + for (int i = 0; i < chars.Length; i++) + { + // ֻASCIIĸַĵUnicodeֲַ䣩 + if (chars[i] >= 'A' && chars[i] <= 'Z') + { + chars[i] = (char)(chars[i] | 0x20); // תСд + } + } + return new string(chars); + } +} \ No newline at end of file