From 1d54232415deba403255e65c0a077e5e19d95078 Mon Sep 17 00:00:00 2001 From: Doiiars Date: Sun, 4 Feb 2024 23:43:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复bug 增加日志管理 --- cddagl/VERSION | 2 +- cddagl/launcher.py | 39 +++++++++++++ cddagl/ui/views/fonts.py | 115 ++++++++++++++++++++++++++++--------- cddagl/ui/views/main.py | 1 + cddagl/ui/views/tabbed.py | 16 +++--- userdata/config/fonts.json | 14 +++++ 6 files changed, 152 insertions(+), 35 deletions(-) create mode 100644 userdata/config/fonts.json diff --git a/cddagl/VERSION b/cddagl/VERSION index 0bfbd57..fe4e75f 100644 --- a/cddagl/VERSION +++ b/cddagl/VERSION @@ -1 +1 @@ -1.8.2 \ No newline at end of file +1.8.3 \ No newline at end of file diff --git a/cddagl/launcher.py b/cddagl/launcher.py index de7127b..acff6f0 100644 --- a/cddagl/launcher.py +++ b/cddagl/launcher.py @@ -36,7 +36,46 @@ from cddagl.ui.views.tabbed import TabbedWindow from cddagl.win32 import get_ui_locale, SingleInstance, write_named_pipe +import logging +import os +import time +from glob import glob + +# 创建日志器 logger = logging.getLogger('cddagl') +logger.setLevel(logging.DEBUG) # 设置日志级别 + +# 确保 /log 文件夹存在 +log_dir = os.path.join(os.getcwd(),'log') +if not os.path.exists(log_dir): + os.makedirs(log_dir) + +# 删除旧的日志文件,如果超过三个 +log_files = sorted(glob(os.path.join(log_dir, '*.log'))) +while len(log_files) > 3: + os.remove(log_files.pop(0)) + +# 使用当前时间创建日志文件名 +log_filename = time.strftime("%Y%m%d%H%M%S") + '.log' +log_filepath = os.path.join(log_dir, log_filename) + +# 创建文件处理器 +file_handler = logging.FileHandler(log_filepath, encoding='utf-8') +file_handler.setLevel(logging.DEBUG) # 设置文件日志级别 + +# 创建控制台处理器 +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.DEBUG) # 设置控制台日志级别 + +# 创建日志格式 +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +file_handler.setFormatter(formatter) +console_handler.setFormatter(formatter) + +# 添加处理器到日志器 +logger.addHandler(file_handler) +logger.addHandler(console_handler) + def init_single_instance(): diff --git a/cddagl/ui/views/fonts.py b/cddagl/ui/views/fonts.py index f26a424..ed626af 100644 --- a/cddagl/ui/views/fonts.py +++ b/cddagl/ui/views/fonts.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: MIT import logging import os +import json from PyQt5.QtCore import Qt, QStringListModel from PyQt5.QtGui import QFontDatabase, QFont @@ -132,20 +133,20 @@ def __init__(self): self.load_font_settings() def load_font_settings(self): - # 从fontjson中加载字体设置 - self.fontjson = { - "typeface": ["data/font/Terminus.ttf", "data/font/unifont.ttf"], - "map_typeface": ["data/font/Terminus.ttf", "data/font/unifont.ttf"], - "over_map_typeface": ["data/font/Terminus.ttf", "data/font/unifont.ttf"] + # 从fontsjson中加载字体设置 + self.fontsjson = { + "typeface": ["userdata/font/Terminus.ttf", "userdata/font/unifont.ttf"], + "map_typeface": ["userdata/font/Terminus.ttf", "userdata/font/unifont.ttf"], + "over_map_typeface": ["userdata/font/Terminus.ttf", "userdata/font/unifont.ttf"] } # 更新current_font_text_edit的文本 self.update_current_font_text_edit() def update_current_font_text_edit(self): - typeface = self.fontjson.get("typeface", []) - map_typeface = self.fontjson.get("map_typeface", []) - over_map_typeface = self.fontjson.get("over_map_typeface", []) + typeface = self.fontsjson.get("typeface", []) + map_typeface = self.fontsjson.get("map_typeface", []) + over_map_typeface = self.fontsjson.get("over_map_typeface", []) current_font_text = f"""UI: {typeface[0]} => {typeface[1] if len(typeface) > 1 else ''} 地图: {map_typeface[0]} => {map_typeface[1] if len(map_typeface) > 1 else ''} @@ -163,22 +164,28 @@ def set_ui_font(self): logger.debug("触发设为UI字体事件") print(self.game_dir) if self.selected_font is not None: - # 更新fontjson的UI字体部分 - self.fontjson["typeface"][0] = self.selected_font + # 更新fontsjson的UI字体部分 + self.fontsjson["typeface"][0] = self.selected_font + write_fontsjson_to_path(self.fontsjson) + copy_font_to_directory_by_name(self.selected_font) self.update_current_font_text_edit() def set_map_font(self): logger.debug("触发设为地图字体事件") if self.selected_font is not None: - # 更新fontjson的地图字体部分 - self.fontjson["map_typeface"][0] = self.selected_font + # 更新fontsjson的地图字体部分 + self.fontsjson["map_typeface"][0] = self.selected_font + write_fontsjson_to_path(self.fontsjson) + copy_font_to_directory_by_name(self.selected_font) self.update_current_font_text_edit() def set_large_map_font(self): logger.debug("触发设为大地图字体事件") if self.selected_font is not None: - # 更新fontjson的大地图字体部分 - self.fontjson["over_map_typeface"][0] = self.selected_font + # 更新fontsjson的大地图字体部分 + self.fontsjson["over_map_typeface"][0] = self.selected_font + write_fontsjson_to_path(self.fontsjson) + copy_font_to_directory_by_name(self.selected_font) self.update_current_font_text_edit() def set_all_font(self): @@ -187,10 +194,11 @@ def set_all_font(self): selected_font = self.selected_font # 更新字体设置字典中的所有字体 - self.fontjson["typeface"] = [selected_font, selected_font] - self.fontjson["map_typeface"] = [selected_font, selected_font] - self.fontjson["over_map_typeface"] = [selected_font, selected_font] - + self.fontsjson["typeface"] = [selected_font, selected_font] + self.fontsjson["map_typeface"] = [selected_font, selected_font] + self.fontsjson["over_map_typeface"] = [selected_font, selected_font] + write_fontsjson_to_path(self.fontsjson) + copy_font_to_directory_by_name(self.selected_font) # 更新UI界面 self.update_current_font_text_edit() @@ -236,14 +244,69 @@ def get_main_window(self): def get_main_tab(self): return self.parentWidget().parentWidget().main_tab - - - - - - - - + + + + +def write_fontsjson_to_path(data, subpath='userdata\config'): + try: + # 检查路径是否存在,如果不存在则创建它 + if not os.path.exists(subpath): + os.makedirs(subpath) + + # 构建JSON文件的完整路径 + json_path = os.path.join(os.getcwd(), subpath, 'fonts.json') + + # 写入JSON数据到文件 + with open(json_path, 'w') as json_file: + json.dump(data, json_file, indent=4) + + print(f"JSON数据已成功写入到路径: {json_path}") + except Exception as e: + print(f"写入JSON数据时出错: {str(e)}") + +def copy_font_to_directory_by_name(font_name, subpath='data/fonts', font_size=14): + target_directory = os.path.join(os.getcwd(), subpath) + + # 使用 QFontDatabase 来获取字体文件路径 + font_database = QFontDatabase() + font_id = font_database.addApplicationFont(QFont(font_name, font_size).toString()) + + if font_id == -1: + print(f"无法找到字体文件: {font_name}") + return + + font_file_paths = font_database.applicationFontPaths(font_id) + font_file_name = os.path.basename(font_file_paths[0]) + + # 检查目标目录是否存在字体文件 + target_font_path = os.path.join(target_directory, font_file_name) + if os.path.exists(target_font_path): + print(f"字体 '{font_name}' 已存在于目标目录中.") + return + + try: + # 复制字体文件到目标目录 + shutil.copy(font_file_paths[0], target_directory) + print(f"字体 '{font_name}' 已成功复制到目标目录.") + except Exception as e: + print(f"复制字体 '{font_name}' 到目标目录时发生错误: {str(e)}") + +def get_windows_fonts_directory(): + # 获取系统文件夹路径 + windir = os.environ.get('WINDIR', 'C:\\Windows') + + # 检查系统位数(32位或64位) + is_64bit = ctypes.windll.kernel32.IsWow64Process() + + if is_64bit: + # 64位系统 + fonts_directory = os.path.join(windir, 'SysNative', 'fonts') + else: + # 32位系统 + fonts_directory = os.path.join(windir, 'fonts') + + return fonts_directory diff --git a/cddagl/ui/views/main.py b/cddagl/ui/views/main.py index 5aea8e5..b24c276 100644 --- a/cddagl/ui/views/main.py +++ b/cddagl/ui/views/main.py @@ -430,6 +430,7 @@ def launch_game_process(self): try: game_process = subprocess.Popen(cmd, cwd=exe_dir, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) + logger.debug(f"cmd:{cmd}") except OSError as e: main_window = self.get_main_window() status_bar = main_window.statusBar() diff --git a/cddagl/ui/views/tabbed.py b/cddagl/ui/views/tabbed.py index b53bf93..88650f8 100644 --- a/cddagl/ui/views/tabbed.py +++ b/cddagl/ui/views/tabbed.py @@ -32,7 +32,7 @@ from cddagl.sql.functions import get_config_value, set_config_value, config_true from cddagl.ui.views.backups import BackupsTab from cddagl.ui.views.dialogs import AboutDialog, FaqDialog -from cddagl.ui.views.fonts import FontsTab +# from cddagl.ui.views.fonts import FontsTab from cddagl.ui.views.main import MainTab from cddagl.ui.views.mods import ModsTab from cddagl.ui.views.settings import SettingsTab @@ -429,7 +429,7 @@ def __init__(self): self.create_mods_tab() #self.create_tilesets_tab() self.create_soundpacks_tab() - self.create_fonts_tab() + # self.create_fonts_tab() self.create_settings_tab() self.create_statistics_tab() self.create_cddaguide_tab() @@ -440,7 +440,7 @@ def set_text(self): self.setTabText(self.indexOf(self.mods_tab), _('Mods')) #self.setTabText(self.indexOf(self.tilesets_tab), _('Tilesets')) self.setTabText(self.indexOf(self.soundpacks_tab), _('Soundpacks')) - self.setTabText(self.indexOf(self.fonts_tab), _('Fonts')) + # self.setTabText(self.indexOf(self.fonts_tab), _('Fonts')) self.setTabText(self.indexOf(self.settings_tab), _('Settings')) self.setTabText(self.indexOf(self.statistics_tab), _('Statistics')) self.setTabText(self.indexOf(self.cddaguide_tab), _('CDDA Guide')) @@ -450,7 +450,7 @@ def set_text(self): self.mods_tab.set_text() #self.tilesets_tab.set_text() self.soundpacks_tab.set_text() - self.fonts_tab.set_text() + # self.fonts_tab.set_text() self.settings_tab.set_text() self.statistics_tab.set_text() self.cddaguide_tab.set_text() @@ -480,10 +480,10 @@ def create_soundpacks_tab(self): self.addTab(soundpacks_tab, _('Soundpacks')) self.soundpacks_tab = soundpacks_tab - def create_fonts_tab(self): - fonts_tab = FontsTab() - self.addTab(fonts_tab, _('Fonts')) - self.fonts_tab = fonts_tab + # def create_fonts_tab(self): + # fonts_tab = FontsTab() + # self.addTab(fonts_tab, _('Fonts')) + # self.fonts_tab = fonts_tab def create_settings_tab(self): settings_tab = SettingsTab() diff --git a/userdata/config/fonts.json b/userdata/config/fonts.json new file mode 100644 index 0000000..1f5da0e --- /dev/null +++ b/userdata/config/fonts.json @@ -0,0 +1,14 @@ +{ + "typeface": [ + "Adobe Fan Heiti Std", + "userdata/font/unifont.ttf" + ], + "map_typeface": [ + "userdata/font/Terminus.ttf", + "userdata/font/unifont.ttf" + ], + "over_map_typeface": [ + "userdata/font/Terminus.ttf", + "userdata/font/unifont.ttf" + ] +} \ No newline at end of file