Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 160 additions & 16 deletions backend/core/agent.py

Large diffs are not rendered by default.

71 changes: 67 additions & 4 deletions backend/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import traceback
import urllib.parse

from . import (accounts, agent, appupdate, asstlog, copilot, gacha, garrison,
maa, maaplan, multiacct, operbox, packsync, paradox, resources,
scheduler, settings, signin, skills, sklogin, tasks, updater,
version, watch)
from . import (accounts, agent, appupdate, asstlog, autostart, copilot, gacha,
garrison, maa, maaplan, multiacct, operbox, packsync, paradox,
resources, scheduler, settings, signin, skills, sklogin, tasks,
themes, updater, version, watch)

WEB = settings.static_dir()

Expand All @@ -22,6 +22,11 @@
'.png': 'image/png',
'.ico': 'image/x-icon',
'.woff2': 'font/woff2',
'.webm': 'video/webm',
'.mp4': 'video/mp4',
'.jpg': 'image/jpeg',
'.json': 'application/json; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
}


Expand Down Expand Up @@ -399,6 +404,60 @@ def api_pack_update(_q, _body):
'gamedata': version.check(acts)}


def api_themes(q, _body):
"""主题清单:内置黑/白 + 本机已装 + 远端可下载(远端读不到只给一句提示)

``?local=1`` 不联网,只回本机的内置 + 已装。设置页先用它渲染一遍卡片,
再拉全量补「可下载」——远端慢的时候不至于连已装的主题都看不见。
"""
qs = q or {}
local_only = str(qs.get('local') or '').lower() in ('1', 'true')
if local_only:
return themes.catalog(local_only=True)
timeout = 12 if str(qs.get('force') or '').lower() in ('1', 'true') else 6
return themes.catalog(timeout=timeout)


def api_theme_install(q, body):
"""下载并安装第三方主题(用户点「下载」才会走到这里)"""
tid = str((body or {}).get('id') or (q or {}).get('id') or '').strip()
if not tid:
raise ValueError('缺 id(要装哪个主题)')
r = themes.install(tid)
# 装完把「当前主题」的可用性也回给前端:装失败时别让它切过去
st = settings.load()
if st.get('theme') == tid and not themes.usable(tid):
settings.save({'theme': 'dark'})
return r


def api_theme_remove(_q, body):
"""删掉本机那份主题包(设置里的当前主题若正是它,退回深色)"""
tid = str((body or {}).get('id') or '').strip()
r = themes.remove(tid)
if settings.load().get('theme') == tid:
settings.save({'theme': 'dark'})
return r


def api_autostart(_q, body):
"""开机自启:GET 只读,POST 按用户这次的选择写/删注册表

**只由用户点开关触发**(默认 False,settings 里也写着不许改成 True)。
"""
if body:
want = body.get('enabled')
# 字符串 "false" / 0 一律当 False(前端传的是 JSON 布尔,兜个底)
if isinstance(want, str):
want = want.strip().lower() not in ('0', 'false', 'no', 'off', '')
applied = autostart.apply(bool(want))
settings.save({'launchAtLogin': bool(want)})
out = autostart.status()
out['applied'] = applied
return out
return autostart.status()


def api_app_status(q, _body):
"""程序本体有没有新版本(只查不下载;force=1 跳过 6 小时节流)"""
force = str((q or {}).get('force') or '').lower() in ('1', 'true', 'yes')
Expand Down Expand Up @@ -996,6 +1055,10 @@ def api_schedule_detect(q, _body):
'/api/author/suggest': api_author_suggest,
'/api/pack/status': api_pack_status,
'/api/pack/update': api_pack_update,
'/api/themes': api_themes,
'/api/theme/install': api_theme_install,
'/api/theme/remove': api_theme_remove,
'/api/autostart': api_autostart,
'/api/app/status': api_app_status,
'/api/app/download': api_app_download,
'/api/app/install': api_app_install,
Expand Down
136 changes: 136 additions & 0 deletions backend/core/autostart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
"""开机自启(Windows 的 HKCU Run 项,默认关闭)

**纪律(用户 2026-11-25 原话)**:「我极度厌恶没有在我同意情况下默认开机
自启的软件」。所以这里只做三件事:

- ``status()`` 只读,任何时候都能问现在是不是开着;
- ``enable()`` / ``disable()`` 只在用户点了设置里的开关时调用;
- ``sync(enabled)`` 启动时对一次账——**只有设置里是 True 才写**,是 False 就
顺手删掉(用户手动取消勾选后,注册表里残留的那条也要清掉)。

不碰 HKLM,不写计划任务,不建启动文件夹快捷方式:只动当前用户这一条值,
删起来干净,用户自己在「任务管理器 → 启动」里也能看到它。

源码模式(``python tools/dev.py``)没有意义:自启一个 python.exe 不会带参数,
所以 ``supported`` 为 False,前端把开关置灰并说明「打包版才有」。
"""
from __future__ import annotations

import os
import sys

from . import settings

RUN_KEY = r'Software\Microsoft\Windows\CurrentVersion\Run'
VALUE_NAME = 'MAA-PRTS'


def _is_windows() -> bool:
return os.name == 'nt'


def exe_path() -> str:
"""自启要拉起来的那个程序(打包版 = exe 自己;源码模式 = 空)"""
if not getattr(sys, 'frozen', False):
return ''
p = os.path.abspath(sys.executable)
return p if os.path.isfile(p) else ''


def supported() -> bool:
return _is_windows() and bool(exe_path())


def _open_key(write=False):
import winreg
flags = winreg.KEY_READ | (winreg.KEY_WRITE if write else 0)
return winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, flags)


def _read() -> str:
"""注册表里那条命令(没有就返回空串)"""
if not _is_windows():
return ''
import winreg
try:
with _open_key() as k:
val, _typ = winreg.QueryValueEx(k, VALUE_NAME)
return str(val or '')
except FileNotFoundError:
return ''
except OSError:
return ''


def status() -> dict:
"""给设置页看的现状:支不支持 / 注册表里有没有 / 指向哪个程序"""
cmd = _read()
return {
'supported': supported(),
'enabled': bool(cmd),
'target': cmd,
'exe': exe_path(),
# 设置里用户勾的那一栏(正常应该与 enabled 一致,不一致说明被外部改过)
'setting': bool(settings.load().get('launchAtLogin')),
}


def enable() -> dict:
"""写自启项(**只应由用户勾选触发**)"""
exe = exe_path()
if not supported():
raise RuntimeError('源码运行没有开机自启;请用打包版(MAA-PRTS.exe)')
import winreg
cmd = '"%s"' % exe
with _open_key(write=True) as k:
winreg.SetValueEx(k, VALUE_NAME, 0, winreg.REG_SZ, cmd)
return {'ok': True, 'enabled': True, 'target': cmd}


def disable() -> dict:
"""删自启项(幂等:本来就没有也算成功)"""
if not _is_windows():
return {'ok': True, 'enabled': False}
import winreg
try:
with _open_key(write=True) as k:
winreg.DeleteValue(k, VALUE_NAME)
except FileNotFoundError:
pass
except OSError as e:
# 权限问题要如实报,不然用户以为关了其实还开着
raise RuntimeError('删不掉自启项:%s' % e)
return {'ok': True, 'enabled': False}


def apply(enabled: bool) -> dict:
"""按开关应用:True → 写;False → 删"""
if enabled:
return enable()
return disable()


def sync(enabled: bool) -> dict:
"""启动时对账(不抛异常,避免开机自启这种小事把启动搞挂)

- 设置里 False:把残留的值删掉(用户可能在外面手动加过,或旧版本写过)
- 设置里 True:打包版就写/刷新一次(exe 换过路径也能自动修好);
源码模式忽略
"""
try:
# 源码模式一律不碰注册表:开发时跑 dev.py 不能把用户打包版的自启设置
# 顺手删掉(那是真实生效的那一条)
if not getattr(sys, 'frozen', False):
return {'ok': True, 'enabled': bool(_read()), 'skipped': True,
'why': '源码运行不改开机自启'}
if not enabled:
if _read():
return disable()
return {'ok': True, 'enabled': False, 'skipped': True}
if not supported():
return {'ok': True, 'enabled': bool(_read()), 'skipped': True,
'why': '找不到 exe 路径'}
return enable()
except Exception as e: # noqa: BLE001 启动流程不许炸
return {'ok': False, 'error': str(e)}
15 changes: 11 additions & 4 deletions backend/core/eventnav.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@
# 只留商店。入口也不再是左下角的横幅——那个横幅现在归当期活动,TL 的
# 卡片退到终端页 ATTENTION 区第二格,点它才进活动地图。
'closed': True,
# 2026-11-25 重标(用户报「TL 商店导航失效」):卡片被加上「作战已结束」
# 灰条之后整张插画分低了,`_entry_here` 定点比 46.8 分(阈值 16)→
# 认为终端页上没有这条目,导航直接失败。模板改从**真机 1920 截图**整块
# 裁插画(370×230),连灰条一起认——灰条本身就是「这个活动结束了」的
# 标记,且它出现后插画不再变;「3天」倒计时在左边的图标列里,没裁进来。
'entry': {
'tpl': 'TL_card.png',
'roi': [620, 300, 260, 150],
'roi': [640, 283, 247, 153],
'tap': [753, 371],
'wait': 5.0,
},
Expand All @@ -73,9 +78,11 @@
# 小游戏和活动商店。这两项由 MAA 的 Custom 任务接手,所以导航只负责
# 把界面点到门口(见 maatools.py 顶部注释)。
'tools': {
# 名字是用户 2026-11-22 截图里的写法(之前标成「炮火所掩埋的」)
'shop': {'tpl': 'TL_shop.png', 'label': '熄火所掩埋的',
'roi': [880, 120, 200, 130], 'tap': [975, 185],
# 游戏里写的是「炮火所掩埋的」(What Lies Beneath the Ashes),
# 2026-11-25 重抠:活动结束后节点上多了红色时钟和「941」横幅,
# 老模板 14.1 分(阈值 12)认不出,只能按兜底坐标盲点。
'shop': {'tpl': 'TL_shop.png', 'label': '炮火所掩埋的',
'roi': [920, 137, 150, 50], 'tap': [995, 161],
'wait': 3.0},
},
},
Expand Down
56 changes: 56 additions & 0 deletions backend/core/maacore.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,59 @@ def _utf8(s):
_LIVE = set()
_LIVE_LOCK = threading.Lock()

# 用户点名要求「停这一路」的标记:引擎叫停之后,上层还有段循环(扫图第
# 2/3 段、周目下一批、多账号下一个号)会接着往下排——2026-11-24 用户报
# 「账号我按了暂停,但流程和 MaaCore 好像不会停」就是这里。记一笔,让
# skill 的循环在段与段之间能查「这一路是不是已经被叫停了」。
_STOP_REQ = set()
_STOP_REQ_LOCK = threading.Lock()


def request_stop(owners=None):
"""记下「用户要求停这些账号」(真正叫停引擎在 :func:`stop_live`)

``owners`` 空 = 全停(``stop_all_live`` 走这条)。存账号文件名,查询走
:func:`owner_match`,所以传昵称、去扩展名的写法都能对上。
"""
with _STOP_REQ_LOCK:
if not owners:
_STOP_REQ.add('*')
return 1
n = 0
for w in owners:
s = os.path.basename(str(w or '').strip())
if s:
_STOP_REQ.add(s)
n += 1
return n


def stop_requested(owner):
"""这个账号是不是刚被用户叫停过(段循环 / 步骤循环用它及时收手)

**按账号判**:停 A 号不影响 B 号——用户口径是「停这一路,别的账号没动」。
"""
if not owner:
return False
with _STOP_REQ_LOCK:
if not _STOP_REQ:
return False
wants = list(_STOP_REQ)
if '*' in wants:
return True
return owner_match(owner, wants)


def clear_stop(owners=None):
"""这一路重新开工时清掉标记,别把下一轮也一起停了"""
with _STOP_REQ_LOCK:
if not owners:
_STOP_REQ.clear()
return
for w in owners:
s = os.path.basename(str(w or '').strip())
_STOP_REQ.discard(s)


def stop_live(owners=None):
"""叫停直连实例(只 AsstStop,不销毁,让调用方的 wait() 自己收到结束)
Expand All @@ -99,6 +152,9 @@ def stop_live(owners=None):
匹配走 :func:`owner_match`,所以传文件名、``示例昵称#0384 bilibili服`` 这种
去掉扩展名的写法、昵称都能对上。
"""
# 先记账再叫停:用户按的这一刻可能正好在「段与段之间」,那会儿没有
# 活引擎(下面循环一个都停不到),只靠 AsstStop 的话下一段还会照排。
request_stop(owners)
with _LIVE_LOCK:
live = [e for e in _LIVE if e._handle]
n = 0
Expand Down
Loading