Mercurial > touhou
annotate pytouhou/formats/pbg3.py @ 52:ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 22 Aug 2011 22:37:14 +0200 |
parents | 6b2c7af2384c |
children | 3da4de9decd0 |
rev | line source |
---|---|
0 | 1 from pytouhou.utils.bitstream import BitStream |
2 import pytouhou.utils.lzss as lzss | |
3 | |
4 | |
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
5 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
6 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
7 ## 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
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
9 ## 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
|
10 ## 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
|
11 ## 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
|
12 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
13 ## 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
|
14 ## 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
|
15 ## 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
|
16 ## 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
|
17 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
18 |
0 | 19 class PBG3BitStream(BitStream): |
20 def read_int(self): | |
21 size = self.read(2) | |
22 return self.read((size + 1) * 8) | |
23 | |
24 | |
25 def read_string(self, maxsize): | |
26 string = [] | |
27 for i in range(maxsize): | |
28 byte = self.read(8) | |
29 if byte == 0: | |
30 break | |
31 string.append(byte) | |
32 return ''.join(chr(byte) for byte in string) | |
33 | |
34 | |
35 | |
36 class PBG3(object): | |
37 def __init__(self, entries, bitstream=None): | |
38 self.entries = entries | |
39 self.bitstream = bitstream #TODO | |
40 | |
41 | |
42 @classmethod | |
43 def read(cls, file): | |
44 magic = file.read(4) | |
45 if magic != b'PBG3': | |
46 raise Exception #TODO | |
47 | |
48 bitstream = PBG3BitStream(file) | |
49 entries = {} | |
50 | |
51 nb_entries = bitstream.read_int() | |
52 offset = bitstream.read_int() | |
53 bitstream.seek(offset) | |
54 for i in range(nb_entries): | |
55 unknown1 = bitstream.read_int() | |
56 unknown2 = bitstream.read_int() | |
57 checksum = bitstream.read_int() # Checksum of *compressed data* | |
58 offset = bitstream.read_int() | |
59 size = bitstream.read_int() | |
60 name = bitstream.read_string(255).decode('ascii') | |
61 entries[name] = (unknown1, unknown2, checksum, offset, size) | |
62 | |
63 return PBG3(entries, bitstream) | |
64 | |
65 | |
66 def list_files(self): | |
67 return self.entries.keys() | |
68 | |
69 | |
70 def extract(self, filename, check=False): | |
71 unkwn1, unkwn2, checksum, offset, size = self.entries[filename] | |
72 self.bitstream.seek(offset) | |
73 data = lzss.decompress(self.bitstream, size) | |
74 if check: | |
75 # Checking the checksum | |
76 compressed_size = self.bitstream.io.tell() - offset | |
77 self.bitstream.seek(offset) | |
78 value = 0 | |
79 for c in self.bitstream.io.read(compressed_size): | |
80 value += ord(c) | |
81 value &= 0xFFFFFFFF | |
82 if value != checksum: | |
83 print('Warning: corrupted data') #TODO | |
84 return data | |
85 |