from django.db import models from cursesite.wow.database.models import * import string from struct import unpack WDB_FILES = { "WIDB": Item, "WMOB": Creature, "WNPC": NPC, "WPTX": PageText, "WGOB": GameObject, "WQST": Quest, "WITX": ItemText, } class WDBParser: def __init__(self, filename, debug=False): self.filename = filename self.debug = debug def process(self): try: fp = open(self.filename, "rb") self.fetch_header(fp) self.model = WDB_FILES[self.signature] # we have our header, now we begin reading rows row = self.fetch_row(fp) total_rows = 0 while row: total_rows += 1 store = self.model() row = self.fetch_row(fp) print "Type: %s\nTotal Rows: %d" % (self.model, total_rows) except IOError: pass def fetch_header(self, fp): try: self.signature = fp.read(4)[::-1] self.version = unpack("i", fp.read(4))[0] self.language = fp.read(4)[::-1] fp.seek(8, 1) except: raise def fetch_row(self, fp): row = {} try: row[id] = unpack("i", fp.read(4))[0] length = unpack("i", fp.read(4))[0] if row[id] == 0: return False print "ID: %d" % row[id] toread = fp.read(length) x = 1 while toread: field_type = self.model._meta.fields[x].__class__.__name__ field_name = self.model._meta.fields[x].name if field_type == "CharField" or field_type == "TextField": str = "" while toread[0] != "\x00": str += toread[0] toread = toread[1:] row[field_name] = str toread = toread[1:] #x += 1 elif field_type == "FloatField": row[field_name] = unpack("f", toread[0:4])[0] toread = toread[4:] else: row[field_name] = unpack("i", toread[0:4])[0] toread = toread[4:] x += 1 if self.debug: print "%s (%s): %s" % (field_name, field_type, row[field_name]) #return "Row %d: %s" % (id, fp.read(length)) return row except: raise