File: umod.py

package info (click to toggle)
game-data-packager 63
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 15,920 kB
  • sloc: python: 10,537; sh: 515; makefile: 497
file content (124 lines) | stat: -rwxr-xr-x 4,052 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3
# encoding=utf-8
#
# Copyright © 2015 Simon McVittie <smcv@debian.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# You can find the GPL license text on a Debian system under
# /usr/share/common-licenses/GPL-2.

import io
import os
import struct
import sys
import unittest

if 'GDP_UNINSTALLED' not in os.environ:
    sys.path.insert(0, '/usr/share/game-data-packager')
    sys.path.insert(0, '/usr/share/games/game-data-packager')

from game_data_packager.unpack.umod import Umod

HELLO_TXT = b'Hello, world!\n'

MANIFEST_INT = b'''[Setup]\r
LocalProduct=Example Mod\r
ReadMe=Help\\Hello.txt\r
ProductURL=http://example.com/\r
VersionURL=http://example.com/example100/\r
Developer=Game Data Packager\r
DeveloperURL=https://tracker.debian.org/pkg/game-data-packager\r
Logo=Help\\Logo.bmp'''

MANIFEST_INI = ('''[Setup]\r
Product=ExampleMod\r
Version=100\r
Requires=DebianRequirement\r
SrcPath=C:\\temp\r
Group=SetupGroup\r
Group=HelpGroup\r
\r
[DebianRequirement]\r
Product=Debian\r
Version=8\r
\r
[SetupGroup]\r
Copy=(Src=System\\Manifest.ini,Master=System\\Manifest.ini,Size=1234,Flags=3)\r
Copy=(Src=System\\Manifest.int,Master=System\\Manifest.int,Size=%d,Flags=3)\r
\r
[HelpGroup]\r
File=(Src=Help\\Hello.txt,Size=%d)\r
\r
''' % (len(MANIFEST_INT), len(HELLO_TXT))).encode('ascii')

def get_sample_umod():
    def sized_string(b):
        return bytes([len(b) + 1]) + b + b'\0'

    ret = []
    offset = 0
    ret.append(MANIFEST_INI)
    ret.append(MANIFEST_INT)
    ret.append(HELLO_TXT)
    ret.append(b'\x03')
    ret.append(sized_string(b'System\\Manifest.ini'))
    ret.append(struct.pack('<III', offset, len(MANIFEST_INI), 3))
    offset += len(MANIFEST_INI)
    ret.append(sized_string(b'System\\Manifest.int'))
    ret.append(struct.pack('<III', offset, len(MANIFEST_INT), 3))
    offset += len(MANIFEST_INT)
    ret.append(sized_string(b'Help\\Hello.txt'))
    ret.append(struct.pack('<III', offset, len(HELLO_TXT), 3))
    offset += len(HELLO_TXT)

    ret = [b''.join(ret)]

    ret.append(struct.pack('<IIIII',
        0x9fe3c5a3,         # magic number
        offset,             # offset of table-of-contents
        len(ret[0]) + 20,   # total file length
        1,                  # flags, seem to be 1 in real umods
        0xdeadbeef))        # checksum, not checked at the moment
    return b''.join(ret)

SAMPLE_UMOD = get_sample_umod()

class UmodTestCase(unittest.TestCase):
    def setUp(self):
        self.umod = Umod(io.BytesIO(SAMPLE_UMOD))

    def test_basics(self):
        self.assertEqual(self.umod.product, 'ExampleMod')
        self.assertEqual(self.umod.version, '100')
        self.assertEqual(sorted(self.umod.requirements.keys()),
                ['DebianRequirement'])
        self.assertEqual([s.name for s in self.umod.sections],
                ['DebianRequirement', 'SetupGroup', 'HelpGroup'])
        self.assertEqual(sorted(self.umod.groups.keys()),
                ['HelpGroup', 'SetupGroup'])
        self.assertEqual(self.umod.entry_order,
                ['System/Manifest.ini', 'System/Manifest.int',
                    'Help/Hello.txt'])
        self.assertEqual(sorted(self.umod.entries.keys()),
                ['Help/Hello.txt', 'System/Manifest.ini',
                    'System/Manifest.int'])
        self.assertEqual(list(self.umod.unparsed),
                [('SrcPath', 'C:\\temp')])

        with self.umod.open('Help/Hello.txt') as hello:
            self.assertEqual(hello.read(5), HELLO_TXT[:5])
            self.assertEqual(hello.read(), HELLO_TXT[5:])

    def tearDown(self):
        del self.umod

if __name__ == '__main__':
    unittest.main(verbosity=2)