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

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: evan's review feedback 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
« no previous file with comments | « chrome/browser/themes/browser_theme_pack.cc ('k') | tools/grit/grit/format/data_pack.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 raise WrongFileVersion 26 raise WrongFileVersion
27 27
28 resources = {} 28 resources = {}
29 # Read the index and data. 29 # Read the index and data.
30 data = data[HEADER_LENGTH:] 30 data = data[HEADER_LENGTH:]
31 kIndexEntrySize = 3 * 4 # Each entry is 3 uint32s. 31 kIndexEntrySize = 2 + 2 * 4 # Each entry is 1 uint16 and 2 uint32s.
32 for _ in range(num_entries): 32 for _ in range(num_entries):
33 id, offset, length = struct.unpack("<III", data[:kIndexEntrySize]) 33 id, offset, length = struct.unpack("<HII", data[:kIndexEntrySize])
34 data = data[kIndexEntrySize:] 34 data = data[kIndexEntrySize:]
35 resources[id] = original_data[offset:offset + length] 35 resources[id] = original_data[offset:offset + length]
36 36
37 return resources 37 return resources
38 38
39 def WriteDataPack(resources, output_file): 39 def WriteDataPack(resources, output_file):
40 """Write a map of id=>data into output_file as a data pack.""" 40 """Write a map of id=>data into output_file as a data pack."""
41 ids = sorted(resources.keys()) 41 ids = sorted(resources.keys())
42 file = open(output_file, "wb") 42 file = open(output_file, "wb")
43 43
44 # Write file header. 44 # Write file header.
45 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids))) 45 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids)))
46 46
47 index_length = len(ids) * 3 * 4 # Each entry is 3 uint32s. 47 # Each entry is 1 uint16 and 2 uint32s.
48 index_length = len(ids) * (2 + 2 * 4)
48 49
49 # Write index. 50 # Write index.
50 data_offset = HEADER_LENGTH + index_length 51 data_offset = HEADER_LENGTH + index_length
51 for id in ids: 52 for id in ids:
52 file.write(struct.pack("<III", id, data_offset, len(resources[id]))) 53 file.write(struct.pack("<HII", id, data_offset, len(resources[id])))
53 data_offset += len(resources[id]) 54 data_offset += len(resources[id])
54 55
55 # Write data. 56 # Write data.
56 for id in ids: 57 for id in ids:
57 file.write(resources[id]) 58 file.write(resources[id])
58 59
59 def main(): 60 def main():
60 # Just write a simple file. 61 # Just write a simple file.
61 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } 62 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
62 WriteDataPack(data, "datapack1.pak") 63 WriteDataPack(data, "datapack1.pak")
63 data2 = { 1000: "test", 5: "five" } 64 data2 = { 1000: "test", 5: "five" }
64 WriteDataPack(data2, "datapack2.pak") 65 WriteDataPack(data2, "datapack2.pak")
65 print "wrote datapack1 and datapack2 to current directory." 66 print "wrote datapack1 and datapack2 to current directory."
66 67
67 if __name__ == '__main__': 68 if __name__ == '__main__':
68 main() 69 main()
OLDNEW
« no previous file with comments | « chrome/browser/themes/browser_theme_pack.cc ('k') | tools/grit/grit/format/data_pack.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698