1
by edA-qa mort-ora-y
first |
1 |
#!/usr/bin/ruby
|
2 |
#
|
|
3 |
# Converts an SWFMILL resource.xml file into class definitions
|
|
4 |
# for haXe or AS3
|
|
5 |
#
|
|
6 |
# Refer to http://wiki.disemia.com/HaXe_SWFMILL_Resource_Converter
|
|
7 |
||
8 |
require 'rexml/document' |
|
9 |
include REXML |
|
10 |
require 'fileutils' |
|
11 |
||
12 |
if ARGV.length != 4 |
|
13 |
puts "Syntax: convert_resources.rb resource.xml output_dir source_swf mode=[as|hx]" |
|
14 |
exit -1 |
|
15 |
end
|
|
16 |
||
17 |
####################################
|
|
18 |
# Define base output class for common functions
|
|
19 |
class Output |
|
20 |
attr_accessor :out |
|
21 |
# Hmm, why do I need to create initialize here...
|
|
22 |
def initialize |
|
23 |
end
|
|
24 |
||
25 |
def setOutput( file ) |
|
26 |
@out = file |
|
27 |
end
|
|
28 |
end
|
|
29 |
||
30 |
#####################################
|
|
31 |
# Define AS Outputter
|
|
32 |
class Output_as<Output |
|
33 |
def wBase( name ) |
|
34 |
out.puts "package #{name} {" |
|
35 |
end
|
|
36 |
||
37 |
def wEnd( ) |
|
38 |
out.puts "}" |
|
39 |
end
|
|
40 |
||
41 |
def w_clip( name, source, id ) |
|
42 |
out.puts "import flash.display.Sprite;" |
|
43 |
out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]" |
|
44 |
out.puts "public class #{name} extends flash.display.Sprite { }" |
|
45 |
end
|
|
46 |
||
47 |
def w_bitmap( name, source, id ) |
|
48 |
out.puts "import flash.display.BitmapData;" |
|
49 |
out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]" |
|
50 |
out.puts "public class #{name} extends flash.display.BitmapData { public function #{name}() {super( 0, 0 ); } }" |
|
51 |
end
|
|
52 |
||
53 |
def w_sound( name, source, id ) |
|
54 |
out.puts "import flash.media.Sound;" |
|
55 |
out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]" |
|
56 |
out.puts "public class #{name} extends flash.media.Sound { }" |
|
57 |
end
|
|
58 |
||
59 |
end
|
|
60 |
||
61 |
#####################################
|
|
62 |
# Define HX Outputter
|
|
63 |
class Output_hx<Output |
|
64 |
def wBase( name ) |
|
65 |
out.puts "package #{name};" |
|
66 |
end
|
|
67 |
||
68 |
def wEnd( ) |
|
69 |
end
|
|
70 |
||
71 |
def w_clip( name, source, id ) |
|
72 |
out.puts "class #{name} extends flash.display.Sprite { public function new() { super(); } }" |
|
73 |
end
|
|
74 |
||
75 |
def w_bitmap( name, source, id ) |
|
76 |
out.puts "class #{name} extends flash.display.BitmapData { public function new() {super( 0, 0 ); } }" |
|
77 |
end
|
|
78 |
||
79 |
def w_sound( name, source, id ) |
|
80 |
out.puts "class #{name} extends flash.media.Sound {public function new() { super(); } }" |
|
81 |
end
|
|
82 |
||
83 |
def w_font( name, source, id ) |
|
84 |
out.puts "class #{name} extends flash.text.Font { public function new() { super(); } }" |
|
85 |
end
|
|
86 |
end
|
|
87 |
||
88 |
#####################################
|
|
89 |
# Start of processing
|
|
90 |
||
91 |
# Load the resource file
|
|
92 |
file = File.new( ARGV[0] ) |
|
93 |
doc = Document.new(file) |
|
94 |
||
95 |
# Base to write output files
|
|
96 |
output = ARGV[1] |
|
97 |
||
98 |
# Where to load input files from
|
|
99 |
source = ARGV[2] |
|
100 |
||
101 |
# Get the mode (must be as or hx)
|
|
102 |
mode = ARGV[3] |
|
103 |
formatter = Object.const_get( "Output_#{mode}" ).new |
|
104 |
||
105 |
# iterate over the source document
|
|
106 |
doc.elements.each( "//library/*" ) { |el| |
|
107 |
# Extract required values
|
|
108 |
type = el.name |
|
109 |
id = el.attributes['id'] |
|
110 |
lastdot = id.rindex( '.' ) |
|
111 |
if lastdot == nil |
|
112 |
package= |
|
113 |
name=id |
|
114 |
else
|
|
115 |
package = id[0..lastdot-1] |
|
116 |
name=id[lastdot+1..-1] |
|
117 |
end
|
|
118 |
||
119 |
# Make sure the output directory exists for the package
|
|
120 |
toDir = "#{output}/#{package.gsub(/\./,'/')}" |
|
121 |
FileUtils.mkdir_p( toDir ) |
|
122 |
||
123 |
# set the formatter to use this as the output now
|
|
124 |
toFileName = "#{toDir}/#{name}.#{mode}" |
|
125 |
toFile = File.new( toFileName, "w" ) |
|
126 |
formatter.setOutput( toFile ) |
|
127 |
||
128 |
# write the class
|
|
129 |
formatter.wBase( package ) |
|
130 |
eval "formatter.w_#{type}( name, source, id )" |
|
131 |
formatter.wEnd( ) |
|
132 |
}
|