-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
MCScript | ModuleImporter | ||
参考: https://blog.csdn.net/weixin_44733774/article/details/126553664 | ||
主页: https://blog.csdn.net/weixin_44733774 | ||
""" | ||
|
||
|
||
|
||
|
||
|
||
import os | ||
import sys | ||
|
||
class Importer: | ||
def __init__(self, path): | ||
""" | ||
path请传入globals() | ||
""" | ||
|
||
def Import(self,module,args=None): | ||
''' | ||
module为模块名所在路径(不需要.py后缀),支持相对路径: | ||
导入上一级名为M的模块,那么module='../M' | ||
导入目录A下的名为M的模块,那么module='A/M' | ||
args为从module中导入的变量名或者变量名列表: | ||
如果args为空,那么仅导入模块module | ||
如果args不为空,那么将导入模块module中的变量 | ||
例子: | ||
Import('M'):导入模块M。【类似于import M】 | ||
Import('M','info'):导入模块M中名为info的变量。【类似于from M import info】 | ||
Import('M',('info','func')):导入模块M中名为info和func的变量。【类似于from M import info,func】 | ||
Import('M','*'):导入模块M中所有内容。【类似于from M import *】 | ||
Import('../M'):导入上级目录中的模块M | ||
Import('A/M'):导入A目录下的模块M | ||
注: | ||
①、请不要传入各种奇奇怪怪的值,我可没做好各种极端情况的预防工作 | ||
②、import失败必然会抛出异常,这没得洗的。请注意自己的代码规范 | ||
③、经常出现“鸡与蛋的先后问题”。请将该文件复制到要跨目录导入的脚本所在的目录下 | ||
''' | ||
absolutePath=os.path.split(self.__path['__file__'])[0]#调用该函数的文件所在的路径(绝对路径) | ||
relativePath,__module=os.path.split(module)#模块所在目录(相对路径)、模块名 | ||
path=os.path.join(absolutePath,relativePath)#模块所在路径(绝对路径) | ||
|
||
sys.path.append(path) | ||
context={} | ||
|
||
if(args): | ||
if(type(args)==str): | ||
exec(f'from {__module} import {args}',context) | ||
else: | ||
exec(f'from {__module} import {",".join(args)}',context) | ||
else: | ||
exec(f'import {__module}',context) | ||
self.__context.update(context) | ||
|
||
sys.path.pop() |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Optional | ||
import struct | ||
|
||
class PacketDecode: | ||
|
||
def decode(data: bytes, latency: float) -> "PacketDecode.GameData": | ||
data = data[1:] | ||
name_length = struct.unpack(">H", data[32:34])[0] | ||
decoded_data = data[34 : 34 + name_length].decode().split(";") | ||
|
||
try: | ||
map_ = decoded_data[7] | ||
except IndexError: | ||
map_ = None | ||
try: | ||
gamemode = decoded_data[8] | ||
except IndexError: | ||
gamemode = None | ||
|
||
return PacketDecode.Local( | ||
protocol=int(decoded_data[2]), | ||
brand=decoded_data[0], | ||
version=decoded_data[3], | ||
latency=latency, | ||
players_online=int(decoded_data[4]), | ||
players_max=int(decoded_data[5]), | ||
motd=decoded_data[1], | ||
map_=map_, | ||
gamemode=gamemode, | ||
) | ||
|
||
class Local: | ||
class Version: | ||
def __init__(self, protocol: int, brand: str, version: str): | ||
self.protocol = protocol | ||
self.brand = brand | ||
self.version = version | ||
|
||
def __init__( | ||
self, | ||
protocol: int, | ||
brand: str, | ||
version: str, | ||
latency: float, | ||
players_online: int, | ||
players_max: int, | ||
motd: str, | ||
map_: Optional[str], | ||
gamemode: Optional[str], | ||
): | ||
self.version = self.Version(protocol, brand, version) | ||
self.latency = latency | ||
self.players_online = players_online | ||
self.players_max = players_max | ||
self.motd = motd | ||
self.map = map_ | ||
self.gamemode = gamemode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
packets = { | ||
"server": { | ||
"request_status": b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x124Vx", | ||
}, | ||
|
||
"accounts": { | ||
"login": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
MCScript | PingServer | ||
v.1.0.0 | ||
""" | ||
|
||
|
||
import socket | ||
from time import perf_counter | ||
from ModuleImporter import Importer | ||
|
||
# 假导入 | ||
try: | ||
from ..packets.id import packets | ||
from ..packets.decode import PacketDecode | ||
except: | ||
pass | ||
|
||
mt = Importer(globals()) # Module Tools | ||
mt.Import("../packets/id", "packets") | ||
mt.Import("../packets/decode", "PacketDecode") | ||
|
||
def ping(self): | ||
try: | ||
start = perf_counter() | ||
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
s.settimeout(self.timeout) | ||
|
||
s.sendto(packets.server.request_status, self.addr) | ||
data, _ = s.recvfrom(2048) | ||
|
||
return PacketDecode.decode(data, (perf_counter() - start)) | ||
except Exception as err: | ||
return Result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters