File: doom_common.py

package info (click to toggle)
game-data-packager 73
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 23,420 kB
  • sloc: python: 11,086; sh: 609; makefile: 59
file content (250 lines) | stat: -rw-r--r-- 9,954 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/python3
# encoding=utf-8
#
# Copyright © 2015-2016 Simon McVittie <smcv@debian.org>
# Copyright © 2015-2016 Alexandre Detiste <alexandre@detiste.be>
# SPDX-License-Identifier: GPL-2.0-or-later

import configparser
import logging
import os
import subprocess

from ..build import (PackagingTask)
from ..data import (Package)
from ..game import (GameData)
from ..paths import (DATADIR, ICONS)
from ..util import (copy_with_substitutions, mkdir_p)

logger = logging.getLogger(__name__)

try:
    from omg import WAD
    from PIL import Image
    have_omg = True
except ImportError:
    have_omg = False

def install_data(from_, to):
    subprocess.check_call(['cp', '--reflink=auto', from_, to])

class DoomGameData(GameData):
    """Special subclass of GameData for games descended from Doom.
    These games install their own icon and .desktop file, and share a
    considerable amount of other data.

    Please do not follow this example for newly-supported games other than
    the Doom family (Doom, Heretic, Hexen, Strife, Hacx, Chex Quest).

    For new games it is probably better to use game-data-packager to ship only
    the non-distributable files, and ship DFSG files (such as icons
    and .desktop files) somewhere else.

    One way is to have the engine package contain the wrapper scripts,
    .desktop files etc. (e.g. src:openjk, src:iortcw). This is the simplest
    thing if the engine is unlikely to be used for other games and alternative
    engine versions are unlikely to be packaged.

    Another approach is to have a package for the engine (like src:ioquake3)
    and a package for the user-visible game (like src:quake, containing
    wrapper scripts, .desktop files etc.). This is more flexible if the engine
    can be used for other user-visible games (e.g. OpenArena, Nexuiz Classic)
    or there could reasonably be multiple packaged engines (e.g. Quakespasm,
    Darkplaces).
    """

    def __init__(self, shortname, data):
        super(DoomGameData, self).__init__(shortname, data)

        self.wikibase = 'http://doomwiki.org/wiki/'
        assert self.wiki

        if self.engine is None:
            self.engine = {
                    'deb': "chocolate-doom | doom-engine",
                    'generic': 'chocolate-doom'
                    }

        if self.genre is None:
            self.genre = 'First-person shooter'

        for package in self.packages.values():
            for main_wad in package.main_wads.values():
                if 'args' not in main_wad and package.expansion_for:
                    assert self.packages[package.expansion_for].only_file

    def construct_package(self, binary, data):
        return DoomPackage(binary, data)

    def construct_task(self, **kwargs):
        return DoomTask(self, **kwargs)

class DoomPackage(Package):
    def __init__(self, binary, data):
        super(DoomPackage, self).__init__(binary, data)

        assert 'install_to' not in data, self.name
        assert 'data_type' not in data, self.name

        self.install_to = '$assets/doom'

        if self.expansion_for or self.expansion_for_ext:
            self.data_type = 'PWAD'
        else:
            self.data_type = 'IWAD'

        if 'main_wads' in data:
            self.main_wads = data['main_wads']
        else:
            assert self.only_file
            self.main_wads = { self.only_file: {} }

        assert type(self.main_wads) == dict
        for main_wad in self.main_wads.values():
            assert type(main_wad) == dict
            if 'args' in main_wad:
                # assert that it has one string placeholder
                main_wad['args'] % 'deadbeef'

class DoomTask(PackagingTask):
    def fill_extra_files(self, per_package_state):
        super().fill_extra_files(per_package_state)
        package = per_package_state.package
        destdir = per_package_state.destdir

        for main_wad, quirks in package.main_wads.items():
            engine = self.packaging.substitute(package.engine or self.game.engine,
                    package.name)
            engine = engine.split('|')[-1].strip()
            engine = engine.split(maxsplit=1)[0]
            program = self.packaging.tool_for_package(engine)

            wad_base = os.path.splitext(main_wad)[0]

            pixdir = os.path.join(destdir, 'usr/share/pixmaps')
            mkdir_p(pixdir)
            # FIXME: would be nice if non-Doom games could replace this
            # Cacodemon with something appropriate
            desktop_file = package.name
            if len(package.main_wads) > 1:
                desktop_file += '-' + wad_base

            for basename in (
                    quirks.get('icon', wad_base),
                    package.name,
                    self.game.shortname,
                    'doom-common',
            ):
                from_ = os.path.join(ICONS, basename + '.png')
                if os.path.exists(from_):
                    install_data(from_,
                        os.path.join(pixdir, '%s.png' % desktop_file))
                    break
            else:
                raise AssertionError(
                    'doom-common.png should have existed in {}'.format(ICONS)
                )

            for ext in ('.svgz', '.svg'):
                from_ = os.path.splitext(from_)[0] + ext
                if os.path.exists(from_):
                    svgdir = os.path.join(destdir,
                                      'usr/share/icons/hicolor/scalable/apps')
                    mkdir_p(svgdir)
                    install_data(from_,
                        os.path.join(svgdir, desktop_file + ext))

            appdir = os.path.join(destdir, 'usr/share/applications')
            mkdir_p(appdir)

            desktop = configparser.RawConfigParser()
            desktop.optionxform = lambda option: option
            desktop['Desktop Entry'] = {}
            entry = desktop['Desktop Entry']
            entry['Name'] = package.longname or self.game.longname
            if 'name' in quirks:
                entry['Name'] += ' - ' + quirks['name']
            entry['GenericName'] = self.game.genre + ' game'
            entry['TryExec'] = program

            install_to = self.packaging.substitute(package.install_to,
                    package.name)

            if have_omg:
                wad_file = os.path.join(destdir,
                    self.packaging.substitute(package.install_to, package.name).strip('/'),
                    main_wad)
                wad_obj = WAD(wad_file)
                lump_obj = None
                for lump in 'DMENUPIC', 'TITLEPIC', 'INTERPIC':
                    try:
                        lump_obj = wad_obj.graphics[lump]
                        break
                    except KeyError:
                        continue
                if lump_obj is not None:
                    im = lump_obj.to_Image()
                    w, h = im.size
                    # maintain the proper 1.2:1 pixel aspect ratio
                    im = im.resize((5 * w, 6 * h), Image.NEAREST)
                    canvas = (256, 256)
                    im.thumbnail(canvas, Image.ANTIALIAS)
                    w, h = im.size
                    icon = Image.new('RGBA', canvas)
                    offset = (canvas[0] - w) // 2, (canvas[1] - h) // 2
                    icon.paste(im, offset)
                    pixdir = os.path.join(destdir, 'usr', 'share', 'icons', 'hicolor',
                        'x'.join([str(x) for x in canvas]), 'apps')
                    mkdir_p(pixdir)
                    icon.save(os.path.join(pixdir, '%s.png' % desktop_file))
            else:
                logger.warning(
                    'Unable to load omgifol and PIL modules. '
                    'No icons will get extracted from WAD files.'
                )

            if 'args' in quirks:
                pwad = os.path.join('/', install_to, main_wad)
                args = quirks['args'] % pwad
            elif package.expansion_for:
                iwad = self.game.packages[package.expansion_for].only_file
                assert iwad is not None, "Couldn't find %s's IWAD" % main_wad
                iwad = os.path.join('/', install_to, iwad)
                pwad = os.path.join('/', install_to, main_wad)
                args = ' '.join(('-iwad', iwad, '-file', pwad))
            else:
                iwad = os.path.join('/', install_to, main_wad)
                args = ' '.join(('-iwad', iwad))
            entry['Exec'] = program + ' ' + args
            entry['Icon'] = desktop_file
            entry['Terminal'] = 'false'
            entry['Type'] = 'Application'
            entry['Categories'] = 'Game;'
            entry['Keywords'] = wad_base + ';'

            with open(os.path.join(appdir, '%s.desktop' % desktop_file),
                      'w', encoding='utf-8') as output:
                 desktop.write(output, space_around_delimiters=False)

            per_package_state.lintian_overrides.add(
                'desktop-command-not-in-package {} '
                '[usr/share/applications/{}.desktop]'.format(
                    program, desktop_file,
                )
            )

        preinst_in = os.path.join(DATADIR, 'doom-common.preinst.in')
        if self.packaging.derives_from('deb') and os.path.isfile(preinst_in):
            for main_wad in package.main_wads:
                if main_wad in ('doom.wad', 'doom2.wad', 'plutonia.wad',
                        'tnt.wad'):
                    debdir = os.path.join(destdir, 'DEBIAN')
                    mkdir_p(debdir)
                    copy_with_substitutions(
                        open(preinst_in, encoding='utf-8'),
                        open(os.path.join(debdir, 'preinst'), 'w',
                            encoding='utf-8'),
                        IWAD=main_wad)
                    os.chmod(os.path.join(debdir, 'preinst'), 0o755)

GAME_DATA_SUBCLASS = DoomGameData