11import os
2- from typing import BinaryIO
2+ from typing import BinaryIO , Generator
33from io import BytesIO , FileIO
44from PyCriCodecsEx .chunk import *
55from PyCriCodecsEx .utf import UTF , UTFBuilder
@@ -14,21 +14,24 @@ def _worker_do_compression(src : str, dst: str):
1414 compressed = CriCodecsEx .CriLaylaCompress (data )
1515 fdst .write (compressed )
1616@dataclass
17- class _PackFile ():
17+ class PackedFile ():
18+ """Helper class for packed files within a CPK."""
1819 stream : BinaryIO
1920 path : str
2021 offset : int
2122 size : int
2223 compressed : bool = False
2324
2425 def get_bytes (self ) -> bytes :
26+ """Get the raw bytes of the packed file, decompressing if necessary."""
2527 self .stream .seek (self .offset )
2628 data = self .stream .read (self .size )
2729 if self .compressed :
2830 data = CriCodecsEx .CriLaylaDecompress (data )
2931 return data
3032
3133 def save (self , path : str ):
34+ """Save the packed file to a specified path."""
3235 with open (path , "wb" ) as f :
3336 f .write (self .get_bytes ())
3437class _TOC ():
@@ -115,6 +118,9 @@ def _load_tocs(self) -> None:
115118
116119 @property
117120 def mode (self ):
121+ """Get the current mode of the CPK archive. [0,1,2,3]
122+
123+ See also CPKBuilder"""
118124 TOC , ITOC , GTOC = 'TOC' in self .tables , 'ITOC' in self .tables , 'GTOC' in self .tables
119125 if TOC and ITOC and GTOC :
120126 return 3
@@ -127,8 +133,8 @@ def mode(self):
127133 raise ValueError ("Unknown CPK mode." )
128134
129135 @property
130- def files (self ):
131- """Retrieves a list of all files in the CPK archive."""
136+ def files (self ) -> Generator [ PackedFile , None , None ] :
137+ """Creates a generator for all files in the CPK archive as PackedFile ."""
132138 if "TOC" in self .tables :
133139 toctable = self .tables ['TOC' ]
134140 rel_off = 0x800
@@ -139,10 +145,10 @@ def files(self):
139145 filename = filename [:250 ] + "_" + str (i ) # 250 because i might be 4 digits long.
140146 if toctable ['ExtractSize' ][i ] > toctable ['FileSize' ][i ]:
141147 self .stream .seek (rel_off + toctable ["FileOffset" ][i ], 0 )
142- yield _PackFile (self .stream , os .path .join (dirname ,filename ), self .stream .tell (), toctable ['FileSize' ][i ], compressed = True )
148+ yield PackedFile (self .stream , os .path .join (dirname ,filename ), self .stream .tell (), toctable ['FileSize' ][i ], compressed = True )
143149 else :
144150 self .stream .seek (rel_off + toctable ["FileOffset" ][i ], 0 )
145- yield _PackFile (self .stream , os .path .join (dirname ,filename ), self .stream .tell (), toctable ['FileSize' ][i ])
151+ yield PackedFile (self .stream , os .path .join (dirname ,filename ), self .stream .tell (), toctable ['FileSize' ][i ])
146152 elif "ITOC" in self .tables :
147153 toctableL = self .tables ["ITOC" ]['DataL' ][0 ]
148154 toctableH = self .tables ["ITOC" ]['DataH' ][0 ]
@@ -154,18 +160,18 @@ def files(self):
154160 if i in toctableH ['ID' ]:
155161 idx = toctableH ['ID' ].index (i )
156162 if toctableH ['ExtractSize' ][idx ] > toctableH ['FileSize' ][idx ]:
157- yield _PackFile (self .stream , str (i ), self .stream .tell (), toctableH ['FileSize' ][idx ], compressed = True )
163+ yield PackedFile (self .stream , str (i ), self .stream .tell (), toctableH ['FileSize' ][idx ], compressed = True )
158164 else :
159- yield _PackFile (self .stream , str (i ), self .stream .tell (), toctableH ['FileSize' ][idx ])
165+ yield PackedFile (self .stream , str (i ), self .stream .tell (), toctableH ['FileSize' ][idx ])
160166 if toctableH ['FileSize' ][idx ] % align != 0 :
161167 seek_size = (align - toctableH ['FileSize' ][idx ] % align )
162168 self .stream .seek (seek_size , 1 )
163169 elif i in toctableL ['ID' ]:
164170 idx = toctableL ['ID' ].index (i )
165171 if toctableL ['ExtractSize' ][idx ] > toctableL ['FileSize' ][idx ]:
166- yield _PackFile (self .stream , str (i ), self .stream .tell (), toctableL ['FileSize' ][idx ], compressed = True )
172+ yield PackedFile (self .stream , str (i ), self .stream .tell (), toctableL ['FileSize' ][idx ], compressed = True )
167173 else :
168- yield _PackFile (self .stream , str (i ), self .stream .tell (), toctableL ['FileSize' ][idx ])
174+ yield PackedFile (self .stream , str (i ), self .stream .tell (), toctableL ['FileSize' ][idx ])
169175 if toctableL ['FileSize' ][idx ] % align != 0 :
170176 seek_size = (align - toctableL ['FileSize' ][idx ] % align )
171177 self .stream .seek (seek_size , 1 )
0 commit comments