Skip to content

Instantly share code, notes, and snippets.

@jcward
Last active June 28, 2017 04:32
Show Gist options
  • Save jcward/106e68eb376db0c0649e to your computer and use it in GitHub Desktop.
Save jcward/106e68eb376db0c0649e to your computer and use it in GitHub Desktop.
RocketAMF can de/serialize AMF serialized byte streams, but it throws (at least, the 0.2.1 version from gem install throws) when it encounters an IExternalizable type in the byte steam. This patch allows it to parsed custom IExternalizable classes properly.
#!/usr/bin/env ruby
require 'rocketamf'
require 'stringio'
# Monkey patch for RocketAMF (0.2.1 gem) that handles IExrternalizable types
# in the input, storing their type as "__as3_type" parameter:
module RocketAMF
module Values #:nodoc:
class TypedHash
def externalized_data= obj
obj.each { |k,v|
self[k] = v
}
self["__as3_type"] = @type
end
end
end
end
if (ARGV.length==0) then
puts "usage: print_amf.rb <infile.amf>"
exit
end
# And a little command-line utility for printing AMF as JSON
require 'json'
puts JSON.pretty_generate RocketAMF.deserialize(File.read(ARGV[0]), 3)
#!/usr/bin/env ruby
# Utility to convert .json or .yaml files to .amf
require 'json'
require 'yaml'
require 'rocketamf'
infile = ARGV[0]
outfile = ARGV[1]
if (infile==nil || outfile==nil) then
puts "usage: to_amf.rb <json_or_yaml_file> <outfile.amf>"
exit
end
if (infile.match(/\.yaml$/)) then
File.open(outfile, 'wb') { |file| file.write RocketAMF.serialize(YAML.load(IO.read(infile)), 3) }
elsif (infile.match(/\.json$/)) then
File.open(outfile, 'wb') { |file| file.write RocketAMF.serialize(JSON.parse(IO.read(infile)), 3) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment