Beispiel #1
0
 def parse(self, stream: KaitaiStream, context: AST):
     offset = stream.pos()
     c = stream.read_bytes(len(self.contents))
     if c != self.contents:
         print(context.root.to_dict())
         raise RuntimeError(
             f"File offset {offset}: Expected {self.contents!r} but instead got {c!r}"
         )
     return RawBytes(c, offset)
Beispiel #2
0
        def contents(self):
            if hasattr(self, '_m_contents'):
                return self._m_contents if hasattr(self,
                                                   '_m_contents') else None

            io = self._root._io
            _pos = io.pos()
            io.seek(self.offset)
            _on = self.name
            if _on == u"SECTORS":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Sectors(io, self, self._root)
            elif _on == u"TEXTURE1":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Texture12(io, self, self._root)
            elif _on == u"VERTEXES":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Vertexes(io, self, self._root)
            elif _on == u"BLOCKMAP":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Blockmap(io, self, self._root)
            elif _on == u"PNAMES":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Pnames(io, self, self._root)
            elif _on == u"TEXTURE2":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Texture12(io, self, self._root)
            elif _on == u"THINGS":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Things(io, self, self._root)
            elif _on == u"LINEDEFS":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Linedefs(io, self, self._root)
            elif _on == u"SIDEDEFS":
                self._raw__m_contents = io.read_bytes(self.size)
                io = KaitaiStream(BytesIO(self._raw__m_contents))
                self._m_contents = self._root.Sidedefs(io, self, self._root)
            else:
                self._m_contents = io.read_bytes(self.size)
            io.seek(_pos)
            return self._m_contents if hasattr(self, '_m_contents') else None
Beispiel #3
0
 def parse(self, stream: KaitaiStream, context: AST) -> RawBytes:
     offset = stream.pos()
     ret = bytearray()
     if self.size is None:
         size = None
     else:
         size = int(self.size.interpret(context))
     while self.size is None or not self._size_met(ret, size):
         try:
             b = stream.read_bytes(1)
         except EOFError:
             if self.size_eos:
                 break
             else:
                 raise RuntimeError("Unexpected end of stream")
         ret.extend(b)
         if self.terminator is not None and b == self.terminator:
             break
     return RawBytes(bytes(ret), offset)
        def value(self):
            if hasattr(self, '_m_value'):
                return self._m_value if hasattr(self, '_m_value') else None

            io = self._root._io
            _pos = io.pos()
            io.seek(self.offset)
            _on = self.tag
            if _on == u"head":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Head(io, self, self._root)
            elif _on == u"cvt ":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Cvt(io, self, self._root)
            elif _on == u"prep":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Prep(io, self, self._root)
            elif _on == u"kern":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Kern(io, self, self._root)
            elif _on == u"hhea":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Hhea(io, self, self._root)
            elif _on == u"post":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Post(io, self, self._root)
            elif _on == u"OS/2":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Os2(io, self, self._root)
            elif _on == u"name":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Name(io, self, self._root)
            elif _on == u"maxp":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Maxp(io, self, self._root)
            elif _on == u"glyf":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Glyf(io, self, self._root)
            elif _on == u"fpgm":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Fpgm(io, self, self._root)
            elif _on == u"cmap":
                self._raw__m_value = io.read_bytes(self.length)
                io = KaitaiStream(BytesIO(self._raw__m_value))
                self._m_value = self._root.Cmap(io, self, self._root)
            else:
                self._m_value = io.read_bytes(self.length)
            io.seek(_pos)
            return self._m_value if hasattr(self, '_m_value') else None
Beispiel #5
0
    def from_file(cls, filename: str, distance_multiplier=0.001):
        if is_binary(filename):
            with open(filename, 'rb') as file:
                stream = KaitaiStream(file)
                name = stream.read_bytes(80).decode('utf8')
                # print(name)
                num_triangles = stream.read_u4le()
                # print(num_triangles)

                triangles = [None] * (num_triangles)
                invalid_triangles = []
                for i in range(num_triangles):
                    if i % 5000 == 0:
                        print('reading stl', round(i / num_triangles * 100, 2),
                              '%')
                    normal = vm.Vector3D(stream.read_f4le(),
                                         stream.read_f4le(),
                                         stream.read_f4le())
                    # print(n)
                    p1 = vm.Point3D(distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le())
                    p2 = vm.Point3D(distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le())
                    p3 = vm.Point3D(distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le(),
                                    distance_multiplier * stream.read_f4le())
                    # print(p1, p2, p3)
                    try:
                        triangles[i] = vmf.Triangle3D(p1, p2, p3)
                    except ZeroDivisionError:
                        invalid_triangles.append(i)

                    stream.read_u2le()
                    # print(abr)
            if invalid_triangles:
                print('invalid_triangles number: ', len(invalid_triangles))
                for i in invalid_triangles[::-1]:
                    del triangles[i]
        else:
            with open(filename, 'r') as file:
                header = file.readline()
                name = header[6:]
                triangles = []
                points = []
                for line in file.readlines():
                    if 'vertex' in line:
                        line = line.replace('vertex', '')
                        line = line.lstrip(' ')
                        x, y, z = line.split(' ')
                        points.append(
                            vm.Point3D(distance_multiplier * float(x),
                                       distance_multiplier * float(y),
                                       distance_multiplier * float(z)))
                    if 'endfacet' in line:
                        try:
                            triangles.append(vmf.Triangle3D(*points))
                        except ZeroDivisionError:
                            pass
                        points = []

        return cls(triangles, name=name)