Permalink
Browse files

Merge branch 'release/0.1'

  • Loading branch information...
2 parents d11a821 + 2dcf019 commit a1b74755d3b4516e2d6cce68cf86f72d2c54c7ca aereal committed Jun 6, 2011
View
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--fail-fast
View
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in swaf.gemspec
+gemspec
+
View
@@ -0,0 +1,8 @@
+Copyright (c) 2011 AOKI,Hanae
+
+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:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+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.
+
View
@@ -0,0 +1,57 @@
+= swaf
+
+== Description
+
+more user-friendly interface for {swf_ruby}[https://github.com/tmtysk/swf_ruby]
+
+== Installation
+
+ % gem install swaf
+
+== Usage
+
+=== Replace images
+
+ require "rubygems"
+ require "swaf"
+
+ src = Swaf.load_file("swf/my.swf")
+ image_replaced = src.replace(1 => jpeg("images/moon.jpg"))
+ File.open("swf/image_replaced.swf", "wb") do |f|
+ f << image_replaced
+ end
+
+`Swaf#replace` method takes `Hash` object which key is ID in SWF and value is file of destination.
+
+=== Replace movie-clips
+
+ require "rubygems"
+ require "swaf"
+
+ src = Swaf.load_file("swf/my.swf")
+ movie_replaced = src.replace(:_mymovie => movie("swf/greeting.swf"))
+ File.open("swf/movie_replaced.swf", "wb") do |f|
+ f << movie_replaced
+ end
+
+`Swaf#replace` method takes `Hash` object which key is instance name and value is file of destination.
+
+=== Replace value of ActionScript variable
+
+ require "rubygems"
+ require "swaf"
+
+ src = Swaf.load_file("swf/my.swf")
+ var_replaced = src.replace(:_name => "Foobar")
+ File.open("swf/var_replaced.swf", "wb") do |f|
+ f << var_replaced
+ end
+
+`Swaf#replace` method takes `Hash` object which key is variable name and value is value of description.
+
+== Copyright
+
+[Copyright] Copyright (c) 2011 AOKI,Hanae
+[Author] AOKI,Hanae (aereal, trasty.loose@gmail.com)
+[License] MIT License
+
View
@@ -0,0 +1 @@
+require 'bundler/gem_tasks'
View
@@ -0,0 +1,74 @@
+# encoding: ASCII-8BIT
+
+require "forwardable"
+require "swf_ruby"
+
+require "swaf/version"
+require "swaf/helper"
+
+class Swaf
+ extend Forwardable
+
+ def self.load(doc)
+ (swaf = self.new).load!(doc)
+ swaf
+ end
+
+ def self.load_file(filename)
+ (swaf = self.new).load!(
+ File.binread(filename)
+ )
+ swaf
+ end
+
+ attr_reader :dumper
+ def_delegators :@dumper, *[
+ :header, :swf, :tags, :tags_addresses, :character_ids
+ ]
+
+ def initialize
+ @dumper = SwfRuby::SwfDumper.new
+ @tamperer = SwfRuby::SwfTamperer.new
+ end
+
+ def load!(swf)
+ self.dumper.dump(swf)
+ end
+
+ def detect(id)
+ if t = @dumper.tags.each_with_index.find {|t, i| t.character_id == id }
+ @dumper.tags_addresses[t[1]]
+ end
+ end
+ alias_method :find, :detect
+
+ def replace(params={})
+ self.class.load(@tamperer.replace(@dumper.swf, params.inject([]) {|targets, (k, v)|
+ targets << make_target(k, v)
+ }))
+ end
+
+ private
+ def make_target(key, value)
+ case key
+ when Symbol
+ if value.kind_of?(Array) && value.size == 2 # movie-clip
+ SwfRuby::SpriteReplaceTarget.build_list_by_instance_var_names(
+ @dumper, key.to_s => value
+ ).first
+ else
+ SwfRuby::AsVarReplaceTarget.build_by_var_name(@dumper, key.to_s).each {|t|
+ t.str = value
+ }.first
+ end
+ when Integer
+ case value.first
+ when :jpeg
+ SwfRuby::Jpeg2ReplaceTarget.new(detect(key), value.last)
+ when :gif, :png
+ SwfRuby::Lossless2ReplaceTarget(detect(key), value.last)
+ end
+ end
+ end
+end
+
View
@@ -0,0 +1,22 @@
+# encoding: ASCII-8BIT
+
+# TODO: refactoring
+module ::Kernel
+ def jpeg(file)
+ [:jpeg, File.binread(file)]
+ end
+ alias_method :jpg, :jpeg
+
+ def png(file)
+ [:png, File.binread(file)]
+ end
+
+ def gif(file)
+ [:gif, File.binread(file)]
+ end
+
+ def movie(file)
+ [:movie, File.binread(file)]
+ end
+end
+
View
@@ -0,0 +1,8 @@
+class Swaf
+ VERSION = [
+ major = 0,
+ minor = 1,
+ patch = 0,
+ ].join('.')
+end
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
View
@@ -0,0 +1,46 @@
+# encoding: ASCII-8BIT
+
+require "pathname"
+require "swaf"
+
+describe Swaf do
+ let(:prefix) { Pathname(__FILE__).expand_path.parent }
+ let(:swf_file) { prefix + 'samples' + 'mock.swf' }
+
+ subject do
+ Swaf.load_file(swf_file)
+ end
+
+ context "when detecting tags" do
+ let(:jpg_offset) { 1370 }
+
+ it "detect a offset of a JPEG image by ID" do
+ subject.detect(1).should == jpg_offset
+ end
+ end
+
+ context "when replacing objects" do
+ let(:jpeg_replaced_swf) {
+ (prefix + 'samples' + 'jpeg_replaced.swf').binread
+ }
+ let(:new_image) {
+ prefix + 'samples' + 'another_fig.jpg'
+ }
+ let(:as_var_replaced_swf) {
+ (prefix + 'samples' + 'replaced_as_var.swf').binread
+ }
+
+ it "replace JPEG images with specified JPEG image" do
+ subject.replace(1 => jpeg(new_image))
+ end
+
+ it "replace value of ActionScript's variable with specified value" do
+ subject.replace(:_itemname => 'fugafuga').swf.should == as_var_replaced_swf
+ end
+
+ it "replace movie-clips with specified ones" do
+ pending
+ end
+ end
+end
+
View
@@ -0,0 +1,42 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "swaf/version"
+
+Gem::Specification.new do |s|
+ s.name = "swaf"
+ s.version = Swaf::VERSION
+ s.authors = ["aereal"]
+ s.email = ["trasty.loose@gmail.com"]
+ s.homepage = "https://github.com/aereal/swaf"
+ s.summary = %q{more user-friendly interface for swf_ruby}
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ devel_deps = {
+ 'bundler' => '~> 1.0.0',
+ 'rspec' => '~> 2.6.0',
+ 'rake' => '~> 0.9.0'
+ }
+
+ runtime_deps = {
+ 'swf_ruby' => '>= 0.2.0'
+ }
+
+ if s.respond_to? :add_runtime_dependency
+ devel_deps.each do |gem, prereq|
+ s.add_development_dependency gem, prereq
+ end
+
+ runtime_deps.each do |gem, prereq|
+ s.add_runtime_dependency gem, prereq
+ end
+ else
+ devel_deps.merge(runtime_deps).each do |gem, prereq|
+ s.add_dependency gem, prereq
+ end
+ end
+end
+

0 comments on commit a1b7475

Please sign in to comment.