Skip to content

Commit

Permalink
#8 更新安装器支持手柄
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorReid committed Jul 28, 2024
1 parent 4cf5fea commit c0b7c27
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 11 deletions.
2 changes: 1 addition & 1 deletion installer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


a = Analysis(
['src\\one_dragon\\gui\\app\\installer.py'],
['src\\zzz_od\\gui\\zzz_installer.py'],
pathex=[],
binaries=[],
datas=[],
Expand Down
15 changes: 15 additions & 0 deletions src/one_dragon/envs/python_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,18 @@ def install_requirements(self, progress_callback: Optional[Callable[[float, str]
success = result is not None
msg = '运行依赖安装成功' if success else '运行依赖安装失败'
return success, msg

def get_module_version(self) -> Optional[str]:
"""
:return: 当前使用的pip版本
"""
log.info('检测当前pip版本')
python_path = self.env_config.python_path
if python_path == '' or not os.path.exists(python_path):
return None

version = cmd_utils.run_command([python_path, '-m', 'pip', '--version']) # Ex: pip 24.0 from xxxx
if version is not None:
return version[4: version.find('from') - 1]
else:
return None
2 changes: 1 addition & 1 deletion src/one_dragon/gui/component/log_display_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LogDisplayCard(PlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent=parent)
_ = QVBoxLayout(self) # 创建内部的 QVBoxLayout 以允许高度自动扩展
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
# self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)

self.update_on_log: bool = False # 在接收到log的时候更新

Expand Down
23 changes: 14 additions & 9 deletions src/one_dragon/gui/view/install_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
class InstallerInterface(VerticalScrollInterface):

def __init__(self, ctx: OneDragonContext, parent=None):
VerticalScrollInterface.__init__(self, ctx=ctx, object_name='install_interface',
parent=parent, content_widget=None,
nav_text_cn='一键安装', nav_icon=FluentIcon.CLOUD_DOWNLOAD)

def get_content_widget(self) -> QWidget:
content_widget = QWidget()
v_layout = QVBoxLayout(content_widget)

Expand All @@ -28,20 +33,20 @@ def __init__(self, ctx: OneDragonContext, parent=None):
self.progress_bar_2.setVisible(False)
v_layout.addWidget(self.progress_bar_2)

self.git_opt = GitInstallCard(ctx)
self.git_opt = GitInstallCard(self.ctx)
self.git_opt.progress_changed.connect(self.update_progress)

self.code_opt = CodeInstallCard(ctx)
self.code_opt = CodeInstallCard(self.ctx)
self.code_opt.progress_changed.connect(self.update_progress)
self.code_opt.finished.connect(self._on_code_updated)

self.python_opt = PythonInstallCard(ctx)
self.python_opt = PythonInstallCard(self.ctx)
self.python_opt.progress_changed.connect(self.update_progress)

self.venv_opt = VenvInstallCard(ctx)
self.venv_opt = VenvInstallCard(self.ctx)
self.venv_opt.progress_changed.connect(self.update_progress)

self.all_opt = AllInstallCard(ctx, [self.git_opt, self.code_opt, self.python_opt, self.venv_opt])
self.all_opt = AllInstallCard(self.ctx, [self.git_opt, self.code_opt, self.python_opt, self.venv_opt])

update_group = SettingCardGroup(gt('运行环境', 'ui'))
update_group.addSettingCard(self.all_opt)
Expand All @@ -53,19 +58,18 @@ def __init__(self, ctx: OneDragonContext, parent=None):
v_layout.addWidget(update_group)

log_group = SettingCardGroup(gt('安装日志', 'ui'))
self.log_card = LogDisplayCard(max_height=200)
self.log_card = LogDisplayCard()
log_group.addSettingCard(self.log_card)
v_layout.addWidget(log_group)

VerticalScrollInterface.__init__(self, ctx=ctx, object_name='install_interface',
parent=parent, content_widget=content_widget,
nav_text_cn='一键安装', nav_icon=FluentIcon.CLOUD_DOWNLOAD)
return content_widget

def on_interface_shown(self) -> None:
"""
页面加载完成后 检测各个组件状态并更新显示
:return:
"""
VerticalScrollInterface.on_interface_shown(self)
self.git_opt.check_and_update_display()
self.code_opt.check_and_update_display()
self.python_opt.check_and_update_display()
Expand All @@ -77,6 +81,7 @@ def on_interface_hidden(self) -> None:
子界面隐藏时的回调
:return:
"""
VerticalScrollInterface.on_interface_hidden(self)
self.log_card.update_on_log = False

def update_progress(self, progress: float, message: str) -> None:
Expand Down
17 changes: 17 additions & 0 deletions src/zzz_od/config/game_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,20 @@ def ds4_key_interact(self) -> str:
@ds4_key_interact.setter
def ds4_key_interact(self, new_value: str) -> None:
self.update('ds4_key_interact', new_value)


@property
def gamepad_requirement_time(self) -> str:
"""
安装依赖时 使用的 requirement-gamepad.txt 的最后修改时间
:return:
"""
return self.get('gamepad_requirement_time', '')

@gamepad_requirement_time.setter
def gamepad_requirement_time(self, new_value: str) -> None:
"""
安装依赖时 使用的 requirement-gamepad.txt 的最后修改时间
:return:
"""
self.update('gamepad_requirement_time', new_value)
8 changes: 8 additions & 0 deletions src/zzz_od/context/battle_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, ctx: ZContext):
self._check_special_attack_lock = threading.Lock()
self._check_ultimate_lock = threading.Lock()
self._check_chain_lock = threading.Lock()
self._check_fast_lock = threading.Lock()

def dodge(self):
e = BattleEventEnum.BTN_DODGE.value
Expand Down Expand Up @@ -126,6 +127,7 @@ def init_context(self, agent_names: Optional[List[str]] = None) -> None:

self.area_btn_special: ScreenArea = self.ctx.screen_loader.get_area('战斗画面', '按键-特殊攻击')
self.area_btn_ultimate: ScreenArea = self.ctx.screen_loader.get_area('战斗画面', '按键-终结技')
self.area_btn_switch: ScreenArea = self.ctx.screen_loader.get_area('战斗画面', '按键-切换角色')

self.area_chain_1: ScreenArea = self.ctx.screen_loader.get_area('战斗画面', '连携技-1')
self.area_chain_2: ScreenArea = self.ctx.screen_loader.get_area('战斗画面', '连携技-2')
Expand Down Expand Up @@ -398,6 +400,12 @@ def _match_chain_agent_in(self, img: MatLike, possible_agents: Optional[List[Age

return None

def check_fast_support(self, screen: MatLike, screenshot_time: float) -> None:
"""
识别快速支援
"""
pass



def __debug():
Expand Down
Empty file.
80 changes: 80 additions & 0 deletions src/zzz_od/gui/view/installer/extend_install_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from PySide6.QtWidgets import QWidget, QVBoxLayout
from qfluentwidgets import ProgressBar, IndeterminateProgressBar, SettingCardGroup, \
FluentIcon

from one_dragon.gui.component.interface.vertical_scroll_interface import VerticalScrollInterface
from one_dragon.gui.component.log_display_card import LogDisplayCard
from one_dragon.utils.i18_utils import gt
from zzz_od.context.zzz_context import ZContext
from zzz_od.gui.view.installer.gamepad_install_card import GamepadInstallCard


class ExtendInstallInterface(VerticalScrollInterface):

def __init__(self, ctx: ZContext, parent=None):
self.ctx: ZContext = ctx
VerticalScrollInterface.__init__(self, ctx=ctx, object_name='extend_install_interface',
parent=parent, content_widget=None,
nav_text_cn='扩展安装', nav_icon=FluentIcon.DEVELOPER_TOOLS)

def get_content_widget(self) -> QWidget:
content_widget = QWidget()
v_layout = QVBoxLayout(content_widget)

self.progress_bar = ProgressBar()
self.progress_bar.setRange(0, 1)
self.progress_bar.setVisible(False)
v_layout.addWidget(self.progress_bar)

self.progress_bar_2 = IndeterminateProgressBar()
self.progress_bar_2.setVisible(False)
v_layout.addWidget(self.progress_bar_2)

self.gamepad_opt = GamepadInstallCard(self.ctx)

update_group = SettingCardGroup(gt('运行环境', 'ui'))
update_group.addSettingCard(self.gamepad_opt)

v_layout.addWidget(update_group)

log_group = SettingCardGroup(gt('安装日志', 'ui'))
self.log_card = LogDisplayCard()
log_group.addSettingCard(self.log_card)
v_layout.addWidget(log_group)

return content_widget

def on_interface_shown(self) -> None:
"""
页面加载完成后 检测各个组件状态并更新显示
:return:
"""
VerticalScrollInterface.on_interface_shown(self)
self.gamepad_opt.check_and_update_display()
self.log_card.update_on_log = True

def on_interface_hidden(self) -> None:
"""
子界面隐藏时的回调
:return:
"""
VerticalScrollInterface.on_interface_hidden(self)
self.log_card.update_on_log = False

def update_progress(self, progress: float, message: str) -> None:
"""
进度回调更新
:param progress: 进度 0~1
:param message: 当前信息
:return:
"""
if progress == -1:
self.progress_bar.setVisible(False)
self.progress_bar_2.setVisible(True)
self.progress_bar_2.start()
else:
self.progress_bar.setVisible(True)
self.progress_bar.setVal(progress)
self.progress_bar_2.setVisible(False)
self.progress_bar_2.stop()

78 changes: 78 additions & 0 deletions src/zzz_od/gui/view/installer/gamepad_install_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
from PySide6.QtGui import QIcon
from qfluentwidgets import FluentIcon, FluentThemeColor
from typing import Tuple, Optional, Callable

from one_dragon.gui.install_card.base_install_card import BaseInstallCard
from one_dragon.utils import cmd_utils, os_utils
from one_dragon.utils.i18_utils import gt
from one_dragon.utils.log_utils import log
from zzz_od.context.zzz_context import ZContext


class GamepadInstallCard(BaseInstallCard):

def __init__(self, ctx: ZContext, parent=None):
self.ctx: ZContext = ctx
BaseInstallCard.__init__(
self,
ctx=ctx,
title_cn='虚拟手柄',
install_method=self.install_requirements,
parent=parent
)

def after_progress_done(self, success: bool, msg: str) -> None:
"""
安装结束的回调,由子类自行实现
:param success: 是否成功
:param msg: 提示信息
:return:
"""
if success:
self.ctx.env_config.update('vgamepad_requirement', self.get_requirement_time())
self.check_and_update_display()
else:
self.update_display(FluentIcon.INFO.icon(color=FluentThemeColor.RED.value), gt(msg, 'ui'))

def get_display_content(self) -> Tuple[QIcon, str]:
"""
获取需要显示的状态,由子类自行实现
:return: 显示的图标、文本
"""
last = self.ctx.env_config.get('vgamepad_requirement', '')

if last != self.get_requirement_time():
icon = FluentIcon.INFO.icon(color=FluentThemeColor.GOLD.value)
msg = gt('需更新,请使用安装器更新', 'ui')
else:
icon = FluentIcon.INFO.icon(color=FluentThemeColor.DEFAULT_BLUE.value)
msg = f"{gt('已安装', 'ui')}" + ' ' + last

return icon, msg

def get_requirement_time(self) -> Optional[str]:
"""
获取 requirements.txt 的最后更新时间
:return:
"""
log.info('获取依赖文件的最后修改时间')
return cmd_utils.run_command([self.ctx.env_config.git_path, 'log', '-1', '--pretty=format:"%ai', '--', self.get_requirement_path()])

def install_requirements(self, progress_callback: Optional[Callable[[float, str], None]]) -> Tuple[bool, str]:
"""
安装依赖
:return:
"""
progress_callback(-1, '正在安装...安装过程可能需要安装驱动 正常安装即可')
result = cmd_utils.run_command([self.ctx.env_config.python_path, '-m', 'pip', 'install', '--upgrade', '-r',
self.get_requirement_path()])
success = result is not None
msg = '运行依赖安装成功' if success else '运行依赖安装失败'
return success, msg

def get_requirement_path(self) -> str:
return os.path.join(
os_utils.get_work_dir(),
self.ctx.project_config.get('vgamepad_requirements', 'requirements-gamepad.txt')
)
37 changes: 37 additions & 0 deletions src/zzz_od/gui/zzz_installer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys

from PySide6.QtWidgets import QApplication
from qfluentwidgets import NavigationItemPosition, Theme, setTheme

from one_dragon.gui.app.fluent_window_base import FluentWindowBase
from one_dragon.gui.view.install_interface import InstallerInterface
from one_dragon.gui.view.installer_setting_interface import InstallerSettingInterface
from zzz_od.context.zzz_context import ZContext
from zzz_od.gui.view.installer.extend_install_interface import ExtendInstallInterface


class ZInstallerWindow(FluentWindowBase):

def __init__(self, ctx: ZContext, win_title: str, parent=None):
self.ctx: ZContext = ctx
FluentWindowBase.__init__(
self,
ctx=ctx,
win_title=win_title,
parent=parent,
app_icon='zzz_logo.ico'
)

def create_sub_interface(self):
self.add_sub_interface(InstallerInterface(self.ctx, parent=self))
self.add_sub_interface(ExtendInstallInterface(self.ctx, parent=self))
self.add_sub_interface(InstallerSettingInterface(self.ctx, parent=self), position=NavigationItemPosition.BOTTOM)


if __name__ == '__main__':
app = QApplication(sys.argv)
_ctx = ZContext()
setTheme(Theme[_ctx.env_config.theme.upper()])
w = ZInstallerWindow(_ctx, f'{_ctx.project_config.project_name}-installer')
w.show()
app.exec()

0 comments on commit c0b7c27

Please sign in to comment.