@echo off
:Init
Title 搜狗拼音
MODE con: Cols=50 Lines=12

:BatchGotAdmin
:--------------------------------------
Set "_Args=%* "
if `%1` neq `` Set "_Args=%_Args:"=""%"
fltmc 1>nul 2>nul||mshta VBScript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c """"%~f0"" %_Args%""",,"runas",1)(Window.Close) 2>nul&&Exit /b
Pushd "%CD%"&cd /d "%~dp0"
:--------------------------------------

REM 参数启动，隐藏批处理窗口
if "%1" == "" Goto Begin
if "%1" == "h" Goto Begin
mshta VBScript:CreateObject("WScript.Shell").Run("""%~nx0"" h %_Args%",0)(Window.Close) 2>nul&&Exit /b

:Begin
Set "SGdir=%CD%"
Set "SGver=10.5.0.4737"
Set "SGreg=0000_sogou_pinyin_105b"
Set "SGnop=替换无执行文件"
cd %SGver%
taskkill /f /im SGTool.exe /t 1>nul 2>nul
if not ErrorLevel 1 Call :Delay 2
if defined SGnop (Set SGIcon=SGTool.bak) else Set SGIcon=SGTool.exe
if defined SGnop if exist SGTool.bak (copy /y SGTool.bak SGTool.exe 1>nul 2>nul) else copy /y SGTool.exe SGTool.bak 1>nul 2>nul
if not exist SGTool.exe (Call :Xecho "cf.:丢失 SGTool.exe 文件！！"&Call :Delay 60&Exit /b)
if `%2` neq `` Goto Route

:Install
for %%i in ("%WinDir%\System32\SogouPY.ime" "%WinDir%\SysWOW64\SogouPY.ime") do if exist %%i Set /a Installed+=1
if "%Installed%" gtr "0" Call :Xecho "cf.:检测到系统有残留，是否继续绿化！！"&Pause&echo.
Call :Xecho "2f.:正在绿化中。。。"
Call :Xecho "5e.:(请不要手动关闭窗口)"
echo       0%%      25%%       50%%      75%%       100%%
echo      ┍--------------------------------------┑
if defined CommonProgramW6432 (
   Set "SW=HKLM\SOFTWARE\WOW6432Node"
   copy /v /y "_Green\SogouPY.ime" "%WinDir%\SysWOW64\" 1>nul 2>nul
   copy /v /y "_Green\SogouPY64.ime" "%WinDir%\System32\SogouPY.ime" 1>nul 2>nul
   copy /v /y "_Green\SogouTSF.ime" "%WinDir%\SysWOW64\" 1>nul 2>nul
   copy /v /y "_Green\SogouTSF64.ime" "%WinDir%\System32\SogouTSF.ime" 1>nul 2>nul
) else (
   Set "SW=HKLM\SOFTWARE"
   copy /v /y "_Green\SogouPY.ime" "%WinDir%\System32\" 1>nul 2>nul
   copy /v /y "_Green\SogouTSF.ime" "%WinDir%\System32\" 1>nul 2>nul
)
ver|find "5.">nul||xcopy /e /r /y _Green\SogouPY "%UserProfile%\AppData\LocalLow\SogouPY\" 1>nul 2>nul
ver|find "5.">nul&&xcopy /e /r /y _Green\SogouPY "%APPDATA%\SogouPY\" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /ve /t REG_SZ /d "%SGdir%" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "EnableSogouEudc" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "ImeHint" /t REG_DWORD /d "0" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "PatchFlag" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "Region" /t REG_SZ /d "%SGreg%" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "Version" /t REG_SZ /d "%SGver%" 1>nul 2>nul
reg add "%SW%\SogouInput" /f /v "VersionType" /t REG_SZ /d "Final" 1>nul 2>nul
reg add "HKCU\SOFTWARE\SogouInput" /f /v "Recover" /t REG_DWORD /d 0 1>nul 2>nul
reg add "HKCU\SOFTWARE\SogouInput" /f /v "UpUseBT" /t REG_DWORD /d 0 1>nul 2>nul
reg add "HKCU\SOFTWARE\SogouInput" /f /v "SendUserExperience" /t REG_DWORD /d 0 1>nul 2>nul
Set /p="进度:│ >>"<nul
SGTool.exe --appid=install -i -w
regsvr32 /s "%CD%\SogouTSF.dll" 1>nul 2>nul
regsvr32 /s "%WinDir%\System32\SogouTSF.ime" 1>nul 2>nul
regsvr32 /s "%WinDir%\SysWOW64\SogouTSF.ime" 1>nul 2>nul
:: netsh advfirewall firewall add rule action=block dir=out name="SGTool" program="%CD%\SGTool.exe" >nul
Set /p=">>>"<nul
SGTool.exe --appid=sysdicmk --src="sysmodel.bin" --des="sgim_core.bin"
Set /p=">>>"<nul
SGTool.exe --appid=ucfont -yahei -extb6 "%CD%"
Set /p=">>>"<nul
SGTool.exe --appid=eudc -install -olddir "%CD%"
Set /p=">>>"<nul
SGTool.exe --appid=scdreg -convv1tov2instpath "%CD%"
Set /p=">>>"<nul&Title ^>^> 正在安装词库，请耐心等待 
for %%a in (Scd\*.scel) do (SGTool.exe --appid=scdreg -add "%%a" -s)
Set /p=">>>"<nul
SGTool.exe --appid=scdreg -combscd "" "%CD%"
Set /p=">>>"<nul
SGTool.exe --appid=mergenfa
Set /p=">>>"<nul
SGTool.exe --appid=dictconv
Set /p=">>>"<nul&Title ^>^> 词库与皮肤文件注册表关联
reg add "HKLM\SOFTWARE\Classes\.scel" /f /ve /t REG_SZ /d "SogouCellDict" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\.ssf" /f /ve /t REG_SZ /d "SogouSkinFile" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict" /f /ve /t REG_SZ /d "Sogou Cell Dict" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict" /f /v "EditFlags" /t REG_DWORD /d "65536" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\%SGIcon%\",-264" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /ve /t REG_SZ /d "搜狗输入法" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /v "Command" /t REG_SZ /d "%CD%\SGTool.exe" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /v "InstallType" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /v "MaxVersion" /t REG_DWORD /d "22244818" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /v "Params" /t REG_SZ /d "-line 0 -border --appid=scdreg -add " 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Entry\SogouPY" /f /v "Priority" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Shell\Open" /f /ve /t REG_SZ /d "安装搜狗输入法细胞词库" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile" /f /ve /t REG_SZ /d "Sogou Skin File" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile" /f /v "EditFlags" /t REG_DWORD /d "65536" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\%SGIcon%\",-128" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /ve /t REG_SZ /d "搜狗输入法" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /v "Command" /t REG_SZ /d "%CD%\SGTool.exe" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /v "InstallType" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /v "MaxVersion" /t REG_DWORD /d "3" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /v "Params" /t REG_SZ /d "-line 0 -border --appid=skinreg -install -c " 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Entry\SogouPY" /f /v "Priority" /t REG_DWORD /d "1" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Shell\Open" /f  /ve /t REG_SZ /d "安装搜狗输入法皮肤" 1>nul 2>nul
assoc .=. 1>nul 2>nul
reg delete "HKCR\." /f 1>nul 2>nul
Set /p=">>>"<nul&Title ^>^> 创建快捷方式
if defined SGnop Call :CreateLnk "%~0" "scdreg" "skinreg" "config" "imemgr" "totalsym" "poem"
if not defined SGnop Call :CreateLnk "%CD%\SGTool.exe" "-line 0 -border --appid=scdreg -list" "-line 0 -border --appid=skinreg -list" "--appid=config" "--appid=config /m" "--appid=exinput -cid=totalsym" "--appid=exinput -cid=poem"
Set /p=">>"<nul&Title ^>^> 正在打开输入法管理器
SGTool.exe --appid=config /m
Set /p=">>>│"<nul&Title ^>^> 正在打开属性设置
echo.&echo      └--------------------------------------┘
Call :Xecho "2f.:绿化完成！！！"
SGTool.exe --appid=config
Goto End

:CreateLnk
Set "SG=%1"&Set "Scd=%2"&Set "Skin=%3"
Set "SG=%SG:"=%"&Set "Scd=%Scd:"=%"&Set "Skin=%Skin:"=%"
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%SG%\" %Scd% \"%%1\"" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%SG%\" %Skin% \"%%1\"" 1>nul 2>nul
_Green\Shortcut.exe "%~dp0属性设置.lnk" "%SG%" %4 "%CD%\" "%CD%\%SGIcon%,0" "_" 1>nul 2>nul
_Green\Shortcut.exe "%~dp0输入法管理器.lnk" "%SG%" %5 "%CD%\" "%CD%\%SGIcon%,0" "_" 1>nul 2>nul
_Green\Shortcut.exe "%~dp0符号大全.lnk" "%SG%" %6 "%CD%\" "%CD%\%SGIcon%,0" "_" 1>nul 2>nul
_Green\Shortcut.exe "%~dp0流行诗词.lnk" "%SG%" %7 "%CD%\" "%CD%\%SGIcon%,0" "_" 1>nul 2>nul
Goto :eof

:Route
if not exist "%WinDir%\System32\SogouPY.ime" (Call :Xecho "cf.:搜狗拼音未绿化！！"&Call :Delay 60)
if /i `%2` == `config` Goto Config
if /i `%2` == `imemgr` Goto IMEMgr
if /i `%2` == `scdreg` Goto ScdReg
if /i `%2` == `skinreg` Goto SkinReg
if /i `%2` == `totalsym` Goto TotalSym
if /i `%2` == `poem` Goto Poem
Goto End

:Config
Title 属性设置
Call :Xecho "9f.:正在打开中，请稍候。。。"
Call :Xecho "5e.:(请不要手动关闭窗口)"
SGTool.exe --appid=config
Goto End

:IMEMgr
Title 输入法管理器
Call :Xecho "9f.:正在打开中，请稍候。。。"
Call :Xecho "5e.:(请不要手动关闭窗口)"
SGTool.exe --appid=config /m
Goto End

:ScdReg
Title 安装词库
Call :Xecho "9f.:正在安装中，请稍候。。。"
SGTool.exe -line 0 -border --appid=scdreg -list %3
Call :Xecho "5e.:(安装完成后，请手动关闭窗口)"
Goto KillSGT

:SkinReg
Title 安装皮肤
Call :Xecho "9f.:正在安装中，请稍候。。。"
SGTool.exe -line 0 -border --appid=skinreg -list %3
Call :Xecho "5e.:(安装完成后，请手动关闭窗口)"
Goto KillSGT

:KillSGT
_Green\Sleep.vbs 500 1>nul 2>nul||Call :Delay 1
Set pn=0
for /f %%a in ('"tasklist /nh|findstr /li "SGTool.exe" 2>nul"') do Set /a pn+=1
if defined tg (if %pn% lss 2 (Set tg=&taskkill /f /im SGTool.exe /t 1>nul 2>nul&Goto End)) else (if %pn% geq 2 Set tg=触发条件)
Goto KillSGT

:TotalSym
Title 符号大全
Call :Xecho "9f.:正在打开中，请稍候。。。"
Call :Xecho "5e.:(请不要手动关闭窗口)"
SGTool.exe --appid=exinput -cid=totalsym
Goto End

:Poem
Title 流行诗词
Call :Xecho "9f.:正在打开中，请稍候。。。"
Call :Xecho "5e.:(请不要手动关闭窗口)"
SGTool.exe --appid=exinput -cid=poem
Goto End

:End
if defined SGnop (
    taskkill /f /im SGTool.exe /t 1>nul 2>nul
    if not ErrorLevel 1 _Green\Sleep.vbs 2000 1>nul 2>nul||Call :Delay 2
    copy /y SGTool.nop SGTool.exe 1>nul 2>nul
)
Exit /b

:Delay
REM 延迟操作，参数1：延迟秒数，参数2：+为显示倒计时
if `%2` == `+` (Set n=2) else Set n=1
if exist %WinDir%\System32\timeout.exe (timeout /t %1 %n%>nul) else if exist %WinDir%\System32\choice.exe (choice /t %1 /d y /n >nul) else ping 127.1 -n %1 >nul
Goto :eof

:Xecho
REM 显示彩色文字，不支持半角字符 \ / : * ? " < > | . % ! ~
setlocal enabledelayedexpansion
Set echox=EchoX.exe&&!echox! 1>nul 2>nul||Set echox=
for %%a in (%*) do (
    Set "param=%%a"&Set "param=!param:"=!"
    Set n=0&(if "!param:~2,2!" == "::" Set /a n+=1)&(if "!param:~2,2!" == ".:" Set /a n+=1)
    if !n! gtr 0 (
        if /i "!param:~0,2!" == "xx" (Set /p="_!param:~4!"<nul) else (Set param=%%~nxa&if defined echox (!echox! -c !param:~0,2! -n "!param:~4!") else (Pushd "%TEMP%"&>"!param:~4!",Set /p= <nul&findstr /a:!param:~0,2! .* "!param:~4!*"&del "!param:~4!"&Popd))
        if "!param:~2,1!" == "." echo.
    ) else if defined param Set /p="_!param!"<nul
)
endlocal
Goto :eof