半年前,我把信息学培训中所有“有用”的习题代码组合成了一个程序——“万能C++机器”。它,就是“万能助手”的前身。
目录
Part 0 / 万能助手2
2020年8月,我给万能助手建了一个官方网站
,并上线了万能助手2.2.0的下载链接。
网站代码是Rain
和吴先森
写的,我只是后来进行了一些小小的修改,让网站管理起来更方便,顺便把logo升级了一下。
升级万能助手3前,万能助手的最新版本是2.3.0。它的源代码(下文中可能会用到)可以在此处下载。
Part 1 / 开始
万能助手3计划使用以Python为主的多种语言开发,IDE使用Visual Studio 2019。
开始开发时,项目结构如下图。
- main.py 主程序
- store.py 模块商店
- update.py 更新检查组件
- wnzs.exe 启动器
- inst.py 安装脚本
- uninst.bat 卸载脚本
- inst_reg.bat 注册脚本
- inst_py.exe Python环境安装器
Part 2 / 第一行代码
启动器(C++)
按我的习惯,先从最简单的写起——那就是启动器。启动器的实际作用可能还没有它的图标大……
#include<cstdlib>
int main(){
return system("python main.py");
}
这个代码我没有用VS来写 (因为没那个必要哈哈) ,用一个Dev-C++控制台项目就能解决了。这可比万能助手2更满足强迫症患者的需求,因为……不用“双闪”啊!
主程序(Python)
接着,主程序写起。万能助手3中,我添加了许多GUI(而不仅仅是控制台+MessageBox
),用tkinter
来实现。主程序代码框架如下:
import update
import tkinter
gui=tkinter.Tk()
#Some Code
gui.mainloop()
更新组件
import easygui
import sys
from os import system
from urllib.request import urlretrieve as file,urlopen
now='3.0.0'
print('正在检查更新……')
ver=urlopen('https://www.wnzs.top/api/ver.php').read().decode()
if not now in ver:
if easygui.buttonbox('当前版本{},发现新版本{}。'.format(now,ver),title='万能助手更新',choices=['更新','取消'])=='更新':
print('正在下载更新包……')
file('https://www.wnzs.top/download/update.exe','wnzs.exe')
easygui.msgbox('更新包下载完毕,请自行重启程序!',title='万能助手更新')
看到用前后两个空行隔开的now='3.0.0'
这行代码,你一定会捧腹大笑:太不智能了!没关系,毕竟这是开发初期嘛。(题外话:我没系统学习Python,代码还保留了一些C++的风格,看习惯就好。)
服务器API(PHP)
当然,这也需要服务器端的支持。像这个update.exe,我就还没有搞,所以现在这个代码运行起来有些问题。放出服务器API的超级简陋 PHP代码(其实我不会PHP,纯属瞎搞):
exit(json_decode(file_get_contents('../data/webdata.json'),true)[0]["ver"]);
安装脚本(Batch)
这些批处理脚本,我基本是照搬万能助手2的代码……
@echo off
title 万能助手安装
set /p wnzsdir=<"%userprofile%\wnzs3.ini"
if "%wnzsdir%"==%~dp0 (
inst_main.exe
echo 正在卸载旧版Python环境……
inst_py.exe /uninstall SimpleInstall=1
echo 正在安装新版Python环境,请点击安装按钮……
start inst_py.exe InstallAllUsers=1 TargetDir="%wnzsdir%py" AssociateFiles=0 PrependPath=1 Shortcuts=0 Include_doc=0 Include_launcher=0 Include_tcltk=1 Include_tools=1 Include_lib=1 Include_pip=1 SimpleInstall=1
exit
)
cls
echo 正在保存路径……
set wnzsdir=%~dp0
echo %wnzsdir% >"%userprofile%\wnzs3.ini"
echo 正在提取文件……
inst_main.exe
echo 正在安装Python环境,请点击安装按钮并等待……
inst_py.exe InstallAllUsers=1 TargetDir="%wnzsdir%py" AssociateFiles=0 PrependPath=1 Shortcuts=0 Include_doc=0 Include_launcher=0 Include_tcltk=1 Include_tools=1 Include_lib=1 Include_pip=1 SimpleInstall=1
echo 正在写入注册表……
inst_reg.bat
注册脚本(Batch)
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
reg add HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\wnzs3\ /f /v DisplayIcon /d "%wnzsdir%wnzs.exe"
reg add HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\wnzs3\ /f /v DisplayName /d 万能助手3
reg add HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\wnzs3\ /f /v Publisher /d 天科蔗蓝FrankGan
reg add HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\wnzs3\ /f /v UninstallString /d "%wnzsdir%uninst.bat"
卸载脚本(Batch)
@echo off
title 正在卸载万能助手
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
set /p wnzsdir=<"%userprofile%\wnzs3.ini"
if "%wnzsdir%"==%~dp0 (
xcopy /y "%0" "%temp%\uninst.bat"
start "%temp%\uninst.bat"
exit
)
cls
set /p var="确定清空所有数据请输入Y:"
if %var%==Y goto uninst
if %var%==y goto uninst
exit
:uninst
echo 请稍候……
del /f %USERPROFILE%\DESKTOP\万能助手.lnk 2>nul
reg delete HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\万能助手 /f 2>nul
del /f "%userprofile%\wnzs3.ini"
rmdir /s /q "%wnzsdir%" 2>nul
inst_py.exe /uninstall SimpleInstall=1
至此为止,我已经使用了C++
、Python
、PHP
、Batch
四种语言了。接下来,我们还可能会用到HTML
、Scratch
(如果这也算语言的话)。
另外,Rain
曾为万能助手2开发了一个浏览器。相信它很快就能派上用场。
服务器端的音乐下载API也搞得差不多了。(由于某些原因,代码就不放上来了)
在这个部分的结尾,水几张刚刚更新的官网图片吧~
我用Scratch编写的二叉树训练机已经上线了万能助手官方网站,广大OIer们可以用它来巩固信奥初赛的二叉树这个知识点哦~