83 lines
4.0 KiB
PowerShell
83 lines
4.0 KiB
PowerShell
# Updates the scripts inside Fishing.rbxlx from src/ WITHOUT touching the map.
|
|
# Studio must be closed. A backup is saved to backups\ first.
|
|
# Run: powershell -ExecutionPolicy Bypass -File update-place.ps1
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$root = $PSScriptRoot
|
|
$enc = New-Object System.Text.UTF8Encoding($false)
|
|
$placePath = Join-Path $root 'Fishing.rbxlx'
|
|
|
|
if (Test-Path "$placePath.lock") { throw "Roblox Studio has Fishing.rbxlx open. Close Studio first." }
|
|
|
|
# src folder -> service class in the place
|
|
$services = [ordered]@{
|
|
'ReplicatedFirst' = 'ReplicatedFirst'
|
|
'ReplicatedStorage' = 'ReplicatedStorage'
|
|
'ServerScriptService' = 'ServerScriptService'
|
|
'StarterPlayer\StarterPlayerScripts' = 'StarterPlayerScripts'
|
|
}
|
|
|
|
$scripts = @()
|
|
foreach ($rel in $services.Keys) {
|
|
$dir = Join-Path "$root\src" $rel
|
|
if (-not (Test-Path $dir)) { continue }
|
|
Get-ChildItem $dir -File -Filter *.lua | ForEach-Object {
|
|
$n = $_.Name
|
|
if ($n -like '*.server.lua') { $class = 'Script'; $name = $n -replace '\.server\.lua$', '' }
|
|
elseif ($n -like '*.client.lua') { $class = 'LocalScript'; $name = $n -replace '\.client\.lua$', '' }
|
|
else { $class = 'ModuleScript'; $name = $n -replace '\.lua$', '' }
|
|
$src = [System.IO.File]::ReadAllText($_.FullName, $enc)
|
|
if ($src.Contains(']]>') -or $src.Contains('</Properties>')) { throw "Unsupported text in $n" }
|
|
$scripts += @{ Name = $name; Class = $class; Service = $services[$rel]; Source = $src }
|
|
}
|
|
}
|
|
|
|
# backup
|
|
$backupDir = Join-Path $root 'backups'
|
|
New-Item -ItemType Directory -Force $backupDir | Out-Null
|
|
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
|
|
Copy-Item $placePath (Join-Path $backupDir "Fishing-$stamp.rbxlx")
|
|
|
|
$xml = [System.IO.File]::ReadAllText($placePath, $enc)
|
|
|
|
foreach ($s in $scripts) {
|
|
# all items of this class with this exact name
|
|
$pattern = '<Item class="' + $s.Class + '" referent="[^"]+">\s*<Properties>'
|
|
$found = @()
|
|
foreach ($m in [regex]::Matches($xml, $pattern)) {
|
|
$propsEnd = $xml.IndexOf('</Properties>', $m.Index)
|
|
$seg = $xml.Substring($m.Index, $propsEnd - $m.Index)
|
|
if ($seg -match '<string name="Name">([^<]*)</string>' -and $Matches[1] -eq $s.Name) {
|
|
$found += @{ Start = $m.Index; PropsEnd = $propsEnd }
|
|
}
|
|
}
|
|
if ($found.Count -gt 1) { throw "Several $($s.Class) named $($s.Name) in the place - not touching it." }
|
|
|
|
$cdata = '<![CDATA[' + $s.Source + ']]>'
|
|
if ($found.Count -eq 1) {
|
|
$f = $found[0]
|
|
$seg = $xml.Substring($f.Start, $f.PropsEnd - $f.Start)
|
|
$srcTag = [regex]::Match($seg, '<ProtectedString name="Source">[\s\S]*?</ProtectedString>')
|
|
$newTag = '<ProtectedString name="Source">' + $cdata + '</ProtectedString>'
|
|
if ($srcTag.Success) {
|
|
$absStart = $f.Start + $srcTag.Index
|
|
$xml = $xml.Substring(0, $absStart) + $newTag + $xml.Substring($absStart + $srcTag.Length)
|
|
} else {
|
|
$xml = $xml.Substring(0, $f.PropsEnd) + $newTag + $xml.Substring($f.PropsEnd)
|
|
}
|
|
Write-Host "updated $($s.Service)/$($s.Name)"
|
|
} else {
|
|
# new script: put it right after the service's own <Properties> block
|
|
$svc = [regex]::Match($xml, '<Item class="' + $s.Service + '" referent="[^"]+">\s*<Properties>')
|
|
if (-not $svc.Success) { throw "Service $($s.Service) not found in the place." }
|
|
$insertAt = $xml.IndexOf('</Properties>', $svc.Index) + '</Properties>'.Length
|
|
$ref = 'RBX' + ([guid]::NewGuid().ToString('N').ToUpper())
|
|
$item = "`n<Item class=`"$($s.Class)`" referent=`"$ref`"><Properties><string name=`"Name`">$($s.Name)</string><ProtectedString name=`"Source`">$cdata</ProtectedString></Properties></Item>"
|
|
$xml = $xml.Substring(0, $insertAt) + $item + $xml.Substring($insertAt)
|
|
Write-Host "added $($s.Service)/$($s.Name)"
|
|
}
|
|
}
|
|
|
|
[System.IO.File]::WriteAllText($placePath, $xml, $enc)
|
|
Write-Host "Done. Backup: backups\Fishing-$stamp.rbxlx"
|