mirror of
https://github.com/MikuLeaks/KianaBH3.git
synced 2025-12-13 21:34:43 +01:00
refactor: Refactor config
This commit is contained in:
16
utils/aes.py
Normal file
16
utils/aes.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import base64
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
|
||||
|
||||
def encrypt_ecb(key: str, data: str):
|
||||
cipher = AES.new(bytes.fromhex(key.replace(" ", "")), AES.MODE_ECB)
|
||||
encrypted = cipher.encrypt(pad(data.encode(), AES.block_size))
|
||||
return base64.b64encode(encrypted).decode()
|
||||
|
||||
|
||||
def decrypt_ecb(key: str, data: str):
|
||||
data = base64.b64decode(data)
|
||||
cipher = AES.new(bytes.fromhex(key.replace(" ", "")), AES.MODE_ECB)
|
||||
decrypted = cipher.decrypt(data)
|
||||
return unpad(decrypted, AES.block_size).decode()
|
||||
54
utils/config.py
Normal file
54
utils/config.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from dataclasses import asdict, dataclass
|
||||
import os
|
||||
import json
|
||||
from dacite import from_dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class ServerConfig:
|
||||
IP: str
|
||||
Port: int
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConfigData:
|
||||
LogLevel: str
|
||||
GameServer: ServerConfig
|
||||
SDKServer: ServerConfig
|
||||
VerboseLevel: int
|
||||
RegionName: str
|
||||
UseLocalCache: bool
|
||||
AESKeys: dict[str, str]
|
||||
EnableDispatchEncryption: bool
|
||||
|
||||
def write_default_config():
|
||||
config = ConfigData(
|
||||
LogLevel="INFO",
|
||||
GameServer=ServerConfig(IP="127.0.0.1", Port=16100),
|
||||
SDKServer=ServerConfig(IP="127.0.0.1", Port=80),
|
||||
VerboseLevel=1,
|
||||
RegionName="MikuBH3",
|
||||
UseLocalCache=False,
|
||||
EnableDispatchEncryption=True,
|
||||
AESKeys={
|
||||
"7.9.0_gf_pc": "36 31 65 37 64 33 65 66 33 32 30 63 31 35 66 66 61 64 37 61 66 32 31 34 61 64 65 64 32 34 33 38",
|
||||
"7.8.0_os_pc": "64 34 32 33 30 30 31 62 32 36 38 34 62 33 62 30 61 33 30 38 66 37 65 35 63 30 61 38 66 33 65 32",
|
||||
},
|
||||
)
|
||||
with open("Config.json", "w") as f:
|
||||
f.write(json.dumps(asdict(config), indent=2))
|
||||
|
||||
return config
|
||||
|
||||
def load():
|
||||
if not os.path.exists("Config.json"):
|
||||
return ConfigData.write_default_config()
|
||||
|
||||
with open("Config.json", "r", encoding="utf-8") as f:
|
||||
try:
|
||||
return from_dict(ConfigData, json.load(f))
|
||||
except Exception:
|
||||
return ConfigData.write_default_config()
|
||||
|
||||
|
||||
Config = ConfigData.load()
|
||||
39
utils/logger.py
Normal file
39
utils/logger.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import sys
|
||||
from loguru import logger
|
||||
from utils.config import Config
|
||||
|
||||
logger.remove()
|
||||
|
||||
LevelList = ["ERROR", "WARNING", "INFO", "DEBUG"]
|
||||
CodeColorDict = {"ERROR": "red", "WARNING": "yellow", "INFO": "green", "DEBUG": "blue"}
|
||||
|
||||
|
||||
def custom_format(record):
|
||||
color = CodeColorDict[record["level"].name]
|
||||
return f"<{color}>{record['level'].name}</{color}> : {record['message']}\n"
|
||||
|
||||
|
||||
logger.add(sys.stdout, format=custom_format, colorize=True, level=Config.LogLevel)
|
||||
|
||||
|
||||
def Log(msg, types):
|
||||
if types in CodeColorDict and LevelList.index(types) <= LevelList.index(
|
||||
Config.LogLevel
|
||||
):
|
||||
getattr(logger, types.lower())(msg)
|
||||
|
||||
|
||||
def Error(msg):
|
||||
Log(msg, "ERROR")
|
||||
|
||||
|
||||
def Warn(msg):
|
||||
Log(msg, "WARNING")
|
||||
|
||||
|
||||
def Info(msg):
|
||||
Log(msg, "INFO")
|
||||
|
||||
|
||||
def Debug(msg):
|
||||
Log(msg, "DEBUG")
|
||||
Reference in New Issue
Block a user