Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abcSKY666 committed May 4, 2023
1 parent 59e4138 commit 1d22ef8
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 81 deletions.
32 changes: 8 additions & 24 deletions MCScript/Game.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from __future__ import annotations
import warnings
from abc import ABC
from typing import Any, Optional, TYPE_CHECKING

#import dns.resolver
from .decode import *
#import asyncio_dgram
from time import perf_counter
import socket
from .address import Address
from dns import resolver

# Locals
from .packets.decode import *
from .address import Address
from packets.id import packets

class Game:
request_status_data = b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x124Vx"
class Client:

def __init__(self, ip, port):
self.ip = ip
self.port = port
Expand All @@ -23,20 +19,8 @@ def __init__(self, ip, port):
self.client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.client.settimeout(self.timeout)

def get_status(self):
try:
start = perf_counter()

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(self.timeout)

s.sendto(self.request_status_data, self.addr)
data, _ = s.recvfrom(2048)

return decode(data, (perf_counter() - start))
except Exception as err:
print("[ERROR] Reason:"+err)



def send(self, data):
def writePackets(self, data):
self.client.sendto(data, self.addr)
58 changes: 58 additions & 0 deletions MCScript/ModuleImporter.py
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.
56 changes: 0 additions & 56 deletions MCScript/decode.py

This file was deleted.

Empty file added MCScript/foundation/result.py
Empty file.
58 changes: 58 additions & 0 deletions MCScript/packets/decode.py
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

9 changes: 9 additions & 0 deletions MCScript/packets/id.py
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": ""
}
}
35 changes: 35 additions & 0 deletions MCScript/server/ping.py
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</h1>
<h3 align="center">Minecraft Script Library | Python</h3>
<p align="center">
<strong><a href="CHANGELOG.md">Change Log</a></strong>
<strong><a href="https://www.">Change Log</a></strong>
<strong><a href="https://qm.qq.com/cgi-bin/qm/qr?k=RRgf17j1MSV2ajHHiVn_afaFsW7CMVIK&jump_from=webapi&authKey=W26MUk0kXqUfVOoxAd9BbkPk/GdXB1w7rf/DcjmzNOX3ID3gliMxiWPMRgYG/fXx">QQ</a></strong>
</p>

0 comments on commit 1d22ef8

Please sign in to comment.