def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("testenum") self.TestEnum = p if (p is not None) else Parser.newPool( "testenum", None, types, self._knownTypes[0]) p = poolByName.get("testenum:last") self.Testenum_last = p if (p is not None) else Parser.newPool( "testenum:last", self.TestEnum, types, self._knownTypes[1]) p = poolByName.get("testenum:third") self.Testenum_third = p if (p is not None) else Parser.newPool( "testenum:third", self.TestEnum, types, self._knownTypes[2]) p = poolByName.get("testenum:second") self.Testenum_second = p if (p is not None) else Parser.newPool( "testenum:second", self.TestEnum, types, self._knownTypes[3]) p = poolByName.get("testenum:default") self.Testenum_default = p if (p is not None) else Parser.newPool( "testenum:default", self.TestEnum, types, self._knownTypes[4]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("boolean") self.Boolean = p if (p is not None) else Parser.newPool( "boolean", None, types, self._knownTypes[0]) p = poolByName.get("if") self.If = p if (p is not None) else Parser.newPool( "if", None, types, self._knownTypes[1]) p = poolByName.get("int") self.Int = p if (p is not None) else Parser.newPool( "int", None, types, self._knownTypes[2]) p = poolByName.get("∀") self.Z2200 = p if (p is not None) else Parser.newPool( "∀", None, types, self._knownTypes[3]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("comment") self.Comment = p if (p is not None) else Parser.newPool("comment", None, types, self._knownTypes[0]) p = poolByName.get("defaultboardercases") self.DefaultBoarderCases = p if (p is not None) else Parser.newPool("defaultboardercases", None, types, self._knownTypes[1]) p = poolByName.get("operator") self.Operator = p if (p is not None) else Parser.newPool("operator", None, types, self._knownTypes[2]) p = poolByName.get("properties") self.Properties = p if (p is not None) else Parser.newPool("properties", None, types, self._knownTypes[3]) p = poolByName.get("none") self.ZNone = p if (p is not None) else Parser.newPool("none", self.Properties, types, self._knownTypes[4]) p = poolByName.get("regularproperty") self.RegularProperty = p if (p is not None) else Parser.newPool("regularproperty", self.Properties, types, self._knownTypes[5]) p = poolByName.get("system") self.System = p if (p is not None) else Parser.newPool("system", self.Properties, types, self._knownTypes[6]) p = poolByName.get("rangeboardercases") self.RangeBoarderCases = p if (p is not None) else Parser.newPool("rangeboardercases", None, types, self._knownTypes[7]) p = poolByName.get("term") self.Term = p if (p is not None) else Parser.newPool("term", None, types, self._knownTypes[8]) except Exception as e: raise ParseException(inStream, -1, e, "A super type does not match the specification; see cause for details.") for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try:p = None except Exception as e: raise ParseException(inStream, -1, e, "A super type does not match the specification; see cause for details.") for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("basicbool") self.BasicBool = p if (p is not None) else Parser.newPool( "basicbool", None, types, self._knownTypes[0]) p = poolByName.get("basicfloat32") self.BasicFloat32 = p if (p is not None) else Parser.newPool( "basicfloat32", None, types, self._knownTypes[1]) p = poolByName.get("basicfloat64") self.BasicFloat64 = p if (p is not None) else Parser.newPool( "basicfloat64", None, types, self._knownTypes[2]) p = poolByName.get("basicfloats") self.BasicFloats = p if (p is not None) else Parser.newPool( "basicfloats", None, types, self._knownTypes[3]) p = poolByName.get("basicint16") self.BasicInt16 = p if (p is not None) else Parser.newPool( "basicint16", None, types, self._knownTypes[4]) p = poolByName.get("basicint32") self.BasicInt32 = p if (p is not None) else Parser.newPool( "basicint32", None, types, self._knownTypes[5]) p = poolByName.get("basicint64i") self.BasicInt64I = p if (p is not None) else Parser.newPool( "basicint64i", None, types, self._knownTypes[6]) p = poolByName.get("basicint64v") self.BasicInt64V = p if (p is not None) else Parser.newPool( "basicint64v", None, types, self._knownTypes[7]) p = poolByName.get("basicint8") self.BasicInt8 = p if (p is not None) else Parser.newPool( "basicint8", None, types, self._knownTypes[8]) p = poolByName.get("basicintegers") self.BasicIntegers = p if (p is not None) else Parser.newPool( "basicintegers", None, types, self._knownTypes[9]) p = poolByName.get("basicstring") self.BasicString = p if (p is not None) else Parser.newPool( "basicstring", None, types, self._knownTypes[10]) p = poolByName.get("basictypes") self.BasicTypes = p if (p is not None) else Parser.newPool( "basictypes", None, types, self._knownTypes[11]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def fieldType(self): """ Reads typeID and returns corresponding FieldType. :return: FieldType """ typeID = self.inStream.v64() if typeID == 0: return ConstantI8(self.inStream.i8()) elif typeID == 1: return ConstantI16(self.inStream.i16()) elif typeID == 2: return ConstantI32(self.inStream.i32()) elif typeID == 3: return ConstantI64(self.inStream.i64()) elif typeID == 4: return ConstantV64(self.inStream.v64()) elif typeID == 5: return self.annotation elif typeID == 6: return BoolType() elif typeID == 7: return I8() elif typeID == 8: return I16() elif typeID == 9: return I32() elif typeID == 10: return I64() elif typeID == 11: return V64() elif typeID == 12: return F32() elif typeID == 13: return F64() elif typeID == 14: return self.strings elif typeID == 15: return ConstantLengthArray(self.inStream.v64(), self.fieldType()) elif typeID == 17: return VariableLengthArray(self.fieldType()) elif typeID == 18: return ListType(self.fieldType()) elif typeID == 19: return SetType(self.fieldType()) elif typeID == 20: return MapType(self.fieldType(), self.fieldType()) elif typeID >= 32: return self.types[typeID - 32] else: raise ParseException(self.inStream, self.blockCounter, None, "Invalid type ID: []", typeID)
def read(self, cls, writeMode, knownTypes): """ Function to create a SkillState defined in the binding. :param cls: class of generated SkillState :param writeMode: current writeMode :param knownTypes: classes generated in binding :return: """ try: r = cls(self.poolByName, self.strings, self.annotation, self.types, self.inStream, writeMode, knownTypes) return r except Exception as e: raise ParseException(self.inStream, self.blockCounter, e, "State instantiation failed!")
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("colorholder") self.ColorHolder = p if (p is not None) else Parser.newPool("colorholder", None, types, self._knownTypes[0]) p = poolByName.get("abstractnode") self.AbstractNode = p if (p is not None) else Parser.newPool("abstractnode", None, types, self._knownTypes[1]) p = poolByName.get("node") self.Node = p if (p is not None) else Parser.newPool("node", self.AbstractNode, types, self._knownTypes[2]) p = poolByName.get("subnode") self.SubNode = p if (p is not None) else Parser.newPool("subnode", self.Node, types, self._knownTypes[3]) except Exception as e: raise ParseException(inStream, -1, e, "A super type does not match the specification; see cause for details.") for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("a") self.A = p if (p is not None) else Parser.newPool( "a", None, types, self._knownTypes[0]) p = poolByName.get("b") self.B = p if (p is not None) else Parser.newPool( "b", self.A, types, self._knownTypes[1]) p = poolByName.get("c") self.C = p if (p is not None) else Parser.newPool( "c", self.B, types, self._knownTypes[2]) p = poolByName.get("d") self.D = p if (p is not None) else Parser.newPool( "d", self.C, types, self._knownTypes[3]) p = poolByName.get("g") self.G = p if (p is not None) else Parser.newPool( "g", self.C, types, self._knownTypes[4]) p = poolByName.get("h") self.H = p if (p is not None) else Parser.newPool( "h", self.A, types, self._knownTypes[5]) p = poolByName.get("i") self.I = p if (p is not None) else Parser.newPool( "i", self.A, types, self._knownTypes[6]) p = poolByName.get("j") self.J = p if (p is not None) else Parser.newPool( "j", self.A, types, self._knownTypes[7]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("properties") self.Properties = p if (p is not None) else Parser.newPool( "properties", None, types, self._knownTypes[0]) p = poolByName.get("system") self.System = p if (p is not None) else Parser.newPool( "system", self.Properties, types, self._knownTypes[1]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def stringBlock(self): """ Parses string block :return: """ try: count = self.inStream.v64() if count != 0: offset = [] last = 0 for i in range(0, count): offset.append(self.inStream.i32()) for j in range(0, count): add = StringPool.Position(self.inStream.position() + last, offset[j] - last) self.strings.stringPositions.append(add) self.strings.idMap.append(None) last = offset[j] self.inStream.jump(self.inStream.position() + last) except Exception as e: raise ParseException(self.inStream, self.blockCounter, e, "corrupted string block")
def __init__(self, poolByName, strings, annotationType, types, inStream, mode, knownTypes): super(SkillState, self).__init__(strings, inStream.path, mode, types, poolByName, annotationType) self._knownTypes = knownTypes try: p = poolByName.get("abuser") self.Abuser = p if (p is not None) else Parser.newPool( "abuser", None, types, self._knownTypes[0]) p = poolByName.get("badtype") self.BadType = p if (p is not None) else Parser.newPool( "badtype", None, types, self._knownTypes[1]) p = poolByName.get("expression") self.Expression = p if (p is not None) else Parser.newPool( "expression", None, types, self._knownTypes[2]) p = poolByName.get("externmixin") self.ExternMixin = p if (p is not None) else Parser.newPool( "externmixin", None, types, self._knownTypes[3]) p = poolByName.get("nowasingleton") self.NowASingleton = p if (p is not None) else Parser.newPool( "nowasingleton", None, types, self._knownTypes[4]) p = poolByName.get("uid") self.UID = p if (p is not None) else Parser.newPool( "uid", None, types, self._knownTypes[5]) p = poolByName.get("user") self.User = p if (p is not None) else Parser.newPool( "user", None, types, self._knownTypes[6]) except Exception as e: raise ParseException( inStream, -1, e, "A super type does not match the specification; see cause for details." ) for t in types: self._poolByName[t.name()] = t self._finalizePools(inStream)
def typeDefinition(self): """ Parses type definitions in type block :return: """ try: name = self.strings.get(self.inStream.v64()) except InvalidPoolIndexException as e: raise ParseException(self.inStream, self.blockCounter, e, "corrupted type header") if name is None: raise ParseException(self.inStream, self.blockCounter, None, "corrupted file: nullptr in typename") if name in self.seenTypes: raise ParseException(self.inStream, self.blockCounter, None, "Duplicate definition of type {}", name) self.seenTypes.add(name) # try to parse the type definition try: count = self.inStream.v64() # definition: StoragePool = None if name in self.poolByName: definition: StoragePool = self.poolByName.get(name) else: self.typeRestriction() superID = self.inStream.v64() # superDef: StoragePool = None if superID == 0: superDef = None elif superID > len(self.types): raise ParseException( self.inStream, self.blockCounter, None, "Type {} refers to an ill-formed super fType.\n" + " found: {}; current number of other types {}", name, superID, len(self.types)) else: superDef = self.types[superID - 1] try: # search in knowntypes for this class seen = False typ = None escapeName = "Z" + str( binascii.hexlify(name.encode("utf-16_be")))[2:-1] for i in self.knownTypes: if name == i.__name__.lower( ) or i.__name__ == escapeName: typ = i seen = True if not seen: superType = (SkillObject, ) if superDef is None else ( superDef._cls, ) typ = type(name, superType, dict()) definition = self.newPool(name, superDef, self.types, typ) if definition.superPool is not superDef: if superDef is None: raise ParseException( self.inStream, self.blockCounter, None, "The file contains a super type {}" "but {} is specified to be a base type.", "<none>", name) else: raise ParseException( self.inStream, self.blockCounter, None, "The file contains a super type {}" "but {} is specified to be a base type.", superDef.__name__, name) self.poolByName[name] = definition except Exception as ex: raise ParseException( self.inStream, self.blockCounter, ex, "The super type of {} stored in the file does not match the specification!", name) if self.blockIDBarrier < definition.typeID(): self.blockIDBarrier = definition.typeID() else: raise ParseException( self.inStream, self.blockCounter, None, "Found unordered type block. Type {} has id {}, barrier was {}.", name, definition.typeID(), self.blockIDBarrier) bpo = definition.basePool._cachedSize if definition.superPool is not None: if count != 0: bpo += self.inStream.v64() else: bpo += definition.superPool.lastBlock().bpo if definition.superPool is not None: b: Block = definition.superPool.lastBlock() if bpo < b.bpo or (b.bpo + b.staticCount) < bpo: raise ParseException(self.inStream, self.blockCounter, None, "Found broken bpo.") definition.blocks.append(Block(bpo, count, count)) definition.staticDataInstances += count fieldCount = self.inStream.v64() self.localFields.append(LFEntry(definition, fieldCount)) except Exception as exc: raise ParseException(self.inStream, self.blockCounter, exc, "unexpected end of file")
def typeBlock(self): """ Parses type block. :return: """ self.seenTypes.clear() self.blockIDBarrier = 0 self.localFields.clear() self.fieldDataQueue.clear() self.offset = 0 # parse type count = self.inStream.v64() for _ in range(0, count): self.typeDefinition() # resize pools by updating cachedSize and staticCount for e in self.localFields: p: StoragePool = e.p b: Block = p.lastBlock() p._cachedSize += b.count if b.count != 0: parent: StoragePool = p.superPool if parent is not None: sb: Block = parent.lastBlock() delta = sb.staticCount - (b.bpo - sb.bpo) if delta > 0: sb.staticCount -= delta parent.staticDataInstances -= delta # parse fields for lfe in self.localFields: p: StoragePool = lfe.p legalFieldIDBarrier = 1 + len(p._dataFields) lastBlock: Block = p.blocks[len(p.blocks) - 1] for fieldcounter in range(lfe.count, 0, -1): ID = self.inStream.v64() if ID > legalFieldIDBarrier or ID <= 0: raise ParseException(self.inStream, self.blockCounter, None, "Found an illegal field ID: {}", ID) if ID == legalFieldIDBarrier: fieldName = self.strings.get(self.inStream.v64()) if fieldName is None: raise ParseException( self.inStream, self.blockCounter, None, "corrupted file: nullptr in field name") t = self.fieldType() self.fieldRestrictions(t) end = self.inStream.v64() try: f: FieldDeclaration = p.addField(t, fieldName) if len(p.blocks) == 1: f.addChunk( SimpleChunk(self.offset, end, lastBlock.bpo, lastBlock.count)) else: f.addChunk( BulkChunk(self.offset, end, p._cachedSize, len(p.blocks))) except SkillException as e: raise ParseException(self.inStream, self.blockCounter, None, e.message) legalFieldIDBarrier += 1 else: end = self.inStream.v64() p._dataFields[ID - 1].addChunk( SimpleChunk(self.offset, end, lastBlock.bpo, lastBlock.count)) self.offset = end self.fieldDataQueue.append(DataEntry(p, ID)) self.processFieldData()