import collections
import struct
import sys
def ReadFile(filename, encoding):
mode = 'rb' if encoding == 0 else 'rU'
with open(filename, mode) as f:
data = f.read()
if encoding not in (0, 1):
data = data.decode(encoding)
return data
PACK_FILE_VERSION = 4
HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and
# one uint8 (encoding of text resources)
def UnpackDataPack(input_file):
"""Reads a data pack file and returns a dictionary."""
data = ReadFile(input_file, 0)
original_data = data
# Read the header.
version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])
if version != PACK_FILE_VERSION:
print "Wrong file version in ", input_file
raise WrongFileVersion
resources = {}
if num_entries == 0:
return DataPackCo