1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # This program is free software: you can redistribute it and/or modify |
---|
4 | # it under the terms of the GNU General Public License as published by |
---|
5 | # the Free Software Foundation, either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | # This program is distributed in the hope that it will be useful, |
---|
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | # GNU General Public License for more details. |
---|
12 | |
---|
13 | # You should have received a copy of the GNU General Public License |
---|
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
15 | |
---|
16 | import os |
---|
17 | import sys |
---|
18 | import getopt |
---|
19 | import logging |
---|
20 | import re |
---|
21 | |
---|
22 | log = logging.getLogger("agentxml") |
---|
23 | log.setLevel(logging.DEBUG) |
---|
24 | |
---|
25 | def generateAgent(path, delay, x_off, y_off): |
---|
26 | """ Generates the agent XML file and creates animation |
---|
27 | XML files for found children. |
---|
28 | @type path: string |
---|
29 | @param path: Agent path |
---|
30 | @type delay: string |
---|
31 | @param delay: Animation delay (used in animation.xml) |
---|
32 | @type x_off: string |
---|
33 | @param x_off: X offset (used in animation.xml) |
---|
34 | @type y_off: string |
---|
35 | @param y_off: Y offset (used in animation.xml) |
---|
36 | @return True on success, False on failure |
---|
37 | """ |
---|
38 | # Name is the last subdirectory |
---|
39 | name=os.path.split(os.path.abspath(path))[1] |
---|
40 | log.debug("Doing animation for agent: %s", name) |
---|
41 | |
---|
42 | f = open(os.path.join(path,name + ".xml"), 'w') |
---|
43 | log.debug("Opened " + os.path.join(path,name + ".xml")) |
---|
44 | f.write('<?fife type="object"?>\n') |
---|
45 | f.write('<object id="%s" namespace="PARPG" blocking="1" static="0">\n' % \ |
---|
46 | (name,)) |
---|
47 | |
---|
48 | # Iterate the animations |
---|
49 | anims = os.listdir(path) |
---|
50 | anims.sort() |
---|
51 | for anim in anims: |
---|
52 | anim_path=os.path.join(path,anim) |
---|
53 | if os.path.isdir(anim_path) and anim.find("svn")==-1: |
---|
54 | log.debug("Found animation: %s", anim) |
---|
55 | f.write('\t<action id="' + anim +'">\n') |
---|
56 | angles = os.listdir(anim_path) |
---|
57 | angles.sort() |
---|
58 | for angle in angles: |
---|
59 | angle_path=os.path.join(anim_path,angle) |
---|
60 | if os.path.isdir(angle_path) and angle.find("svn")==-1: |
---|
61 | log.debug("Found angle: %s", angle) |
---|
62 | f.write('\t\t<animation source="' + anim + '/' + angle |
---|
63 | + '/animation.xml" direction="' |
---|
64 | + str(int(angle)) + '" />\n') |
---|
65 | if not generateAnimation(angle_path,delay,x_off,y_off): |
---|
66 | log.error("Failed to create animation for " + anum\ |
---|
67 | + " (angle: " + angle + ")") |
---|
68 | f.write('\t</action>\n') |
---|
69 | f.write('</object>\n') |
---|
70 | f.close() |
---|
71 | log.info("Wrote " + os.path.join(path,name + ".xml")) |
---|
72 | return True |
---|
73 | |
---|
74 | def imageCompare(x,y): |
---|
75 | """ Compares two filenames containing a number |
---|
76 | @type x: string |
---|
77 | @param x: First filename |
---|
78 | @type y: string |
---|
79 | @param y: second filename |
---|
80 | @return 1 of x>y, 0 when x==y, -1 when y<x. |
---|
81 | """ |
---|
82 | regex="[^0-9]*([0-9]*).*" |
---|
83 | try: |
---|
84 | int_x = int(re.sub(regex,"\\1",x)) |
---|
85 | except: |
---|
86 | int_x = -1 |
---|
87 | |
---|
88 | try: |
---|
89 | int_y = int(re.sub(regex,"\\1",y)) |
---|
90 | except: |
---|
91 | int_y = -1 |
---|
92 | |
---|
93 | if int_x > int_y: |
---|
94 | return 1 |
---|
95 | elif int_x < int_y: |
---|
96 | return -1; |
---|
97 | |
---|
98 | return 0 |
---|
99 | |
---|
100 | def generateAnimation(path, delay, x_off, y_off): |
---|
101 | """ Generate animation.xml for a given path. |
---|
102 | @type path: string |
---|
103 | @param path: Agent path |
---|
104 | @type delay: string |
---|
105 | @param delay: Animation delay |
---|
106 | @type x_off: string |
---|
107 | @param x_off: X offset |
---|
108 | @type y_off: string |
---|
109 | @param y_off: Y offset |
---|
110 | @return True on success, False on failure """ |
---|
111 | |
---|
112 | (tmp, angle) = os.path.split(os.path.abspath(path)) |
---|
113 | (tmp, anim) = os.path.split(tmp) |
---|
114 | (tmp, agent) = os.path.split(tmp) |
---|
115 | id = "%s:%s:%s"%(agent, anim, angle) |
---|
116 | log.debug("Doing animation with id: %s" % (id)) |
---|
117 | |
---|
118 | images = os.listdir(path) |
---|
119 | # Get those with the correct extension |
---|
120 | images_filtered = [] |
---|
121 | for image in images: |
---|
122 | if image.endswith(".png"): |
---|
123 | images_filtered.append(image) |
---|
124 | |
---|
125 | |
---|
126 | f = open(os.path.join(path,"animation.xml"), 'w') |
---|
127 | log.debug("Opened: " + os.path.join("animation.xml")) |
---|
128 | f.write('<animation delay="' + delay + '" namespace="PAPRG" id="' + id |
---|
129 | + '" x_offset="' + x_off |
---|
130 | + '" y_offset="' + y_off + '">\n') |
---|
131 | |
---|
132 | # Sort them |
---|
133 | images_filtered.sort(cmp=imageCompare) |
---|
134 | for image in images_filtered: |
---|
135 | log.debug("Found image: %s" % image) |
---|
136 | f.write('\t<frame source="' + image + '" />\n') |
---|
137 | |
---|
138 | f.write('</animation>\n') |
---|
139 | f.close() |
---|
140 | log.info("Wrote: " + os.path.join(path,"animation.xml")) |
---|
141 | return True |
---|
142 | |
---|
143 | def usage(): |
---|
144 | """ Prints the help message. """ |
---|
145 | print "Usage: %s [options]" % sys.argv[0] |
---|
146 | print "Options:" |
---|
147 | print " --verbose, -v Verbose" |
---|
148 | print " --help, -h This help message" |
---|
149 | print " --path DIR, -p Root path of the agent (default .)" |
---|
150 | print " --delay DLY, -d Delay value (default 100)" |
---|
151 | print " --x_offset OFF, -x X Offset (default 0)" |
---|
152 | print " --y_offset OFF, -y Y Offset (default 0)" |
---|
153 | |
---|
154 | def main(argv): |
---|
155 | """ Main function, parses the command line arguments |
---|
156 | and launches the xml file creation logic. """ |
---|
157 | |
---|
158 | try: |
---|
159 | opts, args = getopt.getopt(argv, "hp:d:x:y:v", |
---|
160 | ["help","path","delay", "x_offset", "y_offset", "verbose"]) |
---|
161 | except getopt.GetoptError: |
---|
162 | usage() |
---|
163 | sys.exit(2) |
---|
164 | |
---|
165 | agent_path = "." |
---|
166 | delay = "100" |
---|
167 | x_offset = "0" |
---|
168 | y_offset = "0" |
---|
169 | verbose = False |
---|
170 | |
---|
171 | ch = logging.StreamHandler() |
---|
172 | ch.setLevel(logging.INFO) |
---|
173 | |
---|
174 | for opt, arg in opts: |
---|
175 | if opt in ("-p","--path"): |
---|
176 | agent_path = path |
---|
177 | elif opt in ("-d","--delay"): |
---|
178 | delay = arg |
---|
179 | elif opt in ("-x","--x_offset"): |
---|
180 | x_offset = arg |
---|
181 | elif opt in ("-y","--y_offset"): |
---|
182 | y_offset = arg |
---|
183 | elif opt in ("-v","--verbose"): |
---|
184 | ch.setLevel(logging.DEBUG) |
---|
185 | else: |
---|
186 | usage() |
---|
187 | sys.exit() |
---|
188 | |
---|
189 | formatter = logging.Formatter("%(levelname)s: %(message)s") |
---|
190 | ch.setFormatter(formatter) |
---|
191 | log.addHandler(ch) |
---|
192 | |
---|
193 | log.debug("Going to generate animation for %s" % (agent_path)) |
---|
194 | generateAgent(agent_path, delay, x_offset, y_offset) |
---|
195 | log.info("Done"); |
---|
196 | |
---|
197 | if __name__ == '__main__': |
---|
198 | main(sys.argv[1:]) |
---|