Last active
August 29, 2015 14:18
-
-
Save depili/20afa3ca0ccabd427d62 to your computer and use it in GitHub Desktop.
A small and unoptimized script to clean Rime and Frost traps from Pillars of Eternity MobileObjects.save files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
file = File.open(ARGV.last, 'rb') | |
chunks = Array.new | |
buffer = Array.new(10+4+1) | |
# Header is different, read until "b77a5c561934e089" | |
head_limiter = Array.new | |
"b77a5c561934e089".each_char do |c| | |
head_limiter << c.ord | |
end | |
head_limiter << 6 | |
# Chunks after the header | |
# We will read until we encounter the Root string in the next chunk. Then we will roll back that root and 10 bytes | |
# before it. | |
chunk_limiter = Array.new | |
"Root".each_char do |c| | |
chunk_limiter << c.ord | |
end | |
read = Array.new | |
got_header = false | |
$stdout.sync = true | |
file.each_byte do |b| | |
read << b | |
if (!got_header) && (read[-head_limiter.size, head_limiter.size] == head_limiter) | |
# Header | |
chunks[0] = read | |
got_header = true | |
read = Array.new | |
puts "Found the header: #{chunks[0].size} bytes." | |
puts "Now reading chunks:" | |
elsif (got_header) && (read.size > 20) && (read.last == chunk_limiter.last) && (read[-4,4] == chunk_limiter) | |
# Other chunks | |
next_chunk = read.pop(8) | |
chunks << read | |
read = next_chunk | |
print "." | |
end | |
end | |
chunk_total = chunks.size | |
puts "\nDeleting traps:" | |
chunks.delete_if do |chunk| | |
str = String.new | |
chunk.each do |b| | |
str << b.chr | |
end | |
if str.include? "Rime_and_Frost_Trap.prefab" | |
print "X" | |
true | |
else | |
print "." | |
false | |
end | |
end | |
puts "\nDeleted #{chunk_total - chunks.size} traps." | |
new_file = "#{ARGV.last}.cleaned" | |
puts "Writing altered save to file: #{new_file}" | |
File.open(new_file, 'wb') do |f| | |
chunks.each do |chunk| | |
chunk.each do |b| | |
f.putc b | |
end | |
print "." | |
end | |
end | |
puts "\nDone, may you have good luck loading it!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment