def calculate_offsets(db_struct, current_offset=0): """ Recursively calculate byte offsets for each field in the DB structure, applying general alignment rules except inside arrays. """ if isinstance(db_struct, dict): for key, value in db_struct.items(): if isinstance(value, dict) and 'type' in value: is_array_element = value.get('is_array_element', False) type_name = value['type'] size = type_sizes.get(type_name, 1) if 'String' in type_name: match = re.match(r'String\[(\d+)\]', type_name) if match: size = int(match.group(1)) + 2 # String length + 2 for the null terminator and length prefix if not is_array_element and current_offset % 2 != 0: current_offset += 1 # Align to the next even offset if it's not an array element value['offset'] = current_offset current_offset += size # Recursively handle nested structures if isinstance(value, dict): current_offset = calculate_offsets(value, current_offset) return current_offset