ChocolateBird/scripts/loaders/common_gba.gd

37 lines
866 B
GDScript

extends Node
func ByteArray(size: int) -> PoolByteArray:
var arr := PoolByteArray()
arr.resize(size)
return arr
func LZ77_decompress(rom: File, address: int) -> PoolByteArray:
rom.seek(address)
var header := rom.get_32()
assert (header & 0x10 == 0x10)
var length := header >> 8
var output := ByteArray(length)
var ptr := 0
while ptr < length:
var bitmap := rom.get_8()
for i in range(8):
if (bitmap >> (7-i)) & 1:
# Buffer substitution
var h1 := rom.get_8()
var h2 := rom.get_8()
var copy_len := 3 + (h1 >> 4)
var copy_ptr := ptr - 1 - (((h1 & 0x0F)<<8) + h2)
for j in range(copy_len):
output[ptr] = output[copy_ptr]
copy_ptr += 1
ptr += 1
if ptr >= length:
return output
else:
# Literal byte
output[ptr] = rom.get_8()
ptr += 1
if ptr >= length:
return output
return output