%tishi2%
echo - 请选择您的设备状态
echo.
set gn=
echo - 1.系统/rec/TWRP模式下        2.FASTBOOT模式下
echo.
echo.
set /p gn=%t2%
    if "!gn!"=="2" ( echo - 如果无法自动重启到系统模式请手动进入 & call bat\f2.bat )
    echo.
    %awfd%

goto Extracting_boot


:next
    echo - img=!img!
    adb shell rm -rf %TMPDIR1%/boot.img
    adb push !img! %TMPDIR1%/boot.img
    adb shell sh %dir%/run.sh %version% %maskVC%
    if not "%errorlevel%"=="0" ( abort 修补boot失败 )
    adb pull %TMPDIR1%/magisk_patched.img tmp\magisk_patched.img
    if not exist tmp\magisk_patched.img ( abort 从设备读取文件失败 )
    adb shell rm -rf %TMPDIR1%/^*.img

    echo - 修补boot成功已成功注入Magisk到tmp/magisk_patched.img
    echo.
    echo.
    echo - 开始一键ROOT……
    call bat\a3.bat
    echo - 等待设备连接到fastboot
    call bat\f1.bat
    set gn=
    echo.
    echo.
    echo - 您的设备是AB设备？是否现在查看当前激活分区？
    echo.
    echo.
    set /p gn=1.为确定查看    回车和任意内容为跳过
    if "!gn!"=="1" (
        echo - 查看当前使用的分区
        fastboot getvar current-slot
    )


:fboot
    set gn=
    echo.
    echo - 选择你要刷入到的分区
    echo - 0.boot      1.boot_a      2.boot_b      3.boot_a和boot_b
    echo.
    echo.
    set /p gn=%t2%
    if not defined gn ( goto fboot )
    if "!gn!"=="0" ( fastboot flash boot tmp/magisk_patched.img & goto jixu )
    if "!gn!"=="1" ( fastboot flash boot_a tmp/magisk_patched.img & goto jixu )
    if "!gn!"=="2" ( fastboot flash boot_b tmp/magisk_patched.img & goto jixu )
    if "!gn!"=="3" ( fastboot flash boot_ab tmp/magisk_patched.img & goto jixu )
    echo - 输入错误请重新输入
    goto fboot


:jixu
    set gn=
    echo - 您的设备是否是联发科设备？
    echo - 禁用AVB2.0的启动校验和DM校验否则会无法开机，当然有的高通机型也有可能需要禁用(小米高通机型不用此操作请跳过)
    echo.
    echo.
    set /p gn=输入1为确定     回车或任意内容为跳过：
        if "!gn!"=="1" ( call bat\f11.bat )
        set gn=
        echo.
        echo.
        set /p gn=输入1为确定重启     回车或任意内容为跳过：
                if "!gn!"=="1" (
                    call bat\f2.bat
                )
                    set gn=
                    echo - 是否现在安装%magisk_vn%？
                    echo.
                    echo.
                    set /p gn=输入1为确定安装     回车或任意内容为跳过：
                        if "!gn!"=="1" (
                            %awfd%
                            echo - 请确保手机已进入系统，部分系统需要在手机上确定才能安装
                            pause
                            adb install -r %magisk_file%
                            echo.
                            echo - 如果上面这行出现了 Success 就说明安装成功了
                        )
                            abort


:Extracting_binboot
    echo - 检测pladload.bin状态
    set num=
    for /f "delims=" %%f in ('payload_dumper -l !file! ^| busybox awk "/^boot$/{print $NF}"') do set num=%%f
        if defined num (
            echo - 开始解压!num!
            payload_dumper -d boot !file!
            move /Y boot.img tmp
        ) else (
            goto Unknown
        )
        exit /B 0


:Extracting_boot
    %rdtmp%
    echo - 本功能可以直接选择tgz/zip文件，自动提取出payload.bin和boot.img
    echo - 当然如果你已经提取出boot.img或者playload.bin也可以直接选择boot.img或者playload.bin
    %tishi1% 选择的tgz、zip、boot.img、playload.bin
    echo.
    set file=
    set /p file=%t1%
    if not defined file ( cls & %error% 未选择文件，刷个鸡儿 & goto Extracting_boot )
    if not exist !file! ( cls &  %error% 选择的文件不存在，刷个鸡儿 & goto Extracting_boot )

    set num=
    set img=
    %suffix% !file!
    if "%suffix2%"==".tgz" (
        for /f "delims=" %%f in ('busybox tar tzvf !file! ^| busybox awk "/boot.img/{print $NF}" ^| busybox tail -1') do set num=%%f
        if defined num (
            echo - 开始解压!num!
            busybox tar xzvf !file! !num! -C tmp
            goto imgfile
        ) else (
            goto Unknown
        )
    )

    if "%suffix2%"==".zip" (
        for /f "delims=" %%f in ('busybox unzip -l !file! ^| busybox awk "/boot.img/{print $NF}" ^| busybox tail -1') do set num=%%f
        if not defined num (
            for /f "delims=" %%f in ('busybox unzip -l !file! ^| busybox awk "/payload.bin/{print $NF}" ^| busybox tail -1') do set num=%%f
        )
            if defined num (
                echo - 开始解压!num!
                busybox unzip -o !file! !num! -d tmp

                if /I "!num:~-11!"=="payload.bin" (
                     set file=tmp\payload.bin
                     call :Extracting_binboot
                )
                goto imgfile
            ) else (
                goto Unknown
            )
    )

    if "%suffix2%"==".bin" (
        call :Extracting_binboot
        goto imgfile
    )

    if "%suffix2%"==".img" (
        set img=!file!
        goto next
    )

    cls & %error% 未知的文件格式%suffix2% & goto Extracting_boot


:imgfile
    for /R tmp %%i in (boot.img) do ( set img=%%i )
    if not exist !img! (
        %error% 提取boot.img错误，错误详情：
        echo.
        dir tmp
    ) else (
        goto next
    )


:Unknown
    cls
    %error% 未从!file!中找到boot.img，请拖入正确的文件
    goto Extracting_boot
