blender_asset_tracer/cli/blocks.py

149 lines
4.6 KiB
Python
Raw Permalink Normal View History

2021-10-18 15:54:04 +02:00
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
#
# (c) 2018, Blender Foundation - Sybren A. Stüvel
"""List count and total size of datablocks in a blend file."""
import collections
import logging
import pathlib
from blender_asset_tracer import blendfile
from . import common
log = logging.getLogger(__name__)
class BlockTypeInfo:
def __init__(self):
self.total_bytes = 0
self.num_blocks = 0
self.sizes = []
self.blocks = []
2023-01-10 11:41:55 +01:00
self.name = "unset"
2021-10-18 15:54:04 +02:00
def add_parser(subparsers):
"""Add argparser for this subcommand."""
2023-01-10 11:41:55 +01:00
parser = subparsers.add_parser("blocks", help=__doc__)
2021-10-18 15:54:04 +02:00
parser.set_defaults(func=cli_blocks)
2023-01-10 11:41:55 +01:00
parser.add_argument("blendfile", type=pathlib.Path)
parser.add_argument(
"-d",
"--dump",
default=False,
action="store_true",
help="Hex-dump the biggest block",
)
parser.add_argument(
"-l",
"--limit",
default=10,
type=int,
help="Limit the number of DNA types shown, default is 10",
)
2021-10-18 15:54:04 +02:00
def by_total_bytes(info: BlockTypeInfo) -> int:
return info.total_bytes
def block_key(block: blendfile.BlendFileBlock) -> str:
2023-01-10 11:41:55 +01:00
return "%s-%s" % (block.dna_type_name, block.code.decode())
2021-10-18 15:54:04 +02:00
def cli_blocks(args):
bpath = args.blendfile
if not bpath.exists():
2023-01-10 11:41:55 +01:00
log.fatal("File %s does not exist", args.blendfile)
2021-10-18 15:54:04 +02:00
return 3
per_blocktype = collections.defaultdict(BlockTypeInfo)
2023-01-10 11:41:55 +01:00
print("Opening %s" % bpath)
2021-10-18 15:54:04 +02:00
bfile = blendfile.BlendFile(bpath)
2023-01-10 11:41:55 +01:00
print("Inspecting %s" % bpath)
2021-10-18 15:54:04 +02:00
for block in bfile.blocks:
2023-01-10 11:41:55 +01:00
if block.code == b"DNA1":
2021-10-18 15:54:04 +02:00
continue
index_as = block_key(block)
info = per_blocktype[index_as]
info.name = index_as
info.total_bytes += block.size
info.num_blocks += 1
info.sizes.append(block.size)
info.blocks.append(block)
2023-01-10 11:41:55 +01:00
fmt = "%-35s %10s %10s %10s %10s"
print(fmt % ("Block type", "Total Size", "Num blocks", "Avg Size", "Median"))
print(fmt % (35 * "-", 10 * "-", 10 * "-", 10 * "-", 10 * "-"))
2021-10-18 15:54:04 +02:00
infos = sorted(per_blocktype.values(), key=by_total_bytes, reverse=True)
2023-01-10 11:41:55 +01:00
for info in infos[: args.limit]:
2021-10-18 15:54:04 +02:00
median_size = sorted(info.sizes)[len(info.sizes) // 2]
2023-01-10 11:41:55 +01:00
print(
fmt
% (
info.name,
common.humanize_bytes(info.total_bytes),
info.num_blocks,
common.humanize_bytes(info.total_bytes // info.num_blocks),
common.humanize_bytes(median_size),
)
)
print(70 * "-")
2021-10-18 15:54:04 +02:00
# From the blocks of the most space-using category, the biggest block.
2023-01-10 11:41:55 +01:00
biggest_block = sorted(infos[0].blocks, key=lambda blck: blck.size, reverse=True)[0]
print(
"Biggest %s block is %s at address %s"
% (
block_key(biggest_block),
common.humanize_bytes(biggest_block.size),
biggest_block.addr_old,
)
)
print("Finding what points there")
2021-10-18 15:54:04 +02:00
addr_to_find = biggest_block.addr_old
found_pointer = False
for block in bfile.blocks:
for prop_path, prop_value in block.items_recursive():
if not isinstance(prop_value, int) or prop_value != addr_to_find:
continue
2023-01-10 11:41:55 +01:00
print(" ", block, prop_path)
2021-10-18 15:54:04 +02:00
found_pointer = True
if not found_pointer:
2023-01-10 11:41:55 +01:00
print("Nothing points there")
2021-10-18 15:54:04 +02:00
if args.dump:
2023-01-10 11:41:55 +01:00
print("Hexdump:")
2021-10-18 15:54:04 +02:00
bfile.fileobj.seek(biggest_block.file_offset)
data = bfile.fileobj.read(biggest_block.size)
line_len_bytes = 32
import codecs
2023-01-10 11:41:55 +01:00
2021-10-18 15:54:04 +02:00
for offset in range(0, len(data), line_len_bytes):
2023-01-10 11:41:55 +01:00
line = codecs.encode(data[offset : offset + line_len_bytes], "hex").decode()
print(
"%6d -" % offset,
" ".join(line[i : i + 2] for i in range(0, len(line), 2)),
)