Skip to content

fix(installer): preserve start type during installs #3867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cmake/packaging/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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:
")

Expand Down
4 changes: 0 additions & 4 deletions src_assets/windows/misc/autostart/autostart-service.bat

This file was deleted.

49 changes: 23 additions & 26 deletions src_assets/windows/misc/service/install-service.bat
Original file line number Diff line number Diff line change
@@ -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
43 changes: 37 additions & 6 deletions src_assets/windows/misc/service/uninstall-service.bat
Original file line number Diff line number Diff line change
@@ -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