|
1 | 1 | /* |
2 | 2 | * AssistantComputerControl |
3 | 3 | * Made by Albert MN. |
4 | | - * Updated: v1.4.3, 21-04-2021 |
| 4 | + * Updated: v1.4.4, 08-05-2021 |
5 | 5 | * |
6 | 6 | * Use: |
7 | 7 | * - Main class. Starts everything. |
|
23 | 23 | using System.Xml; |
24 | 24 | using NLog; |
25 | 25 | using Microsoft.Win32.TaskScheduler; |
| 26 | +using System.Security.Principal; |
26 | 27 |
|
27 | 28 | namespace AssistantComputerControl { |
28 | 29 | class MainProgram { |
29 | 30 | public const string softwareVersion = "1.4.4", |
30 | | - releaseDate = "2021-04-27 00:05:00", //YYYY-MM-DD H:i:s - otherwise it gives an error |
| 31 | + releaseDate = "2021-05-19 00:05:00", //YYYY-MM-DD H:i:s - otherwise it gives an error |
31 | 32 | appName = "AssistantComputerControl", |
32 | 33 |
|
33 | | - sentryToken = "super_secret"; |
| 34 | + sentryToken = "https://[email protected]/1287269"; |
34 | 35 |
|
35 | 36 | static public bool debug = true, |
36 | 37 | unmuteVolumeChange = true, |
@@ -75,9 +76,9 @@ static void Main(string[] args) { |
75 | 76 |
|
76 | 77 | var config = new NLog.Config.LoggingConfiguration(); |
77 | 78 | var logfile = new NLog.Targets.FileTarget("logfile") { FileName = logFilePath }; |
78 | | - var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); |
| 79 | + var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); |
79 | 80 | config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole); |
80 | | - config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile); |
| 81 | + config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile); |
81 | 82 | NLog.LogManager.Configuration = config; |
82 | 83 |
|
83 | 84 | void ActualMain() { |
@@ -139,6 +140,11 @@ void ActualMain() { |
139 | 140 | Properties.Settings.Default.LastUpdated = DateTime.Now; |
140 | 141 | } |
141 | 142 |
|
| 143 | + //Create action mod path |
| 144 | + if (!Directory.Exists(actionModsPath)) { |
| 145 | + Directory.CreateDirectory(actionModsPath); |
| 146 | + } |
| 147 | + |
142 | 148 | //Translator |
143 | 149 | string tempDir = Path.Combine(currentLocation, "Translations"); |
144 | 150 | if (Directory.Exists(tempDir)) { |
@@ -314,6 +320,11 @@ void ActualMain() { |
314 | 320 | } |
315 | 321 |
|
316 | 322 | SetStartup(true); |
| 323 | + } else { |
| 324 | + //Make sure the startup is up to date (fixed task errors in 1.4.4, for example) |
| 325 | + if (ACCStartsWithWindows()) { |
| 326 | + SetStartup(true); |
| 327 | + } |
317 | 328 | } |
318 | 329 |
|
319 | 330 | Properties.Settings.Default.LastUpdated = DateTime.Now; |
@@ -363,6 +374,7 @@ void ActualMain() { |
363 | 374 |
|
364 | 375 | hasStarted = true; |
365 | 376 | SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); //On wake up from sleep |
| 377 | + |
366 | 378 | Application.Run(); |
367 | 379 | } |
368 | 380 |
|
@@ -420,24 +432,23 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) |
420 | 432 |
|
421 | 433 | public static void TaskSchedulerSetup () { |
422 | 434 | //Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock |
423 | | - try { |
424 | | - using (TaskService ts = new TaskService()) { |
425 | | - var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1"); |
| 435 | + var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1"); |
426 | 436 |
|
427 | | - TaskDefinition td = ts.NewTask(); |
428 | | - td.Principal.LogonType = TaskLogonType.S4U; |
| 437 | + try { |
| 438 | + var userId = WindowsIdentity.GetCurrent().Name; |
| 439 | + using (var ts = new TaskService()) { |
| 440 | + var td = ts.NewTask(); |
429 | 441 | td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl"; |
430 | 442 | td.RegistrationInfo.Description = "AssistantComputerControl cleanup - clears the action folder to prevent the same action being executed twice"; |
431 | | - td.Triggers.Add(new BootTrigger()); |
432 | | - td.Triggers.Add(new LogonTrigger()); |
433 | | - td.Triggers.Add(new SessionStateChangeTrigger { StateChange = TaskSessionStateChangeType.SessionUnlock }); |
| 443 | + |
434 | 444 | td.Actions.Add(new ExecAction("powershell.exe", $"-WindowStyle Hidden -file \"{ps1File}\" \"{Path.Combine(MainProgram.CheckPath(), "*")}\" \"*.{Properties.Settings.Default.ActionFileExtension}\"", null)); |
435 | 445 |
|
436 | | - //Register the task in the root folder |
| 446 | + td.Triggers.Add(new LogonTrigger { UserId = userId, }); |
437 | 447 | ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl cleanup", td); |
438 | 448 | } |
439 | 449 | } catch (Exception e) { |
440 | | - DoDebug("Failed to create / update Task Scheduler service; " + e.Message); |
| 450 | + DoDebug("Failed to create / update cleanup Task Scheduler service; " + e.Message); |
| 451 | + Console.WriteLine(e); |
441 | 452 | } |
442 | 453 | } |
443 | 454 |
|
@@ -641,53 +652,112 @@ public static void Exit() { |
641 | 652 | Environment.Exit(1); |
642 | 653 | } |
643 | 654 |
|
| 655 | + //Start with windows (set windows startup task) |
644 | 656 | public static void SetStartup(bool status, bool setThroughSoftware = false) { |
645 | | - try { |
646 | | - if (status) { |
647 | | - //Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell |
| 657 | + if (status) { |
| 658 | + //Start |
| 659 | + bool failedStart = false, failedEnd = false; |
| 660 | + |
| 661 | + try { |
| 662 | + //Try start with Task Scheduler; |
| 663 | + var userId = WindowsIdentity.GetCurrent().Name; |
| 664 | + |
| 665 | + using (var ts = new TaskService()) { |
| 666 | + var td = ts.NewTask(); |
| 667 | + td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl"; |
| 668 | + td.RegistrationInfo.Description = "AssistantComputerControl startup - Runs ACC on reboot/login"; |
| 669 | + |
| 670 | + td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null)); |
| 671 | + |
| 672 | + td.Settings.StopIfGoingOnBatteries = false; |
| 673 | + td.Settings.DisallowStartIfOnBatteries = false; |
| 674 | + |
| 675 | + td.Triggers.Add(new LogonTrigger { UserId = userId, }); |
| 676 | + ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl startup", td); |
| 677 | + DoDebug("ACC now starts with Windows (Task Scheduler)"); |
| 678 | + } |
| 679 | + } catch (Exception e) { |
| 680 | + failedStart = true; |
| 681 | + DoDebug("Failed to create startup task. Defaulting to starting with Windows registry. Exception was; " + e.Message + ", trace; + " + e.StackTrace); |
| 682 | + |
648 | 683 | try { |
649 | | - using (TaskService ts = new TaskService()) { |
650 | | - TaskDefinition td = ts.NewTask(); |
651 | | - td.Principal.LogonType = TaskLogonType.InteractiveToken; |
652 | | - td.Principal.RunLevel = TaskRunLevel.Highest; |
653 | | - td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl"; |
654 | | - td.RegistrationInfo.Description = "AssistantComputerControl startup - Runs ACC on reboot/login"; |
655 | | - td.Triggers.Add(new LogonTrigger()); |
656 | | - td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null)); |
657 | | - |
658 | | - //Register the task in the root folder |
659 | | - ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl startup", td); |
| 684 | + RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
| 685 | + rk.SetValue(appName, Application.ExecutablePath); |
| 686 | + DoDebug("ACC now starts with Windows (Registry)"); |
| 687 | + } catch { |
| 688 | + failedEnd = true; |
| 689 | + DoDebug("Also failed to make ACC start with Windows using Registry; " + e.Message + ", " + e.StackTrace); |
| 690 | + if (!setThroughSoftware) { |
| 691 | + MessageBox.Show("Failed to make ACC start with Windows", messageBoxTitle); |
660 | 692 | } |
661 | | - } catch (Exception e) { |
662 | | - DoDebug("Failed to create / update Task Scheduler startup service; " + e.Message); |
663 | 693 | } |
664 | | - } else { |
665 | | - //Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell |
| 694 | + |
| 695 | + if (!failedEnd) { |
| 696 | + Properties.Settings.Default.StartsUsingRegistry = true; |
| 697 | + Properties.Settings.Default.Save(); |
| 698 | + } |
| 699 | + } |
| 700 | + |
| 701 | + if (!failedStart && !failedEnd) { |
| 702 | + Properties.Settings.Default.StartsUsingRegistry = false; |
| 703 | + Properties.Settings.Default.Save(); |
| 704 | + } |
| 705 | + } else { |
| 706 | + /* No longer start with Windows */ |
| 707 | + if (!Properties.Settings.Default.StartsUsingRegistry) { |
| 708 | + //Delete task |
666 | 709 | try { |
667 | 710 | using (TaskService ts = new TaskService()) { |
668 | 711 | // Register the task in the root folder |
669 | 712 | ts.RootFolder.DeleteTask(@"AssistantComputerControl startup"); |
670 | 713 | } |
| 714 | + DoDebug("ACC no longer starts with Windows (Task Scheduler)"); |
671 | 715 | } catch (Exception e) { |
672 | | - DoDebug("Failed to create / update Task Scheduler startup service; " + e.Message); |
| 716 | + DoDebug("Failed to make ACC stop starting with Windows by deleting scheduled task. Last exception; " + e.Message + ", trace; " + e.StackTrace); |
| 717 | + if (!setThroughSoftware) { |
| 718 | + MessageBox.Show("Failed to make ACC stop starting with Windows. Check the log and contact the developer.", messageBoxTitle); |
| 719 | + } |
673 | 720 | } |
674 | | - } |
| 721 | + } else { |
| 722 | + //Delete registry |
| 723 | + try { |
| 724 | + RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
675 | 725 |
|
676 | | - } catch { |
677 | | - DoDebug("Failed to start ACC with Windows"); |
678 | | - if (!setThroughSoftware) { |
679 | | - MessageBox.Show("Failed to make ACC start with Windows", messageBoxTitle); |
| 726 | + rk.DeleteValue(appName, false); |
| 727 | + DoDebug("ACC no longer starts with Windows (Registry)"); |
| 728 | + } catch (Exception e) { |
| 729 | + DoDebug("Failed to make ACC stop starting with Windows by deleting Registry key. Last exception; " + e.Message + ", trace; " + e.StackTrace); |
| 730 | + if (!setThroughSoftware) { |
| 731 | + MessageBox.Show("Failed to make ACC stop starting with Windows. Check the log and contact the developer.", messageBoxTitle); |
| 732 | + } |
| 733 | + } |
680 | 734 | } |
681 | 735 | } |
682 | | - } |
| 736 | + } //end "Start with Windows" |
683 | 737 |
|
684 | 738 | public static bool ACCStartsWithWindows() { |
685 | | - try { |
686 | | - using (TaskService ts = new TaskService()) { |
687 | | - return ts.GetTask(@"AssistantComputerControl startup") != null; |
| 739 | + if (!Properties.Settings.Default.StartsUsingRegistry) { |
| 740 | + try { |
| 741 | + using (TaskService ts = new TaskService()) { |
| 742 | + return ts.GetTask(@"AssistantComputerControl startup") != null; |
| 743 | + } |
| 744 | + } catch (Exception e) { |
| 745 | + DoDebug("Something went wrong with TaskService, checking if ACC starts with Windows (Task Scheduler); " + e.Message); |
| 746 | + } |
| 747 | + } else { |
| 748 | + try { |
| 749 | + RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); |
| 750 | + |
| 751 | + var theVal = rk.GetValue(appName); |
| 752 | + if (theVal != null) { |
| 753 | + return true; |
| 754 | + } else { |
| 755 | + return false; |
| 756 | + } |
| 757 | + } catch { |
| 758 | + DoDebug("Failed to get ACC start with windows state (Registry)"); |
| 759 | + return false; |
688 | 760 | } |
689 | | - } catch (Exception e) { |
690 | | - DoDebug("Something went wrong with TaskService, checking if ACC starts with Windows; " + e.Message); |
691 | 761 | } |
692 | 762 |
|
693 | 763 | return false; |
|
0 commit comments