Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """A simple utility function to produce data pack files. | 6 """A simple utility function to produce data pack files. |
| 7 See base/pack_file* for details. | 7 See base/pack_file* for details. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import struct | 10 import struct |
| 11 | 11 |
| 12 FILE_FORMAT_VERSION = 3 | 12 FILE_FORMAT_VERSION = 4 |
| 13 HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries) | 13 HEADER_LENGTH = 3 * 4 # Three uint32s. (file version, number of entries and |
| 14 # encoding of text resources) | |
|
tony
2011/08/29 21:24:39
Can we use something smaller than an uint32 for th
marcvs
2011/08/30 07:58:55
Changed to uint8 (== unsigned char).
| |
| 15 BINARY = 0 | |
| 16 UTF8 = 1 | |
| 17 UTF16 = 2 | |
|
tony
2011/08/29 21:24:39
BINARY, UTF8, UTF16 = range(3)
marcvs
2011/08/30 07:58:55
Done. (also in the other files)
| |
| 14 | 18 |
| 15 class WrongFileVersion(Exception): | 19 class WrongFileVersion(Exception): |
| 16 pass | 20 pass |
| 17 | 21 |
| 18 def ReadDataPack(input_file): | 22 def ReadDataPack(input_file): |
| 19 """Reads a data pack file and returns a dictionary.""" | 23 """Reads a data pack file and returns a dictionary.""" |
|
tony
2011/08/29 21:24:39
It now returns a tuple. I would maybe make a smal
marcvs
2011/08/30 07:58:55
Done.
| |
| 20 data = open(input_file, "rb").read() | 24 data = open(input_file, "rb").read() |
| 21 original_data = data | 25 original_data = data |
| 22 | 26 |
| 23 # Read the header. | 27 # Read the header. |
| 24 version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH]) | 28 version, num_entries, encoding = struct.unpack("<III", data[:HEADER_LENGTH]) |
| 25 if version != FILE_FORMAT_VERSION: | 29 if version != FILE_FORMAT_VERSION: |
| 30 print "Wrong file version in ", input_file | |
| 26 raise WrongFileVersion | 31 raise WrongFileVersion |
| 27 | 32 |
| 28 resources = {} | 33 resources = {} |
| 29 if num_entries == 0: | 34 if num_entries == 0: |
| 30 return resources | 35 return resources, encoding |
| 31 | 36 |
| 32 # Read the index and data. | 37 # Read the index and data. |
| 33 data = data[HEADER_LENGTH:] | 38 data = data[HEADER_LENGTH:] |
| 34 kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32. | 39 kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32. |
| 35 for _ in range(num_entries): | 40 for _ in range(num_entries): |
| 36 id, offset = struct.unpack("<HI", data[:kIndexEntrySize]) | 41 id, offset = struct.unpack("<HI", data[:kIndexEntrySize]) |
| 37 data = data[kIndexEntrySize:] | 42 data = data[kIndexEntrySize:] |
| 38 next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize]) | 43 next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize]) |
| 39 resources[id] = original_data[offset:next_offset] | 44 resources[id] = original_data[offset:next_offset] |
| 40 | 45 |
| 41 return resources | 46 return resources, encoding |
| 42 | 47 |
| 43 def WriteDataPack(resources, output_file): | 48 def WriteDataPack(resources, output_file, encoding): |
| 44 """Write a map of id=>data into output_file as a data pack.""" | 49 """Write a map of id=>data into output_file as a data pack.""" |
| 45 ids = sorted(resources.keys()) | 50 ids = sorted(resources.keys()) |
| 46 file = open(output_file, "wb") | 51 file = open(output_file, "wb") |
| 47 | 52 |
| 48 # Write file header. | 53 # Write file header. |
| 49 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids))) | 54 file.write(struct.pack("<III", FILE_FORMAT_VERSION, len(ids), encoding)) |
| 50 | 55 |
| 51 # Each entry is a uint16 and a uint32. We have one extra entry for the last | 56 # Each entry is a uint16 and a uint32. We have one extra entry for the last |
| 52 # item. | 57 # item. |
| 53 index_length = (len(ids) + 1) * (2 + 4) | 58 index_length = (len(ids) + 1) * (2 + 4) |
| 54 | 59 |
| 55 # Write index. | 60 # Write index. |
| 56 data_offset = HEADER_LENGTH + index_length | 61 data_offset = HEADER_LENGTH + index_length |
| 57 for id in ids: | 62 for id in ids: |
| 58 file.write(struct.pack("<HI", id, data_offset)) | 63 file.write(struct.pack("<HI", id, data_offset)) |
| 59 data_offset += len(resources[id]) | 64 data_offset += len(resources[id]) |
| 60 | 65 |
| 61 file.write(struct.pack("<HI", 0, data_offset)) | 66 file.write(struct.pack("<HI", 0, data_offset)) |
| 62 | 67 |
| 63 # Write data. | 68 # Write data. |
| 64 for id in ids: | 69 for id in ids: |
| 65 file.write(resources[id]) | 70 file.write(resources[id]) |
| 66 | 71 |
| 67 def main(): | 72 def main(): |
| 68 # Just write a simple file. | 73 # Just write a simple file. |
| 69 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } | 74 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } |
| 70 WriteDataPack(data, "datapack1.pak") | 75 WriteDataPack(data, "datapack1.pak", UTF8) |
| 71 data2 = { 1000: "test", 5: "five" } | 76 data2 = { 1000: "test", 5: "five" } |
| 72 WriteDataPack(data2, "datapack2.pak") | 77 WriteDataPack(data2, "datapack2.pak", UTF8) |
| 73 print "wrote datapack1 and datapack2 to current directory." | 78 print "wrote datapack1 and datapack2 to current directory." |
| 74 | 79 |
| 75 if __name__ == '__main__': | 80 if __name__ == '__main__': |
| 76 main() | 81 main() |
| OLD | NEW |