Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Created November 29, 2015 21:09
Show Gist options
  • Save DataKinds/590c0bb228f855f5e65c to your computer and use it in GitHub Desktop.
Save DataKinds/590c0bb228f855f5e65c to your computer and use it in GitHub Desktop.
Extract png spritesheets from gamemaker "data.win" files
#!/usr/bin/ruby
PNGHEADER = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
PNGENDING = [0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82]
pngHeaders = []
File.open("data.win", "rb") do |file|
puts "searching for png headers"
globalIndex = 0
headerInnerIndex = 0
file.each_byte do |byte|
if (byte == PNGHEADER[headerInnerIndex])
headerInnerIndex += 1
else
headerInnerIndex = 0
end
if (headerInnerIndex == PNGHEADER.length)
puts "png header found at index #{globalIndex - PNGHEADER.length + 1}, recording"
pngHeaders << globalIndex - PNGHEADER.length + 1
headerInnerIndex = 0
end
globalIndex += 1
end
puts "connecting headers with endings"
pngHeaders.each_with_index do |headerIndex, fileNum|
foundCorrespondingEnding = false
endingInnerIndex = 0
globalIndex = headerIndex
file.seek(globalIndex, IO::SEEK_SET)
File.open("#{sprintf("%05d", fileNum)}.png", "wb") do |outFile|
until foundCorrespondingEnding do
byte = file.readbyte
outFile.write(byte.chr)
if (byte == PNGENDING[endingInnerIndex])
endingInnerIndex += 1
else
endingInnerIndex = 0
end
if (endingInnerIndex == PNGENDING.length)
puts "png ending found at index #{globalIndex - PNGENDING.length + 1}, finishing write"
foundCorrespondingEnding = true
end
globalIndex += 1
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment