@echo off
Set Args=%*
if `%1` neq `` Set "_Args=%Args:"=""%"
if /i "%UserName%" == "SYSTEM" (Goto GotAdmin) else (reg query "HKLM\SYSTEM\ControlSet001\Control\MiniNT" 1>nul 2>nul&&Goto GotAdmin)
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
Pushd "%CD%"&cd /d "%~dp0"&Title 搜狗拼音&if exist %WinDir%\System32\ureg.dll Mode 50,12 2>nul
REM 设置安装参数
Set "HideIMI=0"      :: 隐藏系统输入法指示器 (1 隐藏，0 不隐藏)，仅 Win10 以后系统有效，隐藏后会导致任务栏搜索框不能出候选字
Set "SGlnk=公共菜单" :: 设置快捷方式路径，可取值：当前路径，用户桌面，用户菜单，公共桌面，公共菜单，(若不创建快捷方式则值置为空)
Set "SwitchIM=1"     :: 绿化后切换到输入法 (1 切换，0 不切换)
Set SGusr=&if exist "用户数据" Set "SGusr=用户数据" :: 设置配置文件路径：默认为当前路径的“用户数据”文件夹 (要求系统分区必须是 NTFS 分区)
REM 判断启动条件
Set SGver=&for /f "delims=" %%i in ('dir /a:d /o:d /b 2^>nul') do if exist "%%~i\Data\runtime.ini" if exist "%%~i\_Green\SogouPY" Set "SGver=%%~i"
if not defined SGver Call :EchoX "cf.: 找不到 [版本号] 文件夹。"&Call :Delay 5 +&Exit /b
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)
REM 安装初始化
Set "SGdir=%CD%"
Set "XD=%SGdir:&=^&%"
cd "%SGver%" 1>nul 2>nul
Set GB=&if defined CommonProgramW6432 (Set NirCmd=_Green\NirCmd64.exe) else Set NirCmd=_Green\NirCmd.exe
Set NirCmd=&if exist "%NirCmd%" for /f %%i in ('%NirCmd% consolewrite ok 2^>nul') do Set "NirCmd=%NirCmd%"
if `%1` == `` ren SGTool.nop SGTool._op 1>nul 2>nul&Set GB=1
echo %Args%|%findstr%"/HideIMI" 1>nul 2>nul&&(Set HideIMI=1&ren SGTool.nop SGTool._op 1>nul 2>nul&if /i "%~1" == "/HideIMI" Set GB=1)
echo %Args%|%findstr%"/SGnop" 1>nul 2>nul&&((if not exist "_Green\SGCert" ren SGTool._op SGTool.nop 1>nul 2>nul)&if /i "%~1" == "/SGnop" Set GB=1)
(if "%GB%" == "1" Goto Begin)&(if /i "%~1" == "h" Goto Begin)&:: 若参数1为其他参数，则隐藏批处理窗口
if defined NirCmd "%NirCmd%" exec hide "%~dpnx0" h %Args% 1>nul 2>nul&&Exit /b
mshta VBScript:CreateObject("WScript.Shell").Run("""%~dpnx0"" h %_Args%",0)(Window.Close) 2>nul&&Exit /b

:Begin
tskill /? 1>nul 2>nul&&Set "KillSGT=tskill SGTool"||(Set "KillSGT=taskkill /f /im SGTool.exe /t"&if defined NirCmd Set "KillSGT="%NirCmd%" killprocess SGTool")
Set "KillSGT=del /q SGTool.run 1>nul 2>nul&%KillSGT% 1>nul 2>nul"
%KillSGT%&&Call :Delay 2
copy /y SGTool.bak SGTool.exe 1>nul 2>nul
if exist SGTool.nop (Set SGIcon=SGTool.bak&if not exist SGTool.bak copy /y SGTool.exe SGTool.bak 1>nul 2>nul) else (Set SGIcon=SGTool.exe&del /q SGTool.bak 1>nul 2>nul)
if not exist SGTool.exe (Call :EchoX "cf.:丢失 SGTool.exe 文件！！"&Call :Delay 60&Exit /b)
if not defined GB if `%2` neq `` Goto Route

:Install
Set SGCount=0
del /f /q "%WinDir%\System32\SogouPY.ime" 1>nul 2>nul&if exist "%WinDir%\System32\SogouPY.ime" Set /a SGCount+=1
(if not exist "%WinDir%\System32\SogouPY.ime" del /f /q "%WinDir%\SysWOW64\SogouPY.ime" 1>nul 2>nul)&if exist "%WinDir%\SysWOW64\SogouPY.ime" Set /a SGCount+=1
if %SGCount% gtr 0 if /i "%~2" == "/Silent" (
    if defined NirCmd "%NirCmd%" infobox "检测到旧安装有残留！~n请卸载后，重启电脑再绿化。" "[ 搜狗拼音 ]" 1>nul 2>nul&Exit /b
    (echo 检测到旧安装有残留！&echo 请卸载后，重启电脑再绿化。)|msg * /time 10&Exit /b
) else Call :EchoX "cf.:检测到旧安装有残留，是否继续绿化！！"&Pause&echo.
if exist SGTool.nop (Call :EchoX "9f.:正在特殊绿化_禁止SGTool.exe进程常驻。") else Call :EchoX "2f.:正在绿化中，请不要手动关闭窗口 。。。"
Call :EchoX "5e.:( 若被安全软件阻挡，请退出后再安装。)"
echo       0%%      25%%       50%%      75%%       100%%
echo      ┍--------------------------------------┑
Set /p="进度:│ "<nul&Title ^>^> 操作系统环境
if exist "%WinDir%\System32\CryptUI.dll" _Green\CertMgr.exe /add _Green\MyCert.cer /s /r localMachine root /all 1>nul 2>nul
Cacls . 1>nul 2>nul&&Set "Cacls=Cacls"||Set "Cacls=_Green\Cacls"
iCacls /? 1>nul 2>nul&&Set "iCacls=iCacls"||Set "iCacls=_Green\iCacls"
if exist "..\%SGver%" (
    %iCacls% .. /SetintegrityLevel l 1>nul 2>nul
    1>nul 2>nul echo y|%Cacls% .. /t /e /c /p "Everyone":r 1>nul 2>nul
    1>nul 2>nul echo y|%Cacls% .. /t /e /c /p "Users":r 1>nul 2>nul
    1>nul 2>nul echo y|%Cacls% .. /t /e /c /p "ALL APPLICATION PACKAGES":r 1>nul 2>nul
    1>nul 2>nul echo y|%Cacls% .. /t /e /c /p "SYSTEM":f 1>nul 2>nul
    1>nul 2>nul echo y|%Cacls% .. /t /e /c /p "Administrators":f 1>nul 2>nul
    for /f "tokens=*" %%i in ('dir .. /b /a:d 2^>nul') do attrib -r "..\%%~i" 1>nul 2>nul&attrib +r "..\%%~i" 1>nul 2>nul
)
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SGTool.exe" /f 1>nul 2>nul
Title ^>^> 搜狗拼音环境
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
    Call :BlockNet + "SG_SogouPY" "%WinDir%\SysWOW64\SogouPY.ime"
    Call :BlockNet + "SG_SogouPY64" "%WinDir%\System32\SogouPY.ime"
    Call :BlockNet + "SG_SogouTSF" "%WinDir%\SysWOW64\SogouTSF.ime"
    Call :BlockNet + "SG_SogouTSF64" "%WinDir%\System32\SogouTSF.ime"
) 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
    Call :BlockNet + "SG_SogouPY" "%WinDir%\System32\SogouPY.ime"
    Call :BlockNet + "SG_SogouTSF" "%WinDir%\System32\SogouTSF.ime"
)
:: Call :BlockNet + "SG_SGTool" "%CD%\SGTool.exe"
ver|%findstr%" 5." 1>nul 2>nul||(Set "SGData=%UserProfile%\AppData\LocalLow")
ver|%findstr%" 5." 1>nul 2>nul&&(Set "SGData=%AppData%"&Set "SGusr=")
rd /s /q "%SGData%\SogouPY" 1>nul 2>nul&rd /s /q "%SGData%\SogouPY.users" 1>nul 2>nul
Set err_n=0&if defined SGusr (
    mkdir "..\%SGusr%\SogouPY" 2>nul&mkdir "..\%SGusr%\SogouPY.users" 2>nul
    if not exist "..\%SGusr%\SogouPY\env.ini" xCopy /e /r /y "_Green\SogouPY" "..\%SGusr%\SogouPY\" 1>nul 2>nul
    mklink /d "%SGData%\SogouPY" "%~dp0%SGusr%\SogouPY" 1>nul 2>nul||Set /a err_n+=1
    mklink /d "%SGData%\SogouPY.users" "%~dp0%SGusr%\SogouPY.users" 1>nul 2>nul||Set /a err_n+=1
) else xCopy /e /r /y "_Green\SogouPY" "%SGData%\SogouPY\" 1>nul 2>nul
if %err_n% gtr 0 (rd /s /q "%SGData%\SogouPY" 1>nul 2>nul&rd /s /q "%SGData%\SogouPY.users" 1>nul 2>nul&xCopy /e /r /y "_Green\SogouPY" "%SGData%\SogouPY\" 1>nul 2>nul)
Title ^>^> 屏蔽日志生成
for %%i in (LOG Logs) do rd /s /q "%SGData%\SogouPY\%%~i" 1>nul 2>nul&del /q "%SGData%\SogouPY\%%~i" 1>nul 2>nul&copy /y "_Green\SogouPY\%%~i" "%SGData%\SogouPY\%%~i" 1>nul 2>nul
if exist "_Green\SGCert" for %%i in (LOG Logs) do rd /s /q "%SGData%\SogouPY\%%~i" 1>nul 2>nul&del /q "%SGData%\SogouPY\%%~i" 1>nul 2>nul
if not exist "%SGData%\SogouPY\LOG" mkdir "%SGData%\SogouPY\LOG" 2>nul&(dir /a:d /b "%SGData%\SogouPY\LOG" 2>nul&&cd.>"%SGData%\SogouPY\LOG\APP")
attrib -s -h -r debug.log 1>nul 2>nul&cd.>debug.log&attrib +s +h +r debug.log 1>nul 2>nul
if exist "SGSmartAssistant.exe" attrib -s -h -r "Data\SmartAssistant.log" 1>nul 2>nul&cd.>"Data\SmartAssistant.log"&attrib +s +h +r "Data\SmartAssistant.log" 1>nul 2>nul
mkdir "..\Components" 2>nul&if exist "..\Components" (
    reg add "%SW%\SogouComponents" /f /ve /t REG_SZ /d "%~dp0Components" 1>nul 2>nul
    if not exist "..\Components\ComponentConfig.ini" cd.>"..\Components\ComponentConfig.ini"
    attrib -s -h -r "..\Components\debug.log" 1>nul 2>nul&cd.>"..\Components\debug.log"&attrib +s +h +r "..\Components\debug.log" 1>nul 2>nul
)
for /f "delims=" %%i in ('dir /a:d /b ..\Components\SGMouseInput 2^>nul') do if exist "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" attrib -s -h -r "%SGData%\SogouPY\Components\SGMouseInput\Dict\imekernel.log" 1>nul 2>nul&mkdir "%SGData%\SogouPY\Components\SGMouseInput\Dict" 2>nul&cd.>"%SGData%\SogouPY\Components\SGMouseInput\Dict\imekernel.log"&attrib +s +h +r "%SGData%\SogouPY\Components\SGMouseInput\Dict\imekernel.log" 1>nul 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\SogouFlash 2^>nul') do if exist "%~dp0Components\SogouFlash\%%~i\SogouFlash.exe" attrib -s -h -r cef_record_nocheck.log 1>nul 2>nul&cd.>cef_record_nocheck.log&attrib +s +h +r cef_record_nocheck.log 1>nul 2>nul
Set /p=">>"<nul&Title ^>^> 开始绿化
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 "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
reg add "HKCU\SOFTWARE\SogouInput.store.user" /f /v "EnableShowJumpSogou" /t REG_DWORD /d 0 1>nul 2>nul
reg add "HKCU\SOFTWARE\SogouInput.store.user" /f /v "status_pop_switch" /t REG_DWORD /d 0 1>nul 2>nul
reg add "HKCU\SOFTWARE\SogouInput.store.user" /f /v "systoast_enable" /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
Set /p=">>>"<nul
SGTool.exe --appid=scdreg -register "%CD%"
SGTool.exe --appid=skinreg -register "%CD%"
Set /p=">>>"<nul&Title ^>^> 正在安装，请耐心等待
SGTool.exe --appid=ucfont -yahei -extb6 "%CD%"
Set /p=">>>"<nul
SGTool.exe --appid=pinyinrepair /k
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=scdreg -cdefault
SGTool.exe --appid=userpage -register_protocol
Set /p=">>>"<nul
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"
if %ver% gtr 61 SGTool.exe --appid=check -o
SGTool.exe --appid=dictconv
Set /p=">>>"<nul
if "%HideIMI%" == "1" if %ver% geq 100 (
    REM 启用隐藏，且为 Win10 以后系统
    Title ^>^> 隐藏系统输入法指示器
    reg delete "HKLM\SOFTWARE\Microsoft\CTF\TIP\{E7EA138E-69F8-11D7-A6EA-00065B844310}\Category\Item\{E7EA138E-69F8-11D7-A6EA-00065B844310}\{13A016DF-560B-46CD-947A-4C3AF1E0E35D}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Microsoft\CTF\TIP\{E7EA138E-69F8-11D7-A6EA-00065B844310}\Category\Item\{E7EA138E-69F8-11D7-A6EA-00065B844310}\{25504FB4-7BAB-4BC1-9C69-CF81890F0EF5}" /f 1>nul 2>nul
)
if %ver% leq 61 (
    REM Win7 以前系统
    Title ^>^> 删除不兼容输入法
    reg add "HKCU\Keyboard Layout\Toggle" /f /v "hotkey" /t REG_SZ /d "3" 1>nul 2>nul
    reg add "HKCU\Keyboard Layout\Toggle" /f /v "Language Hotkey" /t REG_SZ /d "3" 1>nul 2>nul
    reg add "HKCU\Keyboard Layout\Toggle" /f /v "Layout Hotkey" /t REG_SZ /d "2" 1>nul 2>nul
    _Green\IMETool.exe /move2:SogouPY.ime,1 1>nul 2>nul
    Call :FlushIME
    _Green\IMETool.exe /move2:SogouPY.ime,99 1>nul 2>nul
    for /f "delims=" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\CTF\TIP\{E7EA138E-69F8-11D7-A6EA-00065B844310}\LanguageProfile" /s /k /f "{E7EA138F-69F8-11D7-A6EA-00065B844311}" 2^>nul') do reg delete "%%~i" /f 1>nul 2>nul
)
if "%SwitchIM%" == "1" (
    Title ^>^> 切换到输入法
    del /f /q "%SGData%\SogouPY\sgim_cand_font_filter.bin" 1>nul 2>nul
    if defined NirCmd ("%NirCmd%" SendKeypress Ctrl+Comma 1>nul 2>nul) else (
        >"%TEMP%\SK.vbs" (echo Set WSHShell=CreateObject^("WScript.Shell"^)&echo WSHShell.SendKeys "^,")
        CScript /b /nologo "%TEMP%\SK.vbs" 1>nul 2>nul&del /f /q "%TEMP%\SK.vbs" 1>nul 2>nul
    )
    if exist SGTool.nop Call :Delay 2&if not exist "%SGData%\SogouPY\sgim_cand_font_filter.bin" copy /y "_Green\SogouPY\sgim_cand_font_filter.bin" "%SGData%\SogouPY\sgim_cand_font_filter.bin" 1>nul 2>nul
)
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 "搜狗细胞词典" 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 "搜狗皮肤文件" 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 exist SGTool.nop Call :CreateLnk "%~n0%~x0" "/ScdReg" "/SkinReg" "/Config" "/IMEMgr" "/TotalSym" "/Poem"
if not exist SGTool.nop Call :CreateLnk "%SGver%\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
echo.&echo      └--------------------------------------┘
Call :EchoX "2f::绿化完成！！！"
Call :Delay 1&Goto End

:CreateLnk
Set "SG=%~1"&Set "Scd=%~2"&Set "Skin=%~3"&Set "Cfg=%~4"&Set "Ime=%~5"&Set "Sym=%~6"&Set "Ocp=%~7"
reg add "HKLM\SOFTWARE\Classes\SogouCellDict\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%XD%\%SG%\" %Scd% \"%%1\"" 1>nul 2>nul
reg add "HKLM\SOFTWARE\Classes\SogouSkinFile\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%XD%\%SG%\" %Skin% \"%%1\"" 1>nul 2>nul
Set "lnkPath=%CD%"&del /q "%CD%\..\更多设置.lnk" 2>nul&Call :Link
if not defined SGlnk (Goto :eof) else Set lnkPath=
if /i "%SGlnk%" == "用户桌面" for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop" 2^>nul') do Set "lnkPath=%%~j"
if /i "%SGlnk%" == "用户菜单" for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Programs" 2^>nul') do Set "lnkPath=%%~j\搜狗拼音"
if /i "%SGlnk%" == "公共桌面" for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop" 2^>nul') do Set "lnkPath=%%~j"
if /i "%SGlnk%" == "公共菜单" for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Programs" 2^>nul') do Set "lnkPath=%%~j\搜狗拼音"
if defined lnkPath (mkdir "%lnkPath%" 2>nul) else Set "lnkPath=%CD%\.."
del /q "%CD%\..\更多设置.lnk" 2>nul
:Link
"%NirCmd%" shortcut "%SGdir%\%SGver%\SGMyInput.bat" "%CD%\.." "更多设置" "" "%CD%\%SGIcon%" 0 1>nul 2>nul
"%NirCmd%" shortcut "%SGdir%\%SG%" "%lnkPath%" "属性设置" "%Cfg%" "%CD%\%SGIcon%" 0 1>nul 2>nul
"%NirCmd%" shortcut "%SGdir%\%SG%" "%lnkPath%" "输入法管理器" "%Ime%" "%CD%\%SGIcon%" 0 1>nul 2>nul
"%NirCmd%" shortcut "%SGdir%\%SG%" "%lnkPath%" "符号大全" "%Sym%" "%CD%\%SGIcon%" 0 1>nul 2>nul
:: "%NirCmd%" shortcut "%SGdir%\%SG%" "%lnkPath%" "常用诗词" "%Ocp%" "%CD%\%SGIcon%" 0 1>nul 2>nul
if exist "%SGdir%\%SGver%\SGKaomoji.exe" "%NirCmd%" shortcut "%SGdir%\%SGver%\SGKaomoji.exe" "%lnkPath%" "颜文字" "" "%SGdir%\%SGver%\SGKaomoji.exe" 0 1>nul 2>nul
if exist "%SGdir%\%SGver%\SGSmartAssistant.exe" "%NirCmd%" shortcut "%SGdir%\%SGver%\SGSmartAssistant.exe" "%lnkPath%" "语音输入" "-voiceinput=imemenu" "%SGdir%\%SGver%\SGSmartAssistant.exe" 0 1>nul 2>nul
if exist "%SGdir%\%SGver%\ScreenCapture.exe" "%NirCmd%" shortcut "%SGdir%\%SGver%\ScreenCapture.exe" "%lnkPath%" "截图工具" "" "%SGdir%\%SGver%\ScreenCapture.exe" 0 1>nul 2>nul
if exist "%~dp0Components\ScreenCapture\ScreenCapture.exe" "%NirCmd%" shortcut "%~dp0Components\ScreenCapture\ScreenCapture.exe" "%lnkPath%" "截图工具" "" "%~dp0Components\ScreenCapture\ScreenCapture.exe" 0 1>nul 2>nul
if exist "%~dp0Components\ScreenCapture\ScreenCapture.bat" "%NirCmd%" shortcut "%~dp0Components\ScreenCapture\ScreenCapture.bat" "%lnkPath%" "截图工具" "" "%~dp0Components\ScreenCapture\ScreenCapture.exe" 0 1>nul 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\HandInput 2^>nul') do if exist "%~dp0Components\HandInput\%%~i\HandInput.exe" "%NirCmd%" shortcut "%~dp0Components\HandInput\%%~i\HandInput.exe" "%lnkPath%" "手写输入" "" "%~dp0Components\HandInput\%%~i\HandInput.exe" 0 1>nul 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\MobileInput 2^>nul') do if exist "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" "%NirCmd%" shortcut "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" "%lnkPath%" "跨屏输入" "" "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" 0 1>nul 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\PicFace 2^>nul') do if exist "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" "%NirCmd%" shortcut "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" "%lnkPath%" "图片表情" "" "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" 0 1>nul 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\SGMouseInput 2^>nul') do if exist "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" "%NirCmd%" shortcut "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" "%lnkPath%" "点点输入" "" "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" 0 1>nul 2>nul
if defined NirCmd if exist "%CD%\..\更多设置.lnk" Goto :eof
_Green\Shortcut.exe "%CD%\..\更多设置.lnk" "%SGdir%\%SGver%\SGMyInput.bat" "" "%CD%\%SGIcon%" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%CD%\..\更多设置.lnk""):b.TargetPath=""%SGdir%\%SGver%\SGMyInput.bat"":b.IconLocation=""%CD%\%SGIcon%,0"":b.Description=""更多设置"":b.Save:Close") 2>nul
_Green\Shortcut.exe "%lnkPath%\属性设置.lnk" "%SGdir%\%SG%" "%Cfg%" "%CD%\%SGIcon%" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\属性设置.lnk""):b.TargetPath=""%SGdir%\%SG%"":b.Arguments=""%Cfg%"":b.IconLocation=""%CD%\%SGIcon%,0"":b.Description=""属性设置"":b.Save:Close") 2>nul
_Green\Shortcut.exe "%lnkPath%\输入法管理器.lnk" "%SGdir%\%SG%" "%Ime%" "%CD%\%SGIcon%" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\输入法管理器.lnk""):b.TargetPath=""%SGdir%\%SG%"":b.Arguments=""%Ime%"":b.IconLocation=""%CD%\%SGIcon%,0"":b.Description=""输入法管理器"":b.Save:Close") 2>nul
_Green\Shortcut.exe "%lnkPath%\符号大全.lnk" "%SGdir%\%SG%" "%Sym%" "%CD%\%SGIcon%" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\符号大全.lnk""):b.TargetPath=""%SGdir%\%SG%"":b.Arguments=""%Sym%"":b.IconLocation=""%CD%\%SGIcon%,0"":b.Description=""符号大全"":b.Save:Close") 2>nul
:: _Green\Shortcut.exe "%lnkPath%\常用诗词.lnk" "%SGdir%\%SG%" "%Ocp%" "%CD%\%SGIcon%" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\常用诗词.lnk""):b.TargetPath=""%SGdir%\%SG%"":b.Arguments=""%Ocp%"":b.IconLocation=""%CD%\%SGIcon%,0"":b.Description=""常用诗词"":b.Save:Close") 2>nul
if exist "%SGdir%\%SGver%\SGKaomoji.exe" _Green\Shortcut.exe "%lnkPath%\颜文字.lnk" "%SGdir%\%SGver%\SGKaomoji.exe" "" "%SGdir%\%SGver%\SGKaomoji.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\颜文字.lnk""):b.TargetPath=""%SGdir%\%SGver%\SGKaomoji.exe"":b.Description=""颜文字"":b.Save:Close") 2>nul
if exist "%SGdir%\%SGver%\SGSmartAssistant.exe" _Green\Shortcut.exe "%lnkPath%\语音输入.lnk" "%SGdir%\%SGver%\SGSmartAssistant.exe" "-voiceinput=imemenu" "%SGdir%\%SGver%\SGSmartAssistant.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\语音输入.lnk""):b.TargetPath=""%SGdir%\%SGver%\SGSmartAssistant.exe"":b.Arguments=""-voiceinput=imemenu"":b.Description=""语音输入"":b.Save:Close") 2>nul
if exist "%SGdir%\%SGver%\ScreenCapture.exe" _Green\Shortcut.exe "%lnkPath%\截图工具.lnk" "%SGdir%\%SGver%\ScreenCapture.exe" "" "%SGdir%\%SGver%\ScreenCapture.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\截图工具.lnk""):b.TargetPath=""%SGdir%\%SGver%\ScreenCapture.exe"":b.Description=""截图工具"":b.Save:Close") 2>nul
if exist "%~dp0Components\ScreenCapture\ScreenCapture.exe" _Green\Shortcut.exe "%lnkPath%\截图工具.lnk" "%~dp0Components\ScreenCapture\ScreenCapture.exe" "" "%~dp0Components\ScreenCapture\ScreenCapture.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\截图工具.lnk""):b.TargetPath=""%~dp0Components\ScreenCapture\ScreenCapture.exe"":b.Description=""截图工具"":b.Save:Close") 2>nul
if exist "%~dp0Components\ScreenCapture\ScreenCapture.bat" _Green\Shortcut.exe "%lnkPath%\截图工具.lnk" "%~dp0Components\ScreenCapture\ScreenCapture.bat" "" "%~dp0Components\ScreenCapture\ScreenCapture.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\截图工具.lnk""):b.TargetPath=""%~dp0Components\ScreenCapture\ScreenCapture.bat"":b.IconLocation=""%~dp0Components\ScreenCapture\ScreenCapture.exe,0"":b.Description=""截图工具"":b.Save:Close") 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\HandInput 2^>nul') do if exist "%~dp0Components\HandInput\%%~i\HandInput.exe" _Green\Shortcut.exe "%lnkPath%\手写输入.lnk" "%~dp0Components\HandInput\%%~i\HandInput.exe" "" "%~dp0Components\HandInput\%%~i\HandInput.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\手写输入.lnk""):b.TargetPath=""%~dp0Components\HandInput\%%~i\HandInput.exe"":b.Description=""手写输入"":b.Save:Close") 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\MobileInput 2^>nul') do if exist "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" _Green\Shortcut.exe "%lnkPath%\跨屏输入.lnk" "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" "" "%~dp0Components\MobileInput\%%~i\SGMobileInput.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\跨屏输入.lnk""):b.TargetPath=""%~dp0Components\MobileInput\%%~i\SGMobileInput.exe"":b.Description=""跨屏输入"":b.Save:Close") 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\PicFace 2^>nul') do if exist "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" _Green\Shortcut.exe "%lnkPath%\图片表情.lnk" "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" "" "%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\图片表情.lnk""):b.TargetPath=""%~dp0Components\PicFace\%%~i\SGPicFaceTool.exe"":b.Description=""图片表情"":b.Save:Close") 2>nul
for /f "delims=" %%i in ('dir /a:d /b ..\Components\SGMouseInput 2^>nul') do if exist "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" _Green\Shortcut.exe "%lnkPath%\点点输入.lnk" "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" "" "%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe" 0 2>nul||mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(""%lnkPath%\点点输入.lnk""):b.TargetPath=""%~dp0Components\SGMouseInput\%%~i\SGMouseInput\MouseInput.exe"":b.Description=""点点输入"":b.Save:Close") 2>nul
Goto :eof

:Route
if /i "%~2" == "/Silent" Goto Install
if not exist "%WinDir%\System32\SogouPY.ime" (Call :EchoX "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 :EchoX "9f.:正在打开中，请稍候。。。"
Call :EchoX "5e.:(请不要手动关闭窗口)"
cd.>SGTool.run&SGTool.exe --appid=config
Set dn=0&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:IMEMgr
Title 输入法管理器
Call :EchoX "9f.:正在打开中，请稍候。。。"
Call :EchoX "5e.:(请不要手动关闭窗口)"
cd.>SGTool.run&SGTool.exe --appid=config /m
Set dn=0&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:TotalSym
Title 符号大全
Call :EchoX "9f.:正在打开中，请稍候。。。"
Call :EchoX "5e.:(请不要手动关闭窗口)"
cd.>SGTool.run&SGTool.exe --appid=exinput -cid=totalsym
Set dn=0&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:Poem
Title 常用诗词
Call :EchoX "9f.:正在打开中，请稍候。。。"
Call :EchoX "5e.:(请不要手动关闭窗口)"
cd.>SGTool.run&SGTool.exe --appid=exinput -cid=poem
Set dn=0&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:ScdReg
Title 安装词库
Call :EchoX "9f.:正在安装中，请稍候。。。"
cd.>SGTool.run&SGTool.exe -line 0 -border --appid=scdreg -list %3
Call :EchoX "5e.:(安装完成后，请手动关闭窗口)"
Set dn=2&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:SkinReg
Title 安装皮肤
Call :EchoX "9f.:正在安装中，请稍候。。。"
cd.>SGTool.run&SGTool.exe -line 0 -border --appid=skinreg -list %3
Call :EchoX "5e.:(安装完成后，请手动关闭窗口)"
Set dn=1&tasklist /? 1>nul 2>nul&&Goto KillSGT||Goto End

:KillSGT
if not exist SGTool.run (Exit /b) else Call :Delay 1
Set pn=0&for /f %%a in ('"tasklist /nh 2>nul|%findstr%"SGTool.exe" 2>nul"') do Set /a pn+=1
if defined tg (if %pn% lss 2 (Set tg=&%KillSGT%&Goto End)) else (if %pn% geq %dn% Set tg=触发条件)
Goto KillSGT

:End
if exist SGTool.nop (
    %KillSGT%&&Call :Delay 2
    %KillSGT%&del /f /q SGTool.exe 1>nul 2>nul
    REM copy /y SGTool.nop SGTool.exe 1>nul 2>nul
) else (
    REM attrib -s -h -r desktop.ini 1>nul 2>nul&(echo [.ShellClassInfo]&echo IconResource=SGTool.exe,0&echo IconFile=SGTool.exe&echo IconIndex=0)>desktop.ini&attrib +s +h desktop.ini 1>nul 2>nul&attrib +r . 1>nul 2>nul
    REM rd /s /q _Green\SogouPY 1>nul 2>nul&for %%i in (Cacls.exe CertMgr.exe iCacls.exe IMETool.exe MyCert.cer Shortcut.exe SogouPY.ime SogouPY64.ime SogouTSF.ime SogouTSF64.ime) do del /q "_Green\%%~i" 1>nul 2>nul
    REM del /q ..\_静默绿化.lnk 1>nul 2>nul&del /q ..\_静默绿化.bat 1>nul 2>nul&del /q %0 1>nul 2>nul
)
Exit /b

:BlockNet :: 屏蔽应用联网
Set "aName=%~2"&Set "aPath=%~3"
if exist "_Green\SGCert" if defined aName if "%~1" == "+" (
    netsh advfirewall firewall add rule name="%aName%_BlockNet" dir=out program="%aPath%" action=block 1>nul 2>nul
REM netsh advfirewall firewall add rule name="%aName%_BlockNet" dir=in program="%aPath%" action=block 1>nul 2>nul
) else netsh advfirewall firewall delete rule name="%aName%_BlockNet" 1>nul 2>nul
Goto :eof

:FlushIME :: 刷新输入法
Set Attributes=&for /f "tokens=2*" %%i in ('reg query "HKCU\Keyboard Layout" /v "Attributes" 2^>nul') do Set "Attributes=%%~j"
Set Hotkey=&for /f "tokens=2*" %%i in ('reg query "HKCU\Keyboard Layout\Toggle" /v "Hotkey" 2^>nul') do Set "Hotkey=%%~j"
Set LeHotkey=&for /f "tokens=3*" %%i in ('reg query "HKCU\Keyboard Layout\Toggle" /v "Language Hotkey" 2^>nul') do Set "LeHotkey=%%~j"
Set LtHotkey=&for /f "tokens=3*" %%i in ('reg query "HKCU\Keyboard Layout\Toggle" /v "Layout Hotkey" 2^>nul') do Set "LtHotkey=%%~j"
@start "" /wait /b "_Green\IMETool.exe" /xxx 1>nul 2>nul&Call :Delay 3
if defined Attributes (reg add "HKCU\Keyboard Layout" /f /v "Attributes" /t REG_DWORD /d "%Attributes:~2%" 1>nul 2>nul) else reg delete "HKCU\Keyboard Layout" /f /v "Attributes" 1>nul 2>nul
if defined Hotkey (reg add "HKCU\Keyboard Layout\Toggle" /f /v "hotkey" /t REG_SZ /d "%Hotkey%" 1>nul 2>nul) else reg delete "HKCU\Keyboard Layout\Toggle" /f /v "hotkey" 1>nul 2>nul
if defined LeHotkey (reg add "HKCU\Keyboard Layout\Toggle" /f /v "Language Hotkey" /t REG_SZ /d "%LeHotkey%" 1>nul 2>nul) else reg delete "HKCU\Keyboard Layout\Toggle" /f /v "Language Hotkey" 1>nul 2>nul
if defined LtHotkey (reg add "HKCU\Keyboard Layout\Toggle" /f /v "Layout Hotkey" /t REG_SZ /d "%LtHotkey%" 1>nul 2>nul) else reg delete "HKCU\Keyboard Layout\Toggle" /f /v "Layout Hotkey" 1>nul 2>nul
Goto :eof

:Delay :: 延迟操作 <%1=Sec|延迟秒数> [%2=+|显示倒计时]。
if "%~2" == "+" (Set #=2) else Set #=1&if defined NirCmd @start "" /wait "%NirCmd%" wait %~1000 1>nul 2>nul&Goto :eof
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