Mercurial > touhou
annotate pytouhou/formats/pbg3.py @ 83:fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 03 Sep 2011 22:22:58 +0200 |
parents | 3da4de9decd0 |
children | ac2e5e1c2c3c |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
14 |
58 | 15 from pytouhou.utils.bitstream import BitStream |
16 import pytouhou.utils.lzss as lzss | |
17 | |
18 from pytouhou.utils.helpers import get_logger | |
19 | |
20 logger = get_logger(__name__) | |
21 | |
22 | |
0 | 23 class PBG3BitStream(BitStream): |
24 def read_int(self): | |
25 size = self.read(2) | |
26 return self.read((size + 1) * 8) | |
27 | |
28 | |
29 def read_string(self, maxsize): | |
30 string = [] | |
31 for i in range(maxsize): | |
32 byte = self.read(8) | |
33 if byte == 0: | |
34 break | |
35 string.append(byte) | |
36 return ''.join(chr(byte) for byte in string) | |
37 | |
38 | |
39 | |
40 class PBG3(object): | |
41 def __init__(self, entries, bitstream=None): | |
42 self.entries = entries | |
43 self.bitstream = bitstream #TODO | |
44 | |
45 | |
46 @classmethod | |
47 def read(cls, file): | |
48 magic = file.read(4) | |
49 if magic != b'PBG3': | |
50 raise Exception #TODO | |
51 | |
52 bitstream = PBG3BitStream(file) | |
53 entries = {} | |
54 | |
55 nb_entries = bitstream.read_int() | |
56 offset = bitstream.read_int() | |
57 bitstream.seek(offset) | |
58 for i in range(nb_entries): | |
59 unknown1 = bitstream.read_int() | |
60 unknown2 = bitstream.read_int() | |
61 checksum = bitstream.read_int() # Checksum of *compressed data* | |
62 offset = bitstream.read_int() | |
63 size = bitstream.read_int() | |
64 name = bitstream.read_string(255).decode('ascii') | |
65 entries[name] = (unknown1, unknown2, checksum, offset, size) | |
66 | |
67 return PBG3(entries, bitstream) | |
68 | |
69 | |
70 def list_files(self): | |
71 return self.entries.keys() | |
72 | |
73 | |
74 def extract(self, filename, check=False): | |
75 unkwn1, unkwn2, checksum, offset, size = self.entries[filename] | |
76 self.bitstream.seek(offset) | |
77 data = lzss.decompress(self.bitstream, size) | |
78 if check: | |
79 # Checking the checksum | |
80 compressed_size = self.bitstream.io.tell() - offset | |
81 self.bitstream.seek(offset) | |
82 value = 0 | |
83 for c in self.bitstream.io.read(compressed_size): | |
84 value += ord(c) | |
85 value &= 0xFFFFFFFF | |
86 if value != checksum: | |
58 | 87 logger.warn('corrupted data!') |
0 | 88 return data |
89 |