Class: SwfRuby::SpriteReplaceTarget

Inherits:
ReplaceTarget show all
Defined in:
lib/swf_ruby/replace_target.rb

Instance Attribute Summary collapse

Attributes inherited from ReplaceTarget

#offset

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset, swf) ⇒ SpriteReplaceTarget

Returns a new instance of SpriteReplaceTarget.



35
36
37
38
39
40
41
42
43
# File 'lib/swf_ruby/replace_target.rb', line 35

def initialize(offset, swf)
  @offset = offset
  @swf = swf
  @target_swf_dumper = SwfDumper.new.dump(@swf)
  @frame_count = @target_swf_dumper.header.frame_count
  @define_tags = @target_swf_dumper.tags.select { |t| t.define_tag? }
  @control_tags = @target_swf_dumper.tags - @define_tags
  @idmap = {}
end

Instance Attribute Details

#control_tagsObject

Returns the value of attribute control_tags.



30
31
32
# File 'lib/swf_ruby/replace_target.rb', line 30

def control_tags
  @control_tags
end

#define_tagsObject

Returns the value of attribute define_tags.



29
30
31
# File 'lib/swf_ruby/replace_target.rb', line 29

def define_tags
  @define_tags
end

#frame_countObject

Returns the value of attribute frame_count.



28
29
30
# File 'lib/swf_ruby/replace_target.rb', line 28

def frame_count
  @frame_count
end

#idmapObject

Returns the value of attribute idmap.



31
32
33
# File 'lib/swf_ruby/replace_target.rb', line 31

def idmap
  @idmap
end

#swfObject

Returns the value of attribute swf.



27
28
29
# File 'lib/swf_ruby/replace_target.rb', line 27

def swf
  @swf
end

#target_control_tags_stringObject

Returns the value of attribute target_control_tags_string.



33
34
35
# File 'lib/swf_ruby/replace_target.rb', line 33

def target_control_tags_string
  @target_control_tags_string
end

#target_define_tags_stringObject

Returns the value of attribute target_define_tags_string.



32
33
34
# File 'lib/swf_ruby/replace_target.rb', line 32

def target_define_tags_string
  @target_define_tags_string
end

Class Method Details

.build_by_instance_var_name(swf_dumper, var_name, swf, from_character_id = nil) ⇒ Object

指定したインスタンス変数名に対するSpriteReplaceTargetを生成する

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/swf_ruby/replace_target.rb', line 56

def self.build_by_instance_var_name(swf_dumper, var_name, swf, from_character_id = nil)
  from_character_id ||= (swf_dumper.tags.collect { |t| t.define_tag? ? t.character_id : nil }).compact.max + 1
  refer_character_id = nil
  sprite_indices = {}
  swf_dumper.tags.each_with_index do |t,i|
    if t.character_id
      sprite_indices[t.character_id] = i
    end
    if var_name == t.refer_character_inst_name
      refer_character_id = t.refer_character_id
      break
    end
  end
  raise ReplaceTargetError unless refer_character_id
  offset = swf_dumper.tags_addresses[sprite_indices[refer_character_id]]
  srt = SpriteReplaceTarget.new(offset, swf)
  srt.target_define_tags_string, from_character_id = srt.build_define_tags_string(from_character_id)
  srt.target_control_tags_string = srt.build_control_tags_string
  [srt, from_character_id]
end

.build_list_by_instance_var_names(swf_dumper, var_name_to_swf) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/swf_ruby/replace_target.rb', line 45

def self.build_list_by_instance_var_names(swf_dumper, var_name_to_swf)
  from_character_id = (swf_dumper.tags.collect { |t| t.define_tag? ? t.character_id : nil }).compact.max + 1
  repl_targets = []
  var_name_to_swf.each do |var_name, swf|
    repl_target, from_character_id = SwfRuby::SpriteReplaceTarget.build_by_instance_var_name(swf_dumper, var_name, swf, from_character_id)
    repl_targets << repl_target
  end
  repl_targets
end

Instance Method Details

#build_control_tags_stringObject

DefineSpriteに埋め込むためのControl tagsのみを抽出する. 参照先のcharacter_idを変更する必要がある場合は付け替える.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/swf_ruby/replace_target.rb', line 94

def build_control_tags_string
  str = ""
  valid_control_tag_codes = [0, 1, 4, 5, 12, 18, 19, 26, 28, 43, 45, 70, 72]
  @control_tags.each do |t|
    next unless valid_control_tag_codes.include? t.code
    if @idmap[t.refer_character_id]
      str << t.rawdata_with_refer_character_id(@idmap[t.refer_character_id])
    else
      str << t.rawdata
    end
  end
  str
end

#build_define_tags_string(from_character_id) ⇒ Object

置換するSWFからCharacterIdを付け替えながらDefineタグを抽出する. 対象のSWFにBitmapIDの参照が含まれる場合、これも合わせて付け替える. 同時に、CharacterIdの対応付けマップを作成する.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/swf_ruby/replace_target.rb', line 80

def build_define_tags_string(from_character_id)
  str = ""
  @define_tags.each do |t|
    if t.character_id
      from_character_id += 1
      @idmap[t.character_id] = from_character_id
      str << t.rawdata_with_define_character_id(@idmap, @idmap[t.character_id])
    end
  end
  [str, from_character_id+1]
end