Commit d8d9cc7c authored by 苍蓝's avatar 苍蓝

update

parent b6925bd2
app.ico

16.6 KB

import os
import urllib.request
from datetime import datetime, timedelta
# 设置文件夹和文件路径
folder_path = "deck"
formal_file = "to-formal.txt"
pre_file = "to-pre.txt"
url_formal = "https://code.mycard.moe/coccvo/card-password-conversion/-/raw/master/to-formal.txt"
url_pre = "https://code.mycard.moe/coccvo/card-password-conversion/-/raw/master/to-pre.txt"
# 检查 deck 文件夹是否存在
if not os.path.exists(folder_path):
print("Error: 请把此文件放到游戏目录中再运行")
input("按回车键退出...")
exit(1)
# 定义一个函数来下载对照表
def download_file(url, filename):
try:
urllib.request.urlretrieve(url, filename)
except Exception as e:
print(f"Error: 下载对照表失败 {e}")
input("按回车键退出...")
exit(1)
# 检查对照表文件是否存在,以及是否需要更新
def check_and_update_file(filename, url):
if not os.path.exists(filename):
print(f"{filename} 不存在,开始下载...")
download_file(url, filename)
else:
file_mod_time = datetime.fromtimestamp(os.path.getmtime(filename))
current_time = datetime.now()
if (current_time - file_mod_time) > timedelta(days=1):
print(f"更新 {filename}...")
download_file(url, filename)
# 更新对照表文件
check_and_update_file(formal_file, url_formal)
check_and_update_file(pre_file, url_pre)
# 读取对照表并构建替换规则字典
replacements = {}
with open(formal_file, 'r', encoding='utf-8') as file:
for line in file:
parts = line.strip().split('\t')
if len(parts) == 2:
replacements[parts[0]] = parts[1]
# 逆向读取第二个对照表
with open(pre_file, 'r', encoding='utf-8') as file:
for line in file:
parts = line.strip().split('\t')
if len(parts) == 2:
replacements[parts[1]] = parts[0]
# 遍历deck文件夹中的所有.ydk文件
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".ydk"):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
original_content = f.read()
# 替换文本
new_content = original_content
for old, new in replacements.items():
new_content = new_content.replace(old, new)
# 仅当内容发生变化时才写回文件
if new_content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"修改并写入卡组: {file_path}")
else:
print(f"卡组无需修改: {file_path}")
print("卡组更新完成。")
input("按回车键退出...")
89604813 100224032 100224032 89604813
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment