Mercurial > LightClone
annotate LightTools/build.py @ 61:1fe27776627e
Added asserts
author | koryspansel |
---|---|
date | Mon, 03 Oct 2011 15:05:09 -0700 |
parents | 2444937929ae |
children | 6d4437a24aeb |
rev | line source |
---|---|
0 | 1 # |
46 | 2 # build |
0 | 3 # |
4 | |
5 import sys | |
6 import string | |
7 import struct | |
8 import os | |
9 import functools | |
51 | 10 import re |
11 | |
12 _pattern = re.compile('\(\s*(\d+)\s*,\s*(\d+)\s*\)') | |
0 | 13 |
14 class MapDefinition: | |
15 Parse = functools.partial(string.split, sep=',') | |
16 | |
51 | 17 def __init__(self, size, position, direction, grid): |
0 | 18 self.Size = self._ParseVector(size) |
19 self.Position = self._ParseVector(position) | |
20 self.Direction = self._ParseVector(direction) | |
21 self.Grid = self._ParseList(grid) | |
22 | |
23 def Write(self, filename): | |
24 with open(filename, 'wb') as handle: | |
25 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8 | |
26 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8 | |
27 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4 | |
28 | |
29 for tower in self.Grid: | |
30 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y | |
31 | |
32 def _ParseVector(self, line): | |
33 return map(int, self.Parse(line)) | |
34 | |
35 def _ParseList(self, lines): | |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
36 return [map(int, item.groups()) for item in _pattern.finditer(''.join(lines))] |
0 | 37 |
38 def ReadDefinition(filename): | |
39 def FilterLines(lines): | |
40 for line in map(string.strip, lines): | |
41 if line and not line.startswith('#'): | |
42 yield line.split('#')[0] | |
43 | |
44 with open(filename, 'rt') as handle: | |
45 lines = handle.readlines() | |
46 | |
47 definition = tuple(FilterLines(lines)) | |
48 | |
51 | 49 size = definition[0] |
50 position = definition[1] | |
51 direction = definition[2] | |
52 grid = definition[3:] | |
0 | 53 |
51 | 54 return MapDefinition(size, position, direction, grid) |
55 | |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
56 def Compile(input_map, output_map): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
57 if os.path.isfile(input_map): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
58 definition = ReadDefinition(input_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
59 if not definition: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
60 print 'Failed to read definition "%s"' % input_map |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
61 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
62 else: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
63 folder = os.path.dirname(output_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
64 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
65 if not os.path.isdir(folder): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
66 os.makedirs(folder) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
67 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
68 print 'Building %s from %s' % (os.path.basename(output_map), os.path.basename(input_map)) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
69 definition.Write(output_map) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
70 |
0 | 71 if __name__ == '__main__': |
45 | 72 path_root = os.path.dirname(sys.argv[0]) |
73 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps')) | |
74 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps')) | |
0 | 75 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
76 if len(sys.argv) > 1: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
77 Compile(os.path.join(path_assets, sys.argv[1]), os.path.join(path_build, os.path.splitext(sys.argv[1])[0] + '.map')) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
78 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
79 else: |
45 | 80 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
81 if os.path.isdir(path_assets): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
82 for filename in os.listdir(path_assets): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
83 if not filename.endswith('.def'): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
84 continue |
0 | 85 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
86 Compile(os.path.join(path_assets, filename), os.path.join(path_build, os.path.splitext(filename)[0] + '.map')) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
87 #definition = ReadDefinition(os.path.join(path_assets, filename)) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
88 #if not definition: |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
89 # continue |
45 | 90 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
91 #output = os.path.join(path_build, os.path.splitext(filename)[0] + '.map') |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
92 #folder = os.path.dirname(output) |
45 | 93 |
52
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
94 #if not os.path.isdir(folder): |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
95 # os.makedirs(folder) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
96 |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
97 #print 'Building %s from %s' % (os.path.basename(output), os.path.basename(filename)) |
2444937929ae
Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents:
51
diff
changeset
|
98 #definition.Write(output) |