def _load(self, initial_outfit_parts: Dict[BodyType, int]=None) -> bool: target_sim_name = CommonSimNameUtils.get_full_name(self.sim_info) self._outfit_data: OutfitData = CommonOutfitUtils.get_outfit_data(self.sim_info, outfit_category_and_index=self._outfit_category_and_index) if self._outfit_data is None: self.log.error('Missing outfit data for Sim \'{}\' and Outfit Category and Index {}'.format(target_sim_name, self._outfit_category_and_index), throw=True) return False self._outfit_parts = CommonOutfitUtils.get_outfit_parts(self.sim_info, outfit_category_and_index=self._outfit_category_and_index) self._original_outfit_data: FrozenSet[int] = frozenset(self._outfit_parts.items()) if initial_outfit_parts is not None: for (key, value) in initial_outfit_parts.items(): if not isinstance(key, int) or not isinstance(value, int): self.log.error('\'{}\': outfit_body_parts contains non-integer variables key: {} value: {}.'.format(target_sim_name, key, value)) return False self._outfit_body_types = list(initial_outfit_parts.keys()) self._outfit_part_ids = list(initial_outfit_parts.values()) else: if not self._outfit_data.part_ids or not self._outfit_data.body_types: self.log.error('\'{}\' is missing outfit parts or body types for Outfit Category and Index {}.'.format(target_sim_name, self._outfit_category_and_index)) return False self._outfit_body_types: List[Union[BodyType, int]] = list(self._outfit_data.body_types) self._outfit_part_ids: List[int] = list(self._outfit_data.part_ids) if len(self._outfit_body_types) != len(self._outfit_part_ids): self.log.error('\'{}\': The number of outfit parts did not match the number of body types for Outfit Category and Index {}.'.format(target_sim_name, self._outfit_category_and_index)) return False return True
def attach_cas_part_to_sim( sim_info: SimInfo, cas_part_id: int, body_type: Union[BodyType, int] = BodyType.NONE, outfit_category_and_index: Union[Tuple[OutfitCategory, int], None] = None ) -> bool: """attach_cas_part_to_sim(sim_info, cas_part_id, body_type=BodyType.NONE, outfit_category_and_index=None) Add a CAS part at the specified BodyType to the Sims outfit. :param sim_info: The SimInfo of a Sim to add the CAS part to. :type sim_info: SimInfo :param cas_part_id: The decimal identifier of a CAS part to attach to the Sim. :type cas_part_id: int :param body_type: The BodyType the CAS part will be attached to. If no value is provided or it is None, the BodyType of the CAS part itself will be used. :type body_type: Union[BodyType, int], optional :param outfit_category_and_index: The outfit category and index of the Sims outfit to modify. If no value is provided, the Sims current outfit will be used. :type outfit_category_and_index: Union[Tuple[OutfitCategory, int], None], optional :return: True if the CAS part was successfully attached to the Sim. False if the CAS part was not successfully attached to the Sim. :rtype: bool """ log.format_with_message( 'Attempting to attach CAS part to Sim', sim=sim_info, cas_part_id=cas_part_id, body_type=body_type, outfit_category_and_index=outfit_category_and_index) if cas_part_id == -1 or cas_part_id is None: raise RuntimeError('No cas_part_id was provided.') log.debug('Pre-saving outfits.') saved_outfits = sim_info.save_outfits() if outfit_category_and_index is None: outfit_category_and_index = CommonOutfitUtils.get_current_outfit( sim_info) log.format_with_message( 'Using outfit category and index.', outfit_category_and_index=outfit_category_and_index) outfit_data = CommonOutfitUtils.get_outfit_data( sim_info, outfit_category_and_index=outfit_category_and_index) outfit_identifier = frozenset( dict(zip(list(outfit_data.body_types), list(outfit_data.part_ids))).items()) if body_type is None or body_type == BodyType.NONE: body_type = CommonCASUtils.get_body_type_of_cas_part(cas_part_id) log.format_with_message('Using body_type', body_type=body_type) for outfit in saved_outfits.outfits: log.format_with_message('Attempting to update outfit.', outfit=outfit) # noinspection PyUnresolvedReferences _outfit_identifier = frozenset( dict( zip(list(outfit.body_types_list.body_types), list(outfit.parts.ids))).items()) if int(outfit.category) != int( outfit_category_and_index[0] ) or outfit.outfit_id != outfit_data.outfit_id or _outfit_identifier != outfit_identifier: log.format_with_message( 'Outfit is not the outfit we want to update, skipping.', outfit_id=outfit.outfit_id, outfit_category=outfit.category) continue log.debug('Updating outfit.') # noinspection PyUnresolvedReferences previous_cas_parts_list = list(outfit.parts.ids) if cas_part_id not in previous_cas_parts_list: log.format_with_message('Adding CAS part id.', cas_part_id=cas_part_id) previous_cas_parts_list.append(cas_part_id) outfit.parts = S4Common_pb2.IdList() # noinspection PyUnresolvedReferences outfit.parts.ids.extend(previous_cas_parts_list) # noinspection PyUnresolvedReferences previous_body_types_list = list(outfit.body_types_list.body_types) if body_type not in previous_body_types_list: log.format_with_message('Adding BodyType.', body_type=body_type) previous_body_types_list.append(body_type) outfit.body_types_list = Outfits_pb2.BodyTypesList() # noinspection PyUnresolvedReferences outfit.body_types_list.body_types.extend(previous_body_types_list) log.debug('Done updating outfit.') log.debug('Done updating outfits.') sim_info._base.outfits = saved_outfits.SerializeToString() sim_info._base.outfit_type_and_index = outfit_category_and_index log.debug('Resending outfits.') CommonOutfitUtils.resend_outfits(sim_info) log.debug('Done adding CAS part to Sim.') return True