| Class | SwxAssembler |
| In: |
lib/swxruby/swx_assembler.rb
|
| Parent: | Object |
| UNCOMPRESSED_SWF | = | '46' | Header - FCS (uncompressed), version Flash 6 | |
| COMPRESSED_SWF | = | '43' | ||
| HEADER | = | '575306LLLLLLLL300A00A0000101004302FFFFFF' | ||
| NULL_TERMINATOR | = | '00' | Misc | |
| ALLOW_DOMAIN | = | '960900005F706172656E74001C960600005F75726C004E960D0007010000000053797374656D001C960A00007365637572697479004E960D0000616C6C6F77446F6D61696E005217' | Allow domain (*) | |
| SYSTEM_ALLOW_DOMAIN | = | '07010000000053797374656D001C960A00007365637572697479004E960D0000616C6C6F77446F6D61696E005217' | ||
| DEBUG_START | = | '883C000700726573756C74006C63004C6F63616C436F6E6E656374696F6E005F737778446562756767657200636F6E6E6563740064656275670073656E6400' | Debug SWX bytecode. Creates a local connection to the SWX Debugger front-end.) | |
| DEBUG_END | = | '960D0008010600000000000000000802403C9609000803070100000008011C9602000804521796020008001C960500070100000042960B0008050803070300000008011C96020008065217' |
# File lib/swxruby/swx_assembler.rb, line 26
26: def allow_domain_bytecode(allow_domain_url = '')
27: if (allow_domain_url.nil? || allow_domain_url.empty?)
28: # No URL passed -- possibly called by legacy code, use the old _parent._url version.
29: # error_log('[SWX] INFO: No URL passed from client. Defaulting to old behavior. You must call System.security.allowDomain on the dataHolder for cross domain data loading to work.');
30: ALLOW_DOMAIN
31: else
32: # Firefox/Flash (at least, and tested only on a Mac), sends
33: # file:/// (three slashses) in the URI and that fails the validation
34: # so replacing that with two slashes instead.
35: allow_domain_url.gsub!('///', '//')
36: # URL is passed, write that into the returned code
37: allow_domain_bytecode = BytecodeConverter.convert(URI.unescape(allow_domain_url))
38:
39: # The -13 is to accomodate the other elements being pushed to the
40: # stack in the hard-coded part of the bytecode.
41: allow_domain_bytecode_length_dec = allow_domain_bytecode.length/2 + 13
42:
43: allow_domain_bytecode_length = integer_to_hexadecimal(allow_domain_bytecode_length_dec, 2);
44: allow_domain_bytecode = '96' + allow_domain_bytecode_length + allow_domain_bytecode + SYSTEM_ALLOW_DOMAIN;
45: allow_domain_bytecode
46: end
47: end
# File lib/swxruby/swx_assembler.rb, line 49
49: def compress_swx_file(swx_file, compression_level)
50: # The first eight bytes of a compressed SWF file are left uncompressed
51: swx_file.slice!(0...8) + Zlib::Deflate.deflate(swx_file, compression_level)
52: end
# File lib/swxruby/swx_assembler.rb, line 54
54: def generate_data_bytecode(data)
55: data_bytecode = []
56:
57: # Add a flag to the beginning of the bytecode that tells Flash we're setting a variable (result)
58: data_bytecode.push ActionCodes::SET_VARIABLE
59:
60: # Convert the data (payload) to bytecode
61: data_bytecode.push BytecodeConverter.convert(data)
62:
63: # Generate a push tag if the data was not an Array or a Hash
64: data_bytecode.push generate_push_statement(data_bytecode) unless data.is_a?(Array) || data.is_a?(Hash)
65:
66: # Add the 'result' variable name -- either
67: # using the constant table if in debug mode
68: # or as a regular string otherwise
69: if @debug
70: data_bytecode.push '9602000800'
71: else
72: data_bytecode.push '96080000726573756C7400'
73: end
74:
75: # (SWF bytecode is stored in reverse, so we reverse it here)
76: data_bytecode.reverse.join
77: end
# File lib/swxruby/swx_assembler.rb, line 79
79: def generate_swx_bytecode(data)
80: # Create the DoAction tag
81: do_action_block = []
82:
83: # Wrap the data bytecode in debug flags if debugging is turned on
84: do_action_block.push DEBUG_START if @debug
85:
86: # Generate bytecode for the data (payload)
87: do_action_block.push generate_data_bytecode(data)
88:
89: # Allow domain? If so add allow domain statement to the SWF
90: if (@allow_domain)
91: do_action_block.push allow_domain_bytecode(@allow_domain_url)
92: end
93:
94: # Wrap the data bytecode in debug flags if debugging is turned on
95: do_action_block.push DEBUG_END if @debug
96:
97: # Calculate the size of the do_action block
98: do_action_block_size_in_bytes = string_length_in_bytes_hex(do_action_block.join, 4)
99: # Add the appropriate flags to the do_action block and concat the finished do_action block into a string
100: do_action_block_string = ActionCodes::DO_ACTION + do_action_block_size_in_bytes + do_action_block.join
101:
102: # Create the rest of the SWF
103: header_type = if @compression_level > 0 then COMPRESSED_SWF else UNCOMPRESSED_SWF end
104:
105: swf = header_type + HEADER + do_action_block_string + ActionCodes::SHOW_FRAME + ActionCodes::END_SWF
106: swf_size_in_bytes = string_length_in_bytes_hex(swf, 4)
107:
108: # Replace length placeholder (from HEADER constant) with actual bytecode length
109: swf.sub('LLLLLLLL', swf_size_in_bytes)
110: end
# File lib/swxruby/swx_assembler.rb, line 112
112: def write_swf(data, debug=false, compression_level=4, allow_domain_url='', allow_domain=true)
113: # Set up SwfAssembler state
114: @debug = debug
115: @compression_level = compression_level
116: @allow_domain_url = allow_domain_url
117: @allow_domain = allow_domain
118:
119: swx_bytecode = generate_swx_bytecode(data)
120:
121: # Convert the bytecode string to ASCII file format
122: swx_file = swx_bytecode.hex_to_ascii
123:
124: # Compress the file if compression is turned on
125: swx_file = compress_swx_file(swx_file, compression_level) if compression_level > 0
126:
127: # ====================================
128: # = TODO: Remove this before release =
129: # ====================================
130: # Write the file (for manual 'loadMovie' testing in Flash)
131: # File.open('/Users/Jed/Development/Libraries/rSWX/testing/flash/rswx_data.swx', 'w+') do |file|
132: # file << swx_file
133: # end
134:
135: swx_file
136: end