1 #!/usr/bin/python2.4 2 # 3 # Copyright 2007 The Android Open Source Project 4 5 """Dump Quake ms2 files. 6 7 Useful for debugging Quake. 8 """ 9 10 # ms2 file format 11 # int32 numcommands 12 # int32 numorder 13 # int32 commands[numcommands] 14 # int32 vertexorder[numorder] 15 # 16 # Where a command is 17 # 18 # >= 0 --> strip(n) 19 # < 0 --> fan(-n) 20 # followed by struct { float s; float t; } st[n]; 21 22 import array 23 import sys 24 25 def readInt32(f): 26 a = array.array('i') 27 a.read(f, 1) 28 return a[0] 29 30 def readFloat32(f): 31 a = array.array('f') 32 a.read(f, 1) 33 return a[0] 34 35 def dumpms2(path): 36 f = open(path, "rw") 37 numCommands = readInt32(f) 38 numOrder = readInt32(f) 39 commandIndex = 0 40 41 # Seek ahead and read the vertex order information 42 f.seek(4 + 4 + 4 * numCommands) 43 vertexOrder = array.array('i') 44 vertexOrder.read(f, numOrder) 45 46 # Read commands 47 f.seek(4 + 4) 48 vertexOrderIndex = 0 49 50 while commandIndex < numCommands: 51 cmd = readInt32(f) 52 commandIndex = commandIndex + 1 53 if cmd == 0: 54 break 55 elif(cmd > 0): 56 # strip 57 print "strip ", cmd 58 for i in range(cmd): 59 s = readFloat32(f) 60 t = readFloat32(f) 61 print "[", i, "] ", vertexOrder[vertexOrderIndex], \ 62 " (", s, ",", t, ")" 63 commandIndex += 2 64 vertexOrderIndex += 1 65 else: 66 # fan 67 print "fan ", -cmd 68 for i in range(-cmd): 69 s = readFloat32(f) 70 t = readFloat32(f) 71 print "[", i, "] ", vertexOrder[vertexOrderIndex], \ 72 " (", s, ",", t, ")" 73 commandIndex += 2 74 vertexOrderIndex += 1 75 76 f.close() 77 78 if __name__ == '__main__': 79 dumpms2(sys.argv[1]) 80