This commit is contained in:
Chenx221 2023-09-04 17:48:15 +08:00
parent ab64ecdabe
commit e66fe05f53
3 changed files with 156 additions and 0 deletions

25
GameResRenamer.sln Normal file
View File

@ -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

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<Version>1.0.0</Version>
<Company>Chenx221</Company>
<Authors>Chenx221</Authors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.10" />
</ItemGroup>
</Project>

104
GameResRenamer/Program.cs Normal file
View File

@ -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<string, string> md5Values = new Dictionary<string, string>();
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();