Skip to content

Commit

Permalink
A | Frpc更新器
Browse files Browse the repository at this point in the history
  • Loading branch information
LxHTT committed Sep 17, 2023
1 parent 105956a commit 4ef0ac5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
10 changes: 5 additions & 5 deletions OFSettingsController.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import path as ospath
from os import path as osp, makedirs
from json import dumps, loads
from MCSL2Lib.singleton import Singleton

Expand All @@ -13,14 +13,14 @@


def initOFPluginConfiguration():
if not ospath.exists(r"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json"):
if not osp.exists(r"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json"):
with open(
"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json",
"w+",
encoding="utf-8",
) as newConfig:
newConfig.write(dumps(ofConfigTemplate, indent=4))
print(1111)
makedirs(r"./Plugins/OpenFRP_Plugin/frpc", exist_ok=True)
OfSettingsController().readSettings()


Expand All @@ -31,9 +31,9 @@ def __init__(self):

def readSettings(self):
"""重新将文件中的配置强制覆盖到程序中,不管是否保存了"""
if ospath.exists(r"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json"):
if osp.exists(r"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json"):
if (
ospath.getsize(
osp.getsize(
r"./Plugins/OpenFRP_Plugin/MCSL2_OpenFrpPluginConfig.json"
)
!= 0
Expand Down
90 changes: 85 additions & 5 deletions OfFrpcUpdater.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
from MCSL2Lib.Controllers.aria2ClientController import Aria2Controller
from MCSL2Lib.Controllers.networkController import Session
from MCSL2Lib.Widgets.DownloadProgressWidget import DownloadMessageBox
from .OfSettingsController import OfSettingsController
from qfluentwidgets import (
MessageBox,
InfoBar,
InfoBarPosition,
PushButton,
FluentIcon as FIF,
)
from PyQt5.QtCore import Qt
from random import randint
from zipfile import ZipFile
from os import remove

ofSettingsController = OfSettingsController()


class FrpcVersion:
def __init__(self, ver: str):
ver = ver.split(".")
Expand All @@ -12,18 +26,25 @@ def __init__(self, ver: str):


class OfFrpcUpdater:
def __init__(self):
def __init__(self, parent):
self.parent = parent
self.updateAPI = "https://of-dev-api.bfsea.xyz/commonQuery/get?key=software"
self.oldVer = FrpcVersion(ofSettingsController.fileSettings["frpc_version"]) if not ofSettingsController.fileSettings["frpc_version"] == "" else None
self.oldVer = (
FrpcVersion(ofSettingsController.fileSettings["frpc_version"])
if not ofSettingsController.fileSettings["frpc_version"] == ""
else None
)

def getUpdateInfo(self):
info = Session.get(self.updateAPI).json() # type: dict
onlineVer = FrpcVersion(info["latest_ver"])
if self.oldVer is not None:
stat = self.cmp(oldVer=self.oldVer, newVer=onlineVer)
if stat:
self.downloadFrpc(latest=info["latest"], source=info["source"], soft_win=info["soft"][2])

self.downloadFrpc(
latest=info["latest"], source=info["source"], soft_win="frpc_windows_386.zip"
)
OfSettingsController.changeSettings({"frpc_version": info["latest_ver"]})

def cmp(self, oldVer: FrpcVersion, newVer: FrpcVersion):
return (
Expand All @@ -32,5 +53,64 @@ def cmp(self, oldVer: FrpcVersion, newVer: FrpcVersion):
else False
)

def hideDownloadHelper(self):
self.downloadingInfoBar = InfoBar(
icon=FIF.DOWNLOAD,
title="已隐藏下载窗口",
content="仍在下载中,点击按钮恢复下载窗口...",
orient=Qt.Horizontal,
isClosable=False,
duration=-1,
position=InfoBarPosition.TOP_RIGHT,
parent=self.parent,
)
self.downloadingInfoBar.setCustomBackgroundColor("white", "#202020")
showDownloadMsgBoxBtn = PushButton()
showDownloadMsgBoxBtn.setText("恢复")
showDownloadMsgBoxBtn.clicked.connect(self.downloadingBox.show)
showDownloadMsgBoxBtn.clicked.connect(self.downloadingInfoBar.close)
self.downloadingInfoBar.addWidget(showDownloadMsgBoxBtn)
self.downloadingInfoBar.show()

def downloadFinishedHelper(self):
try:
self.downloadingInfoBar.close()
InfoBar.success("下载完毕")
except:
pass

def downloadFrpc(self, latest, source, soft_win):
downloadURL = f"{source[randint(0, len(source)-1)]}{latest}{soft_win}"
if not Aria2Controller.testAria2Service():
if not Aria2Controller.startAria2():
box = MessageBox(title="无法下载", content="Aria2可能未安装或启动失败", parent=self.parent)
box.exec()
return
self.downloadingBox = DownloadMessageBox(soft_win, parent=self)
self.downloadingBox.DownloadWidget().closeBoxBtnFinished.clicked.connect(
self.downloadingBox.close
)
self.downloadingBox.DownloadWidget().closeBoxBtnFailed.clicked.connect(
self.downloadingBox.close
)
gid = Aria2Controller.download(
uri=f"{source[randint(0, len(source)-1)]}{latest}{soft_win}",
watch=True,
info_get=self.downloadingBox.onInfoGet,
stopped=self.downloadingBox.onDownloadFinished,
interval=0.2,
)
self.downloadingBox.canceled.connect(
lambda: Aria2Controller.cancelDownloadTask(gid)
)
self.downloadingBox.paused.connect(
lambda x: Aria2Controller.pauseDownloadTask(gid)
if x
else Aria2Controller.resumeDownloadTask(gid)
)
self.downloadingBox.show()

def extract(self, file):
frpcArchive = ZipFile(file, "r")
frpcArchive.extractall("./Plugins/OpenFRP_Plugin/frpc")
frpcArchive.close()
remove(file)
1 change: 0 additions & 1 deletion OpenFRP_Plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def enable():
"OpenFrp设置",
position=NavigationItemPosition.SCROLL,
)
Window().navigationInterface.addSeparator(position=NavigationItemPosition.SCROLL)
InfoBar.success(
title="提示",
content="OpenFrp 插件已启用。",
Expand Down

0 comments on commit 4ef0ac5

Please sign in to comment.