Exemplo n.º 1
0
def vorl(retriever, value):
    """
    Variable or List

    Args:
        retriever (Retriever): The retriever
        value (List[Any]): A value to be put in the retriever

    Returns:
        The given list or the value inside it
    """
    if retriever.datatype.repeat != 1:
        return listify(value)

    if retriever.is_list is not None:
        return listify(value) if retriever.is_list else value[0]

    # Fallback to length check
    if len(value) == 1:
        return value[0]

    return value
Exemplo n.º 2
0
    def get_data_as_bytes(self):
        self.update_datatype_repeat()

        if self.data is not None and self.datatype.repeat != 0:
            result = []
            if self.datatype.type == "struct":
                for struct in self.data:
                    result.append(struct.get_data_as_bytes())
            else:
                for value in listify(self.data):
                    result.append(parse_val_to_bytes(self, value))

            joined_result = b''.join(result)
        else:
            joined_result = b''

        if self.log_value:
            print(
                f"{self.to_simple_string()} (Data: {self.data}) retrieved: {joined_result}"
            )

        return joined_result
Exemplo n.º 3
0
    def get_byte_structure_as_string(self, sections, skip_retrievers=None):
        if skip_retrievers is None:
            skip_retrievers = []

        byte_structure = "\n" + self.get_header_string()

        for retriever in self.retrievers:
            if retriever.name in skip_retrievers:
                continue
            byte_structure += "\n"

            listed_retriever_data = listify(retriever.data)
            struct_header_set = False
            for struct in listed_retriever_data:
                if isinstance(struct, AoE2FileSection):
                    if not struct_header_set:
                        byte_structure += f"\n{'#' * 27} {retriever.name} ({retriever.datatype.to_simple_string()})"
                        struct_header_set = True
                    byte_structure += struct.get_byte_structure_as_string(
                        sections)
            # Struct Header was set. Retriever was struct, data retrieved using recursion. Next retriever.
            if struct_header_set:
                byte_structure += f"{'#' * 27} End of: {retriever.name} ({retriever.datatype.to_simple_string()})\n"
                continue

            retriever_data_bytes = retriever.get_data_as_bytes()
            if retriever_data_bytes is None:
                return byte_structure
            else:
                retriever_data_bytes = retriever_data_bytes.hex()

            retriever_short_string: str = retriever.get_short_str()
            retriever_hex = create_textual_hex(retriever_data_bytes,
                                               space_distance=2,
                                               enter_distance=24)

            split_hex = retriever_hex.split("\n")
            split_hex_length = len(split_hex)

            split_data_string = retriever_short_string.replace(
                '\x00', ' ').splitlines()
            data_lines = []
            for x in split_data_string:
                if len(x) > 120:
                    data_lines += [
                        f'\t{x}'
                        for x in insert_char(x, '\r\n', 120).splitlines()
                    ]
                else:
                    data_lines.append(x)
            split_data_length = len(data_lines)

            lines = max(split_hex_length, split_data_length)

            combined_strings = []
            for i in range(0, lines):
                hex_part = split_hex[i] if i < split_hex_length else ""
                data_part = data_lines[i] if i < split_data_length else ""
                combined_strings.append(
                    string_manipulations.add_suffix_chars(hex_part, " ", 28) +
                    data_part)

            byte_structure += "\n".join(combined_strings)

        return byte_structure + "\n"
Exemplo n.º 4
0
 def selected_object_ids(self, val: List[int]):
     val = listify(val)
     self._selected_object_ids = val