~eda-qa/dhlib/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/ruby
#
# Converts an SWFMILL resource.xml file into class definitions
# for haXe or AS3
#
# Refer to http://wiki.disemia.com/HaXe_SWFMILL_Resource_Converter

require 'rexml/document'
include REXML
require 'fileutils'

if ARGV.length != 4
	puts "Syntax: convert_resources.rb resource.xml output_dir source_swf mode=[as|hx]"
	exit -1
end

####################################
# Define base output class for common functions
class Output
	attr_accessor :out
	# Hmm, why do I need to create initialize here...
	def initialize
	end
	
	def setOutput( file )
		@out = file
	end
end

#####################################
# Define AS Outputter
class Output_as<Output
	def wBase( name )
		out.puts "package #{name} {"
	end
	
	def wEnd( )
		out.puts "}"
	end
	
	def w_clip( name, source, id )
		out.puts "import flash.display.Sprite;"
		out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]"
		out.puts "public class #{name} extends flash.display.Sprite { }"
	end
	
	def w_bitmap( name, source, id )
		out.puts "import flash.display.BitmapData;"
		out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]"
		out.puts "public class #{name} extends flash.display.BitmapData { public function #{name}() {super( 0, 0 ); } }"
	end
	
	def w_sound( name, source, id )
		out.puts "import flash.media.Sound;"
		out.puts "[Embed(source=\"#{source}\",symbol=\"#{id}\")]"
		out.puts "public class #{name} extends flash.media.Sound { }"
	end

end

#####################################
# Define HX Outputter
class Output_hx<Output
	def wBase( name )
		out.puts "package #{name};"
	end
	
	def wEnd( )
	end
	
	def w_clip( name, source, id )
		out.puts "class #{name} extends flash.display.Sprite { public function new() { super(); } }"
	end
	
	def w_bitmap( name, source, id )
		out.puts "class #{name} extends flash.display.BitmapData { public function new() {super( 0, 0 ); } }"
	end
	
	def w_sound( name, source, id )
		out.puts "class #{name} extends flash.media.Sound {public function new() { super(); } }"
	end
	
	def w_font( name, source, id )
		out.puts "class #{name} extends flash.text.Font { public function new() { super(); } }"
	end
end

#####################################
# Start of processing

# Load the resource file
file = File.new( ARGV[0] )
doc = Document.new(file)

# Base to write output files
output = ARGV[1]

# Where to load input files from
source = ARGV[2] 

# Get the mode (must be as or hx)
mode = ARGV[3]
formatter = Object.const_get( "Output_#{mode}" ).new

# iterate over the source document
doc.elements.each( "//library/*" ) {	|el|
	# Extract required values
	type = el.name
	id = el.attributes['id']
	lastdot = id.rindex( '.' )
	if lastdot == nil
		package=
		name=id
	else
		package = id[0..lastdot-1]
		name=id[lastdot+1..-1]
	end
	
	# Make sure the output directory exists for the package
	toDir = "#{output}/#{package.gsub(/\./,'/')}"
	FileUtils.mkdir_p( toDir )
	
	# set the formatter to use this as the output now
	toFileName = "#{toDir}/#{name}.#{mode}"
	toFile = File.new( toFileName, "w" )
	formatter.setOutput( toFile )
	
	# write the class
	formatter.wBase( package )
	eval "formatter.w_#{type}( name, source, id )"
	formatter.wEnd( )
}