Skip to content

Commit

Permalink
Added compability with python3
Browse files Browse the repository at this point in the history
  • Loading branch information
SkaceKamen committed Feb 6, 2016
1 parent b1192f6 commit 4664099
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 89 deletions.
31 changes: 15 additions & 16 deletions wot/ModelReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions wot/ModelWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions wot/PackageReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions wot/SpaceReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions wot/XmlUnpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions wot/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 9 additions & 9 deletions wot/chunks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bwst
import bsmo
import bwsg
import bsma
import wgsd
import bwss
import bsmi
import sptr
import bwwa
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
2 changes: 1 addition & 1 deletion wot/chunks/bsma.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from utility import *
from wot.chunks.utility import *

def get(f, strings, debug=False):
result = []
Expand Down
4 changes: 2 additions & 2 deletions wot/chunks/bsmi.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
46 changes: 23 additions & 23 deletions wot/chunks/bsmo.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -157,15 +157,15 @@ 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],
"to": ints[1]
})

if debug:
print "7.Table - Primitive groups"
print("7.Table - Primitive groups")

"""
PRIMITIVE GROUPS
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 4664099

Please sign in to comment.