diff --git a/cmake/packaging/windows.cmake b/cmake/packaging/windows.cmake index fb949aea848..1f55833e1ec 100644 --- a/cmake/packaging/windows.cmake +++ b/cmake/packaging/windows.cmake @@ -24,10 +24,6 @@ install(DIRECTORY "${SUNSHINE_SOURCE_ASSETS_DIR}/windows/misc/path/" DESTINATION "scripts" COMPONENT assets) -# Configurable options for the service -install(DIRECTORY "${SUNSHINE_SOURCE_ASSETS_DIR}/windows/misc/autostart/" - DESTINATION "scripts" - COMPONENT autostart) # scripts install(DIRECTORY "${SUNSHINE_SOURCE_ASSETS_DIR}/windows/misc/firewall/" @@ -72,7 +68,6 @@ SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS nsExec::ExecToLog '\\\"$INSTDIR\\\\scripts\\\\add-firewall-rule.bat\\\"' nsExec::ExecToLog '\\\"$INSTDIR\\\\scripts\\\\install-gamepad.bat\\\"' nsExec::ExecToLog '\\\"$INSTDIR\\\\scripts\\\\install-service.bat\\\"' - nsExec::ExecToLog '\\\"$INSTDIR\\\\scripts\\\\autostart-service.bat\\\"' NoController: ") diff --git a/src_assets/windows/misc/autostart/autostart-service.bat b/src_assets/windows/misc/autostart/autostart-service.bat deleted file mode 100644 index 13c6b6bc9ba..00000000000 --- a/src_assets/windows/misc/autostart/autostart-service.bat +++ /dev/null @@ -1,4 +0,0 @@ -@echo off - -rem Set the service to auto-start -sc config SunshineService start= auto diff --git a/src_assets/windows/misc/service/install-service.bat b/src_assets/windows/misc/service/install-service.bat index 03ce6e6267b..425d71aa263 100644 --- a/src_assets/windows/misc/service/install-service.bat +++ b/src_assets/windows/misc/service/install-service.bat @@ -1,36 +1,33 @@ @echo off +setlocal EnableDelayedExpansion -rem Get sunshine root directory +rem ------------ resolve Sunshine root folder for %%I in ("%~dp0\..") do set "ROOT_DIR=%%~fI" -set SERVICE_NAME=SunshineService -set SERVICE_BIN="%ROOT_DIR%\tools\sunshinesvc.exe" +set "SERVICE_NAME=SunshineService" +set "SERVICE_BIN=%ROOT_DIR%\tools\sunshinesvc.exe" +set "MARKER=%TEMP%\sunshine_start_mode.txt" -rem Set service to demand start. It will be changed to auto later if the user selected that option. -set SERVICE_START_TYPE=demand +rem ------------ default for a brand-new install +set "SERVICE_START_TYPE=auto" -rem Remove the legacy SunshineSvc service -net stop sunshinesvc -sc delete sunshinesvc - -rem Check if SunshineService already exists -sc qc %SERVICE_NAME% > nul 2>&1 -if %ERRORLEVEL%==0 ( - rem Stop the existing service if running - net stop %SERVICE_NAME% - - rem Reconfigure the existing service - set SC_CMD=config -) else ( - rem Create a new service - set SC_CMD=create +rem ------------ if the uninstall script left us a start-mode, use it +if exist "%MARKER%" ( + set /p SERVICE_START_TYPE=<"%MARKER%" + del "%MARKER%" ) -rem Run the sc command to create/reconfigure the service -sc %SC_CMD% %SERVICE_NAME% binPath= %SERVICE_BIN% start= %SERVICE_START_TYPE% DisplayName= "Sunshine Service" +rem ------------ create the service with the preserved mode +sc create "%SERVICE_NAME%" ^ + binPath= "\"%SERVICE_BIN%\"" ^ + start= !SERVICE_START_TYPE! ^ + DisplayName= "Sunshine Service" -rem Set the description of the service -sc description %SERVICE_NAME% "Sunshine is a self-hosted game stream host for Moonlight." +sc description "%SERVICE_NAME%" "Sunshine is a self-hosted game-stream host for Moonlight." + +rem ------------ start it unless the mode is “disabled” +if /i not "!SERVICE_START_TYPE!"=="disabled" ( + net start "%SERVICE_NAME%" >nul 2>&1 +) -rem Start the new service -net start %SERVICE_NAME% +endlocal diff --git a/src_assets/windows/misc/service/uninstall-service.bat b/src_assets/windows/misc/service/uninstall-service.bat index 451ceb61990..2ab80c547ed 100644 --- a/src_assets/windows/misc/service/uninstall-service.bat +++ b/src_assets/windows/misc/service/uninstall-service.bat @@ -1,9 +1,40 @@ @echo off +setlocal EnableDelayedExpansion -rem Stop and delete the legacy SunshineSvc service -net stop sunshinesvc -sc delete sunshinesvc +set "SERVICE_NAME=SunshineService" +set "LEGACY_NAME=sunshinesvc" +set "MARKER=%TEMP%\sunshine_start_mode.txt" -rem Stop and delete the new SunshineService service -net stop SunshineService -sc delete SunshineService +rem ----------------- default in case the query fails +set "SERVICE_START_TYPE=auto" + +rem ----------------- grab existing start-type if the service is present +sc qc "%SERVICE_NAME%" >nul 2>&1 +if %ERRORLEVEL%==0 ( + for /f "tokens=3,*" %%S in ('sc qc "%SERVICE_NAME%" ^| find /i "START_TYPE"') do ( + set "NUM=%%S" + set "EXTRA=%%T" + ) + if "!NUM!"=="2" ( + echo !EXTRA! | find /i "DELAYED" >nul && ( + set "SERVICE_START_TYPE=delayed-auto" + ) || ( + set "SERVICE_START_TYPE=auto" + ) + ) else if "!NUM!"=="3" ( + set "SERVICE_START_TYPE=demand" + ) else if "!NUM!"=="4" ( + set "SERVICE_START_TYPE=disabled" + ) +) + +echo !SERVICE_START_TYPE! > "%MARKER%" + +rem ----------------- stop & delete both service names +net stop "%LEGACY_NAME%" >nul 2>&1 +sc delete "%LEGACY_NAME%" >nul 2>&1 + +net stop "%SERVICE_NAME%" >nul 2>&1 +sc delete "%SERVICE_NAME%" >nul 2>&1 + +endlocal