Guest User

Install.xml generator with grouping

a guest
Nov 5th, 2024
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Specified variable values
  2. $SourceFolder = "C:\Program Files (x86)\Steam\steamapps\common\Red Dead Redemption 2\lml\CharactersOverhaul"   # Folder with source files
  3. $TextFilePath = "C:\Program Files (x86)\Steam\steamapps\common\Red Dead Redemption 2\lml\CharactersOverhaul\source_paths.txt"   # Path to the text file with paths
  4.  
  5. # Define path to the log file and XML file in the same folder as the text file
  6. $LogFolder = Split-Path $TextFilePath
  7. $LogFile = Join-Path $LogFolder "log.log"   # Use .log extension
  8. $XmlFileName = Join-Path $LogFolder "EasyInstall.xml"
  9.  
  10. # Clear old log (ignore errors if the file doesn't exist)
  11. if (Test-Path $LogFile) {
  12.     Clear-Content $LogFile -ErrorAction SilentlyContinue
  13. }
  14.  
  15. # Read content of the text file into an array of strings
  16. $TextFileContent = Get-Content $TextFilePath
  17.  
  18. # Get a list of all files in the folder and subfolders
  19. $FilesInFolder = Get-ChildItem -Path $SourceFolder -File -Recurse
  20.  
  21. # Create XML file
  22. $XmlDocument = New-Object System.Xml.XmlDocument
  23.  
  24. # Create root element
  25. $RootElement = $XmlDocument.CreateElement("EasyInstall")
  26. $XmlDocument.AppendChild($RootElement)
  27.  
  28. # Add Name element (folder name)
  29. $NameElement = $XmlDocument.CreateElement("Name")
  30. $NameElement.InnerText = (Get-Item $SourceFolder).Name
  31. $RootElement.AppendChild($NameElement)
  32.  
  33. # Add Author element
  34. $AuthorElement = $XmlDocument.CreateElement("Author")
  35. $AuthorElement.InnerText = "werwolf969"
  36. $RootElement.AppendChild($AuthorElement)
  37.  
  38. # Add Version element
  39. $VersionElement = $XmlDocument.CreateElement("Version")
  40. $VersionElement.InnerText = "1.0"
  41. $RootElement.AppendChild($VersionElement)
  42.  
  43. # Create Resources section
  44. $ResourcesElement = $XmlDocument.CreateElement("Resources")
  45. $RootElement.AppendChild($ResourcesElement)
  46.  
  47. # Create Resource section
  48. $ResourceElement = $XmlDocument.CreateElement("Resource")
  49. $ResourcesElement.AppendChild($ResourceElement)
  50.  
  51. function Convert-FilePath {
  52.     param ([string]$FilePath)
  53.  
  54.     # Archive and mount point mappings
  55.     $Mapping = @{
  56.         "common_0.rpf" = "common:/"
  57.         "movies_0.rpf" = "common:/"
  58.         "anim_0.rpf" = "platform:/"
  59.         "data_0.rpf" = "platform:/"
  60.         "rowpack_0.rpf" = "platform:/"
  61.         "hd_0.rpf" = "platform:/"
  62.         "levels_0.rpf" = "platform:/"
  63.         "levels_1.rpf" = "platform:/"
  64.         "levels_2.rpf" = "platform:/"
  65.         "levels_3.rpf" = "platform:/"
  66.         "levels_4.rpf" = "platform:/"
  67.         "levels_5.rpf" = "platform:/"
  68.         "levels_6.rpf" = "platform:/"
  69.         "levels_7.rpf" = "platform:/"
  70.         "packs_0.rpf" = "platform:/"
  71.         "packs_1.rpf" = "platform:/"
  72.         "textures_0.rpf" = "platform:/"
  73.         "textures_1.rpf" = "platform:/"
  74.         "shaders_x64.rpf" = "common:/"
  75.         "update_2.rpf" = "update:/"
  76.         "update_3.rpf" = "update:/"
  77.         "update_4.rpf" = "update:/"
  78.         "update_1.rpf" = "update:/"
  79.         "audio_rel.rpf" = "audio:/"
  80.         "dlc_content_extra\dlc.rpf" = "dlc_content_extra:/"
  81.         "mp001\dlc.rpf" = "dlc_mp001:/"
  82.         "mp003\dlc.rpf" = "dlc_mp003:/"
  83.         "mp004\dlc.rpf" = "dlc_mp004:/"
  84.         "mp005\dlc.rpf" = "dlc_mp005:/"
  85.         "mp006\dlc.rpf" = "dlc_mp006:/"
  86.         "mp007\dlc.rpf" = "dlc_mp007:/"
  87.         "mp008\dlc.rpf" = "dlc_mp008:/"
  88.         "mp009\dlc.rpf" = "dlc_mp009:/"
  89.         "opt001\dlc.rpf" = "dlc_opt001:/"
  90.         "patchpack_mp001\dlc.rpf" = "dlc_patchpack_mp001:/"
  91.         "patchpack_mp003\dlc.rpf" = "dlc_patchpack_mp003:/"
  92.         "patchpack_mp006\dlc.rpf" = "dlc_patchpack_mp006:/"
  93.         "patchpack001\dlc.rpf" = "dlc_patchPack001:/"
  94.         "patchpack002\dlc.rpf" = "dlc_patchPack002:/"
  95.         "patchpack003\dlc.rpf" = "dlc_patchPack003:/"
  96.         "patchpack004\dlc.rpf" = "dlc_patchPack004:/"
  97.         "patchpack005\dlc.rpf" = "dlc_patchPack005:/"
  98.         "patchpack006\dlc.rpf" = "dlc_patchPack006:/"
  99.         "patchpack007\dlc.rpf" = "dlc_patchPack007:/"
  100.         "patchpack008\dlc.rpf" = "dlc_patchPack008:/"
  101.         "patchpack009\dlc.rpf" = "dlc_patchPack009:/"
  102.         "row_patchpack002\dlc.rpf" = "dlc_row_patchpack002:/"
  103.         "row_patchpack005\dlc.rpf" = "dlc_row_patchpack005:/"
  104.         "row_patchpack008\dlc.rpf" = "dlc_row_patchpack008:/"
  105.     }
  106.  
  107.     # Search for the longest matching key in the path
  108.     foreach ($key in $Mapping.Keys | Sort-Object Length -Descending) {
  109.         if ($FilePath -like "*$key*") {
  110.             $MountPoint = $Mapping[$key]
  111.             $RemainingPath = $FilePath -replace ".*$([regex]::Escape($key))", ""
  112.             $RemainingPath = $RemainingPath -replace '\\', '/' # Convert to forward slashes
  113.             if ($MountPoint -like "*/" -and $RemainingPath -like "/*") {
  114.                 $RemainingPath = $RemainingPath.TrimStart('/')
  115.             }
  116.             # Remove .rpf from the remaining path
  117.             $RemainingPath = $RemainingPath -replace '\.rpf', ''
  118.             return "$MountPoint$RemainingPath"
  119.         }
  120.     }
  121.     return $FilePath # If no mapping is found, return the original path
  122. }
  123.  
  124. # Convert paths and add unique FileReplacement elements
  125. $FileReplacementMap = @{}
  126.  
  127. foreach ($line in $TextFileContent) {
  128.     $FileName = [System.IO.Path]::GetFileName($line)
  129.     if (!$FileName) { continue }
  130.  
  131.     $MatchingFile = $FilesInFolder | Where-Object { $_.Name -eq $FileName } | Select-Object -First 1
  132.  
  133.     if ($MatchingFile) {
  134.         $TransformedPath = Convert-FilePath -FilePath $line
  135.         $RelativeFilePath = ($MatchingFile.FullName.Substring($SourceFolder.Length + 1) -replace '\\', '/').TrimStart('/')
  136.  
  137.         # Check if a FileReplacement already exists for this FilePath
  138.         if ($FileReplacementMap.ContainsKey($RelativeFilePath)) {
  139.             # Add a new GamePath to the existing FileReplacement if it's unique
  140.             $ExistingFileReplacement = $FileReplacementMap[$RelativeFilePath]
  141.             if (-not ($ExistingFileReplacement.ChildNodes | Where-Object { $_.InnerText -eq $TransformedPath })) {
  142.                 $NewGamePathElement = $XmlDocument.CreateElement("GamePath")
  143.                 $NewGamePathElement.InnerText = $TransformedPath
  144.                 $ExistingFileReplacement.InsertBefore($NewGamePathElement, $ExistingFileReplacement.LastChild)
  145.             }
  146.  
  147.             # Remove duplicate GamePath nodes
  148.             $GamePaths = $ExistingFileReplacement.SelectNodes("GamePath")
  149.             $UniqueGamePaths = $GamePaths | Sort-Object -Unique { $_.InnerText }
  150.             foreach ($GamePath in $GamePaths) {
  151.                 if (-not ($UniqueGamePaths -contains $GamePath)) {
  152.                 $ExistingFileReplacement.RemoveChild($GamePath)
  153.                 }
  154.             }
  155.  
  156.             # Set allowDuplicate="true" if there are multiple unique GamePaths
  157.             if ($UniqueGamePaths.Count -gt 1) {
  158.                 $ExistingFileReplacement.SetAttribute("allowDuplicate", "true")
  159.             } else {
  160.                 $ExistingFileReplacement.Attributes.RemoveNamedItem("allowDuplicate")
  161.             }
  162.         } else {
  163.             # Create a new FileReplacement element if none exists for this FilePath
  164.             $FileReplacementElement = $XmlDocument.CreateElement("FileReplacement")
  165.  
  166.             $GamePathElement = $XmlDocument.CreateElement("GamePath")
  167.             $GamePathElement.InnerText = $TransformedPath
  168.             $FileReplacementElement.AppendChild($GamePathElement)
  169.  
  170.             $FilePathElement = $XmlDocument.CreateElement("FilePath")
  171.             $FilePathElement.InnerText = $RelativeFilePath
  172.             $FileReplacementElement.AppendChild($FilePathElement)
  173.  
  174.             # Add the new FileReplacement element to the Resource
  175.             $ResourceElement.AppendChild($FileReplacementElement)
  176.             $FileReplacementMap[$RelativeFilePath] = $FileReplacementElement
  177.         }
  178.     } else {
  179.         # Log missing files
  180.         Add-Content -Path $LogFile -Value "Missing file: $FileName"
  181.     }
  182. }
  183.  
  184. # Save the XML document
  185. $XmlDocument.Save($XmlFileName)
  186. Write-Output "XML document saved to $XmlFileName"
  187.  
Advertisement
Add Comment
Please, Sign In to add comment