Skip to content

Commit a357d11

Browse files
committed
v1.4.4 fixes; startup errors on laptop, mouse_click action & more
1 parent 9646971 commit a357d11

File tree

12 files changed

+187
-72
lines changed

12 files changed

+187
-72
lines changed

AssistantComputerControl/ActionMods.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ class ActionMods {
2626
//Check mod folder and init mods
2727
public static void CheckMods() {
2828
try {
29-
modActions = new Dictionary<string, string>();
30-
string[] dirs = Directory.GetDirectories(MainProgram.actionModsPath, "*", SearchOption.TopDirectoryOnly);
31-
32-
foreach (string dir in dirs) {
33-
string theFile = Path.Combine(dir, "info.json");
34-
if (File.Exists(theFile)) {
35-
//Info file exists - read it
36-
string fileContent = ReadInfoFile(theFile);
37-
if (fileContent != null) {
38-
ValidateAddMod(fileContent, dir);
29+
if (Directory.Exists(MainProgram.actionModsPath)) {
30+
//Action mods path exists
31+
modActions = new Dictionary<string, string>();
32+
string[] dirs = Directory.GetDirectories(MainProgram.actionModsPath, "*", SearchOption.TopDirectoryOnly);
33+
34+
foreach (string dir in dirs) {
35+
string theFile = Path.Combine(dir, "info.json");
36+
if (File.Exists(theFile)) {
37+
//Info file exists - read it
38+
string fileContent = ReadInfoFile(theFile);
39+
if (fileContent != null) {
40+
ValidateAddMod(fileContent, dir);
41+
} else {
42+
MainProgram.DoDebug("Failed to read info.json file at; " + dir);
43+
}
3944
} else {
40-
MainProgram.DoDebug("Failed to read info.json file at; " + dir);
45+
MainProgram.DoDebug("Invalid folder in action mods; '" + dir + "'. Doesn't contain an info.json file.");
4146
}
42-
} else {
43-
MainProgram.DoDebug("Invalid folder in action mods; '" + dir + "'. Doesn't contain an info.json file.");
4447
}
48+
} else {
49+
//Mod directory doesn't exist
50+
MainProgram.DoDebug("Can't check for mods as the folder doesn't exist");
4551
}
4652
} catch (Exception e) {
4753
Console.WriteLine("The process failed: {0}", e.ToString());

AssistantComputerControl/Actions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ public void MouseClick(string parameter = "", string secondaryParameter = "") {
10571057
*/
10581058

10591059
int timesToClick = 1;
1060-
string type = "left";
1060+
string type = parameter;
10611061

10621062
if (parameter == String.Empty) {
10631063
type = "left";
@@ -1129,6 +1129,11 @@ public void Wait(string parameter) {
11291129
}
11301130
}
11311131

1132+
//Fix for shortcuts; don't show an error
1133+
public void IgnoreMe() {
1134+
successMessage = "Ignoring filename";
1135+
}
1136+
11321137
/* End of actions */
11331138
}
11341139
}

AssistantComputerControl/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
<setting name="AutoFileMarginFixer" serializeAs="String">
8484
<value>0</value>
8585
</setting>
86+
<setting name="StartsUsingRegistry" serializeAs="String">
87+
<value>False</value>
88+
</setting>
8689
</AssistantComputerControl.Properties.Settings>
8790
</userSettings>
8891
<runtime>

AssistantComputerControl/AssistantComputerControl.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
3636
<WebPage>publish.htm</WebPage>
3737
<ApplicationRevision>0</ApplicationRevision>
38-
<ApplicationVersion>1.4.3.%2a</ApplicationVersion>
38+
<ApplicationVersion>1.4.4.%2a</ApplicationVersion>
3939
<UseApplicationTrust>false</UseApplicationTrust>
4040
<CreateDesktopShortcut>true</CreateDesktopShortcut>
4141
<PublishWizardCompleted>true</PublishWizardCompleted>
@@ -332,6 +332,7 @@
332332
<None Include="packages.config" />
333333
<None Include="Properties\Settings.settings">
334334
<Generator>SettingsSingleFileGenerator</Generator>
335+
<LastGenOutput>Settings1.Designer.cs</LastGenOutput>
335336
</None>
336337
<None Include="Resources\ACC_icon.ico" />
337338
</ItemGroup>

AssistantComputerControl/MainProgram.cs

Lines changed: 115 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* AssistantComputerControl
33
* Made by Albert MN.
4-
* Updated: v1.4.3, 21-04-2021
4+
* Updated: v1.4.4, 08-05-2021
55
*
66
* Use:
77
* - Main class. Starts everything.
@@ -23,14 +23,15 @@
2323
using System.Xml;
2424
using NLog;
2525
using Microsoft.Win32.TaskScheduler;
26+
using System.Security.Principal;
2627

2728
namespace AssistantComputerControl {
2829
class MainProgram {
2930
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
3132
appName = "AssistantComputerControl",
3233

33-
sentryToken = "super_secret";
34+
sentryToken = "https://[email protected]/1287269";
3435

3536
static public bool debug = true,
3637
unmuteVolumeChange = true,
@@ -75,9 +76,9 @@ static void Main(string[] args) {
7576

7677
var config = new NLog.Config.LoggingConfiguration();
7778
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");
7980
config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);
80-
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
81+
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
8182
NLog.LogManager.Configuration = config;
8283

8384
void ActualMain() {
@@ -139,6 +140,11 @@ void ActualMain() {
139140
Properties.Settings.Default.LastUpdated = DateTime.Now;
140141
}
141142

143+
//Create action mod path
144+
if (!Directory.Exists(actionModsPath)) {
145+
Directory.CreateDirectory(actionModsPath);
146+
}
147+
142148
//Translator
143149
string tempDir = Path.Combine(currentLocation, "Translations");
144150
if (Directory.Exists(tempDir)) {
@@ -314,6 +320,11 @@ void ActualMain() {
314320
}
315321

316322
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+
}
317328
}
318329

319330
Properties.Settings.Default.LastUpdated = DateTime.Now;
@@ -363,6 +374,7 @@ void ActualMain() {
363374

364375
hasStarted = true;
365376
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); //On wake up from sleep
377+
366378
Application.Run();
367379
}
368380

@@ -420,24 +432,23 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
420432

421433
public static void TaskSchedulerSetup () {
422434
//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");
426436

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();
429441
td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl";
430442
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+
434444
td.Actions.Add(new ExecAction("powershell.exe", $"-WindowStyle Hidden -file \"{ps1File}\" \"{Path.Combine(MainProgram.CheckPath(), "*")}\" \"*.{Properties.Settings.Default.ActionFileExtension}\"", null));
435445

436-
//Register the task in the root folder
446+
td.Triggers.Add(new LogonTrigger { UserId = userId, });
437447
ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl cleanup", td);
438448
}
439449
} 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);
441452
}
442453
}
443454

@@ -641,53 +652,112 @@ public static void Exit() {
641652
Environment.Exit(1);
642653
}
643654

655+
//Start with windows (set windows startup task)
644656
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+
648683
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);
660692
}
661-
} catch (Exception e) {
662-
DoDebug("Failed to create / update Task Scheduler startup service; " + e.Message);
663693
}
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
666709
try {
667710
using (TaskService ts = new TaskService()) {
668711
// Register the task in the root folder
669712
ts.RootFolder.DeleteTask(@"AssistantComputerControl startup");
670713
}
714+
DoDebug("ACC no longer starts with Windows (Task Scheduler)");
671715
} 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+
}
673720
}
674-
}
721+
} else {
722+
//Delete registry
723+
try {
724+
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
675725

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+
}
680734
}
681735
}
682-
}
736+
} //end "Start with Windows"
683737

684738
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;
688760
}
689-
} catch (Exception e) {
690-
DoDebug("Something went wrong with TaskService, checking if ACC starts with Windows; " + e.Message);
691761
}
692762

693763
return false;

AssistantComputerControl/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("1.4.3.0")]
37-
[assembly: AssemblyFileVersion("1.4.3.0")]
36+
[assembly: AssemblyVersion("1.4.4.0")]
37+
[assembly: AssemblyFileVersion("1.4.4.0")]
3838
[assembly: NeutralResourcesLanguage("en")]

AssistantComputerControl/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AssistantComputerControl/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,8 @@
8080
<Setting Name="AutoFileMarginFixer" Type="System.Int32" Scope="User">
8181
<Value Profile="(Default)">0</Value>
8282
</Setting>
83+
<Setting Name="StartsUsingRegistry" Type="System.Boolean" Scope="User">
84+
<Value Profile="(Default)">False</Value>
85+
</Setting>
8386
</Settings>
8487
</SettingsFile>

0 commit comments

Comments
 (0)