def _parse_data_header_object(self): object_piece = self.parsed_data['DataHeaderPiece'] retrievers = object_piece.retrievers return DataHeaderObject( version=get_retriever_by_name(retrievers, "Version").data, filename=get_retriever_by_name(retrievers, "Filename").data)
def _parse_diplomacy_object(self): object_piece = self.parsed_data['DiplomacyPiece'] diplomacy = get_retriever_by_name(object_piece.retrievers, "Per-player diplomacy").data diplomacies = [] for player_id in range(0, 8): # 0-7 Players diplomacies.append( get_retriever_by_name(diplomacy[player_id].retrievers, "Stance with each player").data) return DiplomacyObject(player_stances=diplomacies)
def _parse_file_header_object(self): object_piece = self.parsed_header['FileHeaderPiece'] retrievers = object_piece.retrievers return FileHeaderObject( version=get_retriever_by_name(retrievers, "Version").data, timestamp=get_retriever_by_name(retrievers, "Timestamp of last save").data, instructions=get_retriever_by_name(retrievers, "Scenario instructions").data, player_count=get_retriever_by_name(retrievers, "Player count").data, creator_name=get_retriever_by_name(retrievers, "Creator name").data, )
def __getattr__(self, name): """ Providing a default way to access retriever data labeled 'name' """ try: return get_retriever_by_name(self.retrievers, name).data except: raise AttributeError("No attribute retriever named \'" + name + "\' in piece \'" + str(type(self)) + "\'")
def _debug_log_condition_dataset(self): """ Used for debugging - Only reads One Trigger. """ trigger_data = get_retriever_by_name( self._parsed_data['TriggerPiece'].retrievers, "Trigger data").data conditions = get_retriever_by_name(trigger_data.retrievers, "Condition data").data for condition in conditions: for retriever in condition.retrievers: # if retriever.data != -1 and \ # retriever.data != [] and \ # retriever.data != "" and \ # retriever.data != " " and \ # retriever.name != "static_value_21": if retriever.name != "static_value_21": if retriever.name == "condition_type": print("},\n" + str(retriever.data) + ": {") print("\t\"" + retriever.name + "\": " + (str(retriever.data) if type(retriever.data) is not str else "\"" + retriever.data + "\"") + ",") print("}\n")
def _debug_log_effect_dataset(self): """ Used for debugging - Only reads One Trigger. """ trigger_data = get_retriever_by_name( self._parsed_data['TriggerPiece'].retrievers, "Trigger data").data effects = get_retriever_by_name(trigger_data.retrievers, "Effect data").data for effect in effects: for retriever in effect.retrievers: # if retriever.data != -1 and \ # retriever.data != [] and \ # retriever.data != "" and \ # retriever.data != " " and \ # retriever.name != "static_value_46": if retriever.name != "static_value_46": if retriever.name == "effect_type": print("},\n" + str(retriever.data) + ": {") print("\t\"" + retriever.name + "\": " + (str(retriever.data) if type(retriever.data) is not str else "\"" + retriever.data + "\"") + ",") print("}\n")
def __setattr__(self, name, value): """ Trying to edit retriever data labeled 'name' if available """ if 'retrievers' not in self.__dict__: super().__setattr__(name, value) else: retriever = get_retriever_by_name(self.retrievers, name) if retriever is None: super().__setattr__(name, value) else: retriever.data = value
def handle_retriever_dependency(retriever: Retriever, retrievers: List[Retriever], state, pieces): if state == "construct": retriever_on_x = retriever.on_construct elif state == "commit": retriever_on_x = retriever.on_commit elif state == "refresh": retriever_on_x = retriever.on_refresh else: raise ValueError("State must be any of: construct, commit or refresh") dep_action = retriever_on_x.dependency_type dep_target = retriever_on_x.dependency_target if dep_action == DependencyAction.REFRESH_SELF: handle_retriever_dependency(retriever, retrievers, "refresh", pieces) elif dep_action == DependencyAction.REFRESH: listified_target = listify(dep_target.target_piece) listified_target_attr = listify(dep_target.piece_attr_name) for i in range(len(listified_target)): retriever_list = handle_dependency_target(listified_target[i], retrievers, pieces) retriever_to_be_refreshed = get_retriever_by_name( retriever_list, listified_target_attr[i]) handle_retriever_dependency(retriever_to_be_refreshed, retriever_list, "refresh", pieces) elif dep_action in [ DependencyAction.SET_VALUE, DependencyAction.SET_REPEAT ]: listified_target = listify(dep_target.target_piece) listified_target_attr = listify(dep_target.piece_attr_name) for i in range(len(listified_target)): retriever_list = handle_dependency_target(listified_target[i], retrievers, pieces) retriever_data = get_retriever_by_name( retriever_list, listified_target_attr[i]).data value = handle_dependency_eval(retriever_on_x, retriever_data) if dep_action == DependencyAction.SET_VALUE: retriever.data = value elif dep_action == DependencyAction.SET_REPEAT: retriever.datatype.repeat = value
def _parse_options_object(self): object_piece = self.parsed_data['OptionsPiece'] # ppnd: Per Player Number of Disabled ppnd_techs = get_retriever_by_name( object_piece.retrievers, "Per player number of disabled techs").data ppnd_units = get_retriever_by_name( object_piece.retrievers, "Per player number of disabled units").data ppnd_buildings = get_retriever_by_name( object_piece.retrievers, "Per player number of disabled buildings").data disabled_techs = generator.create_generator( get_retriever_by_name( object_piece.retrievers, "Disabled technology IDs in player order").data, 1) disabled_units = generator.create_generator( get_retriever_by_name(object_piece.retrievers, "Disabled unit IDs in player order").data, 1) disabled_buildings = generator.create_generator( get_retriever_by_name( object_piece.retrievers, "Disabled building IDs in player order").data, 1) disables = list() for player_id in range(0, 8): # 0-7 Players nd_techs = ppnd_techs[player_id] nd_units = ppnd_units[player_id] nd_buildings = ppnd_buildings[player_id] player_disabled_techs = generator.repeat_generator( disabled_techs, nd_techs, return_bytes=False) player_disabled_units = generator.repeat_generator( disabled_units, nd_units, return_bytes=False) player_disabled_buildings = generator.repeat_generator( disabled_buildings, nd_buildings, return_bytes=False) disables.append({ 'techs': player_disabled_techs, 'units': player_disabled_units, 'buildings': player_disabled_buildings, }) return OptionsObject( disables, get_retriever_by_name(object_piece.retrievers, "All techs").data)
def _parse_player_object(self): players = [] data_header_piece = self.parsed_data['DataHeaderPiece'] unit_piece = self.parsed_data['UnitsPiece'] options_piece = self.parsed_data['OptionsPiece'] starting_ages = get_retriever_by_name(options_piece.retrievers, "Per player starting age").data # Player Data >>> 0-7 Players & 8 Gaia <<< player_data_one = get_retriever_by_name(data_header_piece.retrievers, "Player data#1").data player_data_two = self.parsed_data['PlayerDataTwoPiece'] resources = get_retriever_by_name(player_data_two.retrievers, "Resources").data # player_data_three = find_retriever(unit_piece.retrievers, "Player data #3").data player_data_four = get_retriever_by_name(unit_piece.retrievers, "Player data #4").data for player_id in range(0, 9): # 0-7 Players & 8 Gaia: try: # If gaia isn't saved. (PlayerDataThree and PlayerDataFour) pop_limit = get_retriever_by_name( player_data_four[player_id].retrievers, "Population limit").data except IndexError: pop_limit = -1 players.append( PlayerObject( player_number=player_id, active=get_retriever_by_name( player_data_one[player_id].retrievers, "Active").data, human=get_retriever_by_name( player_data_one[player_id].retrievers, "Human").data, civilization=get_retriever_by_name( player_data_one[player_id].retrievers, "Civilization").data, gold=get_retriever_by_name(resources[player_id].retrievers, "Gold").data, wood=get_retriever_by_name(resources[player_id].retrievers, "Wood").data, food=get_retriever_by_name(resources[player_id].retrievers, "Food").data, stone=get_retriever_by_name( resources[player_id].retrievers, "Stone").data, color=get_retriever_by_name( resources[player_id].retrievers, "Player color").data, starting_age=starting_ages[player_id], pop_limit=pop_limit)) return players
def _parse_messages_object(self): object_piece = self.parsed_data['MessagesPiece'] retrievers = object_piece.retrievers return MessagesObject( instructions=get_retriever_by_name(retrievers, "Instructions").data, hints=get_retriever_by_name(retrievers, "Hints").data, victory=get_retriever_by_name(retrievers, "Victory").data, loss=get_retriever_by_name(retrievers, "Loss").data, history=get_retriever_by_name(retrievers, "History").data, scouts=get_retriever_by_name(retrievers, "Scouts").data, ascii_instructions=get_retriever_by_name( retrievers, "ASCII Instructions").data, ascii_hints=get_retriever_by_name(retrievers, "ASCII Hints").data, ascii_victory=get_retriever_by_name(retrievers, "ASCII Victory").data, ascii_loss=get_retriever_by_name(retrievers, "ASCII Loss").data, ascii_history=get_retriever_by_name(retrievers, "ASCII History").data, ascii_scouts=get_retriever_by_name(retrievers, "ASCII Scouts").data, )
def get_value(self, retriever_key): return get_retriever_by_name(self.retrievers, retriever_key).data
def commit(self, pieces, host_obj): # Object-only retrievers for the ease of access of information. # Not actually representing a value in the scenario file. if self.retrieve_instance_number or self.retrieve_history_number >= 0: return instance_number_history = host_obj._instance_number_history instance_number = AoE2Object.get_instance_number(obj=host_obj) value = host_obj.__getattribute__(self.name) # Replace __index__ values temp_link = self.link if instance_number_history: for i in instance_number_history: temp_link = temp_link.replace("__index__", str(i), 1) temp_link = temp_link.replace("__index__", str(instance_number), 1) if self.is_special_unit_case: temp_link = temp_link.replace('[]', '[0]', 1) split_temp_link: List[str] = temp_link.split(".") retriever = None piece = pieces[self.piece] for attribute in split_temp_link: if '[' in attribute: index_location = attribute.index('[') index = int(attribute[index_location + 1:len(attribute) - 1]) piece = get_retriever_by_name( piece.retrievers, attribute[:index_location]).data[index] else: retriever = get_retriever_by_name(piece.retrievers, attribute) if retriever is None: raise ValueError( "RetrieverObjectLink is unable to connect to retriever") retriever_list = piece.retrievers if self.process_as_object is not None: link_piece = retriever.datatype.var if self.is_special_unit_case: self._commit_special_unit_case(host_obj, pieces, link_piece, value) return try: old_length = len(retriever.data) except TypeError: # retriever.data was not set before (list of 0 -> None) retriever.data = [] old_length = 0 new_length = len(value) if new_length < old_length: retriever.data = retriever.data[:new_length] elif new_length > old_length: retriever.data += [ link_piece() for x in range(new_length - old_length) ] if retriever.log_value: retriever._update_print( f"[{link_piece.__name__}] * {old_length}", f"[{link_piece.__name__}] * {new_length}") for index, obj in enumerate(value): obj._pieces = pieces obj._instance_number_history = host_obj._instance_number_history + [ index ] obj.commit() else: retriever.data = value if hasattr(retriever, 'on_commit'): handle_retriever_dependency(retriever, retriever_list, "commit", pieces)