::call framwork startpre
::              adbpre   [blktool all]
::              theme    [主题名]
::              conf     配置文件名       变量名  变量值

@ECHO OFF
set var1=%1& set var2=%2& set var3=%3& set var4=%4& set var5=%5& set var6=%6& set var7=%7& set var8=%8& set var9=%9
goto %var1%



:CONF
SETLOCAL
set logger=framwork.bat-conf
call log %logger% I 将向conf\%var2%写入%var3%=%var4%
if not exist conf\%var2% ECHOC {%c_e%}找不到conf\%var2%{%c_i%}{\n}& call log %logger% F 找不到conf\%var2%& goto FATAL
find "set %var3%=" "conf\%var2%" 1>nul 2>nul || echo.set %var3%=%var4%|findstr "set" 1>>conf\%var2%&& goto CONF-DONE
type conf\%var2% | find "set " | find /v "set %var3%=" 1>tmp\output.txt
echo.set %var3%=%var4%|findstr "set" 1>>tmp\output.txt
move /Y tmp\output.txt conf\%var2% 1>nul || ECHOC {%c_e%}移动tmp\output.txt到conf\%var2%失败{%c_i%}{\n}&& call log %logger% F 移动tmp\output.txt到conf\%var2%失败&& goto FATAL
:CONF-DONE
ENDLOCAL
goto :eof


:THEME
set c_i=0F& set c_w=0E& set c_e=0C& set c_s=0A& set c_h=0D
if "%var2%"=="" set var2=%framwork_theme%
if "%var2%"=="default" set c_i=0F& set c_w=0E& set c_e=0C& set c_s=0A& set c_h=0D
if "%var2%"=="douyinhacker" set c_i=0A& set c_w=0E& set c_e=0C& set c_s=0F& set c_h=0D
if "%var2%"=="ubuntu" set c_i=5F& set c_w=5E& set c_e=5C& set c_s=5A& set c_h=59
if "%var2%"=="classic" set c_i=3F& set c_w=3E& set c_e=3C& set c_s=3A& set c_h=3D
if "%var2%"=="gold" set c_i=8E& set c_w=E0& set c_e=CF& set c_s=A0& set c_h=6F
if "%var2%"=="dos" set c_i=1F& set c_w=1E& set c_e=1C& set c_s=A0& set c_h=80
if "%var2%"=="ChineseNewYear" set c_i=CF& set c_w=6F& set c_e=0F& set c_s=C0& set c_h=7C
goto :eof


:STARTPRE
ECHO.正在准备启动...
::获取Windows版本号...
for /f "tokens=4 delims=[] " %%a in ('ver ^| find " "') do set winver=%%a
::ECHO.检查find命令...
find /? | find "string" 1>nul || ECHO.执行find命令失败&& goto FATAL
::ECHO.检查findstr命令...
findstr /? | findstr "string" 1>nul || ECHO.执行findstr命令失败&& goto FATAL
::ECHO.检查和保存工作目录路径...
for /f "tokens=1 delims=() " %%i in ('echo.%cd%') do (if not "%%i"=="%cd%" ECHO.工具箱路径中不允许有空格或英文括号& goto FATAL)
set framwork_workspace=%cd%
::设置path...
set path=%framwork_workspace%;%framwork_workspace%\tool\Windows;%path%
::ECHO.检查ECHOC...
if not exist tool\Windows\ECHOC.exe ECHO.找不到ECHOC.exe& goto FATAL
ECHOC | find "Usage" 1>nul 2>nul || ECHO.ECHOC.exe无法运行&& goto FATAL
::ECHO.准备日志系统...
if "%framwork_log%"=="n" set logfile=nul& set logger=CLOSED
if "%framwork_log%"=="n" SETLOCAL & goto STARTPRE-2
if not exist tool\Windows\gettime.exe ECHOC {%c_e%}找不到gettime.exe{%c_i%}{\n}& goto FATAL
gettime.exe | find "." 1>nul 2>nul || ECHOC {%c_e%}gettime.exe无法运行{%c_i%}{\n}&& goto FATAL
for /f %%a in ('gettime.exe ^| find "."') do set logfile=%framwork_workspace%\log\%%a.log
set logger=UNKNOWN
SETLOCAL
set logger=framwork.bat-startpre
call log %logger% I 系统信息:%processor_architecture%.%winver%.工作目录:%framwork_workspace%
::ECHO.清理日志...
for /f %%a in ('dir /B log ^| find /C ".log"') do (if %%a LEQ 6 goto STARTPRE-2)
for /f "tokens=1 delims=[]" %%a in ('dir /B log ^| find /N ".log"') do set /a var=%%a-6
:STARTPRE-1
dir /B log | find /N ".log" | find "[%var%]" 1>nul 2>nul || goto STARTPRE-2
for /f "tokens=2 delims=[]" %%a in ('dir /B log ^| find /N ".log" ^| find "[%var%]"') do del log\%%a 1>nul
set /a var+=-1& goto STARTPRE-1
:STARTPRE-2
::ECHO.检查Notepad3...
if not exist tool\Windows\Notepad3\Notepad3.exe ECHOC {%c_e%}找不到Notepad3.exe{%c_i%}{\n}& call log %logger% F 找不到Notepad3.exe& goto FATAL
::ECHO.检查scrcpy...
if not exist tool\Windows\scrcpy\scrcpy.exe ECHOC {%c_e%}找不到scrcpy.exe{%c_i%}{\n}& call log %logger% F 找不到scrcpy.exe& goto FATAL
tool\Windows\scrcpy\scrcpy.exe -h | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}scrcpy.exe无法运行{%c_i%}{\n}&& call log %logger% F scrcpy.exe无法运行&& goto FATAL
::ECHO.检查7z...
if not exist tool\Windows\7z.dll ECHOC {%c_e%}找不到7z.dll{%c_i%}{\n}& call log %logger% F 找不到7z.dll& goto FATAL
if not exist tool\Windows\7z.exe ECHOC {%c_e%}找不到7z.exe{%c_i%}{\n}& call log %logger% F 找不到7z.exe& goto FATAL
7z.exe | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}7z.exe无法运行{%c_i%}{\n}&& call log %logger% F 7z.exe无法运行&& goto FATAL
::ECHO.检查adb...
if not exist tool\Windows\adb.exe ECHOC {%c_e%}找不到adb.exe{%c_i%}{\n}& call log %logger% F 找不到adb.exe& goto FATAL
if not exist tool\Windows\AdbWinApi.dll ECHOC {%c_e%}找不到AdbWinApi.dll{%c_i%}{\n}& call log %logger% F 找不到AdbWinApi.dll& goto FATAL
if not exist tool\Windows\AdbWinUsbApi.dll ECHOC {%c_e%}找不到AdbWinUsbApi.dll{%c_i%}{\n}& call log %logger% F 找不到AdbWinUsbApi.dll& goto FATAL
adb.exe start-server>nul
adb.exe devices | find "List of devices attached" 1>nul 2>nul || ECHOC {%c_e%}adb.exe无法运行{%c_i%}{\n}&& call log %logger% F adb.exe无法运行&& goto FATAL
::ECHO.检查aria2c...
if not exist tool\Windows\aria2c.exe ECHOC {%c_e%}找不到aria2c.exe{%c_i%}{\n}& call log %logger% F 找不到aria2c.exe& goto FATAL
aria2c.exe -h | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}aria2c.exe无法运行{%c_i%}{\n}&& call log %logger% F aria2c.exe无法运行&& goto FATAL
::ECHO.检查busybox...
if not exist tool\Windows\busybox.exe ECHOC {%c_e%}找不到busybox.exe{%c_i%}{\n}& call log %logger% F 找不到busybox.exe& goto FATAL
busybox.exe | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}busybox.exe无法运行{%c_i%}{\n}&& call log %logger% F busybox.exe无法运行&& goto FATAL
::ECHO.检查calc...
if not exist tool\Windows\calc.exe ECHOC {%c_e%}找不到calc.exe{%c_i%}{\n}& call log %logger% F 找不到calc.exe& goto FATAL
calc.exe 6 m 6 | find "36.00" 1>nul 2>nul || ECHOC {%c_e%}calc.exe无法运行{%c_i%}{\n}&& call log %logger% F calc.exe无法运行&& goto FATAL


::ECHO.检查curl...
if not exist tool\Windows\curl.exe ECHOC {%c_e%}找不到curl.exe{%c_i%}{\n}& call log %logger% F 找不到curl.exe& goto FATAL
curl.exe --help | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}curl.exe无法运行{%c_i%}{\n}&& call log %logger% F curl.exe无法运行&& goto FATAL
::ECHO.检查cygwin1.dll...
if not exist tool\Windows\cygwin1.dll ECHOC {%c_e%}找不到cygwin1.dll{%c_i%}{\n}& call log %logger% F 找不到cygwin1.dll& goto FATAL
::ECHO.检查devcon...
if not exist tool\Windows\devcon.exe ECHOC {%c_e%}找不到devcon.exe{%c_i%}{\n}& call log %logger% F 找不到devcon.exe& goto FATAL
devcon.exe help | find "Device" 1>nul 2>nul || ECHOC {%c_e%}devcon.exe无法运行{%c_i%}{\n}&& call log %logger% F devcon.exe无法运行&& goto FATAL
::ECHO.检查fastboot...
if not exist tool\Windows\fastboot.exe ECHOC {%c_e%}找不到fastboot.exe{%c_i%}{\n}& call log %logger% F 找不到fastboot.exe& goto FATAL
fastboot.exe -h 2>&1 | find "usage" 1>nul 2>nul || ECHOC {%c_e%}fastboot.exe无法运行{%c_i%}{\n}&& call log %logger% F fastboot.exe无法运行&& goto FATAL
::ECHO.检查fh_loader...
if not exist tool\Windows\fh_loader.exe ECHOC {%c_e%}找不到fh_loader.exe{%c_i%}{\n}& call log %logger% F 找不到fh_loader.exe& goto FATAL
fh_loader.exe -6 2>&1 | find "Base" 1>nul 2>nul || ECHOC {%c_e%}fh_loader.exe无法运行{%c_i%}{\n}&& call log %logger% F fh_loader.exe无法运行&& goto FATAL
::ECHO.检查filedialog...
if not exist tool\Windows\filedialog.exe ECHOC {%c_e%}找不到filedialog.exe{%c_i%}{\n}& call log %logger% F 找不到filedialog.exe& goto FATAL
filedialog.exe | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}filedialog.exe无法运行{%c_i%}{\n}&& call log %logger% F filedialog.exe无法运行&& goto FATAL
::ECHO.检查libcurl.def...
if not exist tool\Windows\libcurl.def ECHOC {%c_e%}找不到libcurl.def{%c_i%}{\n}& call log %logger% F 找不到libcurl.def& goto FATAL
::ECHO.检查libcurl.dll...
if not exist tool\Windows\libcurl.dll ECHOC {%c_e%}找不到libcurl.dll{%c_i%}{\n}& call log %logger% F 找不到libcurl.dll& goto FATAL
::ECHO.检查magiskboot...
if not exist tool\Windows\magiskboot.exe ECHOC {%c_e%}找不到magiskboot.exe{%c_i%}{\n}& call log %logger% F 找不到magiskboot.exe& goto FATAL
magiskboot.exe 2>&1 | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}magiskboot.exe无法运行{%c_i%}{\n}&& call log %logger% F magiskboot.exe无法运行&& goto FATAL
::ECHO.检查magiskpatcher...
if not exist tool\Windows\magiskpatcher.exe ECHOC {%c_e%}找不到magiskpatcher.exe{%c_i%}{\n}& call log %logger% F 找不到magiskpatcher.exe& goto FATAL
magiskpatcher.exe -h 2>&1 | find "usage" 1>nul 2>nul || ECHOC {%c_e%}magiskpatcher.exe无法运行{%c_i%}{\n}&& call log %logger% F magiskpatcher.exe无法运行&& goto FATAL
::ECHO.检查QSaharaServer...
if not exist tool\Windows\QSaharaServer.exe ECHOC {%c_e%}找不到QSaharaServer.exe{%c_i%}{\n}& call log %logger% F 找不到QSaharaServer.exe& goto FATAL
QSaharaServer.exe | find "Usage" 1>nul 2>nul || ECHOC {%c_e%}QSaharaServer.exe无法运行{%c_i%}{\n}&& call log %logger% F QSaharaServer.exe无法运行&& goto FATAL
ENDLOCAL
::ECHO.准备安卓工具,安卓工具路径可以用变量
::ECHO.检查magisk\24100.apk...
if not exist tool\Android\magisk\24100.apk ECHOC {%c_e%}找不到magisk\24100.apk{%c_i%}{\n}& call log %logger% F 找不到magisk\24100.apk& goto FATAL
::ECHO.检查blktool...
if not exist tool\Android\blktool (ECHOC {%c_e%}找不到blktool{%c_i%}{\n}& call log %logger% F 找不到blktool& goto FATAL) else (set blktool=%framwork_workspace%\tool\Android\blktool)
::ECHO.检查misc.img...
if not exist tool\Android\misc.img (ECHOC {%c_e%}找不到misc.img{%c_i%}{\n}& call log %logger% F 找不到misc.img& goto FATAL) else (set misc.img=%framwork_workspace%\tool\Android\misc.img)
::ECHO.检查mke2fs...
if not exist tool\Android\mke2fs (ECHOC {%c_e%}找不到mke2fs{%c_i%}{\n}& call log %logger% F 找不到mke2fs& goto FATAL) else (set mke2fs=%framwork_workspace%\tool\Android\mke2fs)
::ECHO.检查bootctl...
if not exist tool\Android\bootctl (ECHOC {%c_e%}找不到bootctl{%c_i%}{\n}& call log %logger% F 找不到bootctl& goto FATAL) else (set bootctl=%framwork_workspace%\tool\Android\bootctl)
::ECHO.检查sgdisk...
if not exist tool\Android\sgdisk (ECHOC {%c_e%}找不到sgdisk{%c_i%}{\n}& call log %logger% F 找不到sgdisk& goto FATAL) else (set sgdisk=%framwork_workspace%\tool\Android\sgdisk)
::ECHO.检查dmsetup...
if not exist tool\Android\dmsetup (ECHOC {%c_e%}找不到dmsetup{%c_i%}{\n}& call log %logger% F 找不到dmsetup& goto FATAL) else (set dmsetup=%framwork_workspace%\tool\Android\dmsetup)
::ECHO.检查busybox...
if not exist tool\Android\busybox (ECHOC {%c_e%}找不到busybox{%c_i%}{\n}& call log %logger% F 找不到busybox& goto FATAL) else (set busybox=%framwork_workspace%\tool\Android\busybox)
::ECHO.检查parted...
if not exist tool\Android\parted (ECHOC {%c_e%}找不到parted{%c_i%}{\n}& call log %logger% F 找不到parted& goto FATAL) else (set parted=%framwork_workspace%\tool\Android\parted)
call log framwork.bat-pre I 启动准备工作完成
goto :eof


:ADBPRE
SETLOCAL
set logger=framwork.bat-adbpre
::接收变量
set target_name=%var2%
if "%target_name%"=="" set target_name=all
if "%target_name%"=="all" call log %logger% I 接收变量:target_name:%target_name%& goto ADBPRE-1
SETLOCAL EnableDelayedExpansion & set target_path=!%target_name%!
ENDLOCAL & set target_path=%target_path%
call log %logger% I 接收变量:target_name:%target_name%.target_path:%target_path%
:ADBPRE-1
::检查模式
call chkdev all 1>nul
if not "%chkdev__all__mode%"=="system" (if not "%chkdev__all__mode%"=="recovery" ECHOC {%c_e%}设备模式错误, 只支持在系统或Recovery准备ADB环境. {%c_h%}按任意键重试...{%c_i%}{\n}& call log %logger% E 模式错误:%chkdev__all__mode%.应在系统或Recovery& pause>nul & ECHO.重试... & goto ADBPRE-1)
:ADBPRE-2
::系统下要检查Root
if "%chkdev__all__mode%"=="system" (
    call log %logger% I 开始检查Root
    echo.su>tmp\cmd.txt& echo.exit>>tmp\cmd.txt& echo.exit>>tmp\cmd.txt
    adb.exe shell < tmp\cmd.txt 1>>%logfile% 2>&1 || ECHOC {%c_e%}获取Root失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 获取Root失败&& pause>nul && ECHO.重试... && goto ADBPRE-2)
::推送和授权
if not "%target_name%"=="all" (
    call :adbpre-start %target_name% %target_path%
    ) else (
    call :adbpre-start blktool %blktool%
    call :adbpre-start mke2fs %mke2fs%
    call :adbpre-start bootctl %bootctl%
    call :adbpre-start sgdisk %sgdisk%
    call :adbpre-start dmsetup %dmsetup%
    call :adbpre-start busybox %busybox%
    call :adbpre-start parted %parted%)
ENDLOCAL
goto :eof
:adbpre-start
SETLOCAL
set target_name=%1& set target_path=%2
if "%chkdev__all__mode%"=="system" (
    call log %logger% I 开始推送%target_path%到./sdcard/%target_name%
    adb.exe push %target_path% ./sdcard/%target_name% 1>>%logfile% 2>&1 || ECHOC {%c_e%}推送%target_path%到./sdcard/%target_name%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 推送%target_path%到./sdcard/%target_name%失败&& pause>nul && ECHO.重试... && goto ADBPRE-2
    echo.su>tmp\cmd.txt& echo.mv ./sdcard/%target_name% ./data/local/tmp/%target_name% >>tmp\cmd.txt& echo.chmod +x ./data/local/tmp/%target_name% >>tmp\cmd.txt
    call log %logger% I 开始移动和授权%target_name%
    adb.exe shell < tmp\cmd.txt 1>>%logfile% 2>&1 || ECHOC {%c_e%}移动或授权%target_name%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 移动或授权%target_name%失败&& pause>nul && ECHO.重试... && goto ADBPRE-2)
if "%chkdev__all__mode%"=="recovery" (
    call log %logger% I 开始推送%target_path%到./%target_name%
    adb.exe push %target_path% ./%target_name% 1>>%logfile% 2>&1 || ECHOC {%c_e%}推送%target_path%到./%target_name%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 推送%target_path%到./%target_name%失败&& pause>nul && ECHO.重试... && goto ADBPRE-2
    call log %logger% I 开始授权%target_name%
    adb.exe shell chmod +x ./%target_name% 1>>%logfile% 2>&1 || ECHOC {%c_e%}授权%target_name%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 授权%target_name%失败&& pause>nul && ECHO.重试... && goto ADBPRE-2)
ENDLOCAL
goto :eof






:FATAL
ECHO. & if exist tool\Windows\ECHOC.exe (tool\Windows\ECHOC {%c_e%}抱歉, 脚本遇到问题, 无法继续运行. 请查看日志. {%c_h%}按任意键退出...{%c_i%}{\n}& pause>nul & EXIT) else (ECHO.抱歉, 脚本遇到问题, 无法继续运行. 按任意键退出...& pause>nul & EXIT)
