pak文件解析工具,由吾爱大神编辑制作,能够解析pak文件,由Python环境制作,使用相应的编辑软件即可加载并解析,另外还附带一个pak的测试解析文件,可以通过代码来进行解析测试效果,有相关pak文件解析工具使用需求的朋友们不妨试试吧!
importos
importstruct
classBinary:
def__init__(self, data: [bytearray, bytes,str]):
self.index=0
ifisinstance(data, bytearray):
self.data=data
elifisinstance(data, bytes):
self.data=bytearray(data)
elifisinstance(data,str):
self.data=bytearray(data.encode(encoding='utf-8'))
else:
raiseTypeError('data type is invalid:', data.__class__.__name__)
self.size=len(self.data)
defread_int8(self)->int:
ret=self.data[self.index]
self.index+=1
returnret
defread_int16(self)->int:
ret,=struct.unpack('h',self.data[self.index:self.index+2])
self.index+=2
returnret
defread_int32(self)->int:
ret,=struct.unpack('i',self.data[self.index:self.index+4])
self.index+=4
returnret
defread_int64(self)->int:
ret,=struct.unpack('q',self.data[self.index:self.index+8])
self.index+=8
returnret
defread_string(self)->str:
pass
defread_bytes(self, size:int)-> bytearray:
ifsize >self.rest():
ret=self.data[self.index:]
self.index+=self.rest()
returnret
ret=self.data[self.index:self.index+size]
self.index+=size
returnret
def__len__(self):
returnlen(self.data)
defrest(self)->int:
returnlen(self.data)-self.index
@staticmethod
defbytes2str(data: [bytearray, bytes]):
out=''
ifdataisNoneorlen(data)==0:
return'Null'
forbindata:
ifout:
out+=','
out+='0x%02X'%b
out+=' '
out+=data.decode('utf-8')
returnout
classPakData:
def__init__(self, br: Binary):
self.version=br.read_int32()
self.count=br.read_int32()
self.coding=br.read_int8()
self.resources=list()
foriinrange(self.count):
item={
'id': br.read_int16(),
'offset': br.read_int32(),
'data': bytearray(),
'size':0
}
self.resources.append(item)
fori, iteminenumerate(self.resources):
ifi+1<self.count:
item['size']=self.resources[i+1]['offset']-item['offset']
else:
item['size']=len(br)-item['offset']
item['data']=br.data[item['offset']:item['offset']+item['size']]
classPakFile:
def__init__(self, path:str):
self.path=path
ifos.path.exists(path):
self.decode()
return
self.data=None
defset_path(self, path):
self.path=path
defdecode(self)->int:
ifos.path.exists(self.path)isFalse:
return-1
withopen(self.path, mode='rb') asfile:
raw=bytearray(file.read())
file.close()
br=Binary(raw)
self.data=PakData(br)
defmain():
pak=PakFile(r'.zh-CN.pak')
ifisinstance(pak.data, PakData):
print('版本:', pak.data.version)
print('数量:', pak.data.count)
print('编码:', pak.data.coding)
foriinrange(pak.data.count):
print('ID:', pak.data.resources[i]['id'])
print('偏移:', pak.data.resources[i]['offset'])
print('大小:', pak.data.resources[i]['size'])
print('内容:', Binary.bytes2str(pak.data.resources[i]['data']))
if__name__=='__main__':
main()