Advertisement
Guest User

Untitled

a guest
May 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. require 'base64'
  2. require 'zlib'
  3. require 'json'
  4.  
  5. # Sample blueprint, one burner mining drill and one stone furnace
  6. blueprint = "0eNqF0N0KwjAMBeB3yXU7ZtkG9lVEZD9xBLpstJ04Rt/ddiIMRb1rCufrSVdozIyTJfagV6B2ZAf6tIKjnmuT7vwyIWggjwMI4HpIUzNbRisHYuJedpaMgSCAuMM76EMQfwXnR0Z5jU7d4i6rwlkAsidP+KyyDcuF56FBG/EvhIBpdDE1cnozSjLPSgFLPByyMqRKb5L6uc6H9+LypMWS2z5694ECbmjdFqiKQhXHXFVlFcIDjTN2Tg=="
  7.  
  8. # @param [String] blueprint
  9. # @return [Hash]
  10. def blueprint_to_hash(blueprint)
  11. JSON.parse(Zlib.inflate(Base64.decode64(blueprint.slice(1..-1))))
  12. end
  13.  
  14. # @param [Hash] data
  15. # @return [String]
  16. def hash_to_blueprint(data)
  17. "0#{Base64.encode64(Zlib.deflate(data.to_json, Zlib::BEST_COMPRESSION))}".gsub("\n", '')
  18. end
  19.  
  20. # pretty print
  21. require 'pp'
  22. pp blueprint_to_hash(blueprint)
  23. puts hash_to_blueprint(blueprint_to_hash(blueprint)) == blueprint
  24.  
  25. ##
  26. # Output:
  27. #
  28. # {"blueprint"=>
  29. # {"icons"=>
  30. # [{"signal"=>{"type"=>"item", "name"=>"burner-mining-drill"}, "index"=>1},
  31. # {"signal"=>{"type"=>"item", "name"=>"stone-furnace"}, "index"=>2}],
  32. # "entities"=>
  33. # [{"entity_number"=>1,
  34. # "name"=>"stone-furnace",
  35. # "position"=>{"x"=>-0.5, "y"=>-1.5}},
  36. # {"entity_number"=>2,
  37. # "name"=>"burner-mining-drill",
  38. # "position"=>{"x"=>0.5, "y"=>0.5}}],
  39. # "item"=>"blueprint",
  40. # "version"=>64424902656}}
  41. # true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement