Files
neoLegacy/tools/structures/nbt.ps1
T
Tranqlmao ff562d1644 TU46 Test content and patches
TU46 Test content, added missing commits by amending commit and cherry-picking to new branch, ready for iggy guis and tu46 bug fixes
2026-10-22 13:55:39 -04:00

38 lines
2.1 KiB
PowerShell

Add-Type -AssemblyName System.IO.Compression
$script:buf = $null
$script:pos = 0
function RdU8 { $v = $script:buf[$script:pos]; $script:pos += 1; return [int]$v }
function RdI16 { $v = ([int]$script:buf[$script:pos] -shl 8) -bor $script:buf[$script:pos+1]; $script:pos += 2; if ($v -ge 32768) { $v -= 65536 }; return $v }
function RdI32 { $b = $script:buf; $p = $script:pos; $v = ([int64]$b[$p] -shl 24) -bor ([int64]$b[$p+1] -shl 16) -bor ([int64]$b[$p+2] -shl 8) -bor [int64]$b[$p+3]; $script:pos += 4; if ($v -ge 2147483648) { $v -= 4294967296 }; return [int]$v }
function RdI64 { $hi = RdI32; $lo = RdI32; return ([int64]$hi -shl 32) -bor ([int64]$lo -band 0xFFFFFFFF) }
function RdStr { $n = (RdI16) -band 0xFFFF; $s = [System.Text.Encoding]::UTF8.GetString($script:buf, $script:pos, $n); $script:pos += $n; return $s }
function RdPayload([int]$type) {
switch ($type) {
1 { $v = RdU8; if ($v -ge 128) { $v -= 256 }; return $v }
2 { return RdI16 }
3 { return RdI32 }
4 { return RdI64 }
5 { $i = RdI32; return [BitConverter]::ToSingle([BitConverter]::GetBytes($i), 0) }
6 { $l = RdI64; return [BitConverter]::Int64BitsToDouble($l) }
7 { $n = RdI32; $a = New-Object byte[] $n; [Array]::Copy($script:buf, $script:pos, $a, 0, $n); $script:pos += $n; return ,$a }
8 { return RdStr }
9 { $et = RdU8; $n = RdI32; $list = New-Object System.Collections.ArrayList; for ($i = 0; $i -lt $n; $i++) { [void]$list.Add((RdPayload $et)) }; return ,$list }
10 { $h = [ordered]@{}; while ($true) { $t = RdU8; if ($t -eq 0) { break }; $name = RdStr; $h[$name] = RdPayload $t }; return $h }
11 { $n = RdI32; $a = @(); for ($i = 0; $i -lt $n; $i++) { $a += RdI32 }; return ,$a }
}
throw "bad tag $type"
}
function Read-Nbt([string]$path) {
$fs = [System.IO.File]::OpenRead($path)
$gz = New-Object System.IO.Compression.GZipStream($fs, [System.IO.Compression.CompressionMode]::Decompress)
$ms = New-Object System.IO.MemoryStream
$gz.CopyTo($ms); $gz.Close(); $fs.Close()
$script:buf = $ms.ToArray(); $script:pos = 0
$t = RdU8; $null = RdStr
return RdPayload $t
}