begin organize textures FlatColors merge

This commit is contained in:
Nathan
2026-01-09 12:05:08 -07:00
parent 9b6b461c22
commit 47a05bf4f2
3 changed files with 11507 additions and 356 deletions

View File

@@ -1,6 +1,6 @@
{ {
"dailyFormat": "YYYY-MM-DD", "dailyFormat": "YYYY-MM-DD",
"structDir": "D:\\0 ProjectStructure", "structDir": "A:\\1 Amazon_Active_Projects\\3 ProjectStructure",
"zipper": "7z", "zipper": "7z",
"compression": 0, "compression": 0,
"Max7zInst": 0 "Max7zInst": 0

View File

@@ -21,6 +21,9 @@ if (-not (Test-Path -Path $textureFolderPath -PathType Container)) {
$textureFolderPath = (Resolve-Path $textureFolderPath).ProviderPath $textureFolderPath = (Resolve-Path $textureFolderPath).ProviderPath
Write-Host "Processing texture folder: $textureFolderPath" -ForegroundColor Cyan Write-Host "Processing texture folder: $textureFolderPath" -ForegroundColor Cyan
# Add required .NET assemblies for image processing (for FlatColors standardization)
Add-Type -AssemblyName System.Drawing
# Function to calculate checksums for files # Function to calculate checksums for files
function Get-FilesWithChecksums { function Get-FilesWithChecksums {
param( param(
@@ -266,6 +269,81 @@ function Process-DuplicateGroup {
} }
} }
# Function to extract and normalize hex color code from filename (for FlatColors)
function Get-NormalizedColorCode {
param([string]$FileName)
# Remove extension
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($FileName)
# Match hex color pattern: #RRGGBB or #RRGGBBAA (with optional suffix like _1, _2, etc.)
# Pattern matches: #RRGGBB, #RRGGBBAA, or just RRGGBB (without #)
# Also handles suffixes like _1, _2, etc.
if ($baseName -match '^(#?)([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?') {
$hasHash = $Matches[1] -eq "#"
$hexCode = $Matches[2].ToUpper()
# Return normalized format: #RRGGBB (always with #, always uppercase, no suffix)
return "#$hexCode"
}
return $null
}
# Function to convert and resize image to 16x16 JPG (for FlatColors)
function Convert-To16x16Jpg {
param(
[string]$SourcePath,
[string]$DestinationPath
)
try {
$image = [System.Drawing.Image]::FromFile($SourcePath)
# Create 16x16 bitmap
$bitmap = New-Object System.Drawing.Bitmap(16, 16)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
# Draw resized image
$graphics.DrawImage($image, 0, 0, 16, 16)
# Get JPEG codec
$jpegCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq "image/jpeg" }
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, 95)
# Save as JPG
$bitmap.Save($DestinationPath, $jpegCodec, $encoderParams)
# Cleanup
$graphics.Dispose()
$bitmap.Dispose()
$image.Dispose()
$encoderParams.Dispose()
return $true
} catch {
Write-Warning "Failed to convert image: $SourcePath - $($_.Exception.Message)"
return $false
}
}
# Function to check if image is 16x16 (for FlatColors)
function Test-ImageSize {
param([string]$ImagePath)
try {
$image = [System.Drawing.Image]::FromFile($ImagePath)
$is16x16 = ($image.Width -eq 16 -and $image.Height -eq 16)
$image.Dispose()
return $is16x16
} catch {
return $false
}
}
# ============================================================================ # ============================================================================
# PASS 1: Intra-Blendfile Processing # PASS 1: Intra-Blendfile Processing
# ============================================================================ # ============================================================================
@@ -419,20 +497,34 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
$pass2Duplicates += $result.DuplicateCount $pass2Duplicates += $result.DuplicateCount
} }
# Process FlatColors files: merge duplicates and move to \common\FlatColors # Process FlatColors files: standardize to 16x16 JPG (except TGA), merge duplicates by color code
Write-Host "Processing FlatColors files (merging duplicates)..." -ForegroundColor Yellow Write-Host "Processing FlatColors files (standardizing and merging duplicates)..." -ForegroundColor Yellow
if ($null -ne $flatColorsFiles -and $flatColorsFiles.Count -gt 0) { if ($null -ne $flatColorsFiles -and $flatColorsFiles.Count -gt 0) {
Write-Host "Found $($flatColorsFiles.Count) FlatColors file(s) to process" -ForegroundColor Gray Write-Host "Found $($flatColorsFiles.Count) FlatColors file(s) to process" -ForegroundColor Gray
# Calculate checksums for FlatColors files # Group files by normalized color code (instead of checksum)
Write-Host "Calculating checksums for FlatColors files..." -ForegroundColor Yellow Write-Host "Grouping FlatColors files by color code..." -ForegroundColor Yellow
$flatColorsWithChecksums = Get-FilesWithChecksums -Files $flatColorsFiles $filesByColor = @{}
# Group by checksum to find duplicates foreach ($file in $flatColorsFiles) {
$flatColorsGroupedByChecksum = $flatColorsWithChecksums | Group-Object -Property Hash if (-not (Test-Path -Path $file.FullName)) {
continue
}
$colorCode = Get-NormalizedColorCode -FileName $file.Name
if ($null -eq $colorCode) {
Write-Warning "Could not extract color code from: $($file.Name)"
continue
}
if (-not $filesByColor.ContainsKey($colorCode)) {
$filesByColor[$colorCode] = @()
}
$filesByColor[$colorCode] += $file
}
Write-Host "Found $($flatColorsGroupedByChecksum.Count) unique FlatColors file(s)" -ForegroundColor Gray Write-Host "Found $($filesByColor.Count) unique color code(s)" -ForegroundColor Gray
# Create \textures\common\FlatColors directory # Create \textures\common\FlatColors directory
$flatColorsPath = Join-Path -Path $rootCommonPath -ChildPath "FlatColors" $flatColorsPath = Join-Path -Path $rootCommonPath -ChildPath "FlatColors"
@@ -445,64 +537,185 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
$filesInFlatColors = @{} $filesInFlatColors = @{}
$flatColorsMoved = 0 $flatColorsMoved = 0
$flatColorsDuplicates = 0 $flatColorsDuplicates = 0
$flatColorsConverted = 0
$flatColorsResized = 0
$flatColorsMoveLog = [System.Collections.ArrayList]::new() $flatColorsMoveLog = [System.Collections.ArrayList]::new()
foreach ($group in $flatColorsGroupedByChecksum) { foreach ($colorCode in $filesByColor.Keys) {
$files = $group.Group $files = $filesByColor[$colorCode]
if ($files.Count -eq 1) { # Find the best file to keep (prefer existing JPG, then TGA, then others)
# Single file - move to FlatColors folder $fileToKeep = $null
$fileObj = $files[0].File $targetExtension = ".jpg"
$fileName = $fileObj.Name $targetFileName = "$colorCode.jpg"
# Strip blendfile prefix if present # First, check if there's already a correct file in target location
$strippedName = Get-FileNameWithoutPrefix -FileName $fileName -ValidPrefixes $validBlendfilePrefixes $existingTarget = Join-Path -Path $flatColorsPath -ChildPath $targetFileName
if ($strippedName -ne $fileName) { if (Test-Path -Path $existingTarget) {
$fileName = $strippedName $existingFile = Get-Item -Path $existingTarget
# Check if it's already correct
if ($existingFile.Extension -eq ".jpg" -and (Test-ImageSize -ImagePath $existingFile.FullName)) {
$fileToKeep = $existingFile
} }
}
# If no existing correct file, find best source file
if ($null -eq $fileToKeep) {
# Prefer existing JPG file
$jpgFile = $files | Where-Object {
($_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg") -and
(Test-Path -Path $_.FullName)
} | Select-Object -First 1
$destinationPath = Join-Path -Path $flatColorsPath -ChildPath $fileName if ($jpgFile) {
$fileToKeep = $jpgFile
# Handle name conflicts (shouldn't happen for unique files, but just in case) } else {
if ($filesInFlatColors.ContainsKey($fileName) -or (Test-Path -Path $destinationPath)) { # If no JPG, prefer TGA
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName) $tgaFile = $files | Where-Object {
$extension = [System.IO.Path]::GetExtension($fileName) $_.Extension -eq ".tga" -and
$counter = 1 (Test-Path -Path $_.FullName)
do { } | Select-Object -First 1
$newFileName = "${baseName}_${counter}${extension}"
$destinationPath = Join-Path -Path $flatColorsPath -ChildPath $newFileName
$counter++
} while ($filesInFlatColors.ContainsKey($newFileName) -or (Test-Path -Path $destinationPath))
$fileName = $newFileName
}
try {
$originalPath = $fileObj.FullName
Move-Item -Path $originalPath -Destination $destinationPath -Force
$filesInFlatColors[$fileName] = $true
$flatColorsMoved++
# Log the move if ($tgaFile) {
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath) $fileToKeep = $tgaFile
$normalizedNew = [System.IO.Path]::GetFullPath($destinationPath) $targetExtension = ".tga"
$null = $flatColorsMoveLog.Add([PSCustomObject]@{ $targetFileName = "$colorCode.tga"
OriginalPath = $normalizedOriginal } else {
NewPath = $normalizedNew # Otherwise, use the first available file
Type = "moved" $fileToKeep = $files | Where-Object { Test-Path -Path $_.FullName } | Select-Object -First 1
}) }
} catch { }
Write-Warning "Failed to move FlatColors file: $($fileObj.FullName) - $($_.Exception.Message)" }
if ($null -eq $fileToKeep) {
Write-Warning " $colorCode: No valid file found to process"
continue
}
$targetPath = Join-Path -Path $flatColorsPath -ChildPath $targetFileName
# If target is TGA, preserve as-is (don't convert)
if ($targetExtension -eq ".tga") {
if ($fileToKeep.FullName -ne $targetPath) {
# Move TGA to target location
try {
if (Test-Path -Path $targetPath) {
Remove-Item -Path $targetPath -Force
}
$originalPath = $fileToKeep.FullName
Move-Item -Path $originalPath -Destination $targetPath -Force
$filesInFlatColors[$targetFileName] = $true
$flatColorsMoved++
# Log the move
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath)
$normalizedNew = [System.IO.Path]::GetFullPath($targetPath)
$null = $flatColorsMoveLog.Add([PSCustomObject]@{
OriginalPath = $normalizedOriginal
NewPath = $normalizedNew
Type = "moved"
})
} catch {
Write-Warning " $colorCode: Failed to move TGA: $($_.Exception.Message)"
}
} }
} else { } else {
# Multiple files with same checksum (duplicates) - merge them # Target is JPG - convert/resize if needed
# Use Process-DuplicateGroup to handle merging $needsConversion = $false
$result = Process-DuplicateGroup -Files $files -CommonPath $flatColorsPath -FilesInCommon $filesInFlatColors -StripPrefix -ValidPrefixes $validBlendfilePrefixes -MoveLog $flatColorsMoveLog $needsResize = $false
$flatColorsMoved += $result.MovedCount
$flatColorsDuplicates += $result.DuplicateCount if ($fileToKeep.Extension -ne ".jpg" -and $fileToKeep.Extension -ne ".jpeg") {
$needsConversion = $true
}
if (-not (Test-ImageSize -ImagePath $fileToKeep.FullName)) {
$needsResize = $true
}
# Check if target already exists and is correct
if ((Test-Path -Path $targetPath) -and
(Get-Item -Path $targetPath).Extension -eq ".jpg" -and
(Test-ImageSize -ImagePath $targetPath)) {
# Target already exists and is correct, skip
} elseif ($needsConversion -or $needsResize -or $fileToKeep.FullName -ne $targetPath) {
# Convert/resize to target
if (Convert-To16x16Jpg -SourcePath $fileToKeep.FullName -DestinationPath $targetPath) {
if ($needsConversion -and $needsResize) {
$flatColorsConverted++
$flatColorsResized++
} elseif ($needsConversion) {
$flatColorsConverted++
} elseif ($needsResize) {
$flatColorsResized++
} else {
$flatColorsMoved++
}
$filesInFlatColors[$targetFileName] = $true
# Log the conversion/move
$normalizedOriginal = [System.IO.Path]::GetFullPath($fileToKeep.FullName)
$normalizedNew = [System.IO.Path]::GetFullPath($targetPath)
$null = $flatColorsMoveLog.Add([PSCustomObject]@{
OriginalPath = $normalizedOriginal
NewPath = $normalizedNew
Type = "moved"
})
} else {
Write-Warning " $colorCode: Failed to convert/resize"
continue
}
} else {
# File is already correct format and size
if ($fileToKeep.FullName -ne $targetPath) {
try {
if (Test-Path -Path $targetPath) {
Remove-Item -Path $targetPath -Force
}
$originalPath = $fileToKeep.FullName
Move-Item -Path $originalPath -Destination $targetPath -Force
$filesInFlatColors[$targetFileName] = $true
$flatColorsMoved++
# Log the move
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath)
$normalizedNew = [System.IO.Path]::GetFullPath($targetPath)
$null = $flatColorsMoveLog.Add([PSCustomObject]@{
OriginalPath = $normalizedOriginal
NewPath = $normalizedNew
Type = "moved"
})
} catch {
Write-Warning " $colorCode: Failed to move: $($_.Exception.Message)"
}
}
}
}
# Delete duplicate files (all files in the group except the target)
foreach ($file in $files) {
if ($file.FullName -ne $targetPath -and (Test-Path -Path $file.FullName)) {
try {
$originalPath = $file.FullName
Remove-Item -Path $originalPath -Force
$flatColorsDuplicates++
# Log the deletion
$normalizedOriginal = [System.IO.Path]::GetFullPath($originalPath)
$normalizedReplacement = [System.IO.Path]::GetFullPath($targetPath)
$null = $flatColorsMoveLog.Add([PSCustomObject]@{
OriginalPath = $normalizedOriginal
ReplacedBy = $normalizedReplacement
Type = "deleted"
})
} catch {
Write-Warning " Failed to delete duplicate: $($file.FullName) - $($_.Exception.Message)"
}
}
} }
} }
Write-Host "Moved $flatColorsMoved unique FlatColors file(s) to \common\FlatColors, deleted $flatColorsDuplicates duplicate(s)" -ForegroundColor Green Write-Host "Processed FlatColors: $flatColorsMoved moved, $flatColorsConverted converted, $flatColorsResized resized, $flatColorsDuplicates duplicate(s) deleted" -ForegroundColor Green
} else { } else {
Write-Host "No FlatColors files found to process." -ForegroundColor Gray Write-Host "No FlatColors files found to process." -ForegroundColor Gray
} }