Skip to content

Commit

Permalink
update autostart task with opened exe when Aurora opens
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Mar 21, 2024
1 parent adca8e7 commit cb52d55
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 67 deletions.
2 changes: 2 additions & 0 deletions Project-Aurora/Project-Aurora/Modules/UpdateCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AuroraRgb.Utils;
using Lombok.NET;

namespace AuroraRgb.Modules;
Expand All @@ -13,6 +14,7 @@ public partial class UpdateCleanup : AuroraModule
{
protected override Task Initialize()
{
AutoStartUtils.GetAutostartTask(out _);
if (!Global.Configuration.Migrations.Contains("net8v2"))
{
Net8V2Migration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:settings="clr-namespace:AuroraRgb.Settings"
xmlns:audioCapture="clr-namespace:AuroraRgb.Modules.AudioCapture"
xmlns:controls="clr-namespace:AuroraRgb.Settings.Controls"
Loaded="Control_SettingsGeneral_OnLoaded"
mc:Ignorable="d">
<UserControl.Resources>
<utils1:StringFormatConverter Format="x {0:0.0}" x:Key="AwayEffectSpeedLabelFormatter" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,32 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using AuroraRgb.Utils;
using AuroraRgb.Utils.IpApi;
using Microsoft.Win32.TaskScheduler;
using Xceed.Wpf.Toolkit;

namespace AuroraRgb.Settings.Controls;

public partial class Control_SettingsGeneral
{
private const string StartupTaskId = "AuroraStartup";
/// <summary>The excluded program the user has selected in the excluded list.</summary>
private string SelectedExcludedProgram { get; set; } = string.Empty;

public Control_SettingsGeneral()
{
InitializeComponent();

TransparencyCheckbox.IsEnabled = TransparencyComponent.UseMica;

DataContext = Global.Configuration;

SetAutostartTask();
}

private void SetAutostartTask()
private void Control_SettingsGeneral_OnLoaded(object sender, RoutedEventArgs e)
{
try
{
using var service = new TaskService();
var task = service.FindTask(StartupTaskId);
var exePath = Assembly.GetExecutingAssembly().Location.Replace(".dll", ".exe");

var taskDefinition = task != null ? task.Definition : service.NewTask();

//Update path of startup task
taskDefinition.RegistrationInfo.Description = "Start Aurora on Startup";
taskDefinition.Actions.Clear();
taskDefinition.Actions.Add(new ExecAction(exePath, "-silent", Path.GetDirectoryName(exePath)));
if (task != null)
{
startDelayAmount.Value = task.Definition.Triggers.FirstOrDefault(t =>
t.TriggerType == TaskTriggerType.Logon
) is LogonTrigger trigger
? (int)trigger.Delay.TotalSeconds
: 0;
}
else
{
taskDefinition.Triggers.Add(new LogonTrigger { Enabled = true });

taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
}

task = service.RootFolder.RegisterTaskDefinition(StartupTaskId, taskDefinition);
RunAtWinStartup.IsChecked = task.Enabled;
}
catch (Exception exc)
{
Global.logger.Error(exc, "Error caught when updating startup task");
}
DataContext = Global.Configuration;
RunAtWinStartup.IsChecked = AutoStartUtils.GetAutostartTask(out var delayAmount);
startDelayAmount.Value = delayAmount;
}

/// <summary>The excluded program the user has selected in the excluded list.</summary>
public string SelectedExcludedProgram { get; set; }

private void ExcludedAdd_Click(object? sender, RoutedEventArgs e)
{
var dialog = new Window_ProcessSelection { ButtonLabel = "Exclude Process" };
Expand All @@ -90,32 +46,19 @@ private void ExcludedRemove_Click(object? sender, RoutedEventArgs e)
private void RunAtWinStartup_Checked(object? sender, RoutedEventArgs e)
{
if (!IsLoaded || sender is not CheckBox checkBox) return;
try
{
using var ts = new TaskService();
//Find existing task
var task = ts.FindTask(StartupTaskId);
task.Enabled = checkBox.IsChecked.Value;
}
catch (Exception exc)
{
Global.logger.Error(exc, "RunAtWinStartup_Checked Exception: ");
}
AutoStartUtils.SetEnabled(checkBox.IsChecked.GetValueOrDefault());
}

private void HighPriorityCheckbox_Checked(object? sender, RoutedEventArgs e)
{
if (!IsLoaded) return;
Process.GetCurrentProcess().PriorityClass = Global.Configuration.HighPriority ? ProcessPriorityClass.High : ProcessPriorityClass.Normal;
}

private void StartDelayAmount_ValueChanged(object? sender, RoutedPropertyChangedEventArgs<object> e)
{
using var service = new TaskService();
var task = service.FindTask(StartupTaskId);
if (task?.Definition.Triggers.FirstOrDefault(t => t.TriggerType == TaskTriggerType.Logon) is not
LogonTrigger trigger) return;
trigger.Delay = new TimeSpan(0, 0, ((IntegerUpDown)sender).Value ?? 0);
task.RegisterChanges();
if (!IsLoaded || sender is not IntegerUpDown integerUpDown) return;
AutoStartUtils.SetStartupDelay(integerUpDown.Value ?? 0);
}

private async void ResetLocation_Clicked(object sender, RoutedEventArgs e)
Expand Down
81 changes: 81 additions & 0 deletions Project-Aurora/Project-Aurora/Utils/AutoStartUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Win32.TaskScheduler;

namespace AuroraRgb.Utils;

public static class AutoStartUtils
{
private const string StartupTaskId = "AuroraStartup";

public static bool GetAutostartTask(out int startDelayAmount)
{
try
{
using var service = new TaskService();
var task = service.FindTask(StartupTaskId);
var exePath = Assembly.GetExecutingAssembly().Location.Replace(".dll", ".exe");

var taskDefinition = task != null ? task.Definition : service.NewTask();

//Update path of startup task
taskDefinition.RegistrationInfo.Description = "Start Aurora on Startup";
taskDefinition.Actions.Clear();
taskDefinition.Actions.Add(new ExecAction(exePath, "-silent", Path.GetDirectoryName(exePath)));
if (task != null)
{
startDelayAmount = task.Definition.Triggers.FirstOrDefault(t =>
t.TriggerType == TaskTriggerType.Logon
) is LogonTrigger trigger
? (int)trigger.Delay.TotalSeconds
: 0;
}
else
{
startDelayAmount = 0;
taskDefinition.Triggers.Add(new LogonTrigger { Enabled = true });

taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
}

task = service.RootFolder.RegisterTaskDefinition(StartupTaskId, taskDefinition);
return task.Enabled;
}
catch (Exception exc)
{
Global.logger.Error(exc, "Error caught when updating startup task");
startDelayAmount = 0;
return false;
}
}

public static void SetStartupDelay(int delay)
{
using var service = new TaskService();
var task = service.FindTask(StartupTaskId);
if (task?.Definition.Triggers.FirstOrDefault(t => t.TriggerType == TaskTriggerType.Logon) is not
LogonTrigger trigger) return;
trigger.Delay = new TimeSpan(0, 0, delay);
task.RegisterChanges();
}

public static void SetEnabled(bool isEnabled)
{
try
{
using var ts = new TaskService();
//Find existing task
var task = ts.FindTask(StartupTaskId);
task.Enabled = isEnabled;
}
catch (Exception exc)
{
Global.logger.Error(exc, "RunAtWinStartup_Checked Exception: ");
}
}
}

0 comments on commit cb52d55

Please sign in to comment.