@echo off
if /i "%UserName%" == "SYSTEM" (Goto GotAdmin) else (reg query "HKLM\SYSTEM\ControlSet001\Control\MiniNT" 1>nul 2>nul&&Goto GotAdmin)
:BatchGotAdmin
Set _Args=&Set Args=%*
if `%1` neq `` Set "_Args=%Args:"=""%"
if exist %WinDir%\System32\fltMC.exe fltMC 1>nul 2>nul||mshta VBScript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c """"%~f0"" %_Args%""",,"runas",1)(Window.Close) 2>nul&&Exit /b

:GotAdmin
REM 初始化
Pushd "%CD%"&cd /d "%~dp0"
find /? 1>nul 2>nul&&Set "findstr=find /i "||Set findstr=
findstr /? 1>nul 2>nul&&Set "findstr=findstr /i /c:"
if not defined findstr Call :EchoX "cf.: 找不到 find.exe 文件。"&Call :Delay 5 +&Exit /b
reg /? 1>nul 2>nul||(Call :EchoX "cf.: 找不到 reg.exe 文件。"&Call :Delay 5 +&Exit /b)
xcopy /? 1>nul 2>nul||(Call :EchoX "cf.: 找不到 xcopy.exe 文件。"&Call :Delay 5 +&Exit /b)
if not exist "%WinDir%\System32\regsvr32.exe" Call :EchoX "cf.: 找不到 regsvr32.exe 文件。"&Call :Delay 5 +&Exit /b
if not defined CommonProgramW6432 Cls&Call :EchoX "cf:: 该应用，仅支持 64 位操作系统！"&Call :Delay 5&Exit /b
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Startup" 2^>nul') do Set "Startup=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Startup" 2^>nul') do Set "CStartup=%%~j"
Set NirCmd=&if exist "_Green\_NirCmd.exe" for /f %%i in ('"_Green\_NirCmd.exe" consolewrite ok 2^>nul') do Set "NirCmd=_Green\_NirCmd.exe"
tskill /? 1>nul 2>nul&&Set "TsKill=tskill"||if exist "_Green\_TsKill.exe" (Set "TsKill=_Green\_TsKill.exe") else Set "TsKill=Pecmd KILL"
regini /? 1>nul 2>nul&&Set "RegIni=regini"||if exist "_Green\_RegIni.exe" (Set "RegIni=_Green\_RegIni.exe") else Set "RegIni=regini"
Set ver=0&for /f "tokens=2 delims=[]" %%a in ('ver') do for /f "tokens=2-4 delims=. " %%b in ("%%~a") do Set "ver=%%~b%%~c"
Set "mklink=mklink /j"    :: 目录联接 (仅支持本地路径和NTFS分区)
:: Set "mklink=mklink /d" :: 符号链接 (+支持网络路径和FAT32分区)
REM 判断输入参数
Set hint=&Set choice=
if /i "%~1" == "install" Goto Install
if /i "%~1" == "uninsta" Goto UnInsta
if /i "%~1" == "clcache" Goto ClCache
if /i "%~1" == "elevate" Goto Elevate
if /i "%~1" == "smthupg" Goto SmthUpg

:Start
Title &ver|%findstr%" 5." 1>nul 2>nul&&Title Directory Opus Pro&(if exist %WinDir%\System32\ureg.dll Mode 37,20 2>nul)&Color 2f
Set caption=☆ Directory Opus Pro ☆
Set hint=启用提示
Set err=0

:Menu
Cls&Call :EchoX "标题:「 " "2e::%caption%" "xx.: 」"
echo ------------------------------------
echo 菜单:
Call :EchoX "        " "f2::①" "  绿化安装          " "9f.:√"
echo.
Call :EchoX "        " "f2::②" "  清理卸载          " "8f.:メ"
echo.
Call :EchoX "        " "f2::③" "  清空缓存          " "e0.:Ｃ"
echo.
Call :EchoX "        " "f2::④" "  提升权限          " "5f.:Ａ"
echo.
Call :EchoX "        " "f2::⑤" "  平滑升级          " "f5.:Ｓ"
echo.
Call :EchoX "        " "f2::⑥" "  退出              " "cf.:Ｑ"
echo.
echo ------------------------------------
if %err% gtr 0 Call :EchoX "cf::输入无效请重新输入！" "cf.:(№%err%)"
Set choice=&Set /p choice=选择: ||Goto Menu
Set "choice=%choice:"=%"
Set "choice=%choice: =%"
if /i "%choice%" == "1" Cls&Goto Install
if /i "%choice%" == "i" Cls&Goto Install
if /i "%choice%" == "2" Cls&Goto UnInsta
if /i "%choice%" == "u" Cls&Goto UnInsta
if /i "%choice%" == "3" Cls&Goto ClCache
if /i "%choice%" == "c" Cls&Goto ClCache
if /i "%choice%" == "4" Cls&Goto Elevate
if /i "%choice%" == "a" Cls&Goto Elevate
if /i "%choice%" == "5" Cls&Goto SmthUpg
if /i "%choice%" == "s" Cls&Goto SmthUpg
if /i "%choice%" == "6" Popd&Exit
if /i "%choice%" == "q" Popd&Exit
Set /a err+=1&Goto Menu

:Install
if defined hint Call :EchoX "9f.:正在绿化安装，请稍候 。。。"
REM 注册组件
Title 10%%
reg import _Green\Add.reg 1>nul 2>nul&Call _Green\Add.bat 1>nul 2>nul
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do if not exist "%%~i" (ren "%%~i.bak" "%%~nxi" 1>nul 2>nul) else del /f /q "%%~i.bak" 1>nul 2>nul
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll") do regsvr32 /s "%%~i" 1>nul 2>nul
REM 替换资源管理器
Title 20%%
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks" /f /v "{3CF9ECE0-1A9F-11d2-8C73-00C06C2005DE}" /t REG_SZ /d "" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" /f /v "EnableShellExecuteHooks" /t REG_DWORD /d 1 1>nul 2>nul
REM 修复 Win11 中 DOpus 导致的 Chrome 闪退
:: reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks" /f /v "{3CF9ECE0-1A9F-11d2-8C73-00C06C2005DE}" 1>nul 2>nul
:: reg delete "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks" /f /v "{3CF9ECE0-1A9F-11d2-8C73-00C06C2005DE}" 1>nul 2>nul
REM Win10+ 开启 "在 Windows 中显示动画"，降低闪屏概率 (注销生效)
:: reg add "HKCU\Control Panel\Desktop\WindowMetrics" /f /v "MinAnimate" /t REG_SZ /d "1" 1>nul 2>nul
REM 添加资管文件夹空白处右键菜单
Set "DP=%~dp0"
Set "DP=%DP:&=^&%"
reg add "HKLM\SOFTWARE\Classes\Directory\Background\Shell\OpenInDopus" /f /ve /t REG_SZ /d "在 DOpus 中打开(&O)" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\Directory\Background\Shell\OpenInDopus" /f /v "Icon" /t REG_SZ /d "%~dp0dopusrt.exe,0" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\Directory\Background\Shell\OpenInDopus\Command" /f /ve /t REG_SZ /d "\"%DP%dopusrt.exe\" /cmd go newtab=deflister tofront \"%%V\"" 1>nul 2>nul
REM 集成视频设备源码预览
Title 30%%
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /v "%~dp0_Green\视频设备\ECap.exe" /t REG_SZ /d "~ HIGHDPIAWARE" 1>nul 2>nul
if %ver% gtr 63 Call "_Green\源码预览\SourcePreviewHandler.bat" install 2>nul&cd /d "%~dp0"
REM 7-Zip 界面配置
reg import "%~dp0_Green\7z极限压缩\bin\x64\7zCfg.reg" 1>nul 2>nul
Set notepad=%WinDir%\System32\notepad.exe
if not exist "%notepad%" for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\notepad.exe" /ve 2^>nul') do Set "notepad=%%~j"
if exist "%notepad%" (
    reg add "HKCU\SOFTWARE\7-Zip\FM" /f /v "Editor" /t REG_SZ /d "%notepad%" 1>nul 2>nul
    reg add "HKCU\SOFTWARE\7-Zip\FM" /f /v "Viewer" /t REG_SZ /d "%notepad%" 1>nul 2>nul
    reg add "HKU\.DEFAULT\SOFTWARE\7-Zip\FM" /f /v "Editor" /t REG_SZ /d "%notepad%" 1>nul 2>nul
    reg add "HKU\.DEFAULT\SOFTWARE\7-Zip\FM" /f /v "Viewer" /t REG_SZ /d "%notepad%" 1>nul 2>nul
)
REM 恢复配置文件
Title 40%%
(for %%i in (5 s) do if /i "%choice%" == "%%~i" Goto InstCert)&(if /i "%~1" == "smthupg" Goto InstCert)&if not exist _Data Goto InstCert
Set "err_n=0" :: 符号链接，要求系统分区必须是 NTFS 分区
mkdir "_Data\Directory Opus" 2>nul&mkdir "_Data\State Data" 2>nul
rd /s /q "%AppData%\GPSoftware\Directory Opus" 1>nul 2>nul&rd /s /q "%LocalAppData%\GPSoftware\Directory Opus" 1>nul 2>nul
mkdir "%AppData%\GPSoftware" 1>nul 2>nul&%mklink% "%AppData%\GPSoftware\Directory Opus" "%~dp0_Data\Directory Opus" 1>nul 2>nul||Set /a err_n+=1
mkdir "%LocalAppData%\GPSoftware\Directory Opus" 1>nul 2>nul&%mklink% "%LocalAppData%\GPSoftware\Directory Opus\State Data" "%~dp0_Data\State Data" 1>nul 2>nul||Set /a err_n+=1
if %err_n% gtr 0 ( :: 若映射失败，则导入 _Data 配置文件到系统默认路径
    rd /s /q "%AppData%\GPSoftware\Directory Opus" 1>nul 2>nul&xcopy /e /h /r /y "_Data\Directory Opus" "%AppData%\GPSoftware\Directory Opus\" 1>nul 2>nul
    rd /s /q "%LocalAppData%\GPSoftware\Directory Opus\State Data" 1>nul 2>nul&xcopy /e /h /r /y "_Data\State Data" "%LocalAppData%\GPSoftware\Directory Opus\State Data\" 1>nul 2>nul
)
:InstCert
REM 安装授权证书
Title 50%%
assoc .=. 1>nul 2>nul&reg delete "HKCR\." /f 1>nul 2>nul
rd /s /q "%AllUsersProfile%\GPSoftware\Directory Opus" 1>nul 2>nul
if exist dopus_patcher.exe (
    if exist dsrole.dll (copy /b /y dsrole.dll + nul dsrole.dll 1>nul 2>nul||(echo.&Call :EchoX "cf.:dsrole.dll 被占用，" "cf.:按任意键，重启资源管理器 。。。"&Pause >nul&%TsKill% explorer 1>nul 2>nul))
    @start "" /min CScript //nologo _Green\FirstRun.vbs "Question" " " 40 1>nul 2>nul&Call dopus_patcher.exe 1>nul 2>nul&del /q dopus.exe.bak 1>nul 2>nul&del /q cert_file.txt 1>nul 2>nul
) else (
    for /f "tokens=1,* delims==" %%i in (_Green\授权证书.OpusCert) do if /i "%%~i" == "RegistrationCode" dopus.exe /cert _Green\授权证书.OpusCert /regcode "%%~j" /norun 1>nul 2>nul
    for %%i in ("%AllUsersProfile%\GPSoftware\Directory Opus\dopus.cert") do if "%%~zi" == "0" del /f /q "%AllUsersProfile%\GPSoftware\Directory Opus\dopus.cert" 1>nul 2>nul
    if not exist "%AllUsersProfile%\GPSoftware\Directory Opus\dopus.cert" _Green\授权证书.OpusCert&Call _Green\FirstRun.vbs "Directory Opus" " " 40
)
Title 65%%
%TsKill% dopus 1>nul 2>nul
%TsKill% dopusrt 1>nul 2>nul
:: @start "" dopus.exe NOAUTOLISTER STARTUP 1>nul 2>nul
@start "" dopus.exe /ignoresetup autolister=layout 1>nul 2>nul
Title 80%%
REM 添加开始菜单启动项
Shortcut.exe 1>nul 2>nul&&Shortcut.exe "%Startup%\Directory Opus (启动).lnk" "%~dp0dopus.exe" "NOAUTOLISTER STARTUP" "%~dp0dopus.exe" 0 nor "" "GP Software Directory Opus" 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""Startup"")&""\Directory Opus (启动).lnk""):b.TargetPath=""%~dp0dopus.exe"":b.Arguments=""NOAUTOLISTER STARTUP"":b.WorkingDirectory=""%~dp0"":b.IconLocation=""%~dp0dopus.exe,0"":b.Description=""GP Software Directory Opus"":b.Save:Close") 2>nul
REM 创建桌面快捷方式
Shortcut.exe 1>nul 2>nul&&Shortcut.exe "$desktop\Directory Opus.lnk" "%~dp0dopus.exe" "" "%~dp0dopus.exe" 0 nor "" "GP Software Directory Opus" 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""Desktop"")&""\Directory Opus.lnk""):b.TargetPath=""%~dp0dopus.exe"":b.Arguments="""":b.WorkingDirectory=""%~dp0"":b.IconLocation=""%~dp0dopus.exe,0"":b.Description=""GP Software Directory Opus"":b.Save:Close") 2>nul
REM 删除旧版残留文件
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do del /f /q /a "%%~i.old*" 1>nul 2>nul
REM 刷新组策略，使 Win+E 及时生效 (执行 GPUpdate 耗时5秒，不需要可删除)
Title 90%%
if defined NirCmd ("%NirCmd%" SysRefresh Policy 1>nul 2>nul) else GPUpdate /Target:Computer /Force 1>nul 2>nul
Title 100%%
(for %%i in (1 i) do if /i "%choice%" == "%%~i" Goto End)&if /i "%~1" == "install" (Goto End) else Goto :eof

:UnInsta
if defined hint Call :EchoX "8f.:正在清理卸载，请稍候 。。。"
Title 10%%
for %%i in ("dopus" "dopusrt" "dowshlp" "d8viewer" "docsvw32" "docsvw64") do %TsKill% "%%~i" 1>nul 2>nul
REM 清理授权信息
Title 20%%
Set VH=&Set VE=
Setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%i in ('reg query "HKCU\SOFTWARE\Classes" /k /f * 2^>nul') do (
    REM reg query "%%~i" /d /f * 1>nul 2>nul&&(
        reg query "%%~i" /k /f * 1>nul 2>nul||(
            Set n=0&for /f "tokens=2* delims= " %%l in ('reg query "%%~i" /t REG_BINARY 2^>nul') do if /i "%%~l" == "REG_BINARY" Set /a n+=1
            if !n! geq 4 if not defined VH (Set "VH=%%~i") else Set "VE=%%~i"
        )
    REM )
)
Endlocal&Set VH=%VH%&Set VE=%VE%
Title 30%%
Set CP=&for /f "delims=" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\UserAssist" /k /f "CP-" 2^>nul') do if not defined CP Set "CP=%%~i"
Set JVA=&for /f "delims=" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\UserAssist" /k /f "JVA-" 2^>nul') do if not defined JVA Set "JVA=%%~i"
(if /i "%CP:~0,4%" neq "HKEY" Set CP=)&(if /i "%JVA:~0,4%" neq "HKEY" Set JVA=)
if defined JVA (
    _Green\_SetACL64 -on "%JVA%" -ot reg -actn setowner -ownr "n:Administrators" -rec yes -silent
    _Green\_SetACL64 -on "%JVA%" -ot reg -actn ace -ace "n:Administrators;p:full;m:grant;i:so,sc" -op DACL:p_c -rec yes -silent
)
Title 40%%
(echo HKEY_CURRENT_USER\SOFTWARE\Classes\catcatcatcat [1 7 17]
 echo HKEY_CURRENT_USER\SOFTWARE\Classes\dogdogdogdog [1 7 17]
 echo HKEY_CURRENT_USER\Control Panel\International\Time [1 7 17]
 echo HKEY_CURRENT_USER\SOFTWARE\Microsoft\Metro\AppCompat [1 7 17]
 echo HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\DlgInfo [1 7 17]
 echo HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Disallowed [1 7 17]
 echo HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\WinTrust\Trust Handlers [1 7 17]
 echo HKEY_LOCAL_MACHINE\SOFTWARE\GPSoftware\Directory Opus\Config\System\Data [1 7 17]
 echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DesktopInterfaceMethod [1 7 17]
 if defined VH echo %VH% [1 7 17]
 if defined VE echo %VE% [1 7 17]
 if defined CP echo %CP% [1 7 17]
 if defined JVA echo %JVA% [1 7 17]) >"%TEMP%\RegFullAcc.ini"
%RegIni% "%TEMP%\RegFullAcc.ini" 1>nul 2>nul&del /q "%TEMP%\RegFullAcc.ini" 1>nul 2>nul
reg delete "%VH%" /f 1>nul 2>nul&reg delete "%VE%" /f 1>nul 2>nul&reg delete "%CP%" /f 1>nul 2>nul&reg delete "%JVA%" /f 1>nul 2>nul
REM 清理注册表项
Title 50%%
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do regsvr32 /s /u "%%~i" 1>nul 2>nul&del /f /q /a "%%~i.old*" 1>nul 2>nul&if exist "%%~i" del /f /q /a "%%~i.bak" 1>nul 2>nul
reg import _Green\Del.reg 1>nul 2>nul
Title 60%%
Call :DelReg "HKCU\Control Panel\International\Time" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Metro\AppCompat" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Metro" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\ShellNoRoam" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\DlgInfo" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Disallowed" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\WinTrust\Trust Handlers" "subReg"
Call :DelReg "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DesktopInterfaceMethod" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Search\JumplistData" "subReg"
Call :DelReg "HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Font Management" "subReg"
Call :DelReg "HKCU\SOFTWARE\Classes\*\ShellEx\ContextMenuHandlers" "subReg"
Call :DelReg "HKCU\SOFTWARE\Classes\*\ShellEx" "subReg"
Call :DelReg "HKCU\SOFTWARE\GPSoftware" "Directory Opus"
Call :DelReg "HKLM\SOFTWARE\GPSoftware" "Directory Opus"
reg query "HKCU\SOFTWARE\Classes\*" /s 2>nul|%findstr%"REG_" 1>nul 2>nul||reg delete "HKCU\SOFTWARE\Classes\*" /f 1>nul 2>nul
reg query "HKCU\SOFTWARE\Classes\Directory" /s 2>nul|%findstr%"REG_" 1>nul 2>nul||reg delete "HKCU\SOFTWARE\Classes\Directory" /f 1>nul 2>nul
reg query "HKCU\SOFTWARE\Classes\Folder" /s 2>nul|%findstr%"REG_" 1>nul 2>nul||reg delete "HKCU\SOFTWARE\Classes\Folder" /f 1>nul 2>nul
reg query "HKCU\SOFTWARE\Classes\Ftp" /s 2>nul|%findstr%"REG_" 1>nul 2>nul||reg delete "HKCU\SOFTWARE\Classes\Ftp" /f 1>nul 2>nul
Title 70%%
Setlocal EnableDelayedExpansion
for /f "delims=*" %%k in ('reg query "HKLM\SOFTWARE\Classes\CLSID" /f "dopuslib.dll" /s^|%findstr%"CLSID"') do (Set "key=%%~k"&reg delete "!key:~0,-15!" /f 1>nul 2>nul)
Endlocal
REM 删除资管文件夹空白处右键菜单
for %%i in ("HKCU" "HKLM") do reg delete "%%~i\SOFTWARE\Classes\Directory\Background\Shell\OpenInDopus" /f 1>nul 2>nul
REM 清理视频设备源码预览
reg delete "HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /v "%~dp0_Green\视频设备\ECap.exe" 1>nul 2>nul
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /v "%~dp0_Green\视频设备\ECap.exe" 1>nul 2>nul
if %ver% gtr 63 Call "_Green\源码预览\SourcePreviewHandler.bat" uninsta 2>nul&cd /d "%~dp0"
REM 清理配置文件
Title 80%%
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common AppData" 2^>nul') do Set "CAppData=%%~j"
rd /s /q "%CAppData%\_Data" 1>nul 2>nul&rd /s /q "%CAppData%\_Green" 1>nul 2>nul&rd /s /q "%CAppData%\视频设备" 1>nul 2>nul
rd /s /q "%CAppData%\GPSoftware\Directory Opus" 1>nul 2>nul&rmdir "%CAppData%\GPSoftware" 1>nul 2>nul
rd /s /q "%AppData%\GPSoftware\Directory Opus" 1>nul 2>nul&rmdir "%AppData%\GPSoftware" 1>nul 2>nul
rd /s /q "%AppData%\..\Local\GPSoftware\Directory Opus" 1>nul 2>nul&rmdir "%AppData%\..\Local\GPSoftware" 1>nul 2>nul
rd /s /q "%LocalAppData%\GPSoftware\Directory Opus" 1>nul 2>nul&rmdir "%LocalAppData%\GPSoftware" 1>nul 2>nul
rd /s /q "%WinDir%\System32\inf32" 1>nul 2>nul
del /q /a "%CAppData%\sdpsenv.dat" 1>nul 2>nul
del /q /a "%WinDir%\System32\argtmp39.dll" 1>nul 2>nul
del /q /a "%WinDir%\xpcc37.log" 1>nul 2>nul
del /q /a "%~dp0_Data\Directory Opus\dopus.dat" 1>nul 2>nul
for %%i in ("dopus.dat" "dopus.exe.bak" "cert_file.txt") do del /f /q /a "%%~i" 1>nul 2>nul
SchTasks /delete /f /tn "DOpusRT_RunStd_{0B3F6B75-C530-417C-8E60-BB2B81794BAD}" 1>nul 2>nul
REM 删除开始菜单启动项
Title 90%%
del /q "%Startup%\Directory Opus (启动).lnk" 1>nul 2>nul&del /q "%CStartup%\Directory Opus (启动).lnk" 1>nul 2>nul
REM 删除快捷方式
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Programs" 2^>nul') do Set "Programs=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Programs" 2^>nul') do Set "CPrograms=%%~j"
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop" 2^>nul') do Set "Desktop=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop" 2^>nul') do Set "CDesktop=%%~j"
rd /s /q "%Programs%\GPSoftware" 1>nul 2>nul&rd /s /q "%CPrograms%\GPSoftware" 1>nul 2>nul
del /q "%Desktop%\Directory Opus.lnk" 1>nul 2>nul&del /q "%CDesktop%\Directory Opus.lnk" 1>nul 2>nul
Title 100%%
(for %%i in (2 u) do if /i "%choice%" == "%%~i" Goto End)&if /i "%~1" == "uninsta" (Goto End) else Goto :eof

:ClCache
if defined hint Call :EchoX "e0.:正在清空缓存，请稍候 。。。"
rd /s /q "%AppData%\GPSoftware\Directory Opus\Icon Cache Roaming" 1>nul 2>nul
for %%i in ("Help Cache" "Icon Cache" "Thumbnail Cache") do rd /s /q "%LocalAppData%\GPSoftware\Directory Opus\%%~i" 1>nul 2>nul
Cls&Call :EchoX "cf.: 缓存已清空，按任意键重启 DO ！"&Pause >nul
%TsKill% dopus 1>nul 2>nul&%TsKill% dopusrt 1>nul 2>nul
:: del /q "%~dp0清空缓存.lnk" 2>nul
:: Shortcut.exe 1>nul 2>nul&&Shortcut.exe "%~dp0清空缓存.lnk" "%~0" "clcache" "%~dp0dopus.exe" 0 nor "" "" 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%~dp0清空缓存.lnk""):b.TargetPath=""%~0"":b.Arguments=""clcache"":b.WorkingDirectory=""%~dp0"":b.IconLocation=""%~dp0dopus.exe,0"":b.Save:Close") 2>nul
@start "" dopus.exe 1>nul 2>nul
Goto End

:Elevate
if defined hint Call :EchoX "5f.:正在提升权限，请稍候 。。。"
REM 取消用于内置管理员帐户的管理员批准模式 -> 提升普通管理员权限
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f /v "EnableLUA" /t REG_DWORD /d "0" 1>nul 2>nul
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f /v "FilterAdministratorToken" 1>nul 2>nul
:: reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f /v "EnableLUA" /t REG_DWORD /d "1" 1>nul 2>nul
:: reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f /v "FilterAdministratorToken" /t REG_DWORD /d "0" 1>nul 2>nul
Set choice=4&Set hint=启用提示&Goto End

:SmthUpg
if defined hint Call :EchoX "f5.:正在平滑升级，请稍候 。。。"
Set DOPath=&for /f "tokens=2*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\DOpus.exe" /v "Path" 2^>nul') do Set "DOPath=%%~j"
cd /d "%DOPath%" 2>nul&&cd /d "%~dp0"||(Cls&Call :EchoX "cf.: 找不到已安装版本的路径！" "cf.: 按任意键退出 。。。"&Pause >nul&Exit /b)
if not exist "%AppData%\GPSoftware\Directory Opus" (Cls&Call :EchoX "cf.: 找不到已安装版本的配置路径！" "cf.: 按任意键退出 。。。"&Pause >nul&Exit /b)
if "%DOPath:~-1%" == "\" Set "DOPath=%DOPath:~0,-1%"
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do copy /y "%%~i" "%DOPath%\%%~i.bak" 1>nul 2>nul
for %%i in ("dopus" "dopusrt" "dowshlp" "d8viewer" "docsvw32" "docsvw64") do %TsKill% "%%~i" 1>nul 2>nul
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do regsvr32 /s /u "%DOPath%\%%~i" 1>nul 2>nul&del /f /q /a "%DOPath%\%%~i.old*" 1>nul 2>nul
reg import "%DOPath%\_Green\Del.reg" 1>nul 2>nul
if /i "%DOPath%\" == "%~dp0" Set hint=&Call :Install&Set hint=%hint%&Call :UMsg&Goto End
:: if "%DOPath:~3,1%" == "" (Cls&Call :EchoX "cf.: 旧版安装在根路径，不支持平滑升级，" "cf.: 按任意键退出！"&Pause >nul&Exit /b)
:: for %%i in ("HKCU" "HKLM") do for /f "tokens=2*" %%j in ('reg query "%%~i\SOFTWARE\Classes\Folder\Shell" /ve 2^>nul') do if /i "%%~k" == "openindopus" reg delete "%%~i\SOFTWARE\Classes\Folder\Shell" /f /ve 1>nul 2>nul
:: for /f "delims=" %%i in ('dir /a /b "%DOPath%"') do if /i "%%~i" neq "_Data" if /i "%%~i" neq "_Green" if /i "%%~i" neq "_Backup" rd /s /q "%DOPath%\%%~i" 1>nul 2>nul&del /f /q /a "%DOPath%\%%~i" 1>nul 2>nul
for %%i in ("_dopus_patcher.exe" "dopus_patcher.exe" "dsrole.dll") do del /f /q /a "%DOPath%\%%~i" 1>nul 2>nul
for %%i in ("dopuslib.dll" "dopuslib32.dll" "dopushlp.dll" "Language\english.dll") do del /f /q /a "%DOPath%\%%~i" 1>nul 2>nul&if exist "%DOPath%\%%~i" ren "%DOPath%\%%~i" "%%~nxi.old%random:~-1%%random:~-1%%random:~-1%" 1>nul 2>nul
ren _Data _Data_ 1>nul 2>nul&xcopy /e /h /r /y "%CD%" "%DOPath%\" 1>nul 2>nul&ren _Data_ _Data 1>nul 2>nul&rd /s /q "%DOPath%\_Data_" 1>nul 2>nul
Set hint=&Call "%DOPath%\dopus.bat" Install&Set hint=%hint%&Call :UMsg&Goto End

:End
cd.&if defined choice (
    Cls&if defined hint Call :EchoX "2e.:操作完成 ！！"&if /i "%choice%" neq "4" (Call :Delay 2 +) else Call :EchoX "         <<" "cf::建议重启电脑。" "xx::>>"&Call :Delay 8&Exit /b
    Goto Start
) else (Set hint=&Exit /b)

:UMsg
if defined NirCmd @start "" "%NirCmd%" infobox "升级完成！安装路径：↓~n%DOPath%~n~n☆ 若升级文件无用，可手动删除 ☆" "[ 升级旧版 ]" 1>nul 2>nul&Goto :eof
(echo 升级完成！安装路径：↓&echo "%DOPath%"&echo.&echo ☆ 若升级文件无用，可手动删除 ☆)|msg * /time 15&Goto :eof

:DelReg :: 删除注册表项 (父项路径\子项)，删除后父项无任何子项则也删除 <%1=PReg|父项路径> <%2=SReg|子项>。
Set "PReg=%~1"&Set "SReg=%~2"
reg delete "%PReg%\%SReg%" /f 1>nul 2>nul
ver|%findstr%" 5." 1>nul 2>nul&&Set n=-2||Set n=0
for /f "delims=" %%i in ('reg query "%PReg%" /s 2^>nul') do Set /a n+=1
if %n% lss 1 reg delete "%PReg%" /f 1>nul 2>nul
Goto :eof

:Delay :: 延迟操作 <%1=Sec|延迟秒数> [%2=+|显示倒计时]。
if "%~2" == "+" (Set #=2) else Set #=1
if exist %WinDir%\System32\timeout.exe (timeout /t %~1 %#%>nul) else if exist %WinDir%\System32\choice.exe (choice /t %~1 /d y /n >nul) else ping 127.1 -n %~1 >nul
Goto :eof

:EchoX :: 显示彩色文字 (不支持半角字符 \ / : * ? " < >|. % ! ~)。
Setlocal EnableDelayedExpansion
Set echox=EchoX.exe&&!echox! 1>nul 2>nul||(Set echox=&mkdir "%TEMP%\EchoX" 2>nul&&attrib +s +h "%TEMP%\EchoX" 2>nul)
for %%a in (%*) do (
    Set "param=%%~a"&Set "color=!param:~0,2!"&(if not exist %WinDir%\System32\findstr.exe if not defined echox Set "color=xx")
    Set n=0&(if "!param:~2,2!" == "::" Set n=1)&(if "!param:~2,2!" == ".:" Set n=2)
    if !n! gtr 0 (
        if /i "!color!" == "xx" (Set /p="_!param:~4!"<nul) else (Set param=%%~nxa&if defined echox (!echox! -c !color! -n "!param:~4!") else (Pushd "%TEMP%\EchoX" 2>nul&>"!param:~4!",Set /p= <nul&findstr /a:!color! .* "!param:~4!*"&del "!param:~4!"&Popd))
        if !n! == 2 echo.
    ) else if defined param Set /p="_!param!"<nul
)
Endlocal&Goto :eof