remote.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:PyPlanet 作者: PyPlanet 项目源码 文件源码
def handle_scripted(self, handle_nr, method, data):
        # Unpack first.
        method, raw = data

        # Check if we only have one response array length, mostly this is the case due to the gbx handling.
        # Only when we don't get any response we don't have this!
        if len(raw) == 1:
            raw = raw[0]

        # Show warning when using non-supported modes.
        try:
            if 'LibXmlRpc' in method:
                logger.warning('You are using an older gamemode script that isn\'t supported by PyPlanet (usage of LibXmlRpc_)')
        except:
            pass

        # Try to parse JSON, mostly the case.
        try:
            if isinstance(raw, list):
                payload = dict()
                for idx, part in enumerate(raw):
                    try:
                        payload.update(json.loads(part))
                    except:
                        payload['raw_{}'.format(idx)] = part
            else:
                payload = json.loads(raw)
        except Exception as e:
            payload = raw

        # Check if payload contains a responseid, when it does, we call the scripted handler future object.
        if isinstance(payload, dict) and 'responseid' in payload and len(payload['responseid']) > 0:
            response_id = payload['responseid']

            if response_id in self.script_handlers:
                logger.debug('GBX: Received scripted response to method: {} and responseid: {}'.format(method, response_id))
                handler = self.script_handlers.pop(response_id)
                handler.set_result(payload)
                handler.done()
                return
            else:
                # We don't have this handler registered, throw warning in console.
                logger.warning('GBX: Received scripted response with responseid, but no hander was registered! Payload: {}'.format(payload))
                return

        # If not, we should just throw it as an ordinary callback.
        logger.debug('GBX: Received scripted callback: {}: {}'.format(method, payload))

        signal = SignalManager.get_callback('Script.{}'.format(method))
        if signal:
            await signal.send_robust(payload)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号