Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: tools/data_pack/data_pack.py

Issue 7555003: Update the .pak file format to version 2: Make resource ids 16bit (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2008 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 = 1 12 FILE_FORMAT_VERSION = 2
13 HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries) 13 HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries)
14 14
15 class WrongFileVersion(Exception): 15 class WrongFileVersion(Exception):
16 pass 16 pass
17 17
18 def ReadDataPack(input_file): 18 def ReadDataPack(input_file):
19 """Reads a data pack file and returns a dictionary.""" 19 """Reads a data pack file and returns a dictionary."""
20 data = open(input_file, "rb").read() 20 data = open(input_file, "rb").read()
21 original_data = data 21 original_data = data
22 22
23 # Read the header. 23 # Read the header.
24 version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH]) 24 version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH])
25 if version != FILE_FORMAT_VERSION: 25 if version != FILE_FORMAT_VERSION:
26 print input_file
Evan Martin 2011/08/03 18:43:09 Probably didn't mean to leave this in
tony 2011/08/04 17:42:53 Oops, removed.
26 raise WrongFileVersion 27 raise WrongFileVersion
27 28
28 resources = {} 29 resources = {}
29 # Read the index and data. 30 # Read the index and data.
30 data = data[HEADER_LENGTH:] 31 data = data[HEADER_LENGTH:]
31 kIndexEntrySize = 3 * 4 # Each entry is 3 uint32s. 32 kIndexEntrySize = 2 + 2 * 4 # Each entry is 1 uint16 and 2 uint32s.
32 for _ in range(num_entries): 33 for _ in range(num_entries):
33 id, offset, length = struct.unpack("<III", data[:kIndexEntrySize]) 34 id, offset, length = struct.unpack("<HII", data[:kIndexEntrySize])
34 data = data[kIndexEntrySize:] 35 data = data[kIndexEntrySize:]
35 resources[id] = original_data[offset:offset + length] 36 resources[id] = original_data[offset:offset + length]
36 37
37 return resources 38 return resources
38 39
39 def WriteDataPack(resources, output_file): 40 def WriteDataPack(resources, output_file):
40 """Write a map of id=>data into output_file as a data pack.""" 41 """Write a map of id=>data into output_file as a data pack."""
41 ids = sorted(resources.keys()) 42 ids = sorted(resources.keys())
42 file = open(output_file, "wb") 43 file = open(output_file, "wb")
43 44
44 # Write file header. 45 # Write file header.
45 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids))) 46 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids)))
46 47
47 index_length = len(ids) * 3 * 4 # Each entry is 3 uint32s. 48 # Each entry is 1 uint16 and 2 uint32s.
49 index_length = len(ids) * (2 + 2 * 4)
Evan Martin 2011/08/03 18:43:09 Too bad this is duplicated with the above code. N
48 50
49 # Write index. 51 # Write index.
50 data_offset = HEADER_LENGTH + index_length 52 data_offset = HEADER_LENGTH + index_length
51 for id in ids: 53 for id in ids:
52 file.write(struct.pack("<III", id, data_offset, len(resources[id]))) 54 file.write(struct.pack("<HII", id, data_offset, len(resources[id])))
53 data_offset += len(resources[id]) 55 data_offset += len(resources[id])
54 56
55 # Write data. 57 # Write data.
56 for id in ids: 58 for id in ids:
57 file.write(resources[id]) 59 file.write(resources[id])
58 60
59 def main(): 61 def main():
60 # Just write a simple file. 62 # Just write a simple file.
61 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } 63 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
62 WriteDataPack(data, "datapack1.pak") 64 WriteDataPack(data, "datapack1.pak")
63 data2 = { 1000: "test", 5: "five" } 65 data2 = { 1000: "test", 5: "five" }
64 WriteDataPack(data2, "datapack2.pak") 66 WriteDataPack(data2, "datapack2.pak")
65 print "wrote datapack1 and datapack2 to current directory." 67 print "wrote datapack1 and datapack2 to current directory."
66 68
67 if __name__ == '__main__': 69 if __name__ == '__main__':
68 main() 70 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698