import struct import shutil import dispatch from dispatch import strategy, functions, combiners, NoApplicableMethods from flashticle.actions import read_actions, pack_actions from flashticle.actions import read_clip_actions, serialize_clip_actions from flashticle.util import StringIO, BitReader, ZlibReader, read_string from flashticle.util import bitflag, Value, pack_string, iter_only from flashticle.util import BitWriter, ZlibWriter, writelines, msb_toint from flashticle.util import msb_fromstring LANGUAGECODES = { 1: 'Latin', 2: 'Japanese', 3: 'Korean', 4: 'Simplified Chinese', 5: 'Traditional Chinese', } TWIP = 0.05 class Signature(Value): def __init__(self, compressed, version, fileLength): self.compressed = compressed self.version = version self.fileLength = fileLength def __repr__(self): return '%s%r' % (type(self).__name__, (self.compressed, self.version, self.fileLength)) class Header(Value): def __init__(self, signature, frameSize, frameRate, frameCount): self.signature = signature self.frameSize = frameSize self.frameRate = frameRate self.frameCount = frameCount def __repr__(self): return '%s%r' % (type(self).__name__, (self.signature, self.frameSize, self.frameRate, self.frameCount)) class RGB(Value): def __init__(self, red, green, blue): self.red = red self.green = green self.blue = blue def __repr__(self): return '%s%r' % (type(self).__name__, (self.red, self.green, self.blue)) def read_rgb(f): return RGB(*map(ord, f.read(3))) def pack_rgb(self): return struct.pack('= 3: color = read_rgba(f) else: color = read_rgb(f) return FillStyleSolid(color) elif style == 0x10 or style == 0x12: br = BitReader(f) gradientMatrix = read_matrix(br) gradient = read_gradient(br) if style == 0x10: cls = FillStyleGradientLinear else: cls = FillStyleGradientRadial return cls(gradientMatrix, gradient) elif style == 0x40 or style == 0x41: bitmapID = struct.unpack('= 3: cpack = pack_rgba else: cpack = pack_rgb return chr(typ) + cpack(style.color) elif typ == 0x10 or typ == 0x12: bw = BitWriter() write_matrix(style.gradientMatrix, bw) write_gradient(style.gradient, bw) return chr(typ) + bw.tostring() elif typ == 0x40 or typ == 0x41: bw = BitWriter() write_matrix(style.bitmapMatrix, bw) return chr(typ) + struct.pack('= 3: color = read_rgba(f) else: color = read_rgb(f) return LineStyle(width, color) def pack_line_style(style, ctx): width = struct.pack('= 3: cpack = pack_rgba else: cpack = pack_rgb return width + cpack(style.color) def read_count_ex(f, read_one, ctx=None): count = ord(f.read(1)) if count == 0xFF: count = struct.unpack('> 6 length = codeAndLength & 0x3f if length == 0x3f: length = struct.unpack(')' % ( type(self).__name__, self.characterID, self.format, self.width, self.height, len(self.data)) @read_tag.when("code == DefineBitsLossless.code") def read_tag_DefineBitsLossless(code, length, f, header): characterID, fmt, w, h = struct.unpack(')' % ( type(self).__name__, self.characterID, self.format, self.width, self.height, len(self.data)) @read_tag.when("code == DefineBitsLossless2.code") def read_tag_DefineBitsLossless2(code, length, f, header): characterID, fmt, w, h = struct.unpack('= 2: colorTransform = read_color_transform_with_alpha(br) else: colorTransform = None return ButtonRecord(flags, characterID, placeDepth, placeMatrix, colorTransform) def read_button_records(f, ctx, header): records = [] while True: record = read_button_record(f, ctx) if record is None: break records.append(record) return records def write_button_record(f, r, ctx, header): f.write(struct.pack('= 2: write_color_transform_with_alpha(r.colorTransform, bw) bw.flush() def write_button_records(f, records, ctx, header): for record in records: write_button_record(f, record, ctx, header) f.write('\x00') class ButtonCondAction(Value): def __init__(self, conditionBits, keyCode, actions): self.conditionBits = conditionBits self.keyCode = keyCode self.actions = actions def __repr__(self): return '%s%r' % (type(self).__name__, ( self.conditionBits, self.keyCode, self.actions)) def read_button_cond_action(f, ctx, header): br = BitReader(f) conditionBits = br.readbits(8) keyCode = br.readUB(7) conditionBits.extend(br.readbits(1)) actions = list(read_actions(f, header)) return ButtonCondAction(conditionBits, keyCode, actions) def read_button_cond_actions(f, ctx, header): actions = [] while True: actionOffset = struct.unpack('