Mercurial > touhou
annotate pytouhou/formats/pbg3.py @ 181:184196480f59
Don’t use the useless eff00.anm and implement particles (grazing, death, and more).
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 23 Oct 2011 14:50:44 -0700 |
parents | ac2e5e1c2c3c |
children | 88361534c77e |
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 | |
97 | 46 def __enter__(self): |
47 return self | |
48 | |
49 | |
50 def __exit__(self, type, value, traceback): | |
51 return self.bitstream.__exit__(type, value, traceback) | |
52 | |
53 | |
0 | 54 @classmethod |
55 def read(cls, file): | |
56 magic = file.read(4) | |
57 if magic != b'PBG3': | |
58 raise Exception #TODO | |
59 | |
60 bitstream = PBG3BitStream(file) | |
61 entries = {} | |
62 | |
63 nb_entries = bitstream.read_int() | |
64 offset = bitstream.read_int() | |
65 bitstream.seek(offset) | |
66 for i in range(nb_entries): | |
67 unknown1 = bitstream.read_int() | |
68 unknown2 = bitstream.read_int() | |
69 checksum = bitstream.read_int() # Checksum of *compressed data* | |
70 offset = bitstream.read_int() | |
71 size = bitstream.read_int() | |
72 name = bitstream.read_string(255).decode('ascii') | |
73 entries[name] = (unknown1, unknown2, checksum, offset, size) | |
74 | |
75 return PBG3(entries, bitstream) | |
76 | |
77 | |
78 def list_files(self): | |
79 return self.entries.keys() | |
80 | |
81 | |
82 def extract(self, filename, check=False): | |
83 unkwn1, unkwn2, checksum, offset, size = self.entries[filename] | |
84 self.bitstream.seek(offset) | |
85 data = lzss.decompress(self.bitstream, size) | |
86 if check: | |
87 # Checking the checksum | |
88 compressed_size = self.bitstream.io.tell() - offset | |
89 self.bitstream.seek(offset) | |
90 value = 0 | |
91 for c in self.bitstream.io.read(compressed_size): | |
92 value += ord(c) | |
93 value &= 0xFFFFFFFF | |
94 if value != checksum: | |
58 | 95 logger.warn('corrupted data!') |
0 | 96 return data |
97 |