def _debug_byte_structure_to_file( self, filename, trail_generator: IncrementalGenerator = None, commit=False): """ Used for debugging - Writes structure from read file to the filesystem in a easily readable manner. """ if commit and hasattr(self, '_object_manager'): self._object_manager.reconstruct() s_print("\nWriting structure to file...", final=True) with open(filename, 'w', encoding="utf-8") as f: result = [] for section in self.sections.values(): result.append(self._retrieve_byte_structure(section)) if trail_generator is not None: s_print("\tWriting trail...") trail = trail_generator.get_remaining_bytes() result.append(f"\n\n{'#' * 27} TRAIL ({len(trail)})\n\n") result.append( create_textual_hex(trail.hex(), space_distance=2, enter_distance=24)) s_print("\tWriting trail finished successfully.", final=True) f.write(''.join(result)) s_print("Writing structure to file finished successfully.", final=True)
def setup(self): s_print(f"\nSetting up managers ...", final=True) for name, manager in managers[self.game_version].items(): s_print(f"\t🔄 Setting up {name}Manager...") self.managers[name] = manager._construct(self.sections, self.scenario_version) s_print(f"\t✔ {name}Manager", final=True) s_print(f"Setting up managers finished successfully.", final=True)
def reconstruct(self): s_print("\nReconstructing sections and structs from managers...", final=True) manager: AoE2Object for name, manager in self.managers.items(): s_print(f"\t🔄 Reconstructing {name}Manager...") manager.commit(self.sections) s_print(f"\t✔ {name}Manager", final=True) s_print("Reconstruction finished successfully.", final=True)
def _write_from_structure(self, filename): self._object_manager.reconstruct() s_print("\nFile writing from structure started...", final=True) binary = self._get_file_section_data(self.sections.get('FileHeader')) binary_list_to_be_compressed = [] for file_part in self.sections.values(): if file_part.name == "FileHeader": continue binary_list_to_be_compressed.append( self._get_file_section_data(file_part)) compressed = compress_bytes(b''.join(binary_list_to_be_compressed)) with open(filename, 'wb') as f: f.write(binary + compressed) s_print("File writing finished successfully.", final=True) print(f"File successfully written to: '{filename}'")
def _create_and_load_section(self, name, igenerator): s_print(f"\t🔄 Parsing {name}...") section = AoE2FileSection.from_structure(name, self.structure.get(name)) s_print(f"\t🔄 Gathering {name} data...") section.set_data_from_generator(igenerator, self.sections) s_print(f"\t✔ {name}", final=True) return section
def from_file(cls, filename, game_version): print(f"\nReading file: '{filename}'") s_print("Reading scenario file...") igenerator = IncrementalGenerator.from_file(filename) s_print("Reading scenario file finished successfully.", final=True) scenario = cls() scenario.read_mode = "from_file" scenario.game_version = game_version scenario.scenario_version = get_file_version(igenerator) # Log game and scenario version print("\n############### Attributes ###############") print(f">>> Game version: '{scenario.game_version}'") print(f">>> Scenario version: {scenario.scenario_version}") print("##########################################") s_print(f"\nLoading scenario structure...") initialise_version_dependencies(scenario.game_version, scenario.scenario_version) scenario._load_structure() s_print(f"Loading scenario structure finished successfully.", final=True) # scenario._initialize(igenerator) s_print("Parsing scenario file...", final=True) scenario._load_header_section(igenerator) scenario._load_content_sections(igenerator) s_print(f"Parsing scenario file finished successfully.", final=True) scenario._object_manager = AoE2ObjectManager( sections=scenario.sections, game_version=scenario.game_version, scenario_version=scenario.scenario_version) scenario._object_manager.setup() return scenario
def _retrieve_byte_structure(self, file_part): s_print(f"\t🔄 Writing {file_part.name}...") value = file_part.get_byte_structure_as_string(self.sections) s_print(f"\t✔ {file_part.name}", final=True) return value
def _get_file_section_data(self, file_section: AoE2FileSection): s_print(f"\t🔄 Reconstructing {file_section.name}...") value = file_section.get_data_as_bytes() s_print(f"\t✔ {file_section.name}", final=True) return value