BMW_coding/tools/navigation_lookup_generator.ps1
2024-07-12 12:50:20 +02:00

232 lines
9.2 KiB
PowerShell

# Created by David Petric
# This script parses Map informations and helps to create new entry in Lookup.xml used for FSC generators
# https://davidpetric.com
# Set variables
$INFO_MAP_FILENAME = "Info_Map.imp"
$VERBOSE = $false
$GENERATEXML = $false
$MAP_FOLDER = ""
$ARCHIVE_FILE = ""
# Function to install prerequisites
function Install-Prerequisites {
Write-Host "Checking for prerequisites..."
# Check for 7-Zip installation
if (-not (Get-Command "7z" -ErrorAction SilentlyContinue)) {
Write-Host "7-Zip not found. Installing 7-Zip..."
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7z1900-x64.msi" -OutFile "$env:TEMP\7z1900-x64.msi"
Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $env:TEMP\7z1900-x64.msi /quiet" -Wait
Remove-Item "$env:TEMP\7z1900-x64.msi"
Write-Host "7-Zip installed."
} else {
Write-Host "7-Zip is already installed."
}
# Check for hexdump installation
if (-not (Get-Command "hexdump" -ErrorAction SilentlyContinue)) {
Write-Host "Hexdump not found. Installing hexdump..."
$hexdumpZipUrl = "https://www.di-mgt.com.au/src/hexdump-2.1.0.zip"
$hexdumpZipPath = "$env:TEMP\hexdump-2.1.0.zip"
$hexdumpExtractPath = "C:\binaries"
Invoke-WebRequest -Uri $hexdumpZipUrl -OutFile $hexdumpZipPath
Expand-Archive -Path $hexdumpZipPath -DestinationPath $hexdumpExtractPath -Force
Remove-Item $hexdumpZipPath
# Ensure the directory is in the PATH
$env:Path += ";$hexdumpExtractPath"
# Update system PATH
[System.Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
Write-Host "Hexdump installed and added to PATH."
} else {
Write-Host "Hexdump is already installed."
}
}
# Functions declaration
function Parse-InfoMapFile {
param (
[string]$InfoMapFile
)
if ($VERBOSE) { Write-Host "Parsing file..." -NoNewline }
$hexDump = hexdump -C $InfoMapFile
$MAP_NAME = ($hexDump | Select-String -Pattern "\|" | ForEach-Object { $_.Line.Split('|')[1].Trim() }).Substring(20)
$hexDumpUpper = $hexDump | ForEach-Object { $_.ToUpper() }
$MAP_SWID_FscShort = ($hexDumpUpper[0] -split ' ')[9..12] -join ''
$MAP_REGION = "0x$($hexDumpUpper[0].Split(' ')[10])"
$MAP_YEAR = "0x$($hexDumpUpper[0].Split(' ')[12])"
if ($VERBOSE) { Write-Host " [OK]" }
}
function Show-MapInfos {
Write-Host " "
Write-Host "===================== Map Informations =========================" -ForegroundColor Yellow
Write-Host "MapName : `"$MAP_NAME`""
Write-Host "SWID_FscShort: `"$MAP_SWID_FscShort`""
Write-Host "MapRegion : `"$MAP_REGION`""
Write-Host "MapYear : `"$MAP_YEAR`""
Write-Host "================================================================" -ForegroundColor Yellow
Write-Host " "
}
function Show-LookupXMLEntry {
$LONG_MAP_NAME = "$MAP_NAME (SWID_FscShort=$MAP_SWID_FscShort, MapRegion=$MAP_REGION, MapYear=$MAP_YEAR)"
Write-Host "===================== XML Generation ===========================" -ForegroundColor Yellow
Write-Host "Add this entry at the end of the maps list of your Lookup.xml file :"
Write-Host "
<-- From the line below -->
<SgbmId id=`"NAVD_000024A8_255_007_034`" SWID_FscShort=`"$MAP_SWID_FscShort`" name=`"$MAP_NAME (SWID_FscShort=$MAP_SWID_FscShort, MapRegion=$MAP_REGION, MapYear=$MAP_YEAR)`" supplier=`"NTQ`" sop=`"01.05.2021`" version=`"10_09`" MapOrderNumberBMW=`"`" MapOrderNumberMINI=`"`" MapOrderNumberRR=`"`" successorMapOrderNumberBMW=`"`" successorMapOrderNumberMINI=`"`" successorMapOrderNumberRR=`"`" NextNo=`"01826`">
<EcuVariant CompatibilityIdentifier=`"NBTevo-HB`" Format=`"CIC`" />
<SwUpdate SwUpdateEntry=`"`"/>
</SgbmId>
<-- To the upper line --> "
Write-Host " "
Write-Host "You will find a new map called $LONG_MAP_NAME in other tools ;-)" -ForegroundColor Yellow
Write-Host "As is you will have all informations in one place." -ForegroundColor Yellow
Write-Host "================================================================" -ForegroundColor Yellow
}
function Process-ArchiveFile {
if ($ARCHIVE_FILE) {
if (Test-Path $ARCHIVE_FILE) {
if ($VERBOSE) { Write-Host "File archive exists" }
$PATH_TO_INFOMAP_FILE = 7z l "$ARCHIVE_FILE" | Select-String -Pattern "$INFO_MAP_FILENAME" -NotMatch "LIGHT" | ForEach-Object { $_.Line.Split()[3] }
if ($VERBOSE) {
Write-Host "Archive content ..."
7z l "$ARCHIVE_FILE"
}
if ($VERBOSE) { Write-Host "Looking for $INFO_MAP_FILENAME file into archive... [OK] - $PATH_TO_INFOMAP_FILE" }
# Get filename extension
$extension = [System.IO.Path]::GetExtension($ARCHIVE_FILE).Trim('.').ToLower()
if ($VERBOSE) {
Write-Host "Archive extension is : $extension"
Write-Host "OS says : $(file $ARCHIVE_FILE)"
}
# Switch case
if ($extension -eq "7z") {
Write-Host "Please note that only one file will be extracted from archive, so do not worry about spacedisk."
Write-Host " "
Write-Host "Please wait a little bit (i.e. 2 or 3 minutes) while we are looking for the file in a 7zip archive!" -ForegroundColor Yellow
7z e "$ARCHIVE_FILE" -o"/tmp" "$PATH_TO_INFOMAP_FILE" -y
} else {
7z e "$ARCHIVE_FILE" -o"/tmp" "$PATH_TO_INFOMAP_FILE" -y > $null
}
# Verifying presence of Info_Map.imp file on disk
if ($VERBOSE) { Write-Host "Verifying presence of Info_Map.imp file on disk" }
if (Test-Path "/tmp/$INFO_MAP_FILENAME") {
if ($VERBOSE) { Write-Host (ls -l "/tmp/$INFO_MAP_FILENAME") }
$INFOMAP_FILE = "/tmp/$INFO_MAP_FILENAME"
} else {
Write-Host "ERROR : $INFO_MAP_FILENAME file not extracted in /tmp"
exit 2
}
} else {
Write-Host "ERROR : $ARCHIVE_FILE file does not exist"
exit 2
}
} else {
Write-Host "Function Process-ArchiveFile called but variable ARCHIVE_FILE is missing"
exit 99 # Internal error (i.e. BUG)
}
}
function Set-WorkDir {
param (
[string]$MapFolder
)
if (Test-Path -PathType Container $MapFolder) {
Set-Location $MapFolder
if ($VERBOSE) { Write-Host "Using Map folder: $MapFolder ... [OK]" }
} else {
Write-Host "ERROR the given folder in argument doesn't exist"
exit 2
}
}
function Process-Folder {
param (
[string]$MapFolder
)
if ($VERBOSE) { Write-Host "Processing in folder $MapFolder ..." }
if (Test-Path "./1") {
$MAP_DIR = Get-Location
if ($VERBOSE) { Write-Host "We are inside Map folder: $MapFolder ... [OK]" }
if ($VERBOSE) { Write-Host "Looking for InfoMap file ($INFO_MAP_FILENAME)" -NoNewline }
$INFOMAP_FILE = Get-ChildItem -Path 1/INFO*/$INFO_MAP_FILENAME -Exclude *LIGHT* -ErrorAction SilentlyContinue
if (!$INFOMAP_FILE) {
$ERROR = "Cannot find $INFO_MAP_FILENAME file in folder(s) 1/INFO*"
Write-Host "`n $ERROR`n"
exit 2
}
} else {
Write-Host " "
Write-Host "---------------------------------- [ERROR] -----------------------------------------------" -ForegroundColor Red
Write-Host "We are not in a map folder (or try to delete spaces/special characters in folder name/path)" -ForegroundColor Red
Write-Host " "
exit 2
}
}
############# Program starts from here #############
# Install prerequisites
Install-Prerequisites
# Prompt the user to select options and input necessary paths
Write-Host "Select the mode of operation:"
Write-Host "1. Process a map folder"
Write-Host "2. Process an archive file"
$mode = Read-Host "Enter the number corresponding to your choice"
switch ($mode) {
1 {
$MAP_FOLDER = Read-Host "Enter the path to the map folder"
$VERBOSE = Read-Host "Enable verbose mode? (yes/no)" -eq "yes"
$GENERATEXML = Read-Host "Generate XML entry? (yes/no)" -eq "yes"
if ($MAP_FOLDER) {
if ($VERBOSE) { Write-Host "Switching to directory $MAP_FOLDER" }
Set-WorkDir -MapFolder $MAP_FOLDER
Process-Folder -MapFolder $MAP_FOLDER
} else {
Write-Host "No map folder path provided. Exiting..."
exit 1
}
}
2 {
$ARCHIVE_FILE = Read-Host "Enter the path to the archive file"
$VERBOSE = Read-Host "Enable verbose mode? (yes/no)" -eq "yes"
$GENERATEXML = Read-Host "Generate XML entry? (yes/no)" -eq "yes"
if ($ARCHIVE_FILE) {
if ($VERBOSE) { Write-Host "Looking into archive file $ARCHIVE_FILE" }
Process-ArchiveFile
} else {
Write-Host "No archive file path provided. Exiting..."
exit 1
}
}
default {
Write-Host "Invalid selection. Exiting..."
exit 1
}
}
# InfoMap.imp file found
if ($VERBOSE) { Write-Host " $INFOMAP_FILE - [OK]" }
Parse-InfoMapFile -InfoMapFile $INFOMAP_FILE
Show-MapInfos
# Generate XML entry to add in Lookup.xml
if ($GENERATEXML) { Show-LookupXMLEntry }