swaf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --fail-fast
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in swaf.gemspec
4
+ gemspec
5
+
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2011 AOKI,Hanae
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = swaf
2
+
3
+ == Description
4
+
5
+ more user-friendly interface for {swf_ruby}[https://github.com/tmtysk/swf_ruby]
6
+
7
+ == Installation
8
+
9
+ % gem install swaf
10
+
11
+ == Usage
12
+
13
+ === Replace images
14
+
15
+ require "rubygems"
16
+ require "swaf"
17
+
18
+ src = Swaf.load_file("swf/my.swf")
19
+ image_replaced = src.replace(1 => jpeg("images/moon.jpg"))
20
+ File.open("swf/image_replaced.swf", "wb") do |f|
21
+ f << image_replaced
22
+ end
23
+
24
+ `Swaf#replace` method takes `Hash` object which key is ID in SWF and value is file of destination.
25
+
26
+ === Replace movie-clips
27
+
28
+ require "rubygems"
29
+ require "swaf"
30
+
31
+ src = Swaf.load_file("swf/my.swf")
32
+ movie_replaced = src.replace(:_mymovie => movie("swf/greeting.swf"))
33
+ File.open("swf/movie_replaced.swf", "wb") do |f|
34
+ f << movie_replaced
35
+ end
36
+
37
+ `Swaf#replace` method takes `Hash` object which key is instance name and value is file of destination.
38
+
39
+ === Replace value of ActionScript variable
40
+
41
+ require "rubygems"
42
+ require "swaf"
43
+
44
+ src = Swaf.load_file("swf/my.swf")
45
+ var_replaced = src.replace(:_name => "Foobar")
46
+ File.open("swf/var_replaced.swf", "wb") do |f|
47
+ f << var_replaced
48
+ end
49
+
50
+ `Swaf#replace` method takes `Hash` object which key is variable name and value is value of description.
51
+
52
+ == Copyright
53
+
54
+ [Copyright] Copyright (c) 2011 AOKI,Hanae
55
+ [Author] AOKI,Hanae (aereal, trasty.loose@gmail.com)
56
+ [License] MIT License
57
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lib/swaf.rb ADDED
@@ -0,0 +1,74 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require "forwardable"
4
+ require "swf_ruby"
5
+
6
+ require "swaf/version"
7
+ require "swaf/helper"
8
+
9
+ class Swaf
10
+ extend Forwardable
11
+
12
+ def self.load(doc)
13
+ (swaf = self.new).load!(doc)
14
+ swaf
15
+ end
16
+
17
+ def self.load_file(filename)
18
+ (swaf = self.new).load!(
19
+ File.binread(filename)
20
+ )
21
+ swaf
22
+ end
23
+
24
+ attr_reader :dumper
25
+ def_delegators :@dumper, *[
26
+ :header, :swf, :tags, :tags_addresses, :character_ids
27
+ ]
28
+
29
+ def initialize
30
+ @dumper = SwfRuby::SwfDumper.new
31
+ @tamperer = SwfRuby::SwfTamperer.new
32
+ end
33
+
34
+ def load!(swf)
35
+ self.dumper.dump(swf)
36
+ end
37
+
38
+ def detect(id)
39
+ if t = @dumper.tags.each_with_index.find {|t, i| t.character_id == id }
40
+ @dumper.tags_addresses[t[1]]
41
+ end
42
+ end
43
+ alias_method :find, :detect
44
+
45
+ def replace(params={})
46
+ self.class.load(@tamperer.replace(@dumper.swf, params.inject([]) {|targets, (k, v)|
47
+ targets << make_target(k, v)
48
+ }))
49
+ end
50
+
51
+ private
52
+ def make_target(key, value)
53
+ case key
54
+ when Symbol
55
+ if value.kind_of?(Array) && value.size == 2 # movie-clip
56
+ SwfRuby::SpriteReplaceTarget.build_list_by_instance_var_names(
57
+ @dumper, key.to_s => value
58
+ ).first
59
+ else
60
+ SwfRuby::AsVarReplaceTarget.build_by_var_name(@dumper, key.to_s).each {|t|
61
+ t.str = value
62
+ }.first
63
+ end
64
+ when Integer
65
+ case value.first
66
+ when :jpeg
67
+ SwfRuby::Jpeg2ReplaceTarget.new(detect(key), value.last)
68
+ when :gif, :png
69
+ SwfRuby::Lossless2ReplaceTarget(detect(key), value.last)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,22 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ # TODO: refactoring
4
+ module ::Kernel
5
+ def jpeg(file)
6
+ [:jpeg, File.binread(file)]
7
+ end
8
+ alias_method :jpg, :jpeg
9
+
10
+ def png(file)
11
+ [:png, File.binread(file)]
12
+ end
13
+
14
+ def gif(file)
15
+ [:gif, File.binread(file)]
16
+ end
17
+
18
+ def movie(file)
19
+ [:movie, File.binread(file)]
20
+ end
21
+ end
22
+
@@ -0,0 +1,8 @@
1
+ class Swaf
2
+ VERSION = [
3
+ major = 0,
4
+ minor = 1,
5
+ patch = 0,
6
+ ].join('.')
7
+ end
8
+
Binary file
Binary file
Binary file
Binary file
Binary file
data/spec/swaf_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require "pathname"
4
+ require "swaf"
5
+
6
+ describe Swaf do
7
+ let(:prefix) { Pathname(__FILE__).expand_path.parent }
8
+ let(:swf_file) { prefix + 'samples' + 'mock.swf' }
9
+
10
+ subject do
11
+ Swaf.load_file(swf_file)
12
+ end
13
+
14
+ context "when detecting tags" do
15
+ let(:jpg_offset) { 1370 }
16
+
17
+ it "detect a offset of a JPEG image by ID" do
18
+ subject.detect(1).should == jpg_offset
19
+ end
20
+ end
21
+
22
+ context "when replacing objects" do
23
+ let(:jpeg_replaced_swf) {
24
+ (prefix + 'samples' + 'jpeg_replaced.swf').binread
25
+ }
26
+ let(:new_image) {
27
+ prefix + 'samples' + 'another_fig.jpg'
28
+ }
29
+ let(:as_var_replaced_swf) {
30
+ (prefix + 'samples' + 'replaced_as_var.swf').binread
31
+ }
32
+
33
+ it "replace JPEG images with specified JPEG image" do
34
+ subject.replace(1 => jpeg(new_image))
35
+ end
36
+
37
+ it "replace value of ActionScript's variable with specified value" do
38
+ subject.replace(:_itemname => 'fugafuga').swf.should == as_var_replaced_swf
39
+ end
40
+
41
+ it "replace movie-clips with specified ones" do
42
+ pending
43
+ end
44
+ end
45
+ end
46
+
data/swaf.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "swaf/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "swaf"
7
+ s.version = Swaf::VERSION
8
+ s.authors = ["aereal"]
9
+ s.email = ["trasty.loose@gmail.com"]
10
+ s.homepage = "https://github.com/aereal/swaf"
11
+ s.summary = %q{more user-friendly interface for swf_ruby}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ devel_deps = {
19
+ 'bundler' => '~> 1.0.0',
20
+ 'rspec' => '~> 2.6.0',
21
+ 'rake' => '~> 0.9.0'
22
+ }
23
+
24
+ runtime_deps = {
25
+ 'swf_ruby' => '>= 0.2.0'
26
+ }
27
+
28
+ if s.respond_to? :add_runtime_dependency
29
+ devel_deps.each do |gem, prereq|
30
+ s.add_development_dependency gem, prereq
31
+ end
32
+
33
+ runtime_deps.each do |gem, prereq|
34
+ s.add_runtime_dependency gem, prereq
35
+ end
36
+ else
37
+ devel_deps.merge(runtime_deps).each do |gem, prereq|
38
+ s.add_dependency gem, prereq
39
+ end
40
+ end
41
+ end
42
+
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swaf
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - aereal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-07 00:00:00 +09:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 2.6.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: swf_ruby
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 0.2.0
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ description:
61
+ email:
62
+ - trasty.loose@gmail.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.rdoc
75
+ - Rakefile
76
+ - lib/swaf.rb
77
+ - lib/swaf/helper.rb
78
+ - lib/swaf/version.rb
79
+ - spec/samples/another_fig.jpg
80
+ - spec/samples/fig.jpg
81
+ - spec/samples/jpeg_replaced.swf
82
+ - spec/samples/mock.swf
83
+ - spec/samples/replaced_as_var.swf
84
+ - spec/swaf_spec.rb
85
+ - swaf.gemspec
86
+ has_rdoc: true
87
+ homepage: https://github.com/aereal/swaf
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.2
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: more user-friendly interface for swf_ruby
114
+ test_files:
115
+ - spec/samples/another_fig.jpg
116
+ - spec/samples/fig.jpg
117
+ - spec/samples/jpeg_replaced.swf
118
+ - spec/samples/mock.swf
119
+ - spec/samples/replaced_as_var.swf
120
+ - spec/swaf_spec.rb