Skip to content

Commit

Permalink
#424 安装器加入自动更新免费代理;启动器加入检测代码更新;
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorReid committed Jan 27, 2025
1 parent dc1b102 commit 26a558a
Show file tree
Hide file tree
Showing 21 changed files with 336 additions and 103 deletions.
4 changes: 3 additions & 1 deletion src/one_dragon/base/conditional_operation/operation_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self,

# 等待秒数
seconds: float = 0,
seconds_add: float = 0,

# 状态
state: Optional[str] = None,
Expand All @@ -44,7 +45,8 @@ def __init__(self,

self.state_name: str = state # 状态名称
self.state_name_list: List[str] = state_list # 状态名称列表 清除状态时候可用
self.state_seconds: float = seconds # 状态处罚时间偏移量
self.state_seconds: float = seconds # 状态触发时间
self.state_seconds_add: float = seconds_add # 状态触发偏移量
self.state_value: int = value # 设置的状态值
self.state_value_add: int = add # 设置的状态值偏移量 state_value存在时不生效

Expand Down
13 changes: 11 additions & 2 deletions src/one_dragon/base/conditional_operation/state_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ class StateRecord:

def __init__(self, state_name: str,
trigger_time: float = 0,
value: Optional[int] = None, value_to_add: Optional[int] = None,
value: Optional[int] = None,
value_to_add: Optional[int] = None,
trigger_time_add: Optional[float] = None,
is_clear: bool = False,):
"""
单次的状态记录
"""
self.state_name: str = state_name
self.is_clear: bool = is_clear # 是否清除状态
self.trigger_time: float = trigger_time
self.trigger_time_add: float = trigger_time_add #时间修改
self.value: int = value
self.value_add: int = value_to_add

Expand All @@ -32,7 +35,13 @@ def update_state_record(self, record: StateRecord) -> None:
:param record:
:return:
"""
self.last_record_time = record.trigger_time
#不需要增减则照常处理
if record.trigger_time_add is None or record.trigger_time_add == 0:
self.last_record_time = record.trigger_time
else:
if self.last_record_time != -1: #如果是不存在的状态则不做任何处理
self.last_record_time -= record.trigger_time_add

if self.last_value is None:
self.last_value = 0

Expand Down
17 changes: 9 additions & 8 deletions src/one_dragon/base/controller/controller_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ControllerBase:

def __init__(self,
screenshot_alive_seconds: float = 5,
max_screenshot_cnt: int = 10):
max_screenshot_cnt: int = 0):
"""
基础控制器的定义
"""
Expand Down Expand Up @@ -59,13 +59,14 @@ def screenshot(self, independent: bool = False) -> MatLike:
screen = self.get_screenshot(independent)
fix_screen = self.fill_uid_black(screen)

self.screenshot_history.append(ScreenshotWithTime(fix_screen, now))
while len(self.screenshot_history) > self.max_screenshot_cnt:
self.screenshot_history.pop(0)
if self.max_screenshot_cnt > 0:
self.screenshot_history.append(ScreenshotWithTime(fix_screen, now))
while len(self.screenshot_history) > self.max_screenshot_cnt:
self.screenshot_history.pop(0)

while (len(self.screenshot_history) > 0
and now - self.screenshot_history[0].create_time > self.screenshot_alive_seconds):
self.screenshot_history.pop(0)
while (len(self.screenshot_history) > 0
and now - self.screenshot_history[0].create_time > self.screenshot_alive_seconds):
self.screenshot_history.pop(0)

return fix_screen

Expand All @@ -87,7 +88,7 @@ def fill_uid_black(self, screen: MatLike) -> MatLike:
"""
遮挡UID 由子类实现
"""
pass
return screen

def scroll(self, down: int, pos: Point = None):
"""
Expand Down
18 changes: 14 additions & 4 deletions src/one_dragon/base/controller/pc_clipboard.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time

import pywintypes
import win32clipboard
import win32con
import pywintypes
from pynput.keyboard import Controller, Key

from one_dragon.utils.log_utils import log, mask_text


Expand Down Expand Up @@ -32,7 +33,10 @@ def empty_clipboard() -> None:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
finally:
win32clipboard.CloseClipboard()
try:
win32clipboard.CloseClipboard()
except Exception:
pass

@staticmethod
def copy_string(text: str) -> None:
Expand All @@ -50,7 +54,10 @@ def copy_string(text: str) -> None:
win32clipboard.SetClipboardText(text, win32con.CF_UNICODETEXT)
log.info('复制文字到剪切板成功')
finally:
win32clipboard.CloseClipboard()
try:
win32clipboard.CloseClipboard()
except Exception:
pass

@staticmethod
def paste_text() -> str:
Expand All @@ -69,7 +76,10 @@ def paste_text() -> str:
except pywintypes.error:
data = ''
finally:
win32clipboard.CloseClipboard()
try:
win32clipboard.CloseClipboard()
except Exception:
pass

# 使用 pynput 模拟粘贴操作
log.info('粘贴文字, 按下 Ctrl+V')
Expand Down
7 changes: 2 additions & 5 deletions src/one_dragon/base/operation/one_dragon_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
from concurrent.futures import ThreadPoolExecutor

import logging
from enum import Enum
Expand All @@ -14,16 +13,14 @@
from one_dragon.base.matcher.ocr.onnx_ocr_matcher import OnnxOcrMatcher
from one_dragon.base.matcher.template_matcher import TemplateMatcher
from one_dragon.base.operation.context_event_bus import ContextEventBus
from one_dragon.base.operation.one_dragon_env_context import OneDragonEnvContext
from one_dragon.base.operation.one_dragon_env_context import OneDragonEnvContext, ONE_DRAGON_CONTEXT_EXECUTOR
from one_dragon.base.screen.screen_loader import ScreenContext
from one_dragon.base.screen.template_loader import TemplateLoader
from one_dragon.utils import debug_utils, log_utils
from one_dragon.utils import thread_utils
from one_dragon.utils.i18_utils import gt
from one_dragon.utils.log_utils import log

_one_dragon_context_executor = ThreadPoolExecutor(thread_name_prefix='one_dragon_context', max_workers=1)


class ContextRunStateEnum(Enum):

Expand Down Expand Up @@ -214,7 +211,7 @@ def async_init_ocr(self) -> None:
异步初始化OCR
:return:
"""
f = _one_dragon_context_executor.submit(self.ocr.init_model)
f = ONE_DRAGON_CONTEXT_EXECUTOR.submit(self.ocr.init_model)
f.add_done_callback(thread_utils.handle_future_result)


Expand Down
17 changes: 16 additions & 1 deletion src/one_dragon/base/operation/one_dragon_env_context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from concurrent.futures import ThreadPoolExecutor

from one_dragon.envs.env_config import EnvConfig
from one_dragon.envs.ghproxy_service import GhProxyService
from one_dragon.envs.git_service import GitService
from one_dragon.envs.project_config import ProjectConfig
from one_dragon.envs.python_service import PythonService
from one_dragon.utils import thread_utils

ONE_DRAGON_CONTEXT_EXECUTOR = ThreadPoolExecutor(thread_name_prefix='one_dragon_context', max_workers=1)


class OneDragonEnvContext:
Expand All @@ -15,6 +21,15 @@ def __init__(self):
self.env_config: EnvConfig = EnvConfig()
self.git_service: GitService = GitService(self.project_config, self.env_config)
self.python_service: PythonService = PythonService(self.project_config, self.env_config, self.git_service)
self.gh_proxy_service: GhProxyService = GhProxyService(self.env_config)

def init_by_config(self) -> None:
pass
pass

def async_update_gh_proxy(self) -> None:
"""
异步更新gh proxy
:return:
"""
future = ONE_DRAGON_CONTEXT_EXECUTOR.submit(self.gh_proxy_service.update_proxy_url)
future.add_done_callback(thread_utils.handle_future_result)
50 changes: 46 additions & 4 deletions src/one_dragon/devtools/python_launcher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import sys
import subprocess
import datetime
import time

import datetime
import os
import subprocess
import yaml
from colorama import init, Fore, Style

from one_dragon.base.operation.one_dragon_env_context import OneDragonEnvContext

# 初始化 colorama
init(autoreset=True)

Expand Down Expand Up @@ -116,16 +119,55 @@ def execute_python_script(app_path, log_folder, no_windows: bool):
subprocess.Popen(["powershell", "-Command", powershell_command], creationflags=subprocess.CREATE_NO_WINDOW)
else:
subprocess.Popen(["powershell", "-Command", powershell_command])
print_message("绝区零一条龙 正在启动中,大约 5+ 秒...", "INFO")
print_message("一条龙 正在启动中,大约 5+ 秒...", "INFO")


def fetch_latest_code(ctx: OneDragonEnvContext) -> None:
"""
获取最新代码
"""
if not ctx.env_config.auto_update:
print_message("未开启代码自动更新 跳过", "INFO")
return
print_message("开始获取最新代码...", "INFO")
success, msg = ctx.git_service.fetch_latest_code()
if success:
print_message("最新代码获取成功", "PASS")
else:
print_message(f'代码更新失败 {msg}', "ERROR")

check_dependencies(ctx)


def check_dependencies(ctx: OneDragonEnvContext):
"""
安装最新依赖
:return:
"""
current = ctx.env_config.requirement_time
latest = ctx.git_service.get_requirement_time()
if current == latest:
print_message("运行依赖无更新 跳过", "INFO")
return

success, msg = ctx.python_service.install_requirements()
if success:
print_message("运行依赖安装成功", "PASS")
ctx.env_config.requirement_time = latest
else:
print_message(f'运行依赖安装失败 {msg}', "ERROR")


def run_python(app_path, no_windows: bool = True):
# 主函数
try:
ctx = OneDragonEnvContext()
print_message(f"当前工作目录:{path}", "INFO")
verify_path_issues()
configure_environment()
log_folder = create_log_folder()
clean_old_logs(log_folder)
fetch_latest_code(ctx)
execute_python_script(app_path, log_folder, no_windows)
except SystemExit as e:
print_message(f"程序已退出,状态码:{e.code}", "ERROR")
Expand Down
56 changes: 33 additions & 23 deletions src/one_dragon/envs/env_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
from enum import Enum
from typing import Optional

from one_dragon.base.config.config_item import ConfigItem
from one_dragon.base.config.yaml_config import YamlConfig
from one_dragon.gui.widgets.setting_card.yaml_config_adapter import YamlConfigAdapter
from one_dragon.utils import os_utils

DEFAULT_ENV_PATH = os_utils.get_path_under_work_dir('.env')
Expand All @@ -16,7 +14,7 @@
DEFAULT_VENV_PYTHON_PATH = os.path.join(DEFAULT_VENV_DIR_PATH, 'scripts', 'python.exe') # 默认的虚拟环境中python.exe的路径
DEFAULT_PYTHON_PTH_PATH = os.path.join(DEFAULT_PYTHON_DIR_PATH, 'python311._pth') # 默认安装的python配置文件路径

GH_PROXY_URL = 'https://ghfast.top/' # 免费代理的路径
GH_PROXY_URL = 'https://ghfast.top' # 免费代理的路径

class ProxyTypeEnum(Enum):

Expand Down Expand Up @@ -128,7 +126,11 @@ def proxy_type(self, new_value: str) -> None:
self.update('proxy_type', new_value)

@property
def is_ghproxy(self) -> bool:
def is_personal_proxy(self) -> bool:
return self.proxy_type == ProxyTypeEnum.PERSONAL.value.value

@property
def is_gh_proxy(self) -> bool:
return self.proxy_type == ProxyTypeEnum.GHPROXY.value.value

@property
Expand All @@ -147,21 +149,6 @@ def personal_proxy(self, new_value: str) -> None:
"""
self.update('personal_proxy', new_value)

@property
def proxy_address(self) -> Optional[str]:
"""
:return: 真正使用的代理地址
"""
proxy_type = self.proxy_type
if proxy_type == ProxyTypeEnum.NONE.value.value:
return None
elif proxy_type == ProxyTypeEnum.GHPROXY.value.value:
return GH_PROXY_URL
elif proxy_type == ProxyTypeEnum.PERSONAL.value.value:
proxy = self.personal_proxy
return None if proxy == '' else proxy
return None

@property
def requirement_time(self) -> str:
"""
Expand Down Expand Up @@ -228,7 +215,7 @@ def auto_update(self) -> bool:
自动更新
:return:
"""
return self.get('auto_update', False)
return self.get('auto_update', True)

@auto_update.setter
def auto_update(self, new_value: bool) -> None:
Expand All @@ -251,9 +238,32 @@ def pip_source(self, new_value: str) -> None:
self.update('pip_source', new_value)

@property
def pip_source_adapter(self) -> YamlConfigAdapter:
return YamlConfigAdapter(self, 'pip_source', PipSourceEnum.TSING_HUA.value.value,
'str', 'str')
def gh_proxy_url(self) -> str:
"""
免费代理的url
:return:
"""
return self.get('gh_proxy_url', GH_PROXY_URL)

@gh_proxy_url.setter
def gh_proxy_url(self, new_value: str) -> None:
"""
免费代理的url
:return:
"""
self.update('gh_proxy_url', new_value)

@property
def auto_fetch_gh_proxy_url(self) -> bool:
"""
自动获取免费代理的url
:return:
"""
return self.get('auto_fetch_gh_proxy_url', True)

@auto_fetch_gh_proxy_url.setter
def auto_fetch_gh_proxy_url(self, new_value: bool) -> None:
self.update('auto_fetch_gh_proxy_url', new_value)

def write_env_bat(self) -> None:
"""
Expand Down
Loading

0 comments on commit 26a558a

Please sign in to comment.