# -*- coding: utf-8 -*- """ copyright 2008 - Johannes 'josch' Schauer copyright 2010 - Remigiusz 'Enleth' Marcinkiewicz This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import gzip import struct import chardet import binascii #from guess_language import guessLanguage from lxml import etree from lxml import objectify from bf import bf from h3m_ids import * def edet(string): e = chardet.detect(string) if e["encoding"] in h3m_encoding_quirks: e["encoding"] = h3m_encoding_quirks[e["encoding"]] return e def extract(filename): h3m_data = gzip.open(filename) r = lambda s: struct.unpack(s,h3m_data.read(struct.calcsize(s))) E = objectify.ElementMaker(annotate=False) try: vver = h3m_versions[r("30: raise ValueError("Map name longer than 30 characters, possibly an invalid file") vname = h3m_data.read(vlength) enc = edet(vname) vname = vname.decode(enc["encoding"]) m.append(E.name(vname)) m.conversion_metadata.append(E.encoding(enc["encoding"],entity="/map/name[0]",confidence=str(enc["confidence"]))) (vlength, ) = r(" 0: player.append(E.heroes()) for i in range(vheroes_count): (vhero_portrait, ) = r(" 0: #print dict(zip(range(0,8),r("<8B"))) vteams = r("<8B") for pnum in range(0,8): if m.xpath("/map/players/player[@color=$color]/@type",color=h3m_player_colors[pnum])[0] != "Disabled": m.xpath("/map/players/player[@color=$color]",color=h3m_player_colors[pnum])[0].append(E.team(str(vteams[pnum]))) #guess_str = m.xpath("/map/name")[0].text + ' ' + m.xpath("/map/description")[0].text #print guessLanguage(guess_str) if m.xpath("/map/@version")[0] == "RoE": vavailable_heroes = r("<16s")[0] else: vavailable_heroes = r("<20s")[0] vunknown3 = "0x"+binascii.hexlify(r("<4s")[0]) m.conversion_metadata.append(E.unknown3(vunknown3)) if m.xpath("/map/@version")[0] in ["SoD", "WoG"]: (vdisposed_heroes,) = r(" 0: #map_data["free_heroes"] = [] #for i in range(map_data["heroes_count"]): #(hero_id, ) = struct.unpack("> x) & 1]) vdef_accessibility = r("<6B") #passability vdef_accessibility = frozenset([(8-x,6-y) for x in range(8) for y, p in enumerate(vdef_accessibility) if not (p >> x) & 1]) h3m_data.read(2) #landscape h3m_data.read(2) #land_edit_groups (vdef_type,vdef_subtype,vdef_groups,vdef_underlay ) = r(" 0: for i in range(knapsack_count): (knapsackID, ) = struct.unpack(" 0: for i in range(eventQuantity): (length, ) = struct.unpack(" 0: for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack(" 0: for i in range(artefacts): (artID, ) = struct.unpack(" 0: for i in range(spells): (spellID, ) = struct.unpack(" 0: for i in range(monsters): (guard_id, guard_count) = struct.unpack(" 0: for i in range(secSkills): (skill_id, skill_lvl) = struct.unpack(" 0: for i in range(artefacts): (artID, ) = struct.unpack(" 0: for i in range(spells): (spellID, ) = struct.unpack(" 0: for i in range(monsters): (guard_id, guard_count) = struct.unpack("