Python Map Parser

From Soldat Community Wiki
Revision as of 12:41, 15 August 2012 by Freeman (talk | contribs) (Created page with "<pre> <nowiki> import struct import string import glob # for code brevity up = struct.unpack PMS_Vertex = 0 PMS_Vector = 1 PMS_Type = 2 class pmsMap: 'Soldat PMS readin...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

import struct
import string
import glob

# for code brevity
up = struct.unpack

PMS_Vertex = 0
PMS_Vector = 1
PMS_Type = 2

class pmsMap:
    'Soldat PMS reading class'
    def __init__(self, fileName):
        self.fileName = fileName
        f = file(fileName, 'rb')
        self.version = up('l', f.read(4))[0]
        self.descLen = up('B', f.read(1))[0]
        self.desc = ''.join(up("c"*self.descLen, f.read(self.descLen)))
        # discard fillers -- 1 is for os.SEEK_CUR
        f.seek(38 - self.descLen, 1)
        self.textureLen = up('B', f.read(1))[0]
        self.texture = ''.join(up("c"*self.textureLen, f.read(self.textureLen)))
        # discard filler
        f.seek(24 - self.textureLen, 1)
        self.bgColorTop = up("B"*4, f.read(4))
        self.bgColorBottom = up("B"*4, f.read(4))
        self.jetAmount = up('l', f.read(4))[0]
        self.grenades = up('B', f.read(1))[0]
        self.medkits = up('B', f.read(1))[0]
        self.weatherType = up('B', f.read(1))[0]
        self.stepsType = up('B', f.read(1))[0]
        self.randID = up('l', f.read(4))[0]
        self.polyCount = up('l', f.read(4))[0]
        self.polygons = []
        for i in range(0, self.polyCount):
            self.polygons.append( (((up('ffff', f.read(4*4)), up('B'*4, f.read(4)), up('ff', f.read(4*2))) # three vertex
                                  , (up('ffff', f.read(4*4)), up('B'*4, f.read(4)), up('ff', f.read(4*2)))
                                  , (up('ffff', f.read(4*4)), up('B'*4, f.read(4)), up('ff', f.read(4*2))))
                                  , (up('fff', f.read(4*3)), up('fff', f.read(4*3)), up('fff', f.read(4*3)))
                                  , up('B', f.read(1))[0] ))
        # polygons shall be accessed:
        #   polygons[id][PMS_Vertex][0-3]
        #   polygons[id][PMS_Vector][0-3]
        #   polygons[id][PMS_Type]
        # the rest---
        f.close()

maps = glob.glob('c:/soldat_def/maps/*.PMS')
for map in maps:
    m = pmsMap(map)
    print m.fileName + ' => ' + m.texture