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 ;
56using System ;
67using System . Diagnostics ;
78using System . IO ;
8- using System . Linq ;
99using System . Text ;
1010using 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