Initial working dynamic start programs

The startprograms logic should now
work properly. The JSON transformation
from using Enabled to Disable setting has
been tested and works too.
This commit is contained in:
Terry MacDonald 2021-05-14 15:37:40 +12:00
parent 4a1c4cf370
commit dcb55f0063
5 changed files with 91 additions and 20 deletions

View File

@ -739,6 +739,7 @@ namespace DisplayMagician
get => new Version(1, 0); get => new Version(1, 0);
} }
public string UUID public string UUID
{ {
get get

View File

@ -5,6 +5,7 @@ using DisplayMagician.InterProcess;
using DisplayMagicianShared; using DisplayMagicianShared;
using Microsoft.Toolkit.Uwp.Notifications; using Microsoft.Toolkit.Uwp.Notifications;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -396,7 +397,7 @@ namespace DisplayMagician
{ {
string json = ""; string json = "";
try try
{ {
json = File.ReadAllText(_shortcutStorageJsonFileName, Encoding.Unicode); json = File.ReadAllText(_shortcutStorageJsonFileName, Encoding.Unicode);
} }
catch (Exception ex) catch (Exception ex)
@ -406,6 +407,29 @@ namespace DisplayMagician
if (!string.IsNullOrWhiteSpace(json)) if (!string.IsNullOrWhiteSpace(json))
{ {
// "Disabled": false,
// Firstly perform any modifications we need to do to update the JSON structure
// to handle old versions of the file that need updating. Done with a simple regex replace
try
{
// Replace any "Enabled": true with "Disabled": false
json = Regex.Replace(json, @" ""Enabled"": true,", @" ""Disabled"": false,");
// Replace any "Enabled": false with "Disabled": true
json = Regex.Replace(json, @" ""Enabled"": false,", @" ""Disabled"": true,");
}
catch(Exception ex)
{
// problem updating JSON
logger.Error(ex, $"ShortcutRepository/LoadShortcuts: Tried to update the JSON in the {_shortcutStorageJsonFileName} but the Regex Replace threw an exception.");
}
#pragma warning disable IDE0059 // Unnecessary assignment of a value #pragma warning disable IDE0059 // Unnecessary assignment of a value
List<ShortcutItem> shortcuts = new List<ShortcutItem>(); List<ShortcutItem> shortcuts = new List<ShortcutItem>();
#pragma warning restore IDE0059 // Unnecessary assignment of a value #pragma warning restore IDE0059 // Unnecessary assignment of a value
@ -773,7 +797,7 @@ namespace DisplayMagician
// Now run the pre-start applications // Now run the pre-start applications
List<Process> startProgramsToStop = new List<Process>(); List<Process> startProgramsToStop = new List<Process>();
List<StartProgram> startProgramsToStart = shortcutToUse.StartPrograms.Where(program => program.Disabled == true).OrderBy(program => program.Priority).ToList(); List<StartProgram> startProgramsToStart = shortcutToUse.StartPrograms.Where(program => program.Disabled == false).Where(program => !String.IsNullOrWhiteSpace(program.Executable)).OrderBy(program => program.Priority).ToList();
if (startProgramsToStart.Count > 0) if (startProgramsToStart.Count > 0)
{ {
logger.Info($"ShortcutRepository/RunShortcut: Starting {startProgramsToStart.Count} programs before the main game or executable"); logger.Info($"ShortcutRepository/RunShortcut: Starting {startProgramsToStart.Count} programs before the main game or executable");

View File

@ -1172,6 +1172,12 @@ namespace DisplayMagician.UIForms
int spOrder = 1; int spOrder = 1;
foreach (StartProgram myStartProgram in _shortcutToEdit.StartPrograms.OrderBy(sp => sp.Priority)) foreach (StartProgram myStartProgram in _shortcutToEdit.StartPrograms.OrderBy(sp => sp.Priority))
{ {
if (String.IsNullOrWhiteSpace(myStartProgram.Executable))
{
logger.Warn($"ShortcutForm/ShortcutForm_Load: Start program #{myStartProgram.Priority} is empty, so skipping.");
continue;
}
StartProgramControl startProgramControl = new StartProgramControl(myStartProgram,spOrder); StartProgramControl startProgramControl = new StartProgramControl(myStartProgram,spOrder);
startProgramControl.Dock = DockStyle.None; startProgramControl.Dock = DockStyle.None;
if (spOrder == 1) if (spOrder == 1)

View File

@ -57,6 +57,7 @@ namespace DisplayMagician.UIForms
this.cb_dont_start_if_running.Text = "Don\'t start if program already running"; this.cb_dont_start_if_running.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_dont_start_if_running.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running.UseVisualStyleBackColor = true; this.cb_dont_start_if_running.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running.CheckedChanged += new System.EventHandler(this.cb_dont_start_if_running_CheckedChanged);
// //
// txt_start_program // txt_start_program
// //
@ -67,6 +68,7 @@ namespace DisplayMagician.UIForms
this.txt_start_program.Name = "txt_start_program"; this.txt_start_program.Name = "txt_start_program";
this.txt_start_program.Size = new System.Drawing.Size(538, 26); this.txt_start_program.Size = new System.Drawing.Size(538, 26);
this.txt_start_program.TabIndex = 25; this.txt_start_program.TabIndex = 25;
this.txt_start_program.TextChanged += new System.EventHandler(this.txt_start_program_TextChanged);
// //
// cb_start_program_close // cb_start_program_close
// //
@ -80,6 +82,7 @@ namespace DisplayMagician.UIForms
this.cb_start_program_close.Text = "Close started program when you finish playing Game"; this.cb_start_program_close.Text = "Close started program when you finish playing Game";
this.cb_start_program_close.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_start_program_close.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_start_program_close.UseVisualStyleBackColor = true; this.cb_start_program_close.UseVisualStyleBackColor = true;
this.cb_start_program_close.CheckedChanged += new System.EventHandler(this.cb_start_program_close_CheckedChanged);
// //
// btn_start_program // btn_start_program
// //
@ -104,6 +107,7 @@ namespace DisplayMagician.UIForms
this.txt_start_program_args.Name = "txt_start_program_args"; this.txt_start_program_args.Name = "txt_start_program_args";
this.txt_start_program_args.Size = new System.Drawing.Size(506, 26); this.txt_start_program_args.Size = new System.Drawing.Size(506, 26);
this.txt_start_program_args.TabIndex = 22; this.txt_start_program_args.TabIndex = 22;
this.txt_start_program_args.TextChanged += new System.EventHandler(this.txt_start_program_args_TextChanged);
// //
// cb_start_program_pass_args // cb_start_program_pass_args
// //

View File

@ -82,26 +82,28 @@ namespace DisplayMagician.UIForms
private void cb_start_program_CheckedChanged(object sender, EventArgs e) private void cb_start_program_CheckedChanged(object sender, EventArgs e)
{ {
// Disable the start program fields // Disable the start program fields
if (!cb_disable_start_program.Checked) if (cb_disable_start_program.Checked)
{ {
// Enable the Executable Arguments Text field myStartProgram.Disabled = true;
txt_start_program.Visible = true; // Disable the Executable Arguments Text field
btn_start_program.Visible = true; txt_start_program.Enabled = false;
txt_start_program_args.Visible = true; btn_start_program.Enabled = false;
cb_start_program_pass_args.Visible = true; txt_start_program_args.Enabled = false;
cb_start_program_close.Visible = true; cb_start_program_pass_args.Enabled = false;
cb_dont_start_if_running.Visible = true; cb_start_program_close.Enabled = false;
cb_dont_start_if_running.Enabled = false;
} }
else else
{ {
// Disable the Executable Arguments Text field myStartProgram.Disabled = false;
txt_start_program.Visible = false; // Enable the Executable Arguments Text field
btn_start_program.Visible = false; txt_start_program.Enabled = true;
txt_start_program_args.Visible = false; btn_start_program.Enabled = true;
cb_start_program_pass_args.Visible = false; txt_start_program_args.Enabled = true;
cb_start_program_close.Visible = false; cb_start_program_pass_args.Enabled = true;
cb_dont_start_if_running.Visible = false; cb_start_program_close.Enabled = true;
} cb_dont_start_if_running.Enabled = true;
}
} }
private void cb_start_program_pass_args_CheckedChanged(object sender, EventArgs e) private void cb_start_program_pass_args_CheckedChanged(object sender, EventArgs e)
@ -110,12 +112,12 @@ namespace DisplayMagician.UIForms
if (cb_start_program_pass_args.Checked) if (cb_start_program_pass_args.Checked)
{ {
// Enable the Executable Arguments Text field // Enable the Executable Arguments Text field
txt_start_program_args.Visible = true; txt_start_program_args.Enabled = true;
} }
else else
{ {
// Disable the Executable Arguments Text field // Disable the Executable Arguments Text field
txt_start_program_args.Visible = false; txt_start_program_args.Enabled = false;
} }
} }
@ -177,5 +179,39 @@ namespace DisplayMagician.UIForms
{ {
((ShortcutForm)this.Parent.Parent.Parent.Parent).StartProgramEarlier(this); ((ShortcutForm)this.Parent.Parent.Parent.Parent).StartProgramEarlier(this);
} }
private void cb_dont_start_if_running_CheckedChanged(object sender, EventArgs e)
{
if (cb_dont_start_if_running.Checked)
{
myStartProgram.DontStartIfAlreadyRunning = true;
}
else
{
myStartProgram.DontStartIfAlreadyRunning = false;
}
}
private void cb_start_program_close_CheckedChanged(object sender, EventArgs e)
{
if (cb_start_program_close.Checked)
{
myStartProgram.CloseOnFinish = true;
}
else
{
myStartProgram.CloseOnFinish = false;
}
}
private void txt_start_program_TextChanged(object sender, EventArgs e)
{
myStartProgram.Executable = txt_start_program.Text;
}
private void txt_start_program_args_TextChanged(object sender, EventArgs e)
{
myStartProgram.Arguments = txt_start_program_args.Text;
}
} }
} }