@echo off
chcp 936 >nul 2>&1  ::解决中文乱码
setlocal enabledelayedexpansion
title 暗黑4一键反和谐
echo 暗黑4一键反和谐工具开始执行
echo.

:: 初始化暗黑4安装路径变量
set "d4Path="

:: 第一步：检查当前目录是否存在Diablo IV.exe
if exist "Diablo IV.exe" set "d4Path=%cd%"

:: 第二步：当前目录不存在时，读取注册表获取安装路径（优先64位视图）
if not defined d4Path (
    for /f "skip=2 tokens=3*" %%a in (
        'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Diablo IV" /v InstallLocation 2^>nul'
    ) do (
        set "regPath=%%a %%b"
        if exist "!regPath!\Diablo IV.exe" set "d4Path=!regPath!"
    )
    :: 64位视图未找到则尝试32位视图（WOW6432Node）
    if not defined d4Path (
        for /f "skip=2 tokens=3*" %%a in (
            'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Diablo IV" /v InstallLocation 2^>nul'
        ) do (
            set "regPath=%%a %%b"
            if exist "!regPath!\Diablo IV.exe" set "d4Path=!regPath!"
        )
    )
)

:: 第三步注册表未找到时，遍历C-Z盘下的可能安装路径
if not defined d4Path call :diskScan

:: 第四步：判断是否找到有效路径
if not defined d4Path goto :pathNotFound

echo.
echo ======================================
echo 你的暗黑4安装路径：!d4Path!
echo ======================================
echo.
cd /d "!d4Path!" || (echo 无法切换到暗黑4目录：!d4Path! & pause >nul & exit /b 1)

:: 关闭战网进程（避免退出战网时还原配置）
echo 正在关闭战网进程...
taskkill /f /im "Battle.net.exe" >nul 2>&1
taskkill /f /im "Agent.exe" >nul 2>&1
echo 战网进程已关闭

:: 创建WTF文件夹（忽略已存在的错误）
md WTF >nul 2>&1

:: 写入反和谐配置文件
echo SET OverrideArchive "0" > "WTF\Config.wtf"

:: 检查配置文件是否创建成功
if not exist "WTF\Config.wtf" (echo 无法创建Config.wtf文件，请以管理员身份运行本脚本！ & pause >nul & exit /b 1)

:: 自动配置战网启动参数
set "bnAutoSuccess=0"
powershell -NoProfile -ExecutionPolicy Bypass -Command "$cfg=\"$env:APPDATA\Battle.net\Battle.net.config\";if(Test-Path $cfg){try{$json=Get-Content $cfg -Raw -Encoding UTF8|ConvertFrom-Json;if($json.Games.fenris){$has=$json.Games.fenris.PSObject.Properties['AdditionalLaunchArguments'];if($has){$cur=$json.Games.fenris.AdditionalLaunchArguments;if($cur -notmatch '-enableagentmanager'){if(-not $cur){$cur=''};$json.Games.fenris.AdditionalLaunchArguments=$cur+' -enableagentmanager';$json|ConvertTo-Json -Depth 10|Set-Content $cfg -Encoding UTF8}}else{$json.Games.fenris|Add-Member -NotePropertyName 'AdditionalLaunchArguments' -NotePropertyValue ' -enableagentmanager' -Force;$json|ConvertTo-Json -Depth 10|Set-Content $cfg -Encoding UTF8};exit 0}}catch{}};exit 1"
if !errorlevel! equ 0 set "bnAutoSuccess=1"

:: 显示结果提示
if "!bnAutoSuccess!"=="1" goto :showAutoSuccess
goto :showManualHint

:showAutoSuccess
echo.
echo --------------------
echo 反和谐文件配置完成！
echo --------------------
echo.
echo --------------------------------
echo 请重新运行战网使反和谐功能起效！
echo --------------------------------
echo.
echo ----------------------------
echo 工具来源：www.d4hud.com
echo ----------------------------
goto :done

:showManualHint
echo.
echo --------------------
echo 反和谐文件配置完成！
echo --------------------
echo 重要注意事项：
echo 1. 第一次运行需确保启动参数中包含 -enableagentmanager
echo 2. 启动参数位置：战网客户端→暗黑4→选项→游戏设置→额外启动参数
echo 3. 若配置无效，可尝试在执行本脚本前关闭所有战网进程
echo ----------------------------
echo 工具来源：www.d4hud.com
echo ----------------------------
goto :done

:pathNotFound
echo 未检测到暗黑4安装目录，请将本bat文件手动复制到暗黑4安装目录（包含Diablo IV.exe的文件夹）后重新执行！
goto :done

:: ===== 磁盘扫描子程序（标签必须在if块外部） =====
:diskScan
set "candidatePaths=Diablo IV;Blizzard\Diablo IV;游戏\Diablo IV;game\Diablo IV;暴雪游戏\Diablo IV;战网游戏\Diablo IV;单机游戏\Diablo IV;网络游戏\Diablo IV"
set "tempPaths=!candidatePaths!"
:pathLoop
if not defined tempPaths goto :eof
for /f "delims=; tokens=1*" %%p in ("!tempPaths!") do (
    set "currentPath=%%p"
    set "tempPaths=%%q"
)
for %%d in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    if exist "%%d:\" (
        set "fullCheckPath=%%d:\!currentPath!"
        if exist "!fullCheckPath!\Diablo IV.exe" (
            set "d4Path=!fullCheckPath!"
            goto :eof
        )
    )
)
goto :pathLoop

:done
pause >nul
endlocal