diff options
author | Kyomotoi <1172294279@qq.com> | 2020-12-06 16:45:38 +0800 |
---|---|---|
committer | Kyomotoi <1172294279@qq.com> | 2020-12-06 16:45:38 +0800 |
commit | dc4fd407386522bca6bae22933ac1c6f17d1f16d (patch) | |
tree | 916c9c95be9e1719ab67296b40eb297b703e7f2e /ATRI/plugins/plugin_utils | |
parent | 50c41b629532eae599320517387b4b56f6d16ff6 (diff) | |
download | ATRI-dc4fd407386522bca6bae22933ac1c6f17d1f16d.tar.gz ATRI-dc4fd407386522bca6bae22933ac1c6f17d1f16d.tar.bz2 ATRI-dc4fd407386522bca6bae22933ac1c6f17d1f16d.zip |
[Update]
Diffstat (limited to 'ATRI/plugins/plugin_utils')
-rw-r--r-- | ATRI/plugins/plugin_utils/__init__.py | 42 | ||||
-rw-r--r-- | ATRI/plugins/plugin_utils/data_source.py | 143 |
2 files changed, 182 insertions, 3 deletions
diff --git a/ATRI/plugins/plugin_utils/__init__.py b/ATRI/plugins/plugin_utils/__init__.py index 11f6c8b..4181e30 100644 --- a/ATRI/plugins/plugin_utils/__init__.py +++ b/ATRI/plugins/plugin_utils/__init__.py @@ -20,7 +20,7 @@ from nonebot.typing import Bot, Event from ATRI.utils.utils_error import errorRepo from ATRI.utils.utils_rule import check_banlist, check_switch -from .data_source import Generate, Genshin, Roll +from .data_source import Generate, Genshin, Roll, RCNB plugin_name_0 = "one-key-adult" generateID = on_command("我要转大人,一天打25小时游戏", @@ -114,3 +114,43 @@ async def _(bot: Bot, event: Event, state: dict) -> None: else: await genshinInfo.finish('UID检查未通过,请确保此ID为9位数或者是否为国服ID~!') + + +rcnb = RCNB() + +rcnbEncode = on_command('RC一下', + aliases={'rc一下', '啊西一下', '阿西一下'}, + rule=check_banlist()) + + +@rcnbEncode.handle() +async def _(bot: Bot, event: Event, state: dict) -> None: + msg = str(event.message).strip() + + if msg: + state['msg'] = msg + + +@rcnbEncode.got('msg', prompt='请告诉咱需要RC一下的字符~!') +async def _(bot: Bot, event: Event, state: dict) -> None: + msg = state['msg'] + await rcnbEncode.finish(rcnb._encode(msg)) + + +rcnbDecode = on_command('一下RC', + aliases={'一下rc', '一下啊西', '一下阿西'}, + rule=check_banlist()) + + +@rcnbDecode.handle() +async def _(bot: Bot, event: Event, state: dict) -> None: + msg = str(event.message).strip() + + if msg: + state['msg'] = msg + + +@rcnbDecode.got('msg', prompt='请告诉咱需要一下RC的字符~!') +async def _(bot: Bot, event: Event, state: dict) -> None: + msg = state['msg'] + await rcnbDecode.finish(rcnb._decode(msg)) diff --git a/ATRI/plugins/plugin_utils/data_source.py b/ATRI/plugins/plugin_utils/data_source.py index da6c52c..116b977 100644 --- a/ATRI/plugins/plugin_utils/data_source.py +++ b/ATRI/plugins/plugin_utils/data_source.py @@ -18,9 +18,10 @@ import string import random import hashlib import requests +from math import floor from pathlib import Path from zipfile import PyZipFile -from typing import Tuple, Dict, List +from typing import Tuple, Dict, List, Union class Generate: @@ -191,4 +192,142 @@ class Roll: result = f"{par}=({proc})={result}" - return str(result)
\ No newline at end of file + return str(result) + + +class RCNB(): + """R C N B""" + cr = 'rRŔŕŖŗŘřƦȐȑȒȓɌɍ' + cc = 'cCĆćĈĉĊċČčƇƈÇȻȼ' + cn = 'nNŃńŅņŇňƝƞÑǸǹȠȵ' + cb = 'bBƀƁƃƄƅßÞþ' + + sr = len(cr) + sc = len(cc) + sn = len(cn) + sb = len(cb) + src = sr * sc + snb = sn * sb + scnb = sc * snb + + def _div(self, a: int, b: int) -> int: + return floor(a / b) + + def _encodeByte(self, i) -> Union[str, None]: + if i > 0xFF: + raise ValueError('ERROR! rc/nb overflow') + + if i > 0x7F: + i = i & 0x7F + return self.cn[self._div(i, self.sb) + int(self.cb[i % self.sb])] + + return self.cr[self._div(i, self.sc) + int(self.cc[i % self.sc])] + + def _encodeShort(self, i) -> str: + if i > 0xFFFF: + raise ValueError('ERROR! rcnb overflow') + + reverse = False + if i > 0x7FFF: + reverse = True + i = i & 0x7FFF + + char = [ + self._div(i, self.scnb), + self._div(i % self.scnb, self.snb), + self._div(i % self.snb, self.sb), i % self.sb + ] + char = [ + self.cr[char[0]], self.cc[char[1]], self.cn[char[2]], + self.cb[char[3]] + ] + + if reverse: + return char[2] + char[3] + char[0] + char[1] + + return ''.join(char) + + def _decodeByte(self, c) -> int: + nb = False + idx = [self.cr.index(c[0]), self.cc.index(c[1])] + if idx[0] < 0 or idx[1] < 0: + idx = [self.cn.index(c[0]), self.cb.index(c[1])] + nb = True + raise ValueError('ERROR! rc/nb overflow') + + result = idx[0] * self.sb + idx[1] if nb else idx[0] * self.sc + idx[1] + if result > 0x7F: + raise ValueError('ERROR! rc/nb overflow') + + return result | 0x80 if nb else 0 + + def _decodeShort(self, c) -> int: + reverse = c[0] not in self.cr + if not reverse: + idx = [ + self.cr.index(c[0]), + self.cc.index(c[1]), + self.cn.index(c[2]), + self.cb.index(c[3]) + ] + else: + idx = [ + self.cr.index(c[2]), + self.cc.index(c[3]), + self.cn.index(c[0]), + self.cb.index(c[1]) + ] + + if idx[0] < 0 or idx[1] < 0 or idx[2] < 0 or idx[3] < 0: + raise ValueError('ERROR! not rcnb') + + result = idx[0] * self.scnb + idx[1] * self.snb + idx[ + 2] * self.sb + idx[3] + if result > 0x7FFF: + raise ValueError('ERROR! rcnb overflow') + + result |= 0x8000 if reverse else 0 + return result + + def _encodeBytes(self, b) -> str: + result = [] + for i in range(0, (len(b) >> 1)): + result.append(self._encodeShort((b[i * 2] << 8 | b[i * 2 + 1]))) + + if len(b) & 1 == 1: + result.append(self._encodeByte(b[-1])) + + return ''.join(result) + + def _encode(self, s: str, encoding: str = 'utf-8'): + if not isinstance(s, str): + raise ValueError('Please enter str instead of other') + + return self._encodeBytes(s.encode(encoding)) + + def _decodeBytes(self, s: str): + if not isinstance(s, str): + raise ValueError('Please enter str instead of other') + + if len(s) & 1: + raise ValueError('ERROR length') + + result = [] + for i in range(0, (len(s) >> 2)): + result.append(bytes([self._decodeShort(s[i * 4:i * 4 + 4]) >> 8])) + result.append(bytes([self._decodeShort(s[i * 4:i * 4 + 4]) & 0xFF + ])) + + if (len(s) & 2) == 2: + result.append(bytes([self._decodeByte(s[-2:])])) + + return b''.join(result) + + def _decode(self, s: str, encoding: str = 'utf-8') -> str: + if not isinstance(s, str): + raise ValueError('Please enter str instead of other') + + try: + return self._decodeBytes(s).decode(encoding) + except UnicodeDecodeError: + raise ValueError('Decoding failed') |