diff --git a/wot/ModelReader.py b/wot/ModelReader.py index 186473b..ac59a59 100644 --- a/wot/ModelReader.py +++ b/wot/ModelReader.py @@ -4,11 +4,10 @@ just rewritten script to python. """ -from XmlUnpacker import XmlUnpacker +from wot.XmlUnpacker import XmlUnpacker import xml.etree.ElementTree as ET from struct import unpack -import os.path -import StringIO +from io import BytesIO def unp(format, data): return unpack(format, data)[0] @@ -21,7 +20,7 @@ def __init__(self,debug=False): def out(self, text): if self.debug: - print text + print(text) def read(self, primitives_fh, visual_fh): # Read visual file @@ -60,7 +59,7 @@ def read(self, primitives_fh, visual_fh): # Read section name section_name_length = unp("I", f.read(4)) - section_name = f.read(section_name_length) + section_name = f.read(section_name_length).decode("UTF-8") # Section informations section = { @@ -83,9 +82,9 @@ def read(self, primitives_fh, visual_fh): f.read(4 - section_name_length % 4) # Read sections data - for name, section in sections.iteritems(): + for name, section in sections.items(): f.seek(section["position"]) - section['data'] = StringIO.StringIO(f.read(section["size"])) + section['data'] = BytesIO(f.read(section["size"])) # Read visual data nodes = {} @@ -178,12 +177,12 @@ def readVertices(self, data): vertices = [] - type = str(data.read(64)).split('\x00')[0] + type = data.read(64).decode("UTF-8").split('\x00')[0] subtype = None count = unp("I", data.read(4)) if "BPVT" in type: - subtype = str(data.read(64)).split('\x00')[0] + subtype = data.read(64).decode("UTF-8").split('\x00')[0] count = unp("I", data.read(4)) self.out("type %s subtype %s count %d stride %d" % (type, subtype, count, self.getStride(type, subtype))) @@ -246,9 +245,9 @@ def readVertice(self, data, type, subtype): def readNormal(self, data, type, subtype): packed = unp("I", data.read(4)) if "set3" in subtype: - pkz = (long(packed) >> 16) & 0xFF ^0xFF - pky = (long(packed) >> 8) & 0xFF ^0xFF - pkx = (long(packed) ) & 0xFF ^0xFF + pkz = (int(packed) >> 16) & 0xFF ^0xFF + pky = (int(packed) >> 8) & 0xFF ^0xFF + pkx = (int(packed) ) & 0xFF ^0xFF if pkx > 0x7f: x = - float(pkx & 0x7f )/0x7f @@ -264,9 +263,9 @@ def readNormal(self, data, type, subtype): z = float(pkz ^ 0x7f)/0x7f return (x,y,z) else: - pkz = (long(packed) >> 22) & 0x3FF - pky = (long(packed) >> 11) & 0x7FF - pkx = (long(packed)) & 0x7FF + pkz = (int(packed) >> 22) & 0x3FF + pky = (int(packed) >> 11) & 0x7FF + pkx = (int(packed)) & 0x7FF if pkx > 0x3ff: x = - float((pkx & 0x3ff ^ 0x3ff)+1)/0x3ff @@ -290,7 +289,7 @@ def readIndices(self, data): groups = [] # Read type (ended by 0) - type = str(data.read(64)).split('\x00')[0] + type = data.read(64).decode("UTF-8", "ignore").split('\x00')[0] # One indice length stride = 2 diff --git a/wot/ModelWriter.py b/wot/ModelWriter.py index fa12438..42aaf09 100644 --- a/wot/ModelWriter.py +++ b/wot/ModelWriter.py @@ -138,11 +138,11 @@ def write(self, primitive, filename, filename_material=None): mtlc = zlib.compress(mtlc) # Save to result filename - with open(filename, "wb") as f: + with open(filename, "wb" if self.compress else "w") as f: f.write(objc) if self.material: - with open(filename_material, "wb") as f: + with open(filename_material, "wb" if self.compress else "w") as f: f.write(mtlc) return filename, filename_material \ No newline at end of file diff --git a/wot/PackageReader.py b/wot/PackageReader.py index d1858c4..e90c947 100644 --- a/wot/PackageReader.py +++ b/wot/PackageReader.py @@ -60,7 +60,7 @@ def loadIndex(self): return # Walk all packages - for name, pack in self.packages.iteritems(): + for name, pack in self.packages.items(): # In case some unusual symbols are present in name try: @@ -102,7 +102,7 @@ def loadIndex(self): def warn(self, text): """Prints some kind of warning.""" - print "Warning: " + str(text) + print("Warning: %s" % str(text)) def loadPackageList(self): """Loads paths to all avaible packages""" @@ -234,7 +234,7 @@ def walk(self, path, recursive = True): yield "/".join(package) # Iter path now - for key,item in package.iteritems(): + for key,item in package.items(): if not isinstance(item, list): # Return file path yield "/".join(package) + "/" + key diff --git a/wot/SpaceReader.py b/wot/SpaceReader.py index 3a05460..8b330fe 100644 --- a/wot/SpaceReader.py +++ b/wot/SpaceReader.py @@ -2,8 +2,8 @@ Thanks Coffee_ for cooperative work on this format, he did most of the hard work """ -import StringIO -from chunks import * +from io import BytesIO +from wot.chunks import * from struct import unpack class SpaceReader: @@ -53,7 +53,7 @@ def setChunk(self, ident, contents): self.chunks[ident.lower()] = contents def getChunk(self, ident): - return StringIO.StringIO(self.chunks.get(ident)) + return BytesIO(self.chunks.get(ident)) def getMatrices(self): return bsmi.get(self.getChunk("bsmi"), self.debug) diff --git a/wot/XmlUnpacker.py b/wot/XmlUnpacker.py index 408841f..6342d4a 100644 --- a/wot/XmlUnpacker.py +++ b/wot/XmlUnpacker.py @@ -93,7 +93,7 @@ def readString(self,length): if length == 0: return '' - return self.stream.read(length) + return self.stream.read(length).decode("UTF-8") def readNumber(self, length): if length == 0: @@ -136,7 +136,7 @@ def readBoolean(self, length): raise Exception("Boolean with wrong length.") def readBase64(self, length): - return base64.b64encode(self.stream.read(length)) + return base64.b64encode(self.stream.read(length)).decode("UTF-8") def readDictionary(self): self.stream.seek(5); @@ -157,7 +157,7 @@ def readASCIIZ(self): c = self.stream.read(1) if ord(c) == 0: break; - str += c + str += c.decode("UTF-8") return str def isPacked(self): diff --git a/wot/__init__.py b/wot/__init__.py index 9a78029..4937db5 100644 --- a/wot/__init__.py +++ b/wot/__init__.py @@ -1,9 +1,9 @@ -from PackageReader import PackageReader -from XmlUnpacker import XmlUnpacker -from ModelReader import ModelReader -from ModelWriter import OBJModelWriter -from SpaceReader import SpaceReader -from TreesReader import TreesReader +from wot.PackageReader import PackageReader +from wot.XmlUnpacker import XmlUnpacker +from wot.ModelReader import ModelReader +from wot.ModelWriter import OBJModelWriter +from wot.SpaceReader import SpaceReader +from wot.TreesReader import TreesReader import xml.etree.ElementTree as ET diff --git a/wot/chunks/__init__.py b/wot/chunks/__init__.py index aa6fd28..8ef2163 100644 --- a/wot/chunks/__init__.py +++ b/wot/chunks/__init__.py @@ -1,9 +1,9 @@ -import bwst -import bsmo -import bwsg -import bsma -import wgsd -import bwss -import bsmi -import sptr -import bwwa \ No newline at end of file +import wot.chunks.bwst +import wot.chunks.bsmo +import wot.chunks.bwsg +import wot.chunks.bsma +import wot.chunks.wgsd +import wot.chunks.bwss +import wot.chunks.bsmi +import wot.chunks.sptr +import wot.chunks.bwwa \ No newline at end of file diff --git a/wot/chunks/bsma.py b/wot/chunks/bsma.py index c61f169..126f0d4 100644 --- a/wot/chunks/bsma.py +++ b/wot/chunks/bsma.py @@ -1,4 +1,4 @@ -from utility import * +from wot.chunks.utility import * def get(f, strings, debug=False): result = [] diff --git a/wot/chunks/bsmi.py b/wot/chunks/bsmi.py index ab25b42..7874c6a 100644 --- a/wot/chunks/bsmi.py +++ b/wot/chunks/bsmi.py @@ -1,5 +1,5 @@ -from utility import * -from table import Table +from wot.chunks.utility import * +from wot.chunks.table import Table def get(f, debug=False): table = Table(f, debug, "Matrices") diff --git a/wot/chunks/bsmo.py b/wot/chunks/bsmo.py index 39f1ac6..262d713 100644 --- a/wot/chunks/bsmo.py +++ b/wot/chunks/bsmo.py @@ -1,8 +1,8 @@ -from utility import * +from wot.chunks.utility import * def get(f, strings, materials, bwsg, matrices, debug=False): if debug: - print "1.Table - Nodes ranges" + print("1.Table - Nodes ranges") """ NODES RANGES TABLE @@ -26,7 +26,7 @@ def get(f, strings, materials, bwsg, matrices, debug=False): }) if debug: - print "2.Table - models" + print("2.Table - models") """ MODELS TABLE @@ -67,12 +67,12 @@ def get(f, strings, materials, bwsg, matrices, debug=False): models.append(info) if debug: - print index, info["name"], info["from"], info["to"] + print(index, info["name"], info["from"], info["to"]) index += 1 if debug: - print "3.Table" + print("3.Table") """ PURPOSE UNKNOWN @@ -89,10 +89,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): for item in table["entries"]: if debug: - print [ hex2(v, 8) for v in unpack("<" + ("I" * (table["entry_size"]//4)), item) ] + print([ hex2(v, 8) for v in unpack("<" + ("I" * (table["entry_size"]//4)), item) ]) if debug: - print "4.Table - Bounding boxes" + print("4.Table - Bounding boxes") """ BOUDING BOXES TABLE @@ -116,7 +116,7 @@ def get(f, strings, materials, bwsg, matrices, debug=False): index += 1 if debug: - print "5.Table" + print("5.Table") """ UKNOWN TABLE @@ -133,10 +133,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) if debug: - print "6.Table - Nodes" + print("6.Table - Nodes") """ NODES TABLE @@ -157,7 +157,7 @@ def get(f, strings, materials, bwsg, matrices, debug=False): ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) nodes.append({ "from": ints[0], @@ -165,7 +165,7 @@ def get(f, strings, materials, bwsg, matrices, debug=False): }) if debug: - print "7.Table - Primitive groups" + print("7.Table - Primitive groups") """ PRIMITIVE GROUPS @@ -210,13 +210,13 @@ def get(f, strings, materials, bwsg, matrices, debug=False): groups.append(info) if debug: - print [ hex2(v, 8) for v in ints ] - print index, info["vertices"], info["indicies"] + print([ hex2(v, 8) for v in ints ]) + print(index, info["vertices"], info["indicies"]) index += 1 if debug: - print "8.Table" + print("8.Table") """ UNKNOWN @@ -230,10 +230,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): for item in table["entries"]: ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) if debug: - print "9.Table" + print("9.Table") """ UNKNOWN @@ -248,10 +248,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) if debug: - print "10.Table" + print("10.Table") """ UNKNOWN @@ -266,10 +266,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) if debug: - print "11.Table" + print("11.Table") """ UNKNOWN @@ -284,10 +284,10 @@ def get(f, strings, materials, bwsg, matrices, debug=False): ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) if debug: - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) if debug: - print ("position", hex2(f.tell())) + print("position", hex2(f.tell())) index = 0 for model in models: diff --git a/wot/chunks/bwsg.py b/wot/chunks/bwsg.py index da063c6..0253896 100644 --- a/wot/chunks/bwsg.py +++ b/wot/chunks/bwsg.py @@ -1,5 +1,5 @@ -from utility import * -import StringIO +from wot.chunks.utility import * +from io import BytesIO def get(f, debug=False): strings_table = read_table(f) @@ -101,7 +101,7 @@ def get(f, debug=False): if block["type"] == 0: vertices = [] - main = StringIO.StringIO(block["data"]) + main = BytesIO(block["data"]) stride = block["stride"] for i in xrange(model["vertex_count"]): @@ -160,9 +160,7 @@ def get(f, debug=False): for i in range(model["position_from"], model["position_to"]): position = positions[i] f.seek(raw_position + chunks[position["chunk"]]["position"] + position["position"]) - print "POS", position["size"] out.write(f.read(position["size"])) - print model["name"], os.path.getsize("vertices/" + str(model["name"])) return results diff --git a/wot/chunks/bwss.py b/wot/chunks/bwss.py index c49408c..8607b27 100644 --- a/wot/chunks/bwss.py +++ b/wot/chunks/bwss.py @@ -1,4 +1,4 @@ -from utility import * +from wot.chunks.utility import * def get(f, strings, debug=False): table = read_table(f) @@ -6,7 +6,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) index = 0 for i in ints: @@ -28,7 +28,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) index = 0 for i in ints: @@ -43,7 +43,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("I" * (table["entry_size"]//4)), item) - print [ hex2(v, 8) for v in ints ] + print([ hex2(v, 8) for v in ints ]) index = 0 for i in ints: @@ -58,7 +58,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("B" * (table["entry_size"])), item) - print " ".join([ hex2(v, 2) for v in ints ]) + print(" ".join([ hex2(v, 2) for v in ints ])) index = 0 for i in ints: @@ -73,7 +73,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("B" * (table["entry_size"])), item) - print " ".join([ hex2(v, 2) for v in ints ]) + print(" ".join([ hex2(v, 2) for v in ints ])) index = 0 for i in ints: @@ -88,12 +88,12 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("B" * (table["entry_size"])), item) - print " ".join([ hex2(v, 2) for v in ints ]) + print(" ".join([ hex2(v, 2) for v in ints ])) index = 0 for i in ints: if i in strings: - print (index, strings[i]) + print(index, strings[i]) index += 1 print ("position", hex2(f.tell())) @@ -103,7 +103,7 @@ def get(f, strings, debug=False): for item in table["entries"]: ints = unpack("<" + ("I" * (table["entry_size"] // 4)), item) - print " ".join([ hex2(v, 8) for v in ints ]) + print(" ".join([ hex2(v, 8) for v in ints ])) index = 0 for i in ints: diff --git a/wot/chunks/bwst.py b/wot/chunks/bwst.py index 3c3ace1..ab331ba 100644 --- a/wot/chunks/bwst.py +++ b/wot/chunks/bwst.py @@ -1,4 +1,4 @@ -from utility import * +from wot.chunks.utility import * def get(f, debug=False): strings = {} @@ -13,7 +13,7 @@ def get(f, debug=False): for item in table["entries"]: key = unp("