Skip to content

Commit 9ba3280

Browse files
committed
Update script manager to pass event data via stdin
1 parent 109490e commit 9ba3280

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

src/ConfigMigrator.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,46 @@ public static void UpdateConfig(string jsonText)
6060
UpdateToVersion5(jsonText);
6161
}
6262

63-
public static void UpdateObsoleteSettings() // Added 2024-01-22
63+
public static bool UpdateObsoleteSettings(string jsonText) // Added 2024-01-22
6464
{
65+
bool settingsChanged = false;
6566
#pragma warning disable 618
6667
if (JsonConfig.settings.changeLockScreen)
6768
{
6869
JsonConfig.settings.lockScreenDisplayIndex = Array.FindIndex(JsonConfig.settings.activeThemes,
6970
(themeId) => themeId != null);
7071
JsonConfig.settings.changeLockScreen = false;
72+
settingsChanged = true;
7173
}
7274
if (JsonConfig.settings.enableShuffle)
7375
{
7476
JsonConfig.settings.themeShuffleMode = 22;
7577
JsonConfig.settings.enableShuffle = false;
78+
settingsChanged = true;
7679
}
7780
if (JsonConfig.settings.lastShuffleDate != null)
7881
{
7982
JsonConfig.settings.lastShuffleTime = JsonConfig.settings.lastShuffleDate;
8083
JsonConfig.settings.lastShuffleDate = null;
84+
settingsChanged = true;
8185
}
82-
if (JsonConfig.settings.darkMode == true)
86+
if (JsonConfig.settings.darkMode)
8387
{
8488
JsonConfig.settings.appearanceMode = (int)AppearanceMode.Dark;
8589
JsonConfig.settings.darkMode = false;
90+
settingsChanged = true;
8691
}
8792
#pragma warning restore 618
93+
if (JsonConfig.settings.enableScripts && jsonText?.IndexOf("\"appearanceMode\"") == -1)
94+
{
95+
MessageDialog.ShowInfo("Updated to WinDynamicDesktop 5.5 successfully. PowerShell scripts that are " +
96+
"outdated have been temporarily disabled.\n\nTo update them, download the latest scripts from " +
97+
"https://windd.info/scripts/ and revise any custom scripts.\n\nAfter scripts have been updated, " +
98+
"you can re-enable PowerShell scripts in the \"More Options\" menu.");
99+
JsonConfig.settings.enableScripts = false;
100+
settingsChanged = true;
101+
}
102+
return settingsChanged;
88103
}
89104

90105
private static DateTime SafeParse(string dateTime) // Added 2020-05-21

src/EventScheduler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public void Run(bool forceImageUpdate = false, DisplayEvent overrideEvent = null
116116
ScriptManager.RunScripts(new ScriptArgs
117117
{
118118
daySegment2 = displayEvents[0].daySegment2,
119-
daySegment4 = displayEvents[0].daySegment4,
119+
daySegment4 = displayEvents[0].daySegment4 ?? -1,
120+
themeMode = JsonConfig.settings.appearanceMode,
120121
imagePaths = displayEvents.Select(e =>
121122
(e.displayIndex == overrideEvent?.displayIndex ? overrideEvent : e).lastImagePath).ToArray()
122123
}, forceImageUpdate);

src/JsonConfig.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ public static void LoadConfig()
6969
{
7070
autoSaveTimer?.Stop();
7171
ConfigMigrator.RenameSettingsFile();
72+
string jsonText = null;
7273

7374
if (!firstRun)
7475
{
7576
try
7677
{
77-
string jsonText = File.ReadAllText("settings.json");
78+
jsonText = File.ReadAllText("settings.json");
7879
ConfigMigrator.UpdateConfig(jsonText);
7980
settings = JsonConvert.DeserializeObject<AppConfig>(jsonText);
8081
}
@@ -94,10 +95,10 @@ public static void LoadConfig()
9495
}
9596
}
9697

97-
unsavedChanges = false;
98+
unsavedChanges = ConfigMigrator.UpdateObsoleteSettings(jsonText);
9899
autoSaveTimer = new System.Timers.Timer(1000);
99100
autoSaveTimer.AutoReset = false;
100-
ConfigMigrator.UpdateObsoleteSettings();
101+
autoSaveTimer.Enabled = unsavedChanges;
101102

102103
settings.PropertyChanged += OnSettingsPropertyChanged;
103104
autoSaveTimer.Elapsed += OnAutoSaveTimerElapsed;

src/ScriptManager.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
using Newtonsoft.Json;
56
using System;
67
using System.Diagnostics;
78
using System.IO;
8-
using System.Linq;
99
using System.Text;
1010
using System.Threading.Tasks;
1111

@@ -14,20 +14,15 @@ namespace WinDynamicDesktop
1414
class ScriptArgs
1515
{
1616
public int daySegment2;
17-
public int? daySegment4;
17+
public int daySegment4;
18+
public int themeMode;
1819
public string[] imagePaths;
19-
20-
public bool Equals(ScriptArgs other)
21-
{
22-
return other != null && this.daySegment2 == other.daySegment2 &&
23-
this.daySegment4 == other.daySegment4 && this.imagePaths.SequenceEqual(other.imagePaths);
24-
}
2520
}
2621

2722
class ScriptManager
2823
{
2924
private static readonly Func<string, string> _ = Localization.GetTranslation;
30-
private static ScriptArgs lastArgs;
25+
private static string lastArgs;
3126

3227
public static void Initialize()
3328
{
@@ -60,28 +55,29 @@ public static void ToggleEnableScripts()
6055

6156
public static void RunScripts(ScriptArgs args, bool forceUpdate = false)
6257
{
63-
if (!JsonConfig.settings.enableScripts || (args.Equals(lastArgs) && !forceUpdate))
58+
string jsonArgs = JsonConvert.SerializeObject(args, Formatting.None);
59+
if (!JsonConfig.settings.enableScripts || (jsonArgs.Equals(lastArgs) && !forceUpdate))
6460
{
6561
return;
6662
}
6763

6864
LoggingHandler.LogMessage("Running scripts with arguments: {0}", args);
6965
foreach (string scriptPath in Directory.EnumerateFiles("scripts", "*.ps1"))
7066
{
71-
Task.Run(() => RunScript(scriptPath, args));
67+
Task.Run(() => RunScript(scriptPath, jsonArgs));
7268
}
73-
lastArgs = args;
69+
lastArgs = jsonArgs;
7470
}
7571

76-
private static async void RunScript(string path, ScriptArgs args)
72+
private static async void RunScript(string path, string jsonArgs)
7773
{
7874
Process proc = new Process();
79-
string command = BuildCommandString(Path.GetFileName(path), args);
8075
proc.StartInfo = new ProcessStartInfo(ExistsOnPath("pwsh.exe") ? "pwsh.exe" : "powershell.exe",
81-
"-NoProfile -ExecutionPolicy Bypass -Command \"" + command + "\"")
76+
"-NoProfile -ExecutionPolicy Bypass -File \"" + Path.GetFileName(path) + "\"")
8277
{
8378
CreateNoWindow = true,
8479
RedirectStandardError = true,
80+
RedirectStandardInput = true,
8581
UseShellExecute = false,
8682
WindowStyle = ProcessWindowStyle.Hidden,
8783
WorkingDirectory = Path.GetDirectoryName(path)
@@ -98,6 +94,10 @@ private static async void RunScript(string path, ScriptArgs args)
9894
};
9995
proc.Start();
10096
proc.BeginErrorReadLine();
97+
using (StreamWriter sw = proc.StandardInput)
98+
{
99+
sw.WriteLine(jsonArgs);
100+
}
101101
await proc.WaitForExitAsync();
102102

103103
if (proc.ExitCode != 0 || errors.Length > 0)
@@ -108,19 +108,6 @@ private static async void RunScript(string path, ScriptArgs args)
108108
}
109109
}
110110

111-
private static string BuildCommandString(string filename, ScriptArgs args)
112-
{
113-
string cmdStr = "& \\\".\\" + filename + "\\\"" +
114-
" -daySegment2 " + args.daySegment2.ToString() +
115-
" -daySegment4 " + (args.daySegment4 ?? -1).ToString() +
116-
" -nightMode " + Convert.ToInt32(JsonConfig.settings.darkMode);
117-
if (args.imagePaths.Length > 0)
118-
{
119-
cmdStr += " -imagePath \\\"" + args.imagePaths[0] + "\\\"";
120-
}
121-
return cmdStr;
122-
}
123-
124111
private static bool ExistsOnPath(string filename)
125112
{
126113
if (File.Exists(filename))

0 commit comments

Comments
 (0)