def unpack(cls, esd_reader: BinaryReader, commands_offset, count=1): """ Returns a list of Command instances. """ commands = [] if commands_offset == -1: return commands struct_dicts = esd_reader.unpack_structs(cls.STRUCT, count=count, offset=commands_offset) for d in struct_dicts: if d["args_offset"] > 0: esd_reader.seek(d["args_offset"]) arg_structs = cls.ARG_STRUCT.unpack_count(esd_reader, count=d["args_count"]) args = [ esd_reader.unpack_bytes(offset=a["arg_ezl_offset"], length=a["arg_ezl_size"]) for a in arg_structs ] else: args = [] commands.append(cls(d["bank"], d["index"], args)) return commands
def unpack(cls, esd_reader: BinaryReader, condition_pointers_offset, count=1): """Returns a list of `Condition` instances`.""" conditions = [] if condition_pointers_offset == -1: return conditions pointers = esd_reader.unpack_structs(cls.POINTER_STRUCT, count=count, offset=condition_pointers_offset) for p in pointers: d = esd_reader.unpack_struct(cls.STRUCT, offset=p["condition_offset"]) pass_commands = cls.Command.unpack( esd_reader, d["pass_commands_offset"], count=d["pass_commands_count"], ) subconditions = cls.unpack( # safe recursion esd_reader, d["subcondition_pointers_offset"], count=d["subcondition_pointers_count"], ) test_ezl = esd_reader.unpack_bytes(offset=d["test_ezl_offset"], length=d["test_ezl_size"]) if d["next_state_offset"] > 0: next_state_index = esd_reader.unpack_struct( cls.STATE_ID_STRUCT, offset=d["next_state_offset"])["state_id"] else: next_state_index = -1 conditions.append( cls(next_state_index, test_ezl, pass_commands, subconditions)) return conditions
def unpack(self, reader: BinaryReader, **kwargs): self.byte_order = reader.byte_order = ">" if reader.unpack_value( "B", offset=44) == 255 else "<" version_info = reader.unpack("bbb", offset=45) self.flags1 = ParamFlags1(version_info[0]) self.flags2 = ParamFlags2(version_info[1]) self.paramdef_format_version = version_info[2] header_struct = self.GET_HEADER_STRUCT(self.flags1, self.byte_order) header = reader.unpack_struct(header_struct) try: self.param_type = header["param_type"] except KeyError: self.param_type = reader.unpack_string( offset=header["param_type_offset"], encoding="utf-8") self.paramdef_data_version = header["paramdef_data_version"] self.unknown = header["unknown"] # Row data offset in header not used. (It's an unsigned short, yet doesn't limit row count to 5461.) name_data_offset = header[ "name_data_offset"] # CANNOT BE TRUSTED IN VANILLA FILES! Off by +12 bytes. # Load row pointer data. row_struct = self.ROW_STRUCT_64 if self.flags1.LongDataOffset else self.ROW_STRUCT_32 row_pointers = reader.unpack_structs(row_struct, count=header["row_count"]) row_data_offset = reader.position # Reliable row data offset. # Row size is lazily determined. TODO: Unpack row data in sequence and associate with names separately. if len(row_pointers) == 0: return elif len(row_pointers) == 1: # NOTE: The only vanilla param in Dark Souls with one row is LEVELSYNC_PARAM_ST (Remastered only), # for which the row size is hard-coded here. Otherwise, we can trust the repacked offset from Soulstruct # (and SoulsFormats, etc.). if self.param_type == "LEVELSYNC_PARAM_ST": row_size = 220 else: row_size = name_data_offset - row_data_offset else: row_size = row_pointers[1]["data_offset"] - row_pointers[0][ "data_offset"] # Note that we no longer need to track reader offset. name_encoding = self.get_name_encoding() for row_struct in row_pointers: reader.seek(row_struct["data_offset"]) row_data = reader.read(row_size) if row_struct["name_offset"] != 0: try: name = reader.unpack_string( offset=row_struct["name_offset"], encoding=name_encoding, reset_old_offset=False, # no need to reset ) except UnicodeDecodeError as ex: if ex.object in self.undecodable_row_names: name = reader.unpack_bytes( offset=row_struct["name_offset"], reset_old_offset=False, # no need to reset ) else: raise except ValueError: reader.seek(row_struct["name_offset"]) _LOGGER.error( f"Error encountered while parsing row name string in {self.param_type}.\n" f" Header: {header}\n" f" Row Struct: {row_struct}\n" f" 30 chrs of name data: {' '.join(f'{{:02x}}'.format(x) for x in reader.read(30))}" ) raise else: name = "" self.rows[row_struct["id"]] = ParamRow(row_data, self.paramdef, name=name)