Added error handling and made minor adjustments

This commit is contained in:
Campbell 2024-02-29 23:40:33 -05:00
parent c9c4aa5b62
commit b241124240

View File

@ -15,9 +15,9 @@ class ContentRecord:
"""Creates a content record object that contains the details of a content contained in a title.""" """Creates a content record object that contains the details of a content contained in a title."""
cid: int # Content ID cid: int # Content ID
index: int # Index in the list of contents index: int # Index in the list of contents
content_type: int # normal: 0x0001; dlc: 0x4001; shared: 0x8001 content_type: int # Normal: 0x0001, DLC: 0x4001, Shared: 0x8001
content_size: int content_size: int
content_hash: bytearray # SHA1 hash content content_hash: bytes # SHA1 hash content
class TMD: class TMD:
@ -42,8 +42,7 @@ class TMD:
self.title_version: int self.title_version: int
self.num_contents: int self.num_contents: int
self.boot_index: int self.boot_index: int
self.content_record: List[ContentRecord] = [] self.content_records: List[ContentRecord] = []
self.content_record_hdr: List = []
# Load data from TMD file # Load data from TMD file
with io.BytesIO(tmd) as tmddata: with io.BytesIO(tmd) as tmddata:
# Signing certificate issuer # Signing certificate issuer
@ -103,13 +102,14 @@ class TMD:
# Content index in content list that contains the boot file # Content index in content list that contains the boot file
tmddata.seek(0x1E0) tmddata.seek(0x1E0)
self.boot_index = tmddata.read(2) self.boot_index = tmddata.read(2)
# Got content records for the number of contents in num_contents. # Get content records for the number of contents in num_contents.
i = 0 for content in range(0, self.num_contents):
while i < self.num_contents: tmddata.seek(0x1E4 + (36 * content))
tmddata.seek(0x1E4 + (36 * i)) content_record_hdr = struct.unpack(">LHH4x4s20s", tmddata.read(36))
self.content_record_hdr = struct.unpack(">LHH4x4s20s", tmddata.read(36)) self.content_records.append(
self.content_record.append(ContentRecord(int(self.content_record_hdr[0]), int(self.content_record_hdr[1]), int(self.content_record_hdr[2]), int.from_bytes(self.content_record_hdr[3]), binascii.hexlify(self.content_record_hdr[4]).decode())) ContentRecord(int(content_record_hdr[0]), int(content_record_hdr[1]),
i += 1 int(content_record_hdr[2]), int.from_bytes(content_record_hdr[3]),
binascii.hexlify(content_record_hdr[4])))
def get_title_id(self): def get_title_id(self):
"""Returns the TID of the TMD's associated title.""" """Returns the TID of the TMD's associated title."""
@ -193,9 +193,10 @@ class TMD:
"""Returns the number of contents listed in the TMD.""" """Returns the number of contents listed in the TMD."""
return self.num_contents return self.num_contents
def get_content_records(self, record): def get_content_record(self, record):
"""Returns all values for the specified content record.""" """Returns the content record at the specified index."""
if record <= self.num_contents: if record < self.num_contents:
return self.content_record[record].cid, self.content_record[record].index, self.content_record[record].content_type, self.content_record[record].content_size, self.content_record[record].content_hash return self.content_records[record]
else: else:
return raise IndexError("Invalid content record! TMD lists '" + str(self.num_contents) +
"' contents but index was '" + str(record) + "'!")