From e66fe05f5387410bb6dfe599884b140e378a8f4c Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Mon, 4 Sep 2023 17:48:15 +0800 Subject: [PATCH] update --- GameResRenamer.sln | 25 +++++++ GameResRenamer/GameResRenamer.csproj | 27 +++++++ GameResRenamer/Program.cs | 104 +++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 GameResRenamer.sln create mode 100644 GameResRenamer/GameResRenamer.csproj create mode 100644 GameResRenamer/Program.cs diff --git a/GameResRenamer.sln b/GameResRenamer.sln new file mode 100644 index 0000000..b8128b4 --- /dev/null +++ b/GameResRenamer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameResRenamer", "GameResRenamer\GameResRenamer.csproj", "{7B1A6FEF-F296-4157-9AED-910F2D103B09}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7B1A6FEF-F296-4157-9AED-910F2D103B09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7B1A6FEF-F296-4157-9AED-910F2D103B09}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7B1A6FEF-F296-4157-9AED-910F2D103B09}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7B1A6FEF-F296-4157-9AED-910F2D103B09}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DAE6982D-0CBE-4339-943E-2F5594201DCE} + EndGlobalSection +EndGlobal diff --git a/GameResRenamer/GameResRenamer.csproj b/GameResRenamer/GameResRenamer.csproj new file mode 100644 index 0000000..e02d510 --- /dev/null +++ b/GameResRenamer/GameResRenamer.csproj @@ -0,0 +1,27 @@ + + + + Exe + net6.0-windows10.0.22621.0 + enable + enable + 7.0 + 1.0.0 + Chenx221 + Chenx221 + + + + full + + + + full + + + + + + + + diff --git a/GameResRenamer/Program.cs b/GameResRenamer/Program.cs new file mode 100644 index 0000000..66be7d8 --- /dev/null +++ b/GameResRenamer/Program.cs @@ -0,0 +1,104 @@ +using Microsoft.Data.Sqlite; +using SQLitePCL; +using System.Security.Cryptography; + +string? folderPath = string.Empty;//资源所在位置 + +while (string.IsNullOrWhiteSpace(folderPath) || !Directory.Exists(folderPath)) +{ + Console.Write("请输入资源文件所在文件夹的路径:"); + folderPath = Console.ReadLine(); + + if (string.IsNullOrWhiteSpace(folderPath)) + { + Console.WriteLine("错误:路径不能为空。请重新输入。"); + } + else if (!Directory.Exists(folderPath)) + { + Console.WriteLine("错误:无法找到所提供的路径。请重新输入。"); + } +} + +string manifestDbPath = "manifest.db"; // 替换为你的SQLite数据库文件路径 +string connectionString = $"Data Source={manifestDbPath}"; + +using (SqliteConnection connection = new SqliteConnection(connectionString)) +{ + SQLitePCL.raw.SetProvider(new SQLite3Provider_e_sqlite3()); + connection.Open(); + + // 获取所有文件的MD5值 + Dictionary md5Values = new Dictionary(); + void GetFileMD5Values(string directoryPath) + { + string[] files = Directory.GetFiles(directoryPath); + + foreach (string file in files) + { + using (var md5 = MD5.Create()) + using (var stream = File.OpenRead(file)) + { + string md5OfFile = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); + md5Values.Add(file, md5OfFile); + } + } + + string[] subdirectories = Directory.GetDirectories(directoryPath); + + foreach (string subdirectory in subdirectories) + { + GetFileMD5Values(subdirectory); // 递归调用,处理子文件夹中的文件 + } + } + Console.WriteLine("正在计算md5,请稍后..."); + GetFileMD5Values(folderPath); + + // 获取所有文件的路径 + string[] filePaths = Directory.GetFiles(folderPath); + + int totalFiles = filePaths.Length; + + foreach (var kvp in md5Values) + { + string filePath = kvp.Key; + string md5OfFile = kvp.Value; + + // 查询数据库以获取文件名映射 + string query = $"SELECT k FROM t WHERE v = '{md5OfFile}'"; + + using (SqliteCommand command = new SqliteCommand(query, connection)) + using (SqliteDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + string newFileName = reader.GetString(0); + string newFilePath = Path.Combine(folderPath, newFileName); + string newRealFileName = Path.GetFileName(newFileName); + string? directoryPath = Path.GetDirectoryName(newFilePath); + + if (directoryPath == null) + { + return; + } + if (!File.Exists(Path.Combine(directoryPath, newRealFileName))) + { + // 重命名文件 + File.Move(filePath, Path.Combine(directoryPath, newRealFileName)); + Console.WriteLine($"已重命名文件:{newFilePath}"); + } + else + { + Console.WriteLine($"目标文件已存在:{newFilePath}"); + } + + } + else + { + Console.WriteLine($"无记录匹配:{filePath}"); + } + } + } +} + +Console.WriteLine("\n处理完成!"); +Console.ReadKey(); \ No newline at end of file