From 1886443d6fa598a9ab5c7c65b6cdc942d8c0cf38 Mon Sep 17 00:00:00 2001 From: AleaJactaEst Date: Fri, 27 Nov 2020 23:32:51 +0100 Subject: [PATCH] update --- spykhanat.py | 4 +- tools/BitStream.py | 51 ++++++++++-- tools/CAction.py | 2 +- tools/CGenericMultiPartTemp.py | 11 +-- tools/DecodeDatabase.py | 145 ++++++++++++++------------------- tools/Impulse.py | 1 + 6 files changed, 113 insertions(+), 101 deletions(-) diff --git a/spykhanat.py b/spykhanat.py index bb6b17c..d8e5fc5 100755 --- a/spykhanat.py +++ b/spykhanat.py @@ -942,12 +942,12 @@ def main(): #logger.append(logging.getLogger(LOGGER)) # logger.append(logging.getLogger(CImpulseDecoder.LOGGER)) # #logger.append(logging.getLogger(DecodeImpuls.LOGGER)) -# #logger.append(logging.getLogger(BitStream.LOGGER)) + logger.append(logging.getLogger(BitStream.LOGGER)) # logger.append(logging.getLogger(CStringManager.LOGGER)) #logger.append(logging.getLogger(CAction.LOGGER)) #logger.append(logging.getLogger(CActionFactory.LOGGER)) #logger.append(logging.getLogger(BitStream.LOGGER)) - #logger.append(logging.getLogger(DecodeDatabase.LOGGER)) + logger.append(logging.getLogger(DecodeDatabase.LOGGER)) logger.append(logging.getLogger(Impulse.LOGGER)) #logger.append(logging.getLogger(TVPNodeBase.LOGGER)) # CImpulseDecoder diff --git a/tools/BitStream.py b/tools/BitStream.py index 6e05391..b38cc45 100644 --- a/tools/BitStream.py +++ b/tools/BitStream.py @@ -542,7 +542,8 @@ class BitStream(): if nbits == 0: return elif nbits > 32: - raise "Out of range" + logging.getLogger(LOGGER).error("Error: Stream Overflow - nbits:%d/%d name:'%s' decode:'%s' typeName:'%s' emulate:%s msg:%s" % (nbits, self._pos-self._read, name, str(decode), typeName, str(emulate), self.showAllData())) + raise OverflowError if self._read + nbits > self._pos: if self._CheckStreamOverflow: logging.getLogger(LOGGER).error("Error: Stream Overflow - nbits:%d/%d name:'%s' decode:'%s' typeName:'%s' emulate:%s msg:%s" % (nbits, self._pos-self._read, name, str(decode), typeName, str(emulate), self.showAllData())) @@ -568,9 +569,9 @@ class BitStream(): self._groupRead.append((v1, v1+nbits, name, typeName, value, commentValue)) return value - def readSerial64(self, nbits, name="", decode=True, typeName=None, commentValue=None): + def readSerialUint64(self, nbits, name="", decode=True, typeName=None, commentValue=None): + v1 = self._read if nbits > 32: - v1 = self._read msd = self.readSerial(nbits - 32, name, False, typeName) msd2 = self.readSerial(32, name, False, typeName) value = msd << 32 | msd2 @@ -579,10 +580,31 @@ class BitStream(): typeName = 'Uint{0}'.format(nbits) self._groupRead.append((v1, v1+nbits, name, typeName, value, commentValue)) return value + else: + if decode: + if typeName is None: + typeName = 'Uint{0}'.format(nbits) + value = self.readSerial(nbits, name, False, None, commentValue=None) + self._groupRead.append((v1, v1+nbits, name, typeName, value, commentValue)) + return value + + def readSerialSint64(self, nbits, name="", decode=True, typeName=None, commentValue=None): + v1 = self._read + if nbits > 32: + msd = self.readSerial(nbits - 32, name, False, typeName) + msd2 = self.readSerial(32, name, False, typeName) + value = msd << 32 | msd2 + if decode: + if typeName is None: + typeName = 'Sint{0}'.format(nbits) + self._groupRead.append((v1, v1+nbits, name, typeName, value, commentValue)) else: if typeName is None: - typeName = 'Uint{0}'.format(nbits) - return self.readSerial(nbits, name, decode, typeName, commentValue=commentValue) + typeName = 'Sint{0}'.format(nbits) + value = self.readSerial(nbits, name, False, None, commentValue=None) + self._groupRead.append((v1, v1+nbits, name, typeName, value, commentValue)) + #value = self.readSerial(nbits, name, decode, typeName, commentValue=commentValue) + return c_int64(value).value def readCBitSet(self, nbits, name="", decode=True, typeName=None, commentValue=None): # khanat-opennel-code/code/nel/src/misc/bit_mem_stream.cpp void CBitMemStream::readBits( NLMISC::CBitSet& bitfield ) @@ -610,8 +632,9 @@ class BitStream(): if decode: valuereal = cBitSet.showBitString() _size = len(valuereal) - _start = _size - nbits - self._groupRead.append((v1, v1+nbits, name, typeName, valuereal[0:nbits], commentValue)) + # _start = _size - nbits + #self._groupRead.append((v1, v1+nbits, name, typeName, valuereal[0:nbits], commentValue)) + self._groupRead.append((v1, v1+nbits, name, typeName, valuereal[-nbits:], commentValue)) return cBitSet def readCBitSetOld2(self, nbits, name="", decode=True, typeName=None, commentValue=None): @@ -755,6 +778,20 @@ class BitStream(): def readChar(self, name, decode=True): v = self.readUint8(name=name, decode=decode, typeName='Char') return chr(v) + def readNbChar(self, size, name, decode=True): + tmp = '' + _size = size + v1 = self._read + while _size > 0: + x = self.readChar('', decode=False) + tmp += x + _size -= 1 + v2 = self._read + if v2 > self._pos: + raise ValueError + if v1 < v2: + self._groupRead.append((v1, v2, name + ':string', 'String', tmp, None)) + return tmp def readString(self, name): tmp = '' diff --git a/tools/CAction.py b/tools/CAction.py index c334529..13c3863 100644 --- a/tools/CAction.py +++ b/tools/CAction.py @@ -453,7 +453,7 @@ class CActionSint64(CAction): logging.getLogger(LOGGER).debug("nb bit:{0}".format(self.NbBits)) logging.getLogger(LOGGER).debug("msgin:%s" % msgin.showAllData()) # self.value = msgin.readSerial( self.NbBits, 'value') - self.value = msgin.readSerial64( self.NbBits, self.NameProperty) + self.value = msgin.readSerialUint64( self.NbBits, self.NameProperty) logging.getLogger(LOGGER).debug("msgin:%s" % msgin.showAllData()) #self.NbBits = msgin.readUint32('NbBits') logging.getLogger(LOGGER).debug("value:%u" % self.value) diff --git a/tools/CGenericMultiPartTemp.py b/tools/CGenericMultiPartTemp.py index 87b25ec..9397f7e 100644 --- a/tools/CGenericMultiPartTemp.py +++ b/tools/CGenericMultiPartTemp.py @@ -52,25 +52,26 @@ class CGenericMultiPartTemp(): self.block.setdefault(Part, PartCont) self.block.setdefault(Part, None) self.block[Part] = PartCont - logging.getLogger(LOGGER).error("CGenericMultiPartTemp : Number:%d len:%d/%d" % (Number, len(self.block), self.NbBlock)) + logging.getLogger(LOGGER).debug("CGenericMultiPartTemp : Number:%d len:%d/%d" % (Number, len(self.block), self.NbBlock)) if len(self.block) == self.NbBlock: # reform the total action bms = BitStream.BitStream() self.NbBlock == 0xFFFFFFFF for data in self.block: - logging.getLogger(LOGGER).error("CGenericMultiPartTemp : Number:%d id:%d len:%d/%d" % (Number, data, len(self.block), self.NbBlock)) + logging.getLogger(LOGGER).debug("CGenericMultiPartTemp : Number:%d id:%d len:%d/%d" % (Number, data, len(self.block), self.NbBlock)) bms.pushBitStream(self.block[data]) if self.AutoDecompile: try: ret = decodeImpulse.execute(bms, world) except: logging.getLogger(LOGGER).error("CGenericMultiPartTemp : Error to decode - Number:%d len:%d/%d msg:%s" % (Number, len(self.block), self.NbBlock, bms.showAllData())) - return ret - logging.getLogger(LOGGER).error("CGenericMultiPartTemp : data : %s" % bms.showAllData()) + #return ret + raise ValueError + logging.getLogger(LOGGER).debug("CGenericMultiPartTemp : data : %s" % bms.showAllData()) self.MsgDecoded = bms else: - logging.getLogger(LOGGER).error("CGenericMultiPartTemp : Wait other block Number:%d [%d/%d]" % (Number, len(self.block), self.NbBlock)) + logging.getLogger(LOGGER).debug("CGenericMultiPartTemp : Wait other block Number:%d [%d/%d]" % (Number, len(self.block), self.NbBlock)) return ret def isDecoded(self): diff --git a/tools/DecodeDatabase.py b/tools/DecodeDatabase.py index f5a665a..e584c6a 100644 --- a/tools/DecodeDatabase.py +++ b/tools/DecodeDatabase.py @@ -124,8 +124,10 @@ class LeafDatabase(): def countLeaves(self): if self.count: + logging.getLogger(LOGGER).debug("countLeaves leaf %s (nb:%s)" % (self.name, str(self.count))) return self.count else: + logging.getLogger(LOGGER).debug("countLeaves leaf %s (nb:1)" % (self.name)) return 1 def show(self, level=1): @@ -133,22 +135,31 @@ class LeafDatabase(): def execute(self, msgin, name=""): if name: - tmp = "/" + name + tmp = name else: - tmp = "/" + self.name - if self.type[0] == 'I': + tmp = self.name + if self.type[0] == 'I': # Unsigned logging.getLogger(LOGGER).debug("Read:" + str (self.type)) value = int(self.type[1:]) - _ = msgin.readSerial64(value, name='DatabaseXML' + tmp, typeName=self.type) + _ = msgin.readSerialUint64(value, name=tmp, typeName=self.type) + elif self.type[0] == 'S': # Signed + logging.getLogger(LOGGER).debug("Read:" + str (self.type)) + value = int(self.type[1:]) + # _ = msgin.readNbChar(value, name='DatabaseXML' + tmp) + _ = msgin.readSerialUint64(value, name=tmp) + elif self.type == 'TEXT': + logging.getLogger(LOGGER).debug("type:" + str (self.type)) + value = 32 + _ = msgin.readSerialSint64(value, name=idname) else: - logging.getLogger(LOGGER).debug("Type inconnu:" + str (self.type)) + logging.getLogger(LOGGER).error("Type inconnu:" + str (self.type)) raise "type not managed" def execute_atom(self, level, pos, msgin, name=""): if name: - extraName = "/" + name + "/" + self.name + extraName = name + "/" + self.name else: - extraName = "/" + self.name + extraName = self.name if self.count: step = self.count else: @@ -164,8 +175,19 @@ class LeafDatabase(): continue if self.type[0] == 'I': value = int(self.type[1:]) - _ = msgin.readSerial(value, name='DatabaseXML' + idname, typeName=self.type) + if value > 64: + raise "type not managed" + logging.getLogger(LOGGER).debug('value:' + str(value)) + _ = msgin.readSerialUint64(value, name=idname, typeName=self.type) return level+1 + elif self.type[0] == 'S': + logging.getLogger(LOGGER).debug("type:" + str (self.type)) + value = int(self.type[1:]) + _ = msgin.readSerialSint64(value, name=idname) + elif self.type == 'TEXT': + logging.getLogger(LOGGER).debug("type:" + str (self.type)) + value = 32 + _ = msgin.readSerialUint64(value, name=idname) else: logging.getLogger(LOGGER).debug("Type inconnu:", self.type) raise "type not managed" @@ -239,7 +261,8 @@ class BranchDatabase(): count += ele.countLeaves() for ele in self.leaf: count += ele.countLeaves() - if self.count: + if self.count and self.atom: +# logging.getLogger(LOGGER).debug("countLeaves branch <- %s (nb:%s)" % (self.name, str(self.count))) count *= self.count return count @@ -249,7 +272,7 @@ class BranchDatabase(): else: step=1 if name: - extraName = name + "/" + self.name + extraName = name + self.name else: extraName = self.name for step in range(0, step): @@ -282,13 +305,18 @@ class BranchDatabase(): for ele in self.leaf: ele.show(level + 1) - def execute_atom(self, msgin): + def execute_atom(self, msgin, parent): nbchild = self.countLeaves() + # nbchild = len(self.leaf) + len(self.branch) #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild) logging.getLogger(LOGGER).debug("needRead:" + str(msgin.needRead()) + " nbchild:" + str(nbchild)) logging.getLogger(LOGGER).debug(msgin.showAllData()) - cBitSet = msgin.readCBitSet(nbchild, self.name + ":Param", typeName = 'I' + str(nbchild)) + if self.name: + idname = parent + self.name +'/' + else: + idname = parent + cBitSet = msgin.readCBitSet(nbchild, idname + ":Param", typeName = 'I' + str(nbchild)) #cBitSet = CBitSet.CBitSet() #cBitSet.readSerialExtra(msgin, nbchild, self.name + "/Param") logging.getLogger(LOGGER).debug(msgin.showAllData()) @@ -298,26 +326,28 @@ class BranchDatabase(): logging.getLogger(LOGGER).debug(str(i) + " - " + str(cBitSet.get(i))) if cBitSet.get(i): todelete_count_true += 1 - _ = self.execute_atom_found(0, i, msgin) + _ = self.execute_atom_found(0, i, msgin, parent) if todelete_count_true > 1: logging.getLogger(LOGGER).debug(msgin.showAllData()) #raise "A controler" logging.getLogger(LOGGER).debug(msgin.showAllDataRaw()) logging.getLogger(LOGGER).debug(msgin.showAllData()) #raise "A faire" + #if self.name == "GROUP": + # raise "A controler" - def execute_normal(self, msgin): + def execute_normal(self, msgin, parent): nbchild = self.getIdBits() #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild) nbBit = getPowerOf2.getPowerOf2_ter(nbchild) if nbBit > msgin.needRead() : return logging.getLogger(LOGGER).debug("needRead:" + str(msgin.needRead()) + " nbBit:" + str(nbBit)) - id = msgin.readSerial(nbBit, name='DatabaseXML', typeName='Number:'+str(nbBit), emulate=True) + id = msgin.readSerial(nbBit, name=parent, typeName='Number:'+str(nbBit), emulate=True) i = 0 ii = 0 for ele in self.branch: - logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name)) + #logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name)) if ele.count: i += ele.count else: @@ -329,12 +359,13 @@ class BranchDatabase(): comment = str(pos) logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name)) if self.name: - idname = '/' + self.name + '/' + ele.name + idnameshort = parent + self.name + '/' else: - idname = '/' + ele.name - _= msgin.readSerial(nbBit, name='DatabaseXML' + idname, typeName='Number:'+str(nbBit), emulate=False, commentValue=ele.name+comment) + idnameshort = parent + idname = idnameshort + ele.name + _= msgin.readSerial(nbBit, name=idname, typeName='Number:'+str(nbBit), emulate=False, commentValue=ele.name+comment) logging.getLogger(LOGGER).debug("name:" + ele.name + ", count:" + str(ele.count) + ", atom:" + str(ele.atom)) - ele.execute(msgin) + ele.execute(msgin, idnameshort) return ii = i for ele in self.leaf: @@ -349,21 +380,22 @@ class BranchDatabase(): pos = id - ii comment = str(pos) if self.name: - idname = '/' + self.name + '/' + ele.name + comment + idnameshort = parent + self.name + '/' else: - idname = '/' + ele.name + comment + idnameshort = parent + idname = idnameshort + ele.name + comment logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name)) - _ = msgin.readSerial(nbBit, name='DatabaseXML' + idname, typeName='Number:'+str(nbBit), emulate=False, commentValue=ele.name+comment) + _ = msgin.readSerial(nbBit, name= idname, typeName='Number:'+str(nbBit), emulate=False, commentValue=ele.name+comment) logging.getLogger(LOGGER).debug("name:" + ele.name + ", count:" + str(ele.count)) - ele.execute(msgin, name=idname) + ele.execute(msgin, name=idnameshort) return ii = i - def execute(self, msgin): + def execute(self, msgin, parent='DatabaseXML/'): if self.atom: - self.execute_atom(msgin) + self.execute_atom(msgin, parent) else: - self.execute_normal(msgin) + self.execute_normal(msgin, parent) def execute_root(self, msgin): if self.atom: @@ -459,62 +491,3 @@ class DecodeDatabase(): show_dico(self.databasePlr) logging.getLogger(LOGGER).debug("max %d" % str(self.databasePlr['max'])) #raise "Decode" - - def execute2(self, msgin, world): - logging.getLogger(LOGGER).debug("execute") - head = self.databasePlr - listpath = [] - - logging.getLogger(LOGGER).debug(str(head)) - while True: - logging.getLogger(LOGGER).debug("count_elements:" + str(count_elements(head))) - nbchild = count_elements(head) - logging.getLogger(LOGGER).debug("count_elements(head):", nbchild) - if nbchild == 0: - logging.getLogger(LOGGER).debug("Ahhhh", "+"*80) - return True - logging.getLogger(LOGGER).debug("nbchild:", nbchild) - nbBit = getPowerOf2.getPowerOf2_Bis(nbchild) - logging.getLogger(LOGGER).debug("nbBit:", nbBit) -# res=1; -# ret=0; -# print("ret:", ret, "res:", res) -# while res %s" % (nbBit, ':'.join(listpath)) ) - - logging.getLogger(LOGGER).debug("Ahhhh a", "-"*80) - show_dico(head) - ele = get_element(head, int(id)) - logging.getLogger(LOGGER).debug(str(ele)) - show_dico(ele) - logging.getLogger(LOGGER).debug(msgin.showAllData()) - name = ele['name'] - id = msgin.readSerial(nbBit, name='DatabaseXML', typeName='Number:'+str(nbBit), commentValue=name) - listpath.append(name) - fullname = ':'.join(listpath) - logging.getLogger(LOGGER).debug("fullname:", fullname) - logging.getLogger(LOGGER).debug(fullname) - if 'count' in ele: - nbBit = getPowerOf2.getPowerOf2_Bis(int(ele['count'])) - count = msgin.readSerial(nbBit, name='DatabaseXML', typeName='count:'+str(ele['count']+':'+str(nbBit))) - listpath.append(str(count)) - if 'type' in ele: - logging.getLogger(LOGGER).debug("Ahhhh 2", "+"*80) - logging.getLogger(LOGGER).debug(str(ele['type'])) - if ele['type'] == 'I32': - _ = msgin.readSerial(32, name='DatabaseXML', typeName=ele['type']) - else: - pass - logging.getLogger(LOGGER).debug(msgin.showAllData()) - return True - head = ele - logging.getLogger(LOGGER).debug("Ahhhh 3", "-"*80) - return False - diff --git a/tools/Impulse.py b/tools/Impulse.py index 49e8978..0cd6ce8 100644 --- a/tools/Impulse.py +++ b/tools/Impulse.py @@ -664,6 +664,7 @@ class impulseDatabaseInitPlayer(ImpulseBase): # _ = self.readUint32(msgin, '%s_property' % id) for i in range(0, propertyCount): logging.getLogger(LOGGER).debug("read i:%d" % i) + logging.getLogger(LOGGER).debug("#" * 80) databaseXml.execute(msgin, world) #raise "TODO" if msgin.needRead() > 5: