From 1f4684e81526c9f236e78bf39de804c7ed694440 Mon Sep 17 00:00:00 2001 From: sharpjd <59419827+sharpjd@users.noreply.github.com> Date: Sun, 10 Mar 2024 12:49:07 -0400 Subject: [PATCH 1/4] Modified log messages, fixed build paths Currently determining why the File Copier doesn't work when installed in Program Files --- Assemble/install/Install.exe | Bin 8192 -> 8704 bytes Assemble/install/Uninstall.exe | Bin 8192 -> 8704 bytes GPUPrefSwitcher.sln | 4 ++-- GPUPrefSwitcher/AppLogger.cs | 3 +-- GPUPrefSwitcher/FileSwapper.cs | 8 +++----- GPUPrefSwitcher/GPUPrefSwitcher.csproj | 2 +- GPUPrefSwitcher/RegistryHelper.cs | 6 +++--- GPUPrefSwitcher/Switcher.cs | 5 +++-- Install/Install.csproj | 2 +- Uninstall/Uninstall.csproj | 2 +- 10 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Assemble/install/Install.exe b/Assemble/install/Install.exe index 1c2a78aaee9e8fdf9e979a3ec55a54803357b3c0..829d54c33f30d470cb55eb7a2a095d0463bbf2b9 100644 GIT binary patch delta 966 zcmYjQOH30{6g@MYw!?Jjhf)fdn3i7#&_Trou^|>pK>`SdXf&EwK7|-6#VIDlm{1dq znrLEq3xh6*#)ZbX&@M<5UAR$}!cG?iH*QcDYFugbzL_>~JMY~4&i%~0uXnQ1Y|p*x zi-Up<8{5fM6lDu_a0v$hm554<>~19;q7|jZAw)_9BvM=)mIAV55YNSi#0|h}K0CU& zwLorlek_&J=O)JyD9#iw(q?`^ zxp38?(xh<~I3G(J@rC5j*o9eK%uJ+>WKy3@r7S1#uMwdrTUX!b-R>Vgg-ug1WmIRRI#jQsIWXW#2fMmUVvrhAO8lRE|`9Ue}7i|UwzsYLf0*I5* zP-%I}9!x2C&=H*c65H8``NrLV7Bh|k6RI&ojC)8J_Y&gU3A>F0B-ZH8+NQZp%72dK zi1A%4^occof^k~vP6J|#WhA7HJ60-)IO#+o7Vo8lY6wvNZf}NVxP%= z!8lHujRL+=W5{d_o4kT`Qm!E`B~im+{!)o~^Pt~J(S0YP&bPlP3l_(YnX7-S=hg;a zWT)o8emd&mb6Rgiwn{pAT{$@nT>;Z|U4=}fB)KlQiu|+ogfZ|NwncAYHMcG{WHcVp zNSjaRxIOC7J-5zU`%;Caz=zwzdmm2nx_!)jzFaMj)XEog*K75WYBf1Adb#ZkXQj5T H|8o8V!102n diff --git a/Assemble/install/Uninstall.exe b/Assemble/install/Uninstall.exe index c8920a5fe627ea1efec3ff844fcae47b3e3612cb..e574afc67accd2d6bdecc7ab5039ecd78c511748 100644 GIT binary patch delta 1010 zcmYjQ-%C?r7=FHUoSWM^&CR(|i{qAd(pY9xmWoRqYouk25me%w4jIjzakfAZ+Jq8< zi0KK#Afh|NtMVrQfPP$f5p_{FhEf+@bmc`z3H5$wn!cRxdEW2&-tYZ>y{8Z;^cL^t z`m5yZ9NXRH8 zS*_X$arSb09!y3l*tJ7~-X)&aGoog)A}px-y6D!o#1_vSt`23>?89Qf{Gv`H@8{ ztAXBU^45fmtJ^JEewGkSbo8ZZ)0r~I?A!45j*g+=)9JJDeP-+WN=40Kvssj!L1&oi z+}SV6kW3IHNh$|(Mx8-@t729#Greei?aqJvvB*hGeWICH0PNn>r(Kv>;YmldznOp^idQ>eq tk?0>zrZJu!QME+EKblIF27$i~kONDV_DQxRNgI_`$faMQc%H;x>>m&po~i%< delta 813 zcmX|LO%2w1d$*`k0sWF4+Z_so%)0O`F+p#_dEBV`@6T?SB^cO zEsZ_l$ZiEcVn24!3MY>M4P>|vT*`)}V@xKgIbf5T!d^>2iUj#G6r>&lF-spLz*Sot zwL}z)PQ4OQ)flknsssaSUpsJFR2zEy)HHC|2ZX26(^Ir=6f>+}oMR!sta7N>q|Ra; zAdg0uopwh`%@GAjGGv4r0Y{FW?Z{JoGNh9_oj*&mKyT`%n|DBF$z)3SColN*tTrl8I2P76d*=@c)Ntly3(SGgU!#h=W8196_? zB49i3o5x_;hj|Pba%%+*yh77h7o)cEQU%RCKpvEnc!_H;QN;vz5#KT|@hj=q*yZh5 zK{p~6JBfSz3TCL&4vjwc0I?3^2*Zg*q6bUFX1phw*d(68C*lQ*-4=VLZ{h~NP+`cj zk`_nto16z|VPO=|!9-SVWl>R4J+>%RzWPSJXmh<)dh0fTn_Lnq~)cC)|S~ { logger.DumpStandardLogBufferToStandardLog().Wait(); @@ -119,7 +119,6 @@ public static AppLogger Initialize (string logFolderPath_Error, string logFileNa logger.ErrorLog(e.ToString()).Wait(); }; - */ return logger; } diff --git a/GPUPrefSwitcher/FileSwapper.cs b/GPUPrefSwitcher/FileSwapper.cs index 945fbfb..ff86fa7 100644 --- a/GPUPrefSwitcher/FileSwapper.cs +++ b/GPUPrefSwitcher/FileSwapper.cs @@ -101,7 +101,7 @@ public void InitiateFileSwaps(PowerLineStatus forPowerLineStatus, PreferencesXML } //...repeat for every SwapPath - Logger.inst.Log($"Finished FileSwap for AppEntry with target path {AppEntry.AppPath}"); + Logger.inst.Log($"Finished firing FileSwap logic for AppEntry with target path {AppEntry.AppPath}"); } @@ -191,7 +191,6 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int //no need to cancel the save if the substitution fails FileCopyConsiderAccessRules(swapPath, pluggedInStoredFilePath, true, false); //save current config to PluggedIn store string s1 = $"Saved SwapPath {swapPath} for app {AppEntry.AppPath} as PluggedIn"; - Debug.WriteLine(s1); _ = Logger.inst.Log(s1); errorFlag = 1; @@ -199,7 +198,6 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int FileCopyConsiderAccessRules(onBatteryStoredFilePath, swapPath, true, true); //then substitute with OnBattery config string s2 = $"Substituted OnBattery config into SwapPath {swapPath} for app {AppEntry.AppPath}"; _ = Logger.inst.Log(s2); - Debug.WriteLine(s2); } catch (IOException) @@ -237,7 +235,6 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int //no need to cancel the save if the substitution fails FileCopyConsiderAccessRules(swapPath, onBatteryStoredFilePath, true, false); //save current config to OnBattery store string s1 = $"Saved SwapPath {swapPath} for app {AppEntry.AppPath} as OnBattery"; - Debug.WriteLine(s1); _ = Logger.inst.Log(s1); errorFlag = 1; @@ -245,7 +242,6 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int FileCopyConsiderAccessRules(pluggedInStoredFilePath, swapPath, true, true); //then substitute with PluggedIn config string s2 = $"Substituted PluggedIn config into SwapPath {swapPath} for app {AppEntry.AppPath}"; _ = Logger.inst.Log(s2); - Debug.WriteLine(s2); } catch (IOException) { @@ -274,6 +270,8 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int Debug.WriteLine($"Unknown power state: " + forPowerLineStatus.ToString()); } + _ = Logger.inst.Log($"FileSwaps Task finished for {AppEntry.AppPath}"); + OngoingFileSwapTasks.Remove(fileSwapPathTask); } diff --git a/GPUPrefSwitcher/GPUPrefSwitcher.csproj b/GPUPrefSwitcher/GPUPrefSwitcher.csproj index fde2f90..62af8a4 100644 --- a/GPUPrefSwitcher/GPUPrefSwitcher.csproj +++ b/GPUPrefSwitcher/GPUPrefSwitcher.csproj @@ -12,7 +12,7 @@ ..\Assemble - ..\GPUPrefSwitcherGUI\bin\x64\Debug\ + bin\x64\Debug\ bin\x64\Release\ diff --git a/GPUPrefSwitcher/RegistryHelper.cs b/GPUPrefSwitcher/RegistryHelper.cs index 6b66333..b331fab 100644 --- a/GPUPrefSwitcher/RegistryHelper.cs +++ b/GPUPrefSwitcher/RegistryHelper.cs @@ -172,11 +172,11 @@ private static void SetGpuPref_internal(string pathvalue, string data) if (!GpuPrefPathvalueExistsAndIsValid(pathvalue)) throw new ArgumentException("pathvalue " + pathvalue + " does not exist in the registry or doesn't have a GpuPreference data entry"); - RegistryKey gpuPref = GetLoggedInGpuPrefKey(); + RegistryKey gpuPrefKey = GetLoggedInGpuPrefKey(); - Logger.inst.Log($"Modifiying registry key: setting {gpuPref} to {data}"); + Logger.inst.Log($"Modifiying registry key: setting {pathvalue} to {data}"); - gpuPref.SetValue(pathvalue, data); + gpuPrefKey.SetValue(pathvalue, data); } diff --git a/GPUPrefSwitcher/Switcher.cs b/GPUPrefSwitcher/Switcher.cs index 8dc8278..ae4c3c7 100644 --- a/GPUPrefSwitcher/Switcher.cs +++ b/GPUPrefSwitcher/Switcher.cs @@ -412,6 +412,9 @@ internal static void WriteXMLToRegistry(PowerLineStatus powerLineStatus) { IEnumerable appEntries = preferencesXML.GetAppEntries(); + bool systemIsOnbattery = powerLineStatus == PowerLineStatus.Offline; + Logger.inst.Log($"System is on battery: {systemIsOnbattery}"); + foreach (AppEntry appEntry in appEntries) { @@ -449,8 +452,6 @@ internal static void WriteXMLToRegistry(PowerLineStatus powerLineStatus) } Logger.inst.Log("Updating registry for pathvalue: " + pathvalue, 2000); - bool systemIsOnbattery = powerLineStatus == PowerLineStatus.Offline; - try { if (systemIsOnbattery) diff --git a/Install/Install.csproj b/Install/Install.csproj index a9646ba..615fe02 100644 --- a/Install/Install.csproj +++ b/Install/Install.csproj @@ -35,7 +35,7 @@ true full false - bin\Debug\ + ..\Assemble\install\ DEBUG;TRACE prompt 4 diff --git a/Uninstall/Uninstall.csproj b/Uninstall/Uninstall.csproj index e193b24..0b69711 100644 --- a/Uninstall/Uninstall.csproj +++ b/Uninstall/Uninstall.csproj @@ -35,7 +35,7 @@ true full false - bin\Debug\ + ..\Assemble\install\ DEBUG;TRACE prompt 4 From db5fc9d51f4f53959f7b7d0d0700a548324f66ab Mon Sep 17 00:00:00 2001 From: sharpjd <59419827+sharpjd@users.noreply.github.com> Date: Sun, 10 Mar 2024 13:37:47 -0400 Subject: [PATCH 2/4] Problem determined: the File Copy system is erroring out and throwing no errors... --- Assemble/install/Install.exe | Bin 8704 -> 9216 bytes GPUPrefSwitcher.sln | 1 + GPUPrefSwitcher/FileSwapper.cs | 12 ++++++++++-- GPUPrefSwitcher/Switcher.cs | 23 ++++++++++------------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Assemble/install/Install.exe b/Assemble/install/Install.exe index 1b1fdaf6349e76d696c1182f4192faa174ba164e..8e6b8255fe9f2f1ff58334d246b49160905093ee 100644 GIT binary patch delta 1078 zcmYjQT}TvB6#nka?Cd(O)BfnLKb7lWZMIFOP(exSZ)<5~K?EuO?XpOlYqJL8gBm2! zL!n+G=uL_s=%p4y`+0~UiYOE+>cx8MrKch&deF|DUDLzd?>pz4bI;7VoIBY(8JWI3 z(avNX9DA{htbIi}a#;B406P600KS?6q+ zNx_d0a&cS)Vc@BIrp)W?1)LOzLco6Bz(P9$fP;iJa|D3MT0m@!#70Mm+w=G=Ywa-1 zSRy%)sexY7#ov_E+F)UzaYJaiRiZ;OQCflxXUc&0JWr~hP$7 zq%7ceC&l4r!x*wKMfg`9#U;v7X;c+lA#~s_3>vAA zgkF4s(TWnRP*4FoGq4l+gmo4ku&~L}kD{3LQXDDGc%owW?s;I)?S3 z7tM~RyVWl?*L_FzqxUcWG|as-UcE`J+~DobVvp0V>Yf%)i|%RjbkI%lbn**MzuIC0 z^R)5n&PvwC(@rkg8IR=6`|#oP)Gbj{OzbsUQMv+AUZ7XFa$S+bjZHn_$48zRm3MB> z@`xUxF`jCZvSrAGP^31c|gkD}}hiz0Q_jh!j;)vispP=TJNq}ehW>*yOC!uixd p(u~E7Xd;n$39MEj5avtzi?r_g>DI(O7q>A|pI*}I8ULy;`~w_&sH*?~ delta 859 zcmYk4Ur19?9LK-s+}%67&bhm(^G~@ub#hagll}==2{u`ofr$|PW135-k#id;5o}oo zJ*Au=e5%n?Z^b||@AW3{JR-w7k#B2Rn!#O?gN79*hHG@C5M<~-J*>dmF6~wjF2B# zRRQyE6bc;=Ich?7EENn*T;RS;z&q*z)IjnGkWiBMiAEV2qvZsng3y?3B{M}+tJ}?a za{lWuO3f2AhMN3len2yO4KK0S?k!7IlFAanF|aE~78& z#WEr~O^X>U@)ddoL-^#8%q-q=9kbxJ0CKp7-FS>P$wgFoIiBK+)Jl4doO%3|_G1y# z{18^Crwi|iJ=no|afbXHk3wS!j982lH>5G#pdN*O6&td|B0NA4#dt&XU=6`;)ZiBt z^)yHj%_t%6v3Ss8m*w}sLw>*IoV9on&E#Cc05OFj(W3;|xENDBY&<`ue319Gzb^Uu zXrtkHuA}91VqoxcFa7%Gsi diff --git a/GPUPrefSwitcher.sln b/GPUPrefSwitcher.sln index 79a2f9a..b3a4540 100644 --- a/GPUPrefSwitcher.sln +++ b/GPUPrefSwitcher.sln @@ -151,6 +151,7 @@ Global {76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|Any CPU.ActiveCfg = Debug {76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|ARM64.ActiveCfg = Debug {76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x64.ActiveCfg = Debug + {76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x64.Build.0 = Debug {76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x86.ActiveCfg = Debug {76BFD458-C88C-4484-9578-DC1B75BB945C}.Release|Any CPU.ActiveCfg = Release {76BFD458-C88C-4484-9578-DC1B75BB945C}.Release|ARM64.ActiveCfg = Release diff --git a/GPUPrefSwitcher/FileSwapper.cs b/GPUPrefSwitcher/FileSwapper.cs index ff86fa7..a1453fb 100644 --- a/GPUPrefSwitcher/FileSwapper.cs +++ b/GPUPrefSwitcher/FileSwapper.cs @@ -96,8 +96,16 @@ public void InitiateFileSwaps(PowerLineStatus forPowerLineStatus, PreferencesXML ForPowerLineStatus = forPowerLineStatus, }; - //fire and forget - _ = InitiateSingleFileSwap(current, i); + + try + { + Task swap = InitiateSingleFileSwap(current, i); + swap.Wait(); //DEBUG + } + catch (AggregateException) + { + throw; + } } //...repeat for every SwapPath diff --git a/GPUPrefSwitcher/Switcher.cs b/GPUPrefSwitcher/Switcher.cs index ae4c3c7..c0245a7 100644 --- a/GPUPrefSwitcher/Switcher.cs +++ b/GPUPrefSwitcher/Switcher.cs @@ -143,6 +143,7 @@ private static Task RunUpdateLogic() PowerLineStatus currentPowerLineStatus; + currentPowerLineStatus = SystemInformation.PowerStatus.PowerLineStatus; if (spoofPowerStateEnabled) { Logger.inst.Log($"Power State spoofing enabled: {spoofPowerState}"); @@ -161,16 +162,8 @@ private static Task RunUpdateLogic() throw new ArgumentException("Unknown SpoofPowerState"); } } - else - { - currentPowerLineStatus = SystemInformation.PowerStatus.PowerLineStatus; - } - if (!runOnce) - { - Logger.inst.Log("Updating preferences regardless since this is the first update."); - goto CheckAndUpdate; - } + if (!lastShutdownWasClean) { //doesn't actually serve a purpose for now @@ -178,18 +171,22 @@ private static Task RunUpdateLogic() } //skip update if powerline status hasn't changed - if (prevPowerLineStatus == currentPowerLineStatus) + if (prevPowerLineStatus == currentPowerLineStatus && runOnce) { Logger.inst.Log($"PowerLine status has NOT changed since last update. (Current state: {currentPowerLineStatus}; last state: {prevPowerLineStatus})", 2000); return Task.CompletedTask; } else { - Logger.inst.Log($"PowerLine status has changed since last update. Previous: {prevPowerLineStatus}; Current: {currentPowerLineStatus}"); - goto CheckAndUpdate; + if (!runOnce) + { + Logger.inst.Log("Updating preferences regardless since this is the first update."); + } else + { + Logger.inst.Log($"PowerLine status has changed since last update. Previous: {prevPowerLineStatus}; Current: {currentPowerLineStatus}"); + } } - CheckAndUpdate: runOnce = true; RegAndXMLMatchState regAndXMLMatchState = GetRegAndXmlMatchState(); From 355f6749f581664bcc41ca2aed4e3ff7280c0f84 Mon Sep 17 00:00:00 2001 From: sharpjd <59419827+sharpjd@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:15:21 -0400 Subject: [PATCH 3/4] added a button to open the app data directory --- GPUPrefSwitcherGUI/OptionsForm.Designer.cs | 40 ++++++++++++++++++---- GPUPrefSwitcherGUI/OptionsForm.cs | 11 ++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/GPUPrefSwitcherGUI/OptionsForm.Designer.cs b/GPUPrefSwitcherGUI/OptionsForm.Designer.cs index 8e347f7..2838f72 100644 --- a/GPUPrefSwitcherGUI/OptionsForm.Designer.cs +++ b/GPUPrefSwitcherGUI/OptionsForm.Designer.cs @@ -52,6 +52,8 @@ private void InitializeComponent() toolTip1 = new System.Windows.Forms.ToolTip(components); OpenTaskSchedulerButton = new System.Windows.Forms.Button(); button1 = new System.Windows.Forms.Button(); + label5 = new System.Windows.Forms.Label(); + OpenAppDirButton = new System.Windows.Forms.Button(); SuspendLayout(); // // UpdateIntervalTextbox @@ -148,7 +150,7 @@ private void InitializeComponent() // ResetAppPreferencesListButton // ResetAppPreferencesListButton.ImageAlign = System.Drawing.ContentAlignment.TopLeft; - ResetAppPreferencesListButton.Location = new System.Drawing.Point(10, 437); + ResetAppPreferencesListButton.Location = new System.Drawing.Point(10, 478); ResetAppPreferencesListButton.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); ResetAppPreferencesListButton.Name = "ResetAppPreferencesListButton"; ResetAppPreferencesListButton.Size = new System.Drawing.Size(289, 22); @@ -162,7 +164,7 @@ private void InitializeComponent() // label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; label2.ImageAlign = System.Drawing.ContentAlignment.TopLeft; - label2.Location = new System.Drawing.Point(10, 330); + label2.Location = new System.Drawing.Point(10, 371); label2.Name = "label2"; label2.RightToLeft = System.Windows.Forms.RightToLeft.Yes; label2.Size = new System.Drawing.Size(290, 2); @@ -217,7 +219,7 @@ private void InitializeComponent() // CleanSettingsBankButton // CleanSettingsBankButton.ImageAlign = System.Drawing.ContentAlignment.TopLeft; - CleanSettingsBankButton.Location = new System.Drawing.Point(10, 343); + CleanSettingsBankButton.Location = new System.Drawing.Point(10, 384); CleanSettingsBankButton.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); CleanSettingsBankButton.Name = "CleanSettingsBankButton"; CleanSettingsBankButton.Size = new System.Drawing.Size(289, 26); @@ -231,7 +233,7 @@ private void InitializeComponent() // label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; label4.ImageAlign = System.Drawing.ContentAlignment.TopLeft; - label4.Location = new System.Drawing.Point(10, 422); + label4.Location = new System.Drawing.Point(10, 463); label4.Name = "label4"; label4.RightToLeft = System.Windows.Forms.RightToLeft.Yes; label4.Size = new System.Drawing.Size(290, 2); @@ -252,7 +254,7 @@ private void InitializeComponent() // FolderStatsLabel1 // FolderStatsLabel1.AutoSize = true; - FolderStatsLabel1.Location = new System.Drawing.Point(7, 397); + FolderStatsLabel1.Location = new System.Drawing.Point(7, 438); FolderStatsLabel1.Name = "FolderStatsLabel1"; FolderStatsLabel1.Size = new System.Drawing.Size(38, 15); FolderStatsLabel1.TabIndex = 19; @@ -261,7 +263,7 @@ private void InitializeComponent() // FolderStatsLabel2 // FolderStatsLabel2.AutoSize = true; - FolderStatsLabel2.Location = new System.Drawing.Point(7, 377); + FolderStatsLabel2.Location = new System.Drawing.Point(7, 418); FolderStatsLabel2.Name = "FolderStatsLabel2"; FolderStatsLabel2.Size = new System.Drawing.Size(38, 15); FolderStatsLabel2.TabIndex = 20; @@ -288,11 +290,33 @@ private void InitializeComponent() button1.Visible = false; button1.Click += button1_Click; // + // label5 + // + label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; + label5.ImageAlign = System.Drawing.ContentAlignment.TopLeft; + label5.Location = new System.Drawing.Point(10, 325); + label5.Name = "label5"; + label5.RightToLeft = System.Windows.Forms.RightToLeft.Yes; + label5.Size = new System.Drawing.Size(290, 2); + label5.TabIndex = 23; + // + // OpenAppDirButton + // + OpenAppDirButton.Location = new System.Drawing.Point(12, 337); + OpenAppDirButton.Name = "OpenAppDirButton"; + OpenAppDirButton.Size = new System.Drawing.Size(287, 23); + OpenAppDirButton.TabIndex = 24; + OpenAppDirButton.Text = "Open App Data Directory (logs, settings, etc.)"; + OpenAppDirButton.UseVisualStyleBackColor = true; + OpenAppDirButton.Click += OpenAppDirButton_Click; + // // OptionsForm // AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - ClientSize = new System.Drawing.Size(311, 477); + ClientSize = new System.Drawing.Size(311, 513); + Controls.Add(OpenAppDirButton); + Controls.Add(label5); Controls.Add(button1); Controls.Add(OpenTaskSchedulerButton); Controls.Add(FolderStatsLabel2); @@ -348,5 +372,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.Button OpenTaskSchedulerButton; private System.Windows.Forms.Button button1; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.Button OpenAppDirButton; } } \ No newline at end of file diff --git a/GPUPrefSwitcherGUI/OptionsForm.cs b/GPUPrefSwitcherGUI/OptionsForm.cs index ac09e26..b3d7c7e 100644 --- a/GPUPrefSwitcherGUI/OptionsForm.cs +++ b/GPUPrefSwitcherGUI/OptionsForm.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.IO; using System.Windows.Forms; +using System.Windows.Shapes; namespace GPUPrefSwitcherGUI { @@ -333,5 +334,15 @@ private void button1_Click(object sender, EventArgs e) var f = new GPUPrefSwitcherRepairer.RepairForm(); f.Show(); } + + private void OpenAppDirButton_Click(object sender, EventArgs e) + { + using Process fileopener = new Process(); + + fileopener.StartInfo.FileName = "explorer"; + fileopener.StartInfo.Arguments = "\"" + System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"AppData") + "\""; + fileopener.Start(); + + } } } From 5a5a98d231a8fb56f5d3877b4900af2f46df6f5b Mon Sep 17 00:00:00 2001 From: sharpjd <59419827+sharpjd@users.noreply.github.com> Date: Sun, 10 Mar 2024 15:45:33 -0400 Subject: [PATCH 4/4] not sure about "fixed", but file swapper seems to work now --- GPUPrefSwitcher/FileSwapper.cs | 21 ++- GPUPrefSwitcher/PreferencesXML.cs | 278 +++++++++++++++--------------- 2 files changed, 147 insertions(+), 152 deletions(-) diff --git a/GPUPrefSwitcher/FileSwapper.cs b/GPUPrefSwitcher/FileSwapper.cs index a1453fb..a48577d 100644 --- a/GPUPrefSwitcher/FileSwapper.cs +++ b/GPUPrefSwitcher/FileSwapper.cs @@ -68,7 +68,7 @@ public FileSwapper(AppEntry appEntry, PreferencesXML preferencesXML) } - public void InitiateFileSwaps(PowerLineStatus forPowerLineStatus, PreferencesXML preferencesXML) + public async Task InitiateFileSwaps(PowerLineStatus forPowerLineStatus, PreferencesXML preferencesXML) { Logger.inst.Log($"Initiating FileSwap for AppEntry with target path {AppEntry.AppPath}"); @@ -99,8 +99,7 @@ public void InitiateFileSwaps(PowerLineStatus forPowerLineStatus, PreferencesXML try { - Task swap = InitiateSingleFileSwap(current, i); - swap.Wait(); //DEBUG + await InitiateSingleFileSwap(current, i); } catch (AggregateException) { @@ -199,13 +198,13 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int //no need to cancel the save if the substitution fails FileCopyConsiderAccessRules(swapPath, pluggedInStoredFilePath, true, false); //save current config to PluggedIn store string s1 = $"Saved SwapPath {swapPath} for app {AppEntry.AppPath} as PluggedIn"; - _ = Logger.inst.Log(s1); + Logger.inst.Log(s1); errorFlag = 1; FileCopyConsiderAccessRules(onBatteryStoredFilePath, swapPath, true, true); //then substitute with OnBattery config string s2 = $"Substituted OnBattery config into SwapPath {swapPath} for app {AppEntry.AppPath}"; - _ = Logger.inst.Log(s2); + Logger.inst.Log(s2); } catch (IOException) @@ -243,23 +242,23 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int //no need to cancel the save if the substitution fails FileCopyConsiderAccessRules(swapPath, onBatteryStoredFilePath, true, false); //save current config to OnBattery store string s1 = $"Saved SwapPath {swapPath} for app {AppEntry.AppPath} as OnBattery"; - _ = Logger.inst.Log(s1); + Logger.inst.Log(s1); errorFlag = 1; FileCopyConsiderAccessRules(pluggedInStoredFilePath, swapPath, true, true); //then substitute with PluggedIn config string s2 = $"Substituted PluggedIn config into SwapPath {swapPath} for app {AppEntry.AppPath}"; - _ = Logger.inst.Log(s2); + Logger.inst.Log(s2); } catch (IOException) { if (errorFlag == 0) { - _ = Logger.inst.Log($"Could not copy to destination {onBatteryStoredFilePath}, retrying in some time."); + Logger.inst.Log($"Could not copy to destination {onBatteryStoredFilePath}, retrying in some time."); } else if (errorFlag == 1) { - _ = Logger.inst.Log($"Could not copy to destination {swapPath}, retrying in some time."); + Logger.inst.Log($"Could not copy to destination {swapPath}, retrying in some time."); } await Task.Delay(5000); goto TryAgain; @@ -270,7 +269,7 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int preferencesXML.ModifyAppEntryAndSave(AppEntry.AppPath, modified); string s3 = $"Saved SwapPath state for SwapPath {swapPath} for app {AppEntry.AppPath}"; - _ = Logger.inst.Log(s3); + Logger.inst.Log(s3); } } else @@ -278,7 +277,7 @@ private async Task InitiateSingleFileSwap(FileSwapPathTask fileSwapPathTask, int Debug.WriteLine($"Unknown power state: " + forPowerLineStatus.ToString()); } - _ = Logger.inst.Log($"FileSwaps Task finished for {AppEntry.AppPath}"); + Logger.inst.Log($"FileSwaps Task finished for {AppEntry.AppPath}"); OngoingFileSwapTasks.Remove(fileSwapPathTask); diff --git a/GPUPrefSwitcher/PreferencesXML.cs b/GPUPrefSwitcher/PreferencesXML.cs index cd23bbb..73140f3 100644 --- a/GPUPrefSwitcher/PreferencesXML.cs +++ b/GPUPrefSwitcher/PreferencesXML.cs @@ -241,69 +241,67 @@ XmlNode AppEntryNodeByAppPath(string path) /// If an AppEntry with the same AppPath already exists in the data store internal void AddAppEntryAndSave(AppEntry appEntry) { - lock(WriteLock) - { - ReloadXML(); - //check for duplicates - if (GetAppEntries().Any(entry => entry.AppPath == appEntry.AppPath)) - { - throw new InvalidOperationException($"Tried to add an AppEntry with an AppPath that already exists in the data store: {appEntry.AppPath} — this is undefined behavior."); - } + ReloadXML(); + + //check for duplicates + if (GetAppEntries().Any(entry => entry.AppPath == appEntry.AppPath)) + { + throw new InvalidOperationException($"Tried to add an AppEntry with an AppPath that already exists in the data store: {appEntry.AppPath} — this is undefined behavior."); + } - XmlNode root = xmlDocument.DocumentElement; + XmlNode root = xmlDocument.DocumentElement; + { + XmlElement xmlAppEntry = xmlDocument.CreateElement(XML_APP_ENTRY); + XmlElement xmlPath = xmlDocument.CreateElement(XML_APP_PATH); { - XmlElement xmlAppEntry = xmlDocument.CreateElement(XML_APP_ENTRY); - XmlElement xmlPath = xmlDocument.CreateElement(XML_APP_PATH); + xmlPath.InnerText = appEntry.AppPath; { - xmlPath.InnerText = appEntry.AppPath; + XmlElement xmlGpuPref = xmlDocument.CreateElement(XML_GPU_PREFERENCE); + { + xmlGpuPref.SetAttribute(XML_ATTR_ENABLESWITCHER, appEntry.EnableSwitcher.ToString()); + xmlGpuPref.SetAttribute(XML_ATTR_ENABLEFILESWAPPER, appEntry.EnableFileSwapper.ToString()); + + XmlElement runOnBattery = xmlDocument.CreateElement(XML_RUN_ON_BATTERY_PATH); + XmlElement runPluggedIn = xmlDocument.CreateElement(XML_RUN_PLUGGED_IN_PATH); + xmlGpuPref.AppendChild(runOnBattery); + xmlGpuPref.AppendChild(runPluggedIn); + } + XmlElement xmlPluggedIn = xmlDocument.CreateElement(XML_PLUGGED_IN); { - XmlElement xmlGpuPref = xmlDocument.CreateElement(XML_GPU_PREFERENCE); - { - xmlGpuPref.SetAttribute(XML_ATTR_ENABLESWITCHER, appEntry.EnableSwitcher.ToString()); - xmlGpuPref.SetAttribute(XML_ATTR_ENABLEFILESWAPPER, appEntry.EnableFileSwapper.ToString()); - - XmlElement runOnBattery = xmlDocument.CreateElement(XML_RUN_ON_BATTERY_PATH); - XmlElement runPluggedIn = xmlDocument.CreateElement(XML_RUN_PLUGGED_IN_PATH); - xmlGpuPref.AppendChild(runOnBattery); - xmlGpuPref.AppendChild(runPluggedIn); - } - XmlElement xmlPluggedIn = xmlDocument.CreateElement(XML_PLUGGED_IN); - { - xmlPluggedIn.InnerText = appEntry.GPUPrefPluggedIn.ToString(); - } - //xmlPluggedIn.InnerText = defaultPluggedin; - XmlElement xmlOnBattery = xmlDocument.CreateElement(XML_ON_BATTERY); - { - xmlOnBattery.InnerText = appEntry.GPUPrefOnBattery.ToString(); - } - XmlElement xmlFileSwapper = xmlDocument.CreateElement(XML_FILE_SWAPPER); - - - //xmlOnBattery.InnerText = defaultOnBattery; - root.AppendChild(xmlAppEntry); - { - xmlAppEntry.AppendChild(xmlPath); - xmlAppEntry.AppendChild(xmlGpuPref); - xmlAppEntry.AppendChild(xmlFileSwapper); - } - xmlGpuPref.AppendChild(xmlPluggedIn); - xmlGpuPref.AppendChild(xmlOnBattery); - - if (appEntry.PendingAddToRegistry) - { - XmlElement pendingAdd = xmlDocument.CreateElement(XML_PENDING_ADD); - xmlAppEntry.AppendChild(pendingAdd); - } - - XmlElement seenInRegistry = xmlDocument.CreateElement(XML_SEEN_IN_REGISTRY); - seenInRegistry.InnerText = appEntry.SeenInRegistry.ToString(); - xmlAppEntry.AppendChild(seenInRegistry); + xmlPluggedIn.InnerText = appEntry.GPUPrefPluggedIn.ToString(); } + //xmlPluggedIn.InnerText = defaultPluggedin; + XmlElement xmlOnBattery = xmlDocument.CreateElement(XML_ON_BATTERY); + { + xmlOnBattery.InnerText = appEntry.GPUPrefOnBattery.ToString(); + } + XmlElement xmlFileSwapper = xmlDocument.CreateElement(XML_FILE_SWAPPER); + + + //xmlOnBattery.InnerText = defaultOnBattery; + root.AppendChild(xmlAppEntry); + { + xmlAppEntry.AppendChild(xmlPath); + xmlAppEntry.AppendChild(xmlGpuPref); + xmlAppEntry.AppendChild(xmlFileSwapper); + } + xmlGpuPref.AppendChild(xmlPluggedIn); + xmlGpuPref.AppendChild(xmlOnBattery); + + if (appEntry.PendingAddToRegistry) + { + XmlElement pendingAdd = xmlDocument.CreateElement(XML_PENDING_ADD); + xmlAppEntry.AppendChild(pendingAdd); + } + + XmlElement seenInRegistry = xmlDocument.CreateElement(XML_SEEN_IN_REGISTRY); + seenInRegistry.InnerText = appEntry.SeenInRegistry.ToString(); + xmlAppEntry.AppendChild(seenInRegistry); } } - xmlDocument.Save(XML_PREFERENCES_PATH); + xmlDocument.Save(XML_PREFERENCES_PATH); } } @@ -332,112 +330,110 @@ internal bool TryDeleteAppEntryAndSave(string path) public void ModifyAppEntryAndSave(string path, AppEntry newAppEntry) //TODO file swapper functionality { - lock (WriteLock) - { - ReloadXML(); + + ReloadXML(); - XmlNode xmlAppEntry = AppEntryNodeByAppPath(path); - if (xmlAppEntry == null) //I believe an exception is better than a Try function here because it results in less code assuming we get the logic right in the first place - { - throw new XMLHelperException($"ModifyAppEntry: AppEntry with the specified path not found in data store: {path}"); - } + XmlNode xmlAppEntry = AppEntryNodeByAppPath(path); + if (xmlAppEntry == null) //I believe an exception is better than a Try function here because it results in less code assuming we get the logic right in the first place + { + throw new XMLHelperException($"ModifyAppEntry: AppEntry with the specified path not found in data store: {path}"); + } - string newAppPath = newAppEntry.AppPath; - string newEnableSwitcher = newAppEntry.EnableSwitcher.ToString().ToLower(); - string newGPUPrefPluggedIn = newAppEntry.GPUPrefPluggedIn.ToString(); - string newGPUPrefOnBattery = newAppEntry.GPUPrefOnBattery.ToString(); - string newEnableFileSwapper = newAppEntry.EnableFileSwapper.ToString().ToLower(); - string newRunOnBatteryPath = newAppEntry.RunOnBatteryPath; - string newRunPluggedInPath = newAppEntry.RunPluggedInPath; - string newSeenInRegistry = newAppEntry.SeenInRegistry.ToString().ToLower(); - - string[] newFileSwapperPaths = newAppEntry.FileSwapperPaths; - - if (newAppPath == null) - throw new InvalidOperationException("AppEntry passed in had a null AppPath value"); - if (newEnableSwitcher == null) - throw new InvalidOperationException($"AppEntry passed in had a null EnableSwitcher value (by AppPath {path})"); - if (newGPUPrefPluggedIn == null) - throw new InvalidOperationException($"AppEntry passed in had a null GPUPrefPluggedIn value (by AppPath {path})"); - if (newGPUPrefOnBattery == null) - throw new InvalidOperationException($"AppEntry passed in had a null GPUPrefOnBattery value (by AppPath {path})"); - if (newEnableFileSwapper == null) - throw new InvalidOperationException($"AppEntry passed in had a null EnableFileSwapper value (by AppPath{path}"); - if (newRunOnBatteryPath == null) - throw new InvalidOperationException($"AppEntry passed in had a null RunOnBatteryPath value (by AppPath{path}"); - if (newRunPluggedInPath == null) - throw new InvalidOperationException($"AppEntry passed in had a null RunPluggedInPath value (by AppPath{path}"); - if (newSeenInRegistry == null) - throw new InvalidOperationException($"AppEntry passed in had a null SeenInRegistry value (by AppPath{path}"); - - //try - { - xmlAppEntry.SelectSingleNode(XML_APP_PATH).InnerText = newAppPath; + string newAppPath = newAppEntry.AppPath; + string newEnableSwitcher = newAppEntry.EnableSwitcher.ToString().ToLower(); + string newGPUPrefPluggedIn = newAppEntry.GPUPrefPluggedIn.ToString(); + string newGPUPrefOnBattery = newAppEntry.GPUPrefOnBattery.ToString(); + string newEnableFileSwapper = newAppEntry.EnableFileSwapper.ToString().ToLower(); + string newRunOnBatteryPath = newAppEntry.RunOnBatteryPath; + string newRunPluggedInPath = newAppEntry.RunPluggedInPath; + string newSeenInRegistry = newAppEntry.SeenInRegistry.ToString().ToLower(); + + string[] newFileSwapperPaths = newAppEntry.FileSwapperPaths; + + if (newAppPath == null) + throw new InvalidOperationException("AppEntry passed in had a null AppPath value"); + if (newEnableSwitcher == null) + throw new InvalidOperationException($"AppEntry passed in had a null EnableSwitcher value (by AppPath {path})"); + if (newGPUPrefPluggedIn == null) + throw new InvalidOperationException($"AppEntry passed in had a null GPUPrefPluggedIn value (by AppPath {path})"); + if (newGPUPrefOnBattery == null) + throw new InvalidOperationException($"AppEntry passed in had a null GPUPrefOnBattery value (by AppPath {path})"); + if (newEnableFileSwapper == null) + throw new InvalidOperationException($"AppEntry passed in had a null EnableFileSwapper value (by AppPath{path}"); + if (newRunOnBatteryPath == null) + throw new InvalidOperationException($"AppEntry passed in had a null RunOnBatteryPath value (by AppPath{path}"); + if (newRunPluggedInPath == null) + throw new InvalidOperationException($"AppEntry passed in had a null RunPluggedInPath value (by AppPath{path}"); + if (newSeenInRegistry == null) + throw new InvalidOperationException($"AppEntry passed in had a null SeenInRegistry value (by AppPath{path}"); + + //try + { + xmlAppEntry.SelectSingleNode(XML_APP_PATH).InnerText = newAppPath; - xmlAppEntry.SelectSingleNode(XML_SEEN_IN_REGISTRY).InnerText = newSeenInRegistry; + xmlAppEntry.SelectSingleNode(XML_SEEN_IN_REGISTRY).InnerText = newSeenInRegistry; - XmlNode gpuPreference = xmlAppEntry.SelectSingleNode(XML_GPU_PREFERENCE); - gpuPreference.Attributes[XML_ATTR_ENABLESWITCHER].Value = newEnableSwitcher; - gpuPreference.Attributes[XML_ATTR_ENABLEFILESWAPPER].Value = newEnableFileSwapper; - { - gpuPreference.SelectSingleNode(XML_PLUGGED_IN).InnerText = newGPUPrefPluggedIn; - gpuPreference.SelectSingleNode(XML_ON_BATTERY).InnerText = newGPUPrefOnBattery; + XmlNode gpuPreference = xmlAppEntry.SelectSingleNode(XML_GPU_PREFERENCE); + gpuPreference.Attributes[XML_ATTR_ENABLESWITCHER].Value = newEnableSwitcher; + gpuPreference.Attributes[XML_ATTR_ENABLEFILESWAPPER].Value = newEnableFileSwapper; + { + gpuPreference.SelectSingleNode(XML_PLUGGED_IN).InnerText = newGPUPrefPluggedIn; + gpuPreference.SelectSingleNode(XML_ON_BATTERY).InnerText = newGPUPrefOnBattery; - gpuPreference.SelectSingleNode(XML_RUN_PLUGGED_IN_PATH).InnerText = newRunPluggedInPath; - gpuPreference.SelectSingleNode(XML_RUN_ON_BATTERY_PATH).InnerText = newRunOnBatteryPath; + gpuPreference.SelectSingleNode(XML_RUN_PLUGGED_IN_PATH).InnerText = newRunPluggedInPath; + gpuPreference.SelectSingleNode(XML_RUN_ON_BATTERY_PATH).InnerText = newRunOnBatteryPath; - } + } - //TODO this is jank, I think Switcher.cs needs to not have access to this and only access to AppEntrySaveHandler instead - if (newAppEntry.PendingAddToRegistry) + //TODO this is jank, I think Switcher.cs needs to not have access to this and only access to AppEntrySaveHandler instead + if (newAppEntry.PendingAddToRegistry) + { + XmlElement pendingAddToRegistry = xmlDocument.CreateElement(XML_PENDING_ADD); + if (xmlAppEntry.SelectSingleNode(XML_PENDING_ADD) == null) { - XmlElement pendingAddToRegistry = xmlDocument.CreateElement(XML_PENDING_ADD); - if (xmlAppEntry.SelectSingleNode(XML_PENDING_ADD) == null) - { - xmlAppEntry.AppendChild(pendingAddToRegistry); - } + xmlAppEntry.AppendChild(pendingAddToRegistry); } - else + } + else + { + var pendingAdd = xmlAppEntry.SelectSingleNode(XML_PENDING_ADD); + if (pendingAdd != null) { - var pendingAdd = xmlAppEntry.SelectSingleNode(XML_PENDING_ADD); - if (pendingAdd != null) - { - xmlAppEntry.RemoveChild(pendingAdd); - } + xmlAppEntry.RemoveChild(pendingAdd); } + } - //replace the SwapPath's - XmlNode fileSwapper = xmlAppEntry.SelectSingleNode(XML_FILE_SWAPPER); - fileSwapper.RemoveAll(); + //replace the SwapPath's + XmlNode fileSwapper = xmlAppEntry.SelectSingleNode(XML_FILE_SWAPPER); + fileSwapper.RemoveAll(); + { + /* + foreach(string p in newFileSwapperPaths) { - /* - foreach(string p in newFileSwapperPaths) - { - XmlElement xmlElement = xmlDocument.CreateElement(XML_SWAP_PATH); - xmlElement.InnerText = p; - fileSwapper.AppendChild(xmlElement); - } - */ - for (int i = 0; i < newFileSwapperPaths.Count(); i++) - { - string newFileSwapperPath = newFileSwapperPaths[i]; + XmlElement xmlElement = xmlDocument.CreateElement(XML_SWAP_PATH); + xmlElement.InnerText = p; + fileSwapper.AppendChild(xmlElement); + } + */ + for (int i = 0; i < newFileSwapperPaths.Count(); i++) + { + string newFileSwapperPath = newFileSwapperPaths[i]; - XmlElement xmlElement = xmlDocument.CreateElement(XML_SWAP_PATH); + XmlElement xmlElement = xmlDocument.CreateElement(XML_SWAP_PATH); - xmlElement.SetAttribute(XML_ATTR_SWAPPATHSTATUS, PowerLineStatusConversions.PowerLineStatusToOfflineOrOnline(newAppEntry.SwapperStates[i]));//TODO: does this gauruntee order? + xmlElement.SetAttribute(XML_ATTR_SWAPPATHSTATUS, PowerLineStatusConversions.PowerLineStatusToOfflineOrOnline(newAppEntry.SwapperStates[i]));//TODO: does this gauruntee order? - xmlElement.InnerText = newFileSwapperPath; + xmlElement.InnerText = newFileSwapperPath; - fileSwapper.AppendChild(xmlElement);//remember to append it!!! - } + fileSwapper.AppendChild(xmlElement);//remember to append it!!! } + } - xmlDocument.Save(XML_PREFERENCES_PATH); - }/* catch (NullReferenceException) - { - throw new XmlException($"An error occured while trying to modify AppEntry with AppPath {path}; check if the entry is malformed in Preferences.xml"); - }*/ - } + xmlDocument.Save(XML_PREFERENCES_PATH); + }/* catch (NullReferenceException) + { + throw new XmlException($"An error occured while trying to modify AppEntry with AppPath {path}; check if the entry is malformed in Preferences.xml"); + }*/ } //XML navigation reference https://learn.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation