Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- def unpack(f, fmt):
- return struct.unpack(fmt, f.read(struct.calcsize(fmt)))
- def read_fixed_length_null_terminated_string(f, len):
- s = unpack(f, '{}s'.format(len))[0]
- len = next(i for i, v in enumerate(s) if v == 0)
- s = s[:len]
- return s.decode()
- def read_length_prefixed_null_terminated_string(f):
- len = f.read(1)[0]
- s = f.read(len)[:-1]
- print(s)
- return s.decode()
- class Vector2(object):
- def __init__(self):
- self.x = 0
- self.y = 0
- @staticmethod
- def from_buffer(f):
- vector = Vector2()
- vector.x, vector.y = unpack(f, '2f')
- return vector
- class Vector3(object):
- def __init__(self):
- self.x = 0
- self.y = 0
- self.z = 0
- @staticmethod
- def from_buffer(f):
- vector = Vector3()
- vector.x, vector.y, vector.z = unpack(f, '3f')
- return vector
- class Color(object):
- def __init__(self):
- self.r = 0
- self.g = 0
- self.b = 0
- @staticmethod
- def from_buffer(f):
- color = Color()
- color.r, color.g, color.b = unpack(f, '3B')
- return color
- def __repr__(self):
- return ','.join(map(lambda x: str(x), [self.r, self.g, self.b]))
- class VisGroup(object):
- def __init__(self):
- self.name = ''
- self.color = Color()
- self.index = 0
- self.visible = False
- def __repr__(self):
- return self.name
- @staticmethod
- def from_buffer(f):
- visgroup = VisGroup()
- visgroup.name = read_fixed_length_null_terminated_string(f, 128)
- visgroup.color = Color.from_buffer(f)
- visgroup.unk1,\
- visgroup.index,\
- visgroup.is_visible = unpack(f, 'bib')
- return visgroup
- class Face(object):
- def __init__(self):
- self.texture_name = ''
- self.texture_u_axis = Vector3()
- self.texture_u_shift = 0.0
- self.texture_v_axis = Vector3()
- self.texture_v_shift = 0.0
- self.texture_rotation = 0.0
- self.texture_scale = Vector2()
- self.vertices = []
- self.plane = []
- @staticmethod
- def from_buffer(f):
- face = Face()
- face.texture_name = read_fixed_length_null_terminated_string(f, 256)
- unpack(f, 'f')
- face.texture_u_axis = Vector3.from_buffer(f)
- face.texture_u_shift = unpack(f, 'f')
- face.texture_v_axis = Vector3.from_buffer(f)
- face.texture_u_shift = unpack(f, 'f')
- face.texture_rotation = unpack(f, 'f')
- face.texture_scale = Vector2.from_buffer(f)
- unpack(f, '16b')
- vertex_count = unpack(f, 'i')[0]
- face.vertices = [Vector3.from_buffer(f) for i in range(vertex_count)]
- face.plane = [Vector3.from_buffer(f) for i in range(3)]
- return face
- class Solid(object):
- def __init__(self):
- self.visgroup_index = 0
- self.color = Color()
- self.faces = []
- @staticmethod
- def from_buffer(f):
- solid = Solid()
- solid.visgroup_index = unpack(f, 'i')
- solid.color = Color.from_buffer(f)
- unk1 = unpack(f, 'i')[0]
- print(unk1)
- face_count = unpack(f, 'i')[0]
- solid.faces = [Face.from_buffer(f) for i in range(face_count)]
- return solid
- class World(object):
- def __init__(self):
- pass
- @staticmethod
- def from_buffer(f):
- world = World()
- return world
- class Entity(object): # TODO: one of these has to be the rotation
- def __init__(self):
- self.visgroup_index = 0
- self.color = Color()
- self.brushes = []
- self.classname = ''
- self.flags = ''
- self.properties = {}
- self.location = Vector3()
- pass
- @staticmethod
- def from_buffer(f):
- entity = Entity()
- entity.visgroup_index = unpack(f, 'i')[0]
- entity.color = Color.from_buffer(f)
- brush_count = unpack(f, 'i')[0]
- entity.brushes = [Solid.from_buffer(f) for i in range(brush_count)]
- entity.classname = read_length_prefixed_null_terminated_string(f)
- unpack(f, '4b')
- entity.flags = unpack(f, 'i')
- property_count = unpack(f, 'i')[0]
- for i in range(property_count):
- key = read_length_prefixed_null_terminated_string(f)
- value = read_length_prefixed_null_terminated_string(f)
- entity.properties[key] = value
- unpack(f, '14b')
- entity.location = Vector3.from_buffer(f)
- unpack(f, '4b')
- return entity
- class Group(object):
- def __init__(self):
- self.visgroup_index = 0
- self.color = Color()
- self.objects = []
- @staticmethod
- def from_buffer(f):
- group = Group()
- group.visgroup_index = unpack(f, 'i')
- group.color = Color.from_buffer(f)
- object_count = unpack(f, 'i')[0]
- group.objects = [read_object(f) for i in range(object_count)]
- return group
- _object_type_map_ = {
- 'CMapWorld': World,
- 'CMapEntity': Entity,
- 'CMapGroup': Group,
- 'CMapSolid': Solid,
- }
- def read_object(f):
- print(f.tell())
- object_type_string = read_length_prefixed_null_terminated_string(f)
- object_type = _object_type_map_[object_type_string]
- return object_type.from_buffer(f)
- fp = r'C:\Users\Colin\Desktop\Leveling\Day of Defeat\dod_kaust\dod_kaust.rmf'
- with open(fp, 'rb') as f:
- version, magic = unpack(f, 'i3s')
- visgroup_count = unpack(f, 'i')[0]
- for i in range(visgroup_count):
- visgroup = VisGroup.from_buffer(f)
- object_type = read_length_prefixed_null_terminated_string(f)
- f.read(7)
- object_count = unpack(f, 'i')[0]
- objects = [read_object(f) for j in range(object_count)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement