def __init__(self, filename, log_reading=True, log_parsing=False): print("\nLoading file: '" + filename + "'...") scenario = open(filename, "rb") self._file = scenario.read() scenario.seek(0) # Reset file cursor to 0 self._file_header = scenario.read(self._compute_header_length()) self._file_data = zlib.decompress(scenario.read(), -zlib.MAX_WBITS) scenario.close() print("File loaded.") self.parser = parser.Parser() self._read_file(log_reading=log_reading) self.object_manager = AoE2ObjectManager(self._parsed_header, self._parsed_data, log_parsing=log_parsing)
def create_default(cls, log_creating=True, log_parsing=False): scenario = cls() scenario.read_mode = "create_default" lgr = SimpleLogger(log_creating) lgr.print("\nFile creation started...") scenario.parser = parser.Parser() scenario._parsed_header = collections.OrderedDict() scenario._parsed_data = collections.OrderedDict() pieces = OrderedDict(**scenario._parsed_header, **scenario._parsed_data) for piece in _header_structure: piece_name = piece.__name__ lgr.print("\tCreating " + piece_name + "...", replace_line=True) scenario._parsed_header[piece_name] = piece( scenario.parser, data=list(piece.defaults(pieces).values()), pieces=pieces) lgr.print("\tCreating " + piece_name + " finished successfully.", replace_line=True) lgr.print() for piece in _file_structure: pieces = OrderedDict(**scenario._parsed_header, **scenario._parsed_data) piece_name = piece.__name__ lgr.print("\tCreating " + piece_name + "...", replace_line=True) scenario._parsed_data[piece_name] = piece( scenario.parser, data=list(piece.defaults(pieces).values()), pieces=pieces) lgr.print("\tCreating " + piece_name + " finished successfully.", replace_line=True) lgr.print() lgr.print("File creation finished successfully.") scenario._object_manager = AoE2ObjectManager(scenario._parsed_header, scenario._parsed_data, log_parsing=log_parsing) return scenario
def __init__(self, filename, log_reading=True, log_parsing=False): print("\nPreparing & Loading file: '" + filename + "'...") scenario = open(filename, "rb") self._file = scenario.read() scenario.seek(0) # Reset file cursor to 0 self._file_header = scenario.read(self._compute_header_length()) self._file_data = zlib.decompress(scenario.read(), -zlib.MAX_WBITS) scenario.close() # self._debug_write_from_source("../PreParseErrorFile", "hd", write_in_bytes=False) # exit() print("File prepared and loaded.") self.parser = parser.Parser() self._read_file(log_reading=log_reading) self.object_manager = AoE2ObjectManager(self._parsed_header, self._parsed_data, log_parsing=log_parsing)
def from_file(cls, filename, log_reading=True, log_parsing=False): scenario = cls() print("\nPreparing & Loading file: '" + filename + "'...") scenario_file = open(filename, "rb") scenario._file = scenario_file.read() scenario_file.seek(0) # Reset file cursor to 0 scenario._file_header = scenario_file.read( scenario._compute_header_length()) scenario._file_data = zlib.decompress(scenario_file.read(), -zlib.MAX_WBITS) scenario_file.close() print("File prepared and loaded.") scenario.parser = parser.Parser() scenario._read_file(log_reading=log_reading) scenario._object_manager = AoE2ObjectManager(scenario._parsed_header, scenario._parsed_data, log_parsing=log_parsing) return scenario
def _compute_header_length(self): return parser.calculate_length( self._create_file_generator(1), FileHeaderPiece(parser.Parser()).retrievers)