summaryrefslogtreecommitdiff
path: root/client/map-diff.py
blob: b872d47fb3cc6bb49c3c8c71cfea88f51e4eb3fc (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import os
import subprocess
import re
import tempfile

class MapDiff(object):

    @staticmethod
    def check_programs():
        """
            Checks the require programs are available
        """
        def which(program):
            import os
            def is_exe(fpath):
                return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
            fpath, fname = os.path.split(program)
            if fpath:
                if is_exe(program):
                    return program
            else:
                for path in os.environ["PATH"].split(os.pathsep):
                    exe_file = os.path.join(path, program)
                    if is_exe(exe_file):
                        return exe_file
            return None

        platform_programs = MapDiff.PROGRAMS.get(sys.platform, MapDiff.PROGRAMS.get('default'))
        for program in platform_programs.values():
            if not which(program):
                raise Exception('The required "%s" program is missing from your PATH.' % program)

    MAP_RE = re.compile(r'^\d{3}-\d{1}(\.tmx)?$')
    PROGRAMS = {
        'default': {
            'tmxrasterizer': 'tmxrasterizer',
            'im_convert': 'convert',
            'im_display': 'display',
            'git': 'git',
        },
        'win32': {
            'tmxrasterizer': 'tmxrasterizer.exe',
            'im_convert': 'convert.exe',
            'im_display': 'display.exe',
            'git': 'git.exe',
        },
    }

    def __init__(self):
        self.platform_programs = MapDiff.PROGRAMS.get(sys.platform, MapDiff.PROGRAMS.get('default'))

    def _diffmaps(self, tmx1, tmx2, tmxdiffpath):
        tmxraster1 = self._rastermap(tmx1)
        tmxraster2 = self._rastermap(tmx2)
        tmxf, tmxdiff = tempfile.mkstemp(suffix='.png')
        subprocess.check_call([
            self.platform_programs.get('im_convert'), tmxraster1, tmxraster2,
            '-compose', 'Difference',
            '-auto-level',
            '-composite',
            tmxraster2,
            '-compose', 'Screen',
            '-composite',
            tmxdiffpath
        ])
        os.unlink(tmxdiff)
        os.unlink(tmxraster1)
        os.unlink(tmxraster2)
        sys.stdout.write((u'Map diff written to %s\n' % tmxdiffpath).encode('utf-8'))
        subprocess.check_call([self.platform_programs.get('im_display'), tmxdiffpath])

    def _rastermap(self, tmx):
        tmxf, tmxraster = tempfile.mkstemp(suffix='.png')
        subprocess.check_call([self.platform_programs.get('tmxrasterizer'), tmx, tmxraster])
        if os.stat(tmxraster).st_size == 0:
            # the image couldnt be rendered. The most probable reason is
            # that the map was too big (e.g 024-4, 500x500 tiles)
            raise Exception('Map too large to be rendered.')
        return tmxraster


class MapGitRevDiff(MapDiff):

    def __init__(self, map_name):
        super(MapGitRevDiff, self).__init__()
        self.map_name = map_name

    def diff(self):
        if not MapDiff.MAP_RE.match(self.map_name):
            sys.stderr.write(u'Invalid map name: %s.\n' % self.map_name)
            return 1
        if not self.map_name.endswith(u'.tmx'):
            self.map_name = self.map_name+u'.tmx'
        self.tmx_path = os.path.join(u'..', u'maps', self.map_name)
        self.map_number = os.path.splitext(os.path.basename(self.map_name))[0]
        p = subprocess.Popen([self.platform_programs.get('git'), '--no-pager', 'log', '-n', '2', '--oneline', '--follow', self.tmx_path], stdout=subprocess.PIPE)
        log = p.communicate()[0].splitlines()
        if not len(log) == 2:
            raise Exception('This map has only one version')
        c1 = log[0].split(' ')[0]
        c2 = log[1].split(' ')[0]

        # We have the 2 revs to compare. Let's extract the related tmx file
        p1 = self._mktmx_from_rev(c1)
        p2 = self._mktmx_from_rev(c2)
        difftmxpath = '%s_%s-%s.png' % (self.map_number, c1, c2)
        self._diffmaps(p1, p2, difftmxpath)
        os.unlink(p1)
        os.unlink(p2)

    def _mktmx_from_rev(self, rev):
        p = subprocess.Popen([self.platform_programs.get('git'), '--no-pager', 'show', '%s:%s' % (rev, self.tmx_path)], stdout=subprocess.PIPE)
        contents = p.communicate()[0]
        revtmx = '%s-%s.tmx' % (self.map_number, rev)
        f = open(revtmx, 'w')
        f.write(contents)
        f.close()
        return revtmx


class MapFileDiff(MapDiff):

    def __init__(self, map1, map2):
        super(MapFileDiff, self).__init__()
        self.map1 = map1
        self.map2 = map2

    def diff(self):
        b1 = os.path.splitext(os.path.basename(self.map1))[0]
        b2 = os.path.splitext(os.path.basename(self.map2))[0]
        difftmxpath = '%s__%s.png' % (b1, b2)
        self._diffmaps(self.map1, self.map2, difftmxpath)


def usage():
    sys.stderr.write(u'''Usage: %s MAP_NAME
       %s CHANGED_TMX REFERENCE_TMX

    Example:
        $ ./map-diff.py 007-1
    will highlight the changes between the current 007-1 map and its previous version

        $ ./map-diff.py changes-made-by-someone-007-1.tmx ../maps-007-1.tmx
    will highlight the changes between the two tmx maps.
    Note that these 2 tmx to compare have to satisfy their dependancies, e.g tilesets.
    Hence they should be in a sibling directory of the client-data/maps folder.
    \n''' % (sys.argv[0], sys.argv[0]))

def main():
    if not len(sys.argv) > 1:
        usage()
        return 127
    if not os.path.basename(os.path.dirname(os.getcwdu())) == u'client-data':
        sys.stderr.write(u'This script must be run from client-data/tools.\n')
        return 1
    try:
        MapDiff.check_programs()
    except Exception as e:
        sys.stderr.write(u'%s\n' % e)
        return 126
    if len(sys.argv) == 2:
        map_name = sys.argv[1]
        mapdiff = MapGitRevDiff(map_name)
        try:
            mapdiff.diff()
        except Exception as e:
            sys.stderr.write(u'\x1b[31m\x1b[1mError while generating the diff for map %s: %s\x1b[0m\n' % (map_name, e))
            return 1
        else:
            return 0
    else:
        map1 = sys.argv[1]
        map2 = sys.argv[2]
        mapdiff = MapFileDiff(map1, map2)
        try:
            mapdiff.diff()
        except Exception as e:
            sys.stderr.write(u'\x1b[31m\x1b[1mError while generating the diff for %s and %s: %s\x1b[0m\n' % (map1, map2, e))
            return 1
        else:
            return 0

if __name__ == '__main__':
    sys.exit(main())