diff --git a/src/ARKServerManager/Globalization/en-US/en-US.xaml b/src/ARKServerManager/Globalization/en-US/en-US.xaml index adf6f0b6..1d8aa9f5 100644 --- a/src/ARKServerManager/Globalization/en-US/en-US.xaml +++ b/src/ARKServerManager/Globalization/en-US/en-US.xaml @@ -1301,8 +1301,12 @@ Automatic Management Why can't I use this? Server Manager Settings - Auto-Start server on boot - Automatically start the server when the machine starts. + Auto-Start server + Automatically start the server. + on Boot + Start the server when the machine starts. + on Login + Start the server when the user logs in. Shutdown server at The hour and minute of the day to force a server shutdown. then restart diff --git a/src/ARKServerManager/Lib/ServerProfile.cs b/src/ARKServerManager/Lib/ServerProfile.cs index 3bc67640..8a516165 100644 --- a/src/ARKServerManager/Lib/ServerProfile.cs +++ b/src/ARKServerManager/Lib/ServerProfile.cs @@ -870,6 +870,14 @@ namespace ServerManagerTool.Lib set { SetValue(EnableAutoStartProperty, value); } } + public static readonly DependencyProperty AutoStartOnLoginProperty = DependencyProperty.Register(nameof(AutoStartOnLogin), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false)); + [DataMember] + public bool AutoStartOnLogin + { + get { return (bool)GetValue(AutoStartOnLoginProperty); } + set { SetValue(AutoStartOnLoginProperty, value); } + } + public static readonly DependencyProperty EnableAutoUpdateProperty = DependencyProperty.Register(nameof(EnableAutoUpdate), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false)); [DataMember] public bool EnableAutoUpdate @@ -4603,13 +4611,14 @@ namespace ServerManagerTool.Lib var taskKey = GetProfileKey(); - if(!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, true, Config.Default.AutoStart_TaskPriority)) + if (!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, this.AutoStartOnLogin, Config.Default.AutoStart_TaskPriority)) { return false; } + TimeSpan shutdownTime; var command = Assembly.GetEntryAssembly().Location; - if (!TaskSchedulerUtils.ScheduleAutoShutdown(taskKey, "#1", command, this.EnableAutoShutdown1 ? (TimeSpan.TryParseExact(this.AutoShutdownTime1, "g", null, out TimeSpan shutdownTime) ? shutdownTime : (TimeSpan?)null) : null, ShutdownDaysOfTheWeek1, ProfileName, TaskSchedulerUtils.ShutdownType.Shutdown1, Config.Default.AutoShutdown_TaskPriority)) + if (!TaskSchedulerUtils.ScheduleAutoShutdown(taskKey, "#1", command, this.EnableAutoShutdown1 ? (TimeSpan.TryParseExact(this.AutoShutdownTime1, "g", null, out shutdownTime) ? shutdownTime : (TimeSpan?)null) : null, ShutdownDaysOfTheWeek1, ProfileName, TaskSchedulerUtils.ShutdownType.Shutdown1, Config.Default.AutoShutdown_TaskPriority)) { return false; } diff --git a/src/ARKServerManager/UserControls/ServerSettingsControl.xaml b/src/ARKServerManager/UserControls/ServerSettingsControl.xaml index 6d05d915..223d36c4 100644 --- a/src/ARKServerManager/UserControls/ServerSettingsControl.xaml +++ b/src/ARKServerManager/UserControls/ServerSettingsControl.xaml @@ -1400,7 +1400,12 @@ - + + + + + + diff --git a/src/ConanServerManager/Globalization/en-US/en-US.xaml b/src/ConanServerManager/Globalization/en-US/en-US.xaml index 2f24f2ee..1575ba00 100644 --- a/src/ConanServerManager/Globalization/en-US/en-US.xaml +++ b/src/ConanServerManager/Globalization/en-US/en-US.xaml @@ -1237,8 +1237,12 @@ Automatic Management Why can't I use this? Server Manager Settings - Auto-Start server on boot - Automatically start the server when the machine starts. + Auto-Start server + Automatically start the server. + on Boot + Start the server when the machine starts. + on Login + Start the server when the user logs in. Shutdown server at The hour and minute of the day to force a server shutdown. then restart diff --git a/src/ConanServerManager/Lib/ServerProfile.cs b/src/ConanServerManager/Lib/ServerProfile.cs index 7de09b30..cafd1ed3 100644 --- a/src/ConanServerManager/Lib/ServerProfile.cs +++ b/src/ConanServerManager/Lib/ServerProfile.cs @@ -486,6 +486,14 @@ namespace ServerManagerTool.Lib set { SetValue(EnableAutoStartProperty, value); } } + public static readonly DependencyProperty AutoStartOnLoginProperty = DependencyProperty.Register(nameof(AutoStartOnLogin), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false)); + [DataMember] + public bool AutoStartOnLogin + { + get { return (bool)GetValue(AutoStartOnLoginProperty); } + set { SetValue(AutoStartOnLoginProperty, value); } + } + public static readonly DependencyProperty EnableAutoUpdateProperty = DependencyProperty.Register(nameof(EnableAutoUpdate), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false)); [DataMember] public bool EnableAutoUpdate @@ -1240,7 +1248,7 @@ namespace ServerManagerTool.Lib var taskKey = GetProfileKey(); - if(!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, true, Config.Default.AutoStart_TaskPriority)) + if (!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, this.AutoStartOnLogin, Config.Default.AutoStart_TaskPriority)) { return false; } diff --git a/src/ConanServerManager/UserControls/ServerSettingsControl.xaml b/src/ConanServerManager/UserControls/ServerSettingsControl.xaml index f048f845..82ca957e 100644 --- a/src/ConanServerManager/UserControls/ServerSettingsControl.xaml +++ b/src/ConanServerManager/UserControls/ServerSettingsControl.xaml @@ -981,7 +981,11 @@ - + + + + + diff --git a/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs b/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs index 9463e811..5942c1e0 100644 --- a/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs +++ b/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs @@ -185,6 +185,7 @@ namespace ServerManagerTool.Common.Utils Task task = null; TaskDefinition taskDefinition = null; + TaskLogonType taskLogonType = TaskLogonType.InteractiveToken; try { @@ -204,8 +205,9 @@ namespace ServerManagerTool.Common.Utils Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion); - taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken; + taskDefinition.Principal.LogonType = taskLogonType; taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; + taskDefinition.Principal.UserId = null; taskDefinition.RegistrationInfo.Description = "Server Auto-Backup"; taskDefinition.RegistrationInfo.Source = "Server Manager"; @@ -245,7 +247,7 @@ namespace ServerManagerTool.Common.Utils try { - task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken); + task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType); return task != null; } catch (Exception ex) @@ -309,6 +311,7 @@ namespace ServerManagerTool.Common.Utils Task task = null; TaskDefinition taskDefinition = null; + TaskLogonType taskLogonType = TaskLogonType.InteractiveToken; try { @@ -328,8 +331,9 @@ namespace ServerManagerTool.Common.Utils Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion); - taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken; + taskDefinition.Principal.LogonType = taskLogonType; taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; + taskDefinition.Principal.UserId = null; taskDefinition.RegistrationInfo.Description = $"Server Auto-Shutdown - {profileName}"; taskDefinition.RegistrationInfo.Source = "Server Manager"; @@ -393,7 +397,7 @@ namespace ServerManagerTool.Common.Utils try { - task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken); + task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType); return task != null; } catch (Exception ex) @@ -426,7 +430,7 @@ namespace ServerManagerTool.Common.Utils return false; } - public static bool ScheduleAutoStart(string taskKey, string taskSuffix, bool enableAutoStart, string command, string profileName, bool onBoot, ProcessPriorityClass priority) + public static bool ScheduleAutoStart(string taskKey, string taskSuffix, bool enableAutoStart, string command, string profileName, bool onLogin, ProcessPriorityClass priority) { var taskName = GetScheduledTaskName(TaskType.AutoStart, taskKey, taskSuffix); var taskFolder = TaskService.Instance.RootFolder.SubFolders.Exists(TaskFolder) ? TaskService.Instance.RootFolder.SubFolders[TaskFolder] : null; @@ -452,6 +456,8 @@ namespace ServerManagerTool.Common.Utils Task task = null; TaskDefinition taskDefinition = null; + TaskLogonType taskLogonType; + string userId; try { @@ -471,7 +477,6 @@ namespace ServerManagerTool.Common.Utils Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion); - taskDefinition.Principal.LogonType = TaskLogonType.ServiceAccount; taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; taskDefinition.RegistrationInfo.Description = $"Server Auto-Start - {profileName}"; @@ -481,16 +486,28 @@ namespace ServerManagerTool.Common.Utils taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT); taskDefinition.Settings.Priority = priority; - // Add a trigger that will fire after the machine has started - if (onBoot) + if (onLogin) { - var triggers = taskDefinition.Triggers.OfType(); + taskLogonType = TaskLogonType.InteractiveToken; + taskDefinition.Principal.LogonType = taskLogonType; + taskDefinition.Principal.UserId = null; + userId = null; + + var oldtriggers = taskDefinition.Triggers.OfType(); + foreach (var trigger in oldtriggers) + { + taskDefinition.Triggers.Remove(trigger); + } + + // Add a trigger that will fire after a user has logged in + var triggers = taskDefinition.Triggers.OfType(); if (triggers.IsEmpty()) { - var trigger = new BootTrigger + var trigger = new LogonTrigger { Delay = TimeSpan.FromMinutes(1), - ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT) + ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT), + UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name, }; taskDefinition.Triggers.Add(trigger); } @@ -499,18 +516,31 @@ namespace ServerManagerTool.Common.Utils foreach (var trigger in triggers) { trigger.Delay = TimeSpan.FromMinutes(1); + trigger.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name; } } } else { - var triggers = taskDefinition.Triggers.OfType(); + taskLogonType = TaskLogonType.ServiceAccount; + taskDefinition.Principal.LogonType = taskLogonType; + taskDefinition.Principal.UserId = null; + userId = "SYSTEM"; + + var oldtriggers = taskDefinition.Triggers.OfType(); + foreach (var trigger in oldtriggers) + { + taskDefinition.Triggers.Remove(trigger); + } + + // Add a trigger that will fire after the machine has booted + var triggers = taskDefinition.Triggers.OfType(); if (triggers.IsEmpty()) { - var trigger = new LogonTrigger + var trigger = new BootTrigger { Delay = TimeSpan.FromMinutes(1), - ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT) + ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT), }; taskDefinition.Triggers.Add(trigger); } @@ -534,7 +564,7 @@ namespace ServerManagerTool.Common.Utils try { - task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount); + task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, userId, null, taskLogonType); return task != null; } catch (Exception ex) @@ -594,6 +624,7 @@ namespace ServerManagerTool.Common.Utils Task task = null; TaskDefinition taskDefinition = null; + TaskLogonType taskLogonType = TaskLogonType.InteractiveToken; try { @@ -613,8 +644,9 @@ namespace ServerManagerTool.Common.Utils Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion); - taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken; + taskDefinition.Principal.LogonType = taskLogonType; taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; + taskDefinition.Principal.UserId = null; taskDefinition.RegistrationInfo.Description = "Server Auto-Update"; taskDefinition.RegistrationInfo.Source = "Server Manager"; @@ -654,7 +686,7 @@ namespace ServerManagerTool.Common.Utils try { - task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken); + task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType); return task != null; } catch (Exception ex)