Files
AUTO-MAS-test/app/config.py

249 lines
9.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# <AUTO_MAA:A MAA Multi Account Management and Automation Tool>
# Copyright © <2024> <DLmaster361>
# This file is part of AUTO_MAA.
# AUTO_MAA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
# AUTO_MAA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with AUTO_MAA. If not, see <https://www.gnu.org/licenses/>.
# DLmaster_361@163.com
"""
AUTO_MAA
AUTO_MAA配置管理
v4.2
作者DLmaster_361
"""
import sqlite3
import json
import os
import sys
from typing import Dict, Union
class AppConfig:
def __init__(self) -> None:
self.app_path = os.path.normpath(
os.path.dirname(os.path.realpath(sys.argv[0]))
) # 获取软件自身的路径
self.app_path_sys = os.path.realpath(sys.argv[0]) # 获取软件自身的路径
self.app_name = os.path.basename(self.app_path) # 获取软件自身的名称
self.database_path = os.path.normpath(f"{self.app_path}/data/data.db")
self.config_path = os.path.normpath(f"{self.app_path}/config/gui.json")
self.key_path = os.path.normpath(f"{self.app_path}/data/key")
self.gameid_path = os.path.normpath(f"{self.app_path}/data/gameid.txt")
self.version_path = os.path.normpath(f"{self.app_path}/resources/version.json")
# 检查文件完整性
self.initialize()
self.check_config()
self.check_database()
def initialize(self) -> None:
"""初始化程序的配置文件"""
# 检查目录
os.makedirs(os.path.normpath(f"{self.app_path}/data"), exist_ok=True)
os.makedirs(os.path.normpath(f"{self.app_path}/config"), exist_ok=True)
os.makedirs(
os.path.normpath(f"{self.app_path}/data/MAAconfig/simple"), exist_ok=True
)
os.makedirs(
os.path.normpath(f"{self.app_path}/data/MAAconfig/beta"), exist_ok=True
)
os.makedirs(
os.path.normpath(f"{self.app_path}/data/MAAconfig/Default"), exist_ok=True
)
# 生成版本信息文件
if not os.path.exists(self.version_path):
version = {
"main_version": "0.0.0.0",
"updater_version": "0.0.0.0",
}
with open(self.version_path, "w", encoding="utf-8") as f:
json.dump(version, f, indent=4)
# 生成配置文件
if not os.path.exists(self.config_path):
config = {"Default": {}}
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
# 生成预设gameid替换方案文件
if not os.path.exists(self.gameid_path):
with open(self.gameid_path, "w", encoding="utf-8") as f:
print(
"龙门币CE-6\n技能CA-5\n红票AP-5\n经验LS-6\n剿灭模式Annihilation",
file=f,
)
def check_config(self) -> None:
"""检查配置文件字段完整性并补全"""
config_list = [
["TimeSet.set1", "False"],
["TimeSet.run1", "00:00"],
["TimeSet.set2", "False"],
["TimeSet.run2", "00:00"],
["TimeSet.set3", "False"],
["TimeSet.run3", "00:00"],
["TimeSet.set4", "False"],
["TimeSet.run4", "00:00"],
["TimeSet.set5", "False"],
["TimeSet.run5", "00:00"],
["TimeSet.set6", "False"],
["TimeSet.run6", "00:00"],
["TimeSet.set7", "False"],
["TimeSet.run7", "00:00"],
["TimeSet.set8", "False"],
["TimeSet.run8", "00:00"],
["TimeSet.set9", "False"],
["TimeSet.run9", "00:00"],
["TimeSet.set10", "False"],
["TimeSet.run10", "00:00"],
["MaaSet.path", ""],
["TimeLimit.routine", 10],
["TimeLimit.annihilation", 40],
["TimesLimit.run", 3],
["SelfSet.IfSelfStart", "False"],
["SelfSet.IfSleep", "False"],
["SelfSet.IfProxyDirectly", "False"],
["SelfSet.IfSendMail", "False"],
["SelfSet.MailAddress", ""],
["SelfSet.IfSendMail.OnlyError", "False"],
["SelfSet.IfSilence", "False"],
["SelfSet.BossKey", ""],
["SelfSet.IfToTray", "False"],
["SelfSet.UIsize", "1200x700"],
["SelfSet.UIlocation", "100x100"],
["SelfSet.UImaximized", "False"],
["SelfSet.MainIndex", 2],
]
# 导入配置文件
with open(self.config_path, "r", encoding="utf-8") as f:
config = json.load(f)
# 检查并补充缺失的字段
for i in range(len(config_list)):
if not config_list[i][0] in config["Default"]:
config["Default"][config_list[i][0]] = config_list[i][1]
# 初始化配置信息
self.content: Dict[str, Dict[str, Union[str, int]]] = config
# 导出配置文件
self.save_config()
def check_database(self) -> None:
"""检查用户数据库文件并处理数据库版本更新"""
# 生成用户数据库
if not os.path.exists(self.database_path):
db = sqlite3.connect(self.database_path)
cur = db.cursor()
cur.execute(
"CREATE TABLE adminx(admin text,id text,server text,day int,status text,last date,game text,game_1 text,game_2 text,routine text,annihilation text,infrastructure text,password byte,notes text,numb int,mode text,uid int)"
)
cur.execute("CREATE TABLE version(v text)")
cur.execute("INSERT INTO version VALUES(?)", ("v1.3",))
db.commit()
cur.close()
db.close()
# 数据库版本更新
db = sqlite3.connect(self.database_path)
cur = db.cursor()
cur.execute("SELECT * FROM version WHERE True")
version = cur.fetchall()
# v1.0-->v1.1
if version[0][0] == "v1.0":
cur.execute("SELECT * FROM adminx WHERE True")
data = cur.fetchall()
cur.execute("DROP TABLE IF EXISTS adminx")
cur.execute(
"CREATE TABLE adminx(admin text,id text,server text,day int,status text,last date,game text,game_1 text,game_2 text,routines text,annihilation text,infrastructure text,password byte,notes text,numb int,mode text,uid int)"
)
for i in range(len(data)):
cur.execute(
"INSERT INTO adminx VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
(
data[i][0], # 0 0 0
data[i][1], # 1 1 -
"Official", # 2 2 -
data[i][2], # 3 3 1
data[i][3], # 4 4 2
data[i][4], # 5 5 3
data[i][5], # 6 6 -
data[i][6], # 7 7 -
data[i][7], # 8 8 -
"y", # 9 - 4
data[i][8], # 10 9 5
data[i][9], # 11 10 -
data[i][10], # 12 11 6
data[i][11], # 13 12 7
data[i][12], # 14 - -
"simple", # 15 - -
data[i][13], # 16 - -
),
)
cur.execute("DELETE FROM version WHERE v = ?", ("v1.0",))
cur.execute("INSERT INTO version VALUES(?)", ("v1.1",))
db.commit()
# v1.1-->v1.2
if version[0][0] == "v1.1":
cur.execute("SELECT * FROM adminx WHERE True")
data = cur.fetchall()
for i in range(len(data)):
cur.execute(
"UPDATE adminx SET infrastructure = 'n' WHERE mode = ? AND uid = ?",
(
data[i][15],
data[i][16],
),
)
cur.execute("DELETE FROM version WHERE v = ?", ("v1.1",))
cur.execute("INSERT INTO version VALUES(?)", ("v1.2",))
db.commit()
# v1.2-->v1.3
if version[0][0] == "v1.2":
cur.execute("ALTER TABLE adminx RENAME COLUMN routines TO routine")
cur.execute("DELETE FROM version WHERE v = ?", ("v1.2",))
cur.execute("INSERT INTO version VALUES(?)", ("v1.3",))
db.commit()
cur.close()
db.close()
def open_database(self) -> None:
"""打开数据库"""
self.db = sqlite3.connect(self.database_path)
self.cur = self.db.cursor()
def close_database(self) -> None:
"""关闭数据库"""
self.cur.close()
self.db.close()
def save_config(self) -> None:
"""保存配置文件"""
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(self.content, f, indent=4)