From 1f5fffe76aafbb535f1b9e797f179e7d3cbbc301 Mon Sep 17 00:00:00 2001 From: josch Date: Thu, 26 Jun 2014 09:29:46 +0200 Subject: [PATCH] initial commit --- bf.py | 26 ++ h3m.py | 802 +++++++++++++++++++++++++++++++++++++++++++++++++++++ h3m_ids.py | 563 +++++++++++++++++++++++++++++++++++++ mapread.py | 11 + 4 files changed, 1402 insertions(+) create mode 100755 bf.py create mode 100755 h3m.py create mode 100755 h3m_ids.py create mode 100755 mapread.py diff --git a/bf.py b/bf.py new file mode 100755 index 0000000..3e34176 --- /dev/null +++ b/bf.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +class bf(object): + def __init__(self,value=0): + self._d = value + + def __getitem__(self, index): + return (self._d >> index) & 1 + + def __setitem__(self,index,value): + value = (value&1L)<> start) & mask + + def __setslice__(self, start, end, value): + mask = 2L**(end - start) -1 + value = (value & mask) << start + mask = mask << start + self._d = (self._d & ~mask) | value + return (self._d >> start) & mask + + def __int__(self): + return self._d \ No newline at end of file diff --git a/h3m.py b/h3m.py new file mode 100755 index 0000000..1d8a0e0 --- /dev/null +++ b/h3m.py @@ -0,0 +1,802 @@ +# -*- 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("