PowerShell GUI脚本保存按钮异常添加内容问题已解决
解决PowerShell GUI脚本编辑TF2 MVM Pop文件时重复追加内容的问题
问题场景
我开发了一款带GUI的PowerShell脚本,用于辅助编辑《军团要塞2》(TF2)MVM模式的pop文件。脚本已实现打开文件并在编辑区展示所有WaveSpawn区块的功能,但点击保存按钮时,会在保存修改后的WaveSpawn内容同时,额外重复写入原文件中WaveSpawn下方的所有内容,且无任何报错提示。
测试用Dummy Pop代码
用于复现问题的测试pop文件内容:
"PopFile" { "WaveSpawn" { "wave" "1" "spawn_type" "normal" "max_waves" "1" } "BossWave" { "boss" "giant_heavy" "wave" "2" } "FinalWave" { "end_map" "1" } }
问题根源与修复思路
原脚本的保存逻辑错误地将修改后的WaveSpawn内容与原文件全部内容合并写入,而非精准替换原文件中的WaveSpawn区块。修复核心是:
- 读取原文件完整内容,通过正则匹配定位所有
WaveSpawn区块 - 用编辑后的
WaveSpawn内容替换原区块,保留其他配置内容不变 - 确保最终仅将处理后的完整内容写入文件,避免重复追加
修正后的完整PowerShell脚本
Add-Type -AssemblyName System.Windows.Forms # 初始化主窗口 $mainForm = New-Object System.Windows.Forms.Form $mainForm.Text = "TF2 MVM Pop文件编辑器" $mainForm.Size = New-Object System.Drawing.Size(800, 600) $mainForm.StartPosition = "CenterScreen" # 打开文件按钮 $openBtn = New-Object System.Windows.Forms.Button $openBtn.Text = "打开Pop文件" $openBtn.Location = New-Object System.Drawing.Point(20, 20) $openBtn.Add_Click({ $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog $openFileDialog.Filter = "Pop Files (*.pop)|*.pop|所有文件 (*.*)|*.*" $openFileDialog.Title = "选择要编辑的Pop文件" if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $global:popFilePath = $openFileDialog.FileName $fileContent = Get-Content $popFilePath -Raw # 提取所有WaveSpawn区块内容 $waveSpawnPattern = '"WaveSpawn"\s*\{\s*[^}]*\s*\}' $waveSpawnMatches = [regex]::Matches($fileContent, $waveSpawnPattern, [System.Text.RegularExpressions.RegexOptions]::Singleline) $waveSpawnTextBox.Text = $waveSpawnMatches.Value -join "`n`n" } }) $mainForm.Controls.Add($openBtn) # WaveSpawn内容编辑框 $waveSpawnTextBox = New-Object System.Windows.Forms.TextBox $waveSpawnTextBox.Location = New-Object System.Drawing.Point(20, 60) $waveSpawnTextBox.Size = New-Object System.Drawing.Size(750, 300) $waveSpawnTextBox.Multiline = $true $waveSpawnTextBox.ScrollBars = "Vertical" $waveSpawnTextBox.AcceptsReturn = $true $waveSpawnTextBox.AcceptsTab = $true $mainForm.Controls.Add($waveSpawnTextBox) # 保存修改按钮 $saveBtn = New-Object System.Windows.Forms.Button $saveBtn.Text = "保存修改" $saveBtn.Location = New-Object System.Drawing.Point(20, 380) $saveBtn.Add_Click({ if (-not $global:popFilePath) { [System.Windows.Forms.MessageBox]::Show("请先打开一个Pop文件!", "操作提示", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning) return } $originalContent = Get-Content $global:popFilePath -Raw $modifiedWaveSpawn = $waveSpawnTextBox.Text.Trim() # 替换原文件中的所有WaveSpawn区块 $waveSpawnPattern = '"WaveSpawn"\s*\{\s*[^}]*\s*\}' $newContent = [regex]::Replace($originalContent, $waveSpawnPattern, $modifiedWaveSpawn, [System.Text.RegularExpressions.RegexOptions]::Singleline) # 写入文件(UTF-8编码避免乱码) try { $newContent | Set-Content $global:popFilePath -Encoding UTF8 -Force [System.Windows.Forms.MessageBox]::Show("修改已成功保存!", "操作成功", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information) } catch { [System.Windows.Forms.MessageBox]::Show("保存失败:$($_.Exception.Message)", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) } }) $mainForm.Controls.Add($saveBtn) # 启动窗口 $mainForm.ShowDialog() | Out-Null
内容的提问来源于stack exchange,提问作者Maselia
相关产品推荐
相关产品推荐

