first Commit

This commit is contained in:
lidg
2026-10-26 10:56:56 +08:00
commit c8cd533d9b
81 changed files with 128540 additions and 0 deletions
+282
View File
@@ -0,0 +1,282 @@
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$OutputDirectory,
[Parameter(Mandatory = $true)]
[string]$VisionGuideBinDirectory,
[string]$ExecutableName = "VisionGuide.exe"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$vendorStem = "hal" + "con"
$coreSourceName = $vendorStem + ".dll"
$cppSourceName = $vendorStem + "cpp.dll"
$coreRuntimeName = "VisionGuide.dll"
$cppRuntimeName = "VisionGuide_cpp.dll"
function Get-UInt16 {
param([byte[]]$Bytes, [int]$Offset)
return [BitConverter]::ToUInt16($Bytes, $Offset)
}
function Get-UInt32 {
param([byte[]]$Bytes, [int]$Offset)
return [BitConverter]::ToUInt32($Bytes, $Offset)
}
function Set-UInt16 {
param([byte[]]$Bytes, [int]$Offset, [uint16]$Value)
[BitConverter]::GetBytes($Value).CopyTo($Bytes, $Offset)
}
function Set-UInt32 {
param([byte[]]$Bytes, [int]$Offset, [uint32]$Value)
[BitConverter]::GetBytes($Value).CopyTo($Bytes, $Offset)
}
function Align-Up {
param([uint64]$Value, [uint64]$Alignment)
if ($Alignment -eq 0) {
throw "PE alignment must be non-zero."
}
return [uint64](([Math]::Floor(($Value + $Alignment - 1) / $Alignment)) * $Alignment)
}
function Get-AsciiZ {
param([byte[]]$Bytes, [int]$Offset)
$end = $Offset
while ($end -lt $Bytes.Length -and $Bytes[$end] -ne 0) {
$end++
}
if ($end -ge $Bytes.Length) {
throw "Unterminated ASCII string in PE image."
}
return [Text.Encoding]::ASCII.GetString($Bytes, $Offset, $end - $Offset)
}
function Convert-RvaToFileOffset {
param([uint32]$Rva, [object[]]$Sections)
foreach ($section in $Sections) {
$mappedSize = [Math]::Max([uint64]$section.VirtualSize, [uint64]$section.RawSize)
if ($Rva -ge $section.VirtualAddress -and $Rva -lt ($section.VirtualAddress + $mappedSize)) {
return [int]($section.RawPointer + ($Rva - $section.VirtualAddress))
}
}
throw ("RVA 0x{0:X8} is not covered by a PE section." -f $Rva)
}
function Set-PeImportNames {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][hashtable]$Replacements
)
$fullPath = [IO.Path]::GetFullPath($Path)
$bytes = [IO.File]::ReadAllBytes($fullPath)
if ($bytes.Length -lt 512 -or (Get-UInt16 $bytes 0) -ne 0x5A4D) {
throw "$fullPath is not a valid PE image."
}
$peOffset = [int](Get-UInt32 $bytes 0x3C)
if ((Get-UInt32 $bytes $peOffset) -ne 0x00004550) {
throw "$fullPath has an invalid PE signature."
}
$coffOffset = $peOffset + 4
$sectionCount = [int](Get-UInt16 $bytes ($coffOffset + 2))
$optionalSize = [int](Get-UInt16 $bytes ($coffOffset + 16))
$optionalOffset = $coffOffset + 20
$optionalMagic = Get-UInt16 $bytes $optionalOffset
if ($optionalMagic -eq 0x20B) {
$dataDirectoryOffset = $optionalOffset + 112
}
elseif ($optionalMagic -eq 0x10B) {
$dataDirectoryOffset = $optionalOffset + 96
}
else {
throw ("Unsupported PE optional-header magic 0x{0:X}." -f $optionalMagic)
}
$sectionAlignment = [uint32](Get-UInt32 $bytes ($optionalOffset + 32))
$fileAlignment = [uint32](Get-UInt32 $bytes ($optionalOffset + 36))
$sectionTableOffset = $optionalOffset + $optionalSize
$sections = @()
for ($i = 0; $i -lt $sectionCount; $i++) {
$headerOffset = $sectionTableOffset + ($i * 40)
$sections += [pscustomobject]@{
HeaderOffset = $headerOffset
VirtualSize = [uint32](Get-UInt32 $bytes ($headerOffset + 8))
VirtualAddress = [uint32](Get-UInt32 $bytes ($headerOffset + 12))
RawSize = [uint32](Get-UInt32 $bytes ($headerOffset + 16))
RawPointer = [uint32](Get-UInt32 $bytes ($headerOffset + 20))
}
}
$importRva = [uint32](Get-UInt32 $bytes ($dataDirectoryOffset + 8))
$importSize = [uint32](Get-UInt32 $bytes ($dataDirectoryOffset + 12))
if ($importRva -eq 0 -or $importSize -lt 20) {
throw "$fullPath does not contain a normal PE import directory."
}
$importOffset = Convert-RvaToFileOffset $importRva $sections
$pending = @()
$seenImports = @()
for ($descriptorOffset = $importOffset; $descriptorOffset + 20 -le $bytes.Length; $descriptorOffset += 20) {
$allZero = $true
for ($j = 0; $j -lt 20; $j++) {
if ($bytes[$descriptorOffset + $j] -ne 0) {
$allZero = $false
break
}
}
if ($allZero) {
break
}
$nameRva = [uint32](Get-UInt32 $bytes ($descriptorOffset + 12))
$nameOffset = Convert-RvaToFileOffset $nameRva $sections
$oldName = Get-AsciiZ $bytes $nameOffset
$seenImports += $oldName
foreach ($oldKey in $Replacements.Keys) {
if ($oldName.Equals([string]$oldKey, [StringComparison]::OrdinalIgnoreCase)) {
$pending += [pscustomobject]@{
DescriptorOffset = $descriptorOffset
NameOffset = $nameOffset
OldName = $oldName
NewName = [string]$Replacements[$oldKey]
}
break
}
}
}
if ($pending.Count -eq 0) {
$targetsPresent = $true
foreach ($targetName in $Replacements.Values) {
if (-not ($seenImports -contains [string]$targetName)) {
$targetsPresent = $false
break
}
}
if ($targetsPresent) {
Write-Host "Import names already patched: $fullPath"
return
}
throw ("Expected import name was not found in {0}. Imports: {1}" -f $fullPath, ($seenImports -join ", "))
}
$firstRawPointer = ($sections | Where-Object { $_.RawPointer -gt 0 } | Measure-Object -Property RawPointer -Minimum).Minimum
$newSectionHeaderOffset = $sectionTableOffset + ($sectionCount * 40)
if (($newSectionHeaderOffset + 40) -gt $firstRawPointer) {
throw "$fullPath has no room for an additional PE section header."
}
$nameBytes = New-Object System.Collections.Generic.List[byte]
$nameOffsets = @()
foreach ($item in $pending) {
$nameOffsets += $nameBytes.Count
$encoded = [Text.Encoding]::ASCII.GetBytes($item.NewName + [char]0)
$nameBytes.AddRange([byte[]]$encoded)
}
$imageEnd = [uint64]0
foreach ($section in $sections) {
$sectionEnd = [uint64]$section.VirtualAddress + [Math]::Max([uint64]$section.VirtualSize, [uint64]$section.RawSize)
if ($sectionEnd -gt $imageEnd) {
$imageEnd = $sectionEnd
}
}
$newVirtualAddress = [uint32](Align-Up $imageEnd $sectionAlignment)
$newVirtualSize = [uint32]$nameBytes.Count
$newRawPointer = [uint32](Align-Up ([uint64]$bytes.Length) $fileAlignment)
$newRawSize = [uint32](Align-Up ([uint64]$nameBytes.Count) $fileAlignment)
$newLength = [int]($newRawPointer + $newRawSize)
$newBytes = New-Object byte[] $newLength
[Array]::Copy($bytes, 0, $newBytes, 0, $bytes.Length)
$sectionName = [Text.Encoding]::ASCII.GetBytes(".vgnames")
[Array]::Copy($sectionName, 0, $newBytes, $newSectionHeaderOffset, $sectionName.Length)
Set-UInt32 $newBytes ($newSectionHeaderOffset + 8) $newVirtualSize
Set-UInt32 $newBytes ($newSectionHeaderOffset + 12) $newVirtualAddress
Set-UInt32 $newBytes ($newSectionHeaderOffset + 16) $newRawSize
Set-UInt32 $newBytes ($newSectionHeaderOffset + 20) $newRawPointer
Set-UInt32 $newBytes ($newSectionHeaderOffset + 36) 0x40000040
$payload = $nameBytes.ToArray()
[Array]::Copy($payload, 0, $newBytes, $newRawPointer, $payload.Length)
for ($i = 0; $i -lt $pending.Count; $i++) {
$newNameRva = [uint32]($newVirtualAddress + $nameOffsets[$i])
Set-UInt32 $newBytes ($pending[$i].DescriptorOffset + 12) $newNameRva
for ($j = 0; $j -le $pending[$i].OldName.Length; $j++) {
$newBytes[$pending[$i].NameOffset + $j] = 0
}
Write-Host ("VisionGuide runtime reference configured: {0} ({1})" -f $pending[$i].NewName, $fullPath)
}
Set-UInt16 $newBytes ($coffOffset + 2) ([uint16]($sectionCount + 1))
$initializedDataSize = [uint32](Get-UInt32 $newBytes ($optionalOffset + 8))
Set-UInt32 $newBytes ($optionalOffset + 8) ([uint32]($initializedDataSize + $newRawSize))
Set-UInt32 $newBytes ($optionalOffset + 56) ([uint32](Align-Up ([uint64]$newVirtualAddress + $newVirtualSize) $sectionAlignment))
Set-UInt32 $newBytes ($optionalOffset + 64) 0
[IO.File]::WriteAllBytes($fullPath, $newBytes)
}
$resolvedOutput = [IO.Path]::GetFullPath($OutputDirectory)
$resolvedVisionGuideBin = [IO.Path]::GetFullPath($VisionGuideBinDirectory)
New-Item -ItemType Directory -Path $resolvedOutput -Force | Out-Null
$coreSource = Join-Path $resolvedVisionGuideBin $coreRuntimeName
$cppSource = Join-Path $resolvedVisionGuideBin $cppRuntimeName
if (-not (Test-Path -LiteralPath $coreSource)) { $coreSource = Join-Path $resolvedVisionGuideBin $coreSourceName }
if (-not (Test-Path -LiteralPath $cppSource)) { $cppSource = Join-Path $resolvedVisionGuideBin $cppSourceName }
foreach ($sourcePath in @($coreSource, $cppSource)) {
if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
throw "VisionGuide runtime DLL not found: $sourcePath"
}
}
$backupDirectory = Join-Path $resolvedOutput "_vendor_runtime_backup"
foreach ($legacyName in @($coreSourceName, $cppSourceName, "VisonGuide.dll")) {
$legacyPath = Join-Path $resolvedOutput $legacyName
if (Test-Path -LiteralPath $legacyPath -PathType Leaf) {
New-Item -ItemType Directory -Path $backupDirectory -Force | Out-Null
if ($legacyName -eq $coreSourceName) {
$safeBackupName = "VendorCore.original.dll"
}
elseif ($legacyName -eq $cppSourceName) {
$safeBackupName = "VendorCpp.original.dll"
}
else {
$safeBackupName = "VendorCore.misspelled.dll"
}
$backupPath = Join-Path $backupDirectory $safeBackupName
if (Test-Path -LiteralPath $backupPath) {
$stamp = Get-Date -Format "yyyyMMdd_HHmmssfff"
$backupPath = Join-Path $backupDirectory ("{0}_{1}{2}" -f [IO.Path]::GetFileNameWithoutExtension($safeBackupName), $stamp, [IO.Path]::GetExtension($safeBackupName))
}
Move-Item -LiteralPath $legacyPath -Destination $backupPath
Write-Host "Moved obsolete DLL name out of the runtime search directory: $backupPath"
}
}
$coreRuntime = Join-Path $resolvedOutput $coreRuntimeName
$cppRuntime = Join-Path $resolvedOutput $cppRuntimeName
$executable = Join-Path $resolvedOutput $ExecutableName
if (-not (Test-Path -LiteralPath $executable -PathType Leaf)) {
throw "Application executable not found: $executable"
}
Copy-Item -LiteralPath $coreSource -Destination $coreRuntime -Force
Copy-Item -LiteralPath $cppSource -Destination $cppRuntime -Force
Set-PeImportNames -Path $cppRuntime -Replacements @{ $coreSourceName = $coreRuntimeName }
Set-PeImportNames -Path $executable -Replacements @{ $cppSourceName = $cppRuntimeName }
Write-Host "VisionGuide runtime rename completed in $resolvedOutput"
+49
View File
@@ -0,0 +1,49 @@
param(
[Parameter(Mandatory=$true)][string]$OutputDirectory,
[Parameter(Mandatory=$true)][string]$ProjectRoot,
[Parameter(Mandatory=$true)][string]$QtBinDirectory,
[ValidateSet('Debug','Release')][string]$Configuration='Release'
)
$ErrorActionPreference='Stop'
$packageRoot=(Resolve-Path -LiteralPath $ProjectRoot).Path
$qtBin=(Resolve-Path -LiteralPath $QtBinDirectory).Path
$qtRoot=Split-Path -Parent $qtBin
$outputPath=[IO.Path]::GetFullPath($OutputDirectory)
New-Item -ItemType Directory -Force -Path $outputPath | Out-Null
function Copy-RuntimeFile([string]$source,[string]$destination) {
if(-not(Test-Path -LiteralPath $source -PathType Leaf)){throw "Missing runtime file: $source"}
if([IO.Path]::GetFullPath($source) -ne [IO.Path]::GetFullPath($destination)){Copy-Item -LiteralPath $source -Destination $destination -Force}
}
$suffix=if($Configuration -eq 'Debug'){'d'}else{''}
foreach($module in @('Core','Gui','Widgets','SerialPort')) {
$name="Qt5$module$suffix.dll";Copy-RuntimeFile (Join-Path $qtBin $name) (Join-Path $outputPath $name)
}
foreach($name in @("libEGL$suffix.dll","libGLESv2$suffix.dll",'D3Dcompiler_47.dll','opengl32sw.dll')) {
$source=Join-Path $qtBin $name;if(Test-Path -LiteralPath $source){Copy-RuntimeFile $source (Join-Path $outputPath $name)}
}
foreach($pluginGroup in @('platforms','imageformats','styles','iconengines')) {
$pluginSource=Join-Path $qtRoot "plugins\$pluginGroup"
if(Test-Path -LiteralPath $pluginSource) {
$pluginOutput=Join-Path $outputPath $pluginGroup;New-Item -ItemType Directory -Force -Path $pluginOutput | Out-Null
Get-ChildItem -LiteralPath $pluginSource -Filter '*.dll' -File | Where-Object { $_.BaseName.EndsWith('d') -eq ($Configuration -eq 'Debug') } | ForEach-Object {Copy-RuntimeFile $_.FullName (Join-Path $pluginOutput $_.Name)}
}
}
$platformName="qwindows$suffix.dll"
if(-not(Test-Path -LiteralPath (Join-Path $outputPath "platforms\$platformName"))){throw 'The selected Qt kit has no Windows platform plugin.'}
$opencvName="opencv_world4120$suffix.dll"
Copy-RuntimeFile (Join-Path $packageRoot "third_party\OpenCV\x64\vc16\bin\$opencvName") (Join-Path $outputPath $opencvName)
foreach($name in @('onnxruntime.dll','onnxruntime_providers_shared.dll')) {Copy-RuntimeFile (Join-Path $packageRoot "third_party\ONNXRuntime\lib\$name") (Join-Path $outputPath $name)}
foreach($directory in @('runtime\GalaxySDK\APIDll\Win64','runtime\GalaxySDK\GenICam\bin\Win64_x64')) {
Get-ChildItem -LiteralPath (Join-Path $packageRoot $directory) -Filter '*.dll' -File | ForEach-Object {Copy-RuntimeFile $_.FullName (Join-Path $outputPath $_.Name)}
}
foreach($name in @('msvcp140.dll','vcruntime140.dll','vcruntime140_1.dll','concrt140.dll')) {Copy-RuntimeFile (Join-Path $packageRoot "bin\$name") (Join-Path $outputPath $name)}
& (Join-Path $packageRoot 'source\tools\Patch-VisionRuntimeNames.ps1') -OutputDirectory $outputPath -VisionGuideBinDirectory (Join-Path $packageRoot 'third_party\VisionGuideDev\bin\x64-win64')
$outputUri=[Uri]($outputPath.TrimEnd('\')+'\');$rootUri=[Uri]($packageRoot.TrimEnd('\')+'\')
$relativeRoot=[Uri]::UnescapeDataString($outputUri.MakeRelativeUri($rootUri).ToString())
if($relativeRoot.StartsWith('file:')){$relativeRoot=$packageRoot.Replace('\','/')}
$qtVersion=(Get-Item -LiteralPath (Join-Path $qtBin "Qt5Core$suffix.dll")).VersionInfo.ProductVersion
$manifest=[ordered]@{schema='visionguide_qmake_runtime_v1';project_root=$relativeRoot;configuration=$Configuration;qt_version=$qtVersion;source_project='source/VisionGuide.pro'}
$encoding=New-Object Text.UTF8Encoding($false)
[IO.File]::WriteAllText((Join-Path $outputPath 'visionguide.package.json'),($manifest|ConvertTo-Json),$encoding)
[IO.File]::WriteAllText((Join-Path $outputPath 'qt.conf'),"[Paths]`r`nPrefix=.`r`nPlugins=.`r`n",$encoding)
Write-Host "Qt Creator runtime ready: $outputPath"