| Index: tools/data_pack/data_pack.py
|
| diff --git a/tools/data_pack/data_pack.py b/tools/data_pack/data_pack.py
|
| index ffd2cb2b0aa8639f52a4ea6ac5297f52a11a8a80..4e1910ad1fbd5242279bca85a5960bc85999702b 100755
|
| --- a/tools/data_pack/data_pack.py
|
| +++ b/tools/data_pack/data_pack.py
|
| @@ -9,25 +9,33 @@ See base/pack_file* for details.
|
|
|
| import struct
|
|
|
| -FILE_FORMAT_VERSION = 3
|
| -HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries)
|
| +FILE_FORMAT_VERSION = 4
|
| +HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and
|
| + # one uint8 (encoding of text resources)
|
| +BINARY, UTF8, UTF16 = range(3)
|
|
|
| class WrongFileVersion(Exception):
|
| pass
|
|
|
| +class DataPackContents:
|
| + def __init__(self, resources, encoding):
|
| + self.resources = resources
|
| + self.encoding = encoding
|
| +
|
| def ReadDataPack(input_file):
|
| """Reads a data pack file and returns a dictionary."""
|
| data = open(input_file, "rb").read()
|
| original_data = data
|
|
|
| # Read the header.
|
| - version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH])
|
| + version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])
|
| if version != FILE_FORMAT_VERSION:
|
| + print "Wrong file version in ", input_file
|
| raise WrongFileVersion
|
|
|
| resources = {}
|
| if num_entries == 0:
|
| - return resources
|
| + return DataPackContents(resources, encoding)
|
|
|
| # Read the index and data.
|
| data = data[HEADER_LENGTH:]
|
| @@ -38,15 +46,15 @@ def ReadDataPack(input_file):
|
| next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])
|
| resources[id] = original_data[offset:next_offset]
|
|
|
| - return resources
|
| + return DataPackContents(resources, encoding)
|
|
|
| -def WriteDataPack(resources, output_file):
|
| +def WriteDataPack(resources, output_file, encoding):
|
| """Write a map of id=>data into output_file as a data pack."""
|
| ids = sorted(resources.keys())
|
| file = open(output_file, "wb")
|
|
|
| # Write file header.
|
| - file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids)))
|
| + file.write(struct.pack("<IIB", FILE_FORMAT_VERSION, len(ids), encoding))
|
|
|
| # Each entry is a uint16 and a uint32. We have one extra entry for the last
|
| # item.
|
| @@ -67,9 +75,9 @@ def WriteDataPack(resources, output_file):
|
| def main():
|
| # Just write a simple file.
|
| data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
|
| - WriteDataPack(data, "datapack1.pak")
|
| + WriteDataPack(data, "datapack1.pak", UTF8)
|
| data2 = { 1000: "test", 5: "five" }
|
| - WriteDataPack(data2, "datapack2.pak")
|
| + WriteDataPack(data2, "datapack2.pak", UTF8)
|
| print "wrote datapack1 and datapack2 to current directory."
|
|
|
| if __name__ == '__main__':
|
|
|