Last active
July 14, 2017 07:30
-
-
Save 1st8/17e1d942c376e82c6d63e0ac5291a85c to your computer and use it in GitHub Desktop.
Factorio blueprint parsing with ruby
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
require 'base64' | |
require 'zlib' | |
require 'json' | |
# Sample blueprint, one burner mining drill and one stone furnace | |
blueprint = "0eNqF0N0KwjAMBeB3yXU7ZtkG9lVEZD9xBLpstJ04Rt/ddiIMRb1rCufrSVdozIyTJfagV6B2ZAf6tIKjnmuT7vwyIWggjwMI4HpIUzNbRisHYuJedpaMgSCAuMM76EMQfwXnR0Z5jU7d4i6rwlkAsidP+KyyDcuF56FBG/EvhIBpdDE1cnozSjLPSgFLPByyMqRKb5L6uc6H9+LypMWS2z5694ECbmjdFqiKQhXHXFVlFcIDjTN2Tg==" | |
# @param [String] blueprint | |
# @return [Hash] | |
def blueprint_to_hash(blueprint) | |
JSON.parse(Zlib.inflate(Base64.decode64(blueprint.slice(1..-1)))) | |
end | |
# @param [Hash] data | |
# @return [String] | |
def hash_to_blueprint(data) | |
"0#{Base64.encode64(Zlib.deflate(data.to_json, Zlib::BEST_COMPRESSION))}".gsub("\n", '') | |
end | |
# pretty print | |
require 'pp' | |
pp blueprint_to_hash(blueprint) | |
puts hash_to_blueprint(blueprint_to_hash(blueprint)) == blueprint | |
## | |
# Output: | |
# | |
# {"blueprint"=> | |
# {"icons"=> | |
# [{"signal"=>{"type"=>"item", "name"=>"burner-mining-drill"}, "index"=>1}, | |
# {"signal"=>{"type"=>"item", "name"=>"stone-furnace"}, "index"=>2}], | |
# "entities"=> | |
# [{"entity_number"=>1, | |
# "name"=>"stone-furnace", | |
# "position"=>{"x"=>-0.5, "y"=>-1.5}}, | |
# {"entity_number"=>2, | |
# "name"=>"burner-mining-drill", | |
# "position"=>{"x"=>0.5, "y"=>0.5}}], | |
# "item"=>"blueprint", | |
# "version"=>64424902656}} | |
# true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment