@echo off
setlocal enabledelayedexpansion

set "packPath=pack.path"
set "relativePath=\BepInEx\plugins\"


if exist "%packPath%" (

    set /p path=<"%packPath%"
) else (

    set /p "path=CSL2Path "
    echo !path!>"%packPath%"
)

if not exist "!path!\BepInEx" mkdir "!path!\BepInEx"


if not exist "!path!!relativePath!" mkdir "!path!!relativePath!"


for %%i in (pack_*.zip) do (
    move "%%i" "!path!" > nul
    set "fileName=%%~ni"
    
    REM 从文件名中提取目标文件夹名称
    for /f "tokens=2 delims=_" %%j in ("!fileName!") do (
        set "targetFolder=%%j"
        
        REM 替换文件夹名称中的非法字符
        set "targetFolder=!targetFolder:$=!"
        
       REM 检测文件名是否包含 BepInEx，如果是，则解压到path，不创建新文件夹
        if /i "!fileName:BepInEx=!" neq "!fileName!" (
            set "targetPath=!path!"
        ) else (
            REM 创建目标文件夹并解压缩（加上相对路径）
            set "targetPath=!path!!relativePath!!targetFolder!"
            mkdir "!targetPath!"
        )
        
        REM 使用PowerShell解压缩
        C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -Command "Expand-Archive -Path '!path!\%%i' -DestinationPath '!targetPath!'"

        REM 删除移动过去的压缩包
        del "!path!\%%i"
    )
)



endlocal

