355 lines
10 KiB
PowerShell
355 lines
10 KiB
PowerShell
# ========================================
|
||
# UE插件引擎版本自动同步工具
|
||
# 自动检测项目使用的UE版本并同步插件版本
|
||
# 作者: AI Assistant
|
||
# 版本: 2.0.0
|
||
# ========================================
|
||
|
||
param(
|
||
[Parameter(HelpMessage="项目路径")]
|
||
[string]$ProjectPath = "E:\UE5\TG_ARPG",
|
||
|
||
[Parameter(HelpMessage="仅检查不更新")]
|
||
[switch]$CheckOnly,
|
||
|
||
[Parameter(HelpMessage="显示详细信息")]
|
||
[switch]$ShowDetails,
|
||
|
||
[Parameter(HelpMessage="强制指定目标版本(覆盖自动检测)")]
|
||
[string]$ForceVersion
|
||
)
|
||
|
||
function Write-Header {
|
||
param([string]$Title)
|
||
Write-Host "`n========================================" -ForegroundColor Cyan
|
||
Write-Host " $Title" -ForegroundColor Cyan
|
||
Write-Host "========================================`n" -ForegroundColor Cyan
|
||
}
|
||
|
||
function Write-Success {
|
||
param([string]$Message)
|
||
Write-Host "✅ $Message" -ForegroundColor Green
|
||
}
|
||
|
||
function Write-Warning {
|
||
param([string]$Message)
|
||
Write-Host "⚠️ $Message" -ForegroundColor Yellow
|
||
}
|
||
|
||
function Write-Error {
|
||
param([string]$Message)
|
||
Write-Host "❌ $Message" -ForegroundColor Red
|
||
}
|
||
|
||
function Write-Info {
|
||
param([string]$Message)
|
||
Write-Host "ℹ️ $Message" -ForegroundColor Blue
|
||
}
|
||
|
||
function Write-Highlight {
|
||
param([string]$Message)
|
||
Write-Host "🔹 $Message" -ForegroundColor Magenta
|
||
}
|
||
|
||
# 获取项目使用的引擎版本
|
||
function Get-ProjectEngineVersion {
|
||
param([string]$ProjectPath)
|
||
|
||
$uprojectFile = Join-Path $ProjectPath "*.uproject"
|
||
$projectFile = Get-ChildItem -Path $uprojectFile | Select-Object -First 1
|
||
|
||
if (-not $projectFile) {
|
||
Write-Error "未找到.uproject文件"
|
||
return $null
|
||
}
|
||
|
||
Write-Info "项目文件: $($projectFile.Name)"
|
||
|
||
try {
|
||
$content = Get-Content -Path $projectFile.FullName -Raw -ErrorAction Stop
|
||
|
||
# 尝试读取EngineAssociation
|
||
if ($content -match '"EngineAssociation":\s*"([^"]+)"') {
|
||
$engineAssoc = $Matches[1]
|
||
Write-Info "引擎关联: $engineAssoc"
|
||
|
||
# 如果是路径格式
|
||
if ($engineAssoc -match '^[A-Za-z]:\\') {
|
||
# 处理转义的路径
|
||
$enginePath = $engineAssoc -replace '\\\\', '\'
|
||
return Get-EngineVersionFromPath -EnginePath $enginePath
|
||
}
|
||
# 如果是Epic Launcher安装的版本 (如 "5.8", "5.7")
|
||
elseif ($engineAssoc -match '^\d+\.\d+$') {
|
||
Write-Info "检测到Epic Launcher版本: $engineAssoc"
|
||
return "$engineAssoc.0"
|
||
}
|
||
else {
|
||
Write-Warning "无法识别的引擎关联格式: $engineAssoc"
|
||
return $null
|
||
}
|
||
}
|
||
else {
|
||
Write-Warning "未找到EngineAssociation字段"
|
||
return $null
|
||
}
|
||
}
|
||
catch {
|
||
Write-Error "读取项目文件失败: $($_.Exception.Message)"
|
||
return $null
|
||
}
|
||
}
|
||
|
||
# 从引擎路径获取版本号
|
||
function Get-EngineVersionFromPath {
|
||
param([string]$EnginePath)
|
||
|
||
$buildVersionPath = Join-Path $EnginePath "Engine\Build\Build.version"
|
||
|
||
if (-not (Test-Path $buildVersionPath)) {
|
||
Write-Warning "未找到Build.version文件: $buildVersionPath"
|
||
return $null
|
||
}
|
||
|
||
try {
|
||
$buildInfo = Get-Content -Path $buildVersionPath -Raw | ConvertFrom-Json
|
||
$version = "$($buildInfo.MajorVersion).$($buildInfo.MinorVersion).$($buildInfo.PatchVersion)"
|
||
Write-Info "引擎路径: $EnginePath"
|
||
Write-Info "引擎版本: $version"
|
||
return $version
|
||
}
|
||
catch {
|
||
Write-Error "读取引擎版本失败: $($_.Exception.Message)"
|
||
return $null
|
||
}
|
||
}
|
||
|
||
# 获取插件的引擎版本
|
||
function Get-PluginEngineVersion {
|
||
param([string]$PluginFile)
|
||
|
||
try {
|
||
$content = Get-Content -Path $PluginFile -Raw -ErrorAction Stop
|
||
if ($content -match '"EngineVersion":\s*"([^"]+)"') {
|
||
return $Matches[1]
|
||
}
|
||
return $null
|
||
}
|
||
catch {
|
||
return $null
|
||
}
|
||
}
|
||
|
||
# 更新插件引擎版本
|
||
function Update-PluginEngineVersion {
|
||
param(
|
||
[string]$PluginFile,
|
||
[string]$TargetVersion
|
||
)
|
||
|
||
try {
|
||
$content = Get-Content -Path $PluginFile -Raw -ErrorAction Stop
|
||
$newContent = $content -replace '"EngineVersion":\s*"[^"]+"', "`"EngineVersion`": `"$TargetVersion`""
|
||
|
||
if ($content -ne $newContent) {
|
||
Set-Content -Path $PluginFile -Value $newContent -Encoding UTF8 -ErrorAction Stop
|
||
return $true
|
||
}
|
||
return $false
|
||
}
|
||
catch {
|
||
Write-Error "更新失败: $($_.Exception.Message)"
|
||
return $false
|
||
}
|
||
}
|
||
|
||
# 比较版本号
|
||
function Compare-EngineVersion {
|
||
param(
|
||
[string]$CurrentVersion,
|
||
[string]$TargetVersion
|
||
)
|
||
|
||
try {
|
||
$current = [Version]$CurrentVersion
|
||
$target = [Version]$TargetVersion
|
||
|
||
if ($current -lt $target) {
|
||
return "Older" # 当前版本较旧,需要升级
|
||
}
|
||
elseif ($current -gt $target) {
|
||
return "Newer" # 当前版本较新,可能需要降级
|
||
}
|
||
else {
|
||
return "Equal" # 版本相同
|
||
}
|
||
}
|
||
catch {
|
||
return "Unknown" # 无法比较
|
||
}
|
||
}
|
||
|
||
# ========================================
|
||
# 主程序
|
||
# ========================================
|
||
|
||
Write-Header "UE插件引擎版本自动同步工具"
|
||
|
||
# 验证项目路径
|
||
if (-not (Test-Path $ProjectPath)) {
|
||
Write-Error "项目路径不存在: $ProjectPath"
|
||
exit 1
|
||
}
|
||
|
||
Write-Highlight "步骤1: 检测项目引擎版本"
|
||
Write-Host ""
|
||
|
||
# 获取目标版本
|
||
$targetVersion = $null
|
||
if ($ForceVersion) {
|
||
$targetVersion = $ForceVersion
|
||
Write-Info "使用强制指定版本: $targetVersion"
|
||
}
|
||
else {
|
||
$targetVersion = Get-ProjectEngineVersion -ProjectPath $ProjectPath
|
||
}
|
||
|
||
if (-not $targetVersion) {
|
||
Write-Error "无法检测项目引擎版本"
|
||
Write-Host "`n您可以使用 -ForceVersion 参数手动指定版本,例如:" -ForegroundColor Yellow
|
||
Write-Host " .\UE插件版本自动同步工具.ps1 -ForceVersion `"5.8.0`"" -ForegroundColor Gray
|
||
exit 1
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Success "检测到目标版本: $targetVersion"
|
||
Write-Host ""
|
||
|
||
# 检查插件目录
|
||
$PluginsPath = Join-Path $ProjectPath "Plugins"
|
||
if (-not (Test-Path $PluginsPath)) {
|
||
Write-Error "插件目录不存在: $PluginsPath"
|
||
exit 1
|
||
}
|
||
|
||
Write-Highlight "步骤2: 扫描插件"
|
||
Write-Host ""
|
||
|
||
$pluginFiles = Get-ChildItem -Path $PluginsPath -Recurse -Filter "*.uplugin" -ErrorAction SilentlyContinue
|
||
|
||
if (-not $pluginFiles) {
|
||
Write-Warning "未找到任何插件"
|
||
exit 0
|
||
}
|
||
|
||
Write-Info "找到 $($pluginFiles.Count) 个插件"
|
||
Write-Host ""
|
||
|
||
# 统计
|
||
$stats = @{
|
||
Updated = 0
|
||
AlreadyLatest = 0
|
||
NoVersion = 0
|
||
Failed = 0
|
||
Incompatible = 0
|
||
Total = $pluginFiles.Count
|
||
}
|
||
|
||
Write-Highlight "步骤3: 分析并更新插件"
|
||
Write-Host ""
|
||
|
||
# 处理每个插件
|
||
foreach ($pluginFile in $pluginFiles) {
|
||
$pluginName = $pluginFile.BaseName
|
||
$pluginPath = $pluginFile.FullName
|
||
|
||
$currentVersion = Get-PluginEngineVersion -PluginFile $pluginPath
|
||
|
||
# 显示插件信息
|
||
Write-Host "[$pluginName] " -NoNewline -ForegroundColor White
|
||
|
||
if ($null -eq $currentVersion) {
|
||
if ($ShowDetails) {
|
||
Write-Host "未指定引擎版本" -ForegroundColor Gray
|
||
}
|
||
else {
|
||
Write-Host "跳过 (无版本号)" -ForegroundColor Gray
|
||
}
|
||
$stats.NoVersion++
|
||
continue
|
||
}
|
||
|
||
Write-Host "当前: $currentVersion" -NoNewline
|
||
|
||
# 比较版本
|
||
$comparison = Compare-EngineVersion -CurrentVersion $currentVersion -TargetVersion $targetVersion
|
||
|
||
switch ($comparison) {
|
||
"Equal" {
|
||
Write-Host " ✓ 已同步" -ForegroundColor Green
|
||
$stats.AlreadyLatest++
|
||
}
|
||
"Older" {
|
||
Write-Host " -> 需要升级到: $targetVersion" -ForegroundColor Yellow
|
||
|
||
if (-not $CheckOnly) {
|
||
$success = Update-PluginEngineVersion -PluginFile $pluginPath -TargetVersion $targetVersion
|
||
if ($success) {
|
||
Write-Success " 已更新"
|
||
$stats.Updated++
|
||
}
|
||
else {
|
||
Write-Error " 更新失败"
|
||
$stats.Failed++
|
||
}
|
||
}
|
||
else {
|
||
Write-Info " (检查模式 - 未更新)"
|
||
}
|
||
}
|
||
"Newer" {
|
||
Write-Host " -> 警告: 插件版本较新!" -ForegroundColor Red
|
||
Write-Warning " 插件版本($currentVersion)高于项目版本($targetVersion)"
|
||
$stats.Incompatible++
|
||
|
||
if (-not $CheckOnly) {
|
||
$response = Read-Host " 是否降级到 $targetVersion? (Y/N)"
|
||
if ($response -eq 'Y' -or $response -eq 'y') {
|
||
$success = Update-PluginEngineVersion -PluginFile $pluginPath -TargetVersion $targetVersion
|
||
if ($success) {
|
||
Write-Success " 已降级"
|
||
$stats.Updated++
|
||
}
|
||
else {
|
||
Write-Error " 降级失败"
|
||
$stats.Failed++
|
||
}
|
||
}
|
||
}
|
||
}
|
||
"Unknown" {
|
||
Write-Host " -> 无法比较版本" -ForegroundColor Gray
|
||
$stats.NoVersion++
|
||
}
|
||
}
|
||
}
|
||
|
||
# 输出统计
|
||
Write-Header "同步统计"
|
||
|
||
Write-Host "项目引擎版本: " -NoNewline
|
||
Write-Host $targetVersion -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "总插件数: $($stats.Total)" -ForegroundColor White
|
||
Write-Host "已同步: $($stats.AlreadyLatest)" -ForegroundColor Green
|
||
Write-Host "已更新: $($stats.Updated)" -ForegroundColor Blue
|
||
Write-Host "版本较新: $($stats.Incompatible)" -ForegroundColor Red
|
||
Write-Host "无版本号: $($stats.NoVersion)" -ForegroundColor Gray
|
||
Write-Host "更新失败: $($stats.Failed)" -ForegroundColor Red
|
||
|
||
if ($CheckOnly) {
|
||
Write-Host "`n💡 提示: 去掉 -CheckOnly 参数即可执行实际更新" -ForegroundColor Yellow
|
||
}
|
||
|
||
Write-Host "`n✨ 完成!`n" -ForegroundColor Green
|