Example #1
0
def _ExtractMain(args):
    pak = data_pack.ReadDataPack(args.pak_file)
    if args.textual_id:
        info_dict = data_pack.ReadGrdInfo(args.pak_file)
    for resource_id, payload in pak.resources.items():
        filename = (info_dict[resource_id].textual_id
                    if args.textual_id else str(resource_id))
        path = os.path.join(args.output_dir, filename)
        with open(path, 'w') as f:
            f.write(payload)
Example #2
0
def _ListMain(args):
  pak = data_pack.ReadDataPack(args.pak_file)
  if args.textual_id or args.path:
    info_dict = data_pack.ReadGrdInfo(args.pak_file)
    fmt = ''.join([
        '{id}', ' = {textual_id}' if args.textual_id else '',
        ' @ {path}' if args.path else '', '\n'
    ])
    for resource_id in sorted(pak.resources):
      item = info_dict[resource_id]
      args.output.write(
          fmt.format(textual_id=item.textual_id, id=item.id, path=item.path))
  else:
    for resource_id in sorted(pak.resources):
      args.output.write('%d\n' % resource_id)
Example #3
0
def _PrintMain(args):
    pak = data_pack.ReadDataPack(args.pak_file)
    if args.textual_id:
        info_dict = data_pack.ReadGrdInfo(args.pak_file)
    output = args.output
    encoding = 'binary'
    if pak.encoding == 1:
        encoding = 'utf-8'
    elif pak.encoding == 2:
        encoding = 'utf-16'
    else:
        encoding = '?' + str(pak.encoding)

    output.write('version: {}\n'.format(pak.version))
    output.write('encoding: {}\n'.format(encoding))
    output.write('num_resources: {}\n'.format(len(pak.resources)))
    output.write('num_aliases: {}\n'.format(len(pak.aliases)))
    breakdown = ', '.join('{}: {}'.format(*x) for x in pak.sizes)
    output.write('total_size: {} ({})\n'.format(pak.sizes.total, breakdown))

    try_decode = args.decode and encoding.startswith('utf')
    # Print IDs in ascending order, since that's the order in which they appear in
    # the file (order is lost by Python dict).
    for resource_id in sorted(pak.resources):
        data = pak.resources[resource_id]
        canonical_id = pak.aliases.get(resource_id, resource_id)
        desc = '<data>'
        if try_decode:
            try:
                desc = six.text_type(data, encoding)
                if len(desc) > 60:
                    desc = desc[:60] + u'...'
                desc = desc.replace('\n', '\\n')
            except UnicodeDecodeError:
                pass
        sha1 = hashlib.sha1(data).hexdigest()[:10]
        if args.textual_id:
            textual_id = info_dict[resource_id].textual_id
            canonical_textual_id = info_dict[canonical_id].textual_id
            output.write(
                u'Entry(id={}, canonical_id={}, size={}, sha1={}): {}\n'.
                format(textual_id, canonical_textual_id, len(data), sha1,
                       desc).encode('utf-8'))
        else:
            output.write(
                u'Entry(id={}, canonical_id={}, size={}, sha1={}): {}\n'.
                format(resource_id, canonical_id, len(data), sha1,
                       desc).encode('utf-8'))