Skip to content

Commit f1f5c8f

Browse files
committed
code optimization & obfuscation
1 parent 650bf69 commit f1f5c8f

7 files changed

Lines changed: 75 additions & 56 deletions

File tree

.github/workflows/auto-build.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727
- name: Build
2828
run: msbuild winUpdateMiniTool.sln -p:Configuration=Release -m
2929

30-
- name: Publish build
31-
uses: actions/upload-artifact@v4
32-
with:
33-
name: winUpdateMiniTool
34-
path: |
35-
winUpdateMiniTool/bin
30+
# - name: Publish build
31+
# uses: actions/upload-artifact@v4
32+
# with:
33+
# name: winUpdateMiniTool
34+
# path: |
35+
# winUpdateMiniTool/bin

winUpdateMiniTool/MainForm.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
namespace winUpdateMiniTool;
1717

18-
public partial class MainForm : Form {
19-
18+
internal partial class MainForm : Form {
19+
2020
private static Timer mTimer;
2121
private readonly WuAgent agent;
2222
private readonly int idleDelay;
23-
private readonly Gpo.Respect mGpoRespect = Gpo.Respect.Unknown;
23+
private readonly Gpo.Respect mGpoRespect;
2424
private readonly float mWinVersion;
2525
private bool allowShowDisplay = true;
2626
private AutoUpdateOptions autoUpdate = AutoUpdateOptions.No;
@@ -52,7 +52,7 @@ public MainForm() {
5252
notifyIcon.Visible = true;
5353
}
5454

55-
if (!OperatingSystemHelper.IsRunningAsUwp())
55+
if (!OSHelper.IsRunningAsUwp())
5656
Text = Updater.ApplicationTitle;
5757

5858
btnWinUpd.Text = string.Format("Windows Update ({0})", 0);
@@ -151,19 +151,19 @@ public MainForm() {
151151

152152
if (Program.IsAutoStart())
153153
chkAutoRun_CheckedChanged(null, EventArgs.Empty);
154-
if (OperatingSystemHelper.IsRunningAsUwp() && chkAutoRun.CheckState == CheckState.Checked)
154+
if (OSHelper.IsRunningAsUwp() && chkAutoRun.CheckState == CheckState.Checked)
155155
chkAutoRun.Enabled = false;
156156
idleDelay = MiscFunc.ParseInt(GetConfig("IdleDelay", "20"));
157157
if (Program.IsSkipUacRun())
158158
chkNoUAC_CheckedChanged(null, EventArgs.Empty);
159-
chkNoUAC.Enabled = OperatingSystemHelper.IsAdministrator();
160-
chkNoUAC.Visible = chkNoUAC.Enabled || chkNoUAC.Checked || !OperatingSystemHelper.IsRunningAsUwp();
159+
chkNoUAC.Enabled = OSHelper.IsAdministrator();
160+
chkNoUAC.Visible = chkNoUAC.Enabled || chkNoUAC.Checked || !OSHelper.IsRunningAsUwp();
161161

162162
chkOffline.Checked = MiscFunc.ParseInt(GetConfig("Offline", "0")) != 0;
163163
chkDownload.Checked = MiscFunc.ParseInt(GetConfig("Download", "1")) != 0;
164164
chkManual.Checked = MiscFunc.ParseInt(GetConfig("Manual", "0")) != 0;
165-
if (!OperatingSystemHelper.IsAdministrator()) {
166-
if (OperatingSystemHelper.IsRunningAsUwp()) {
165+
if (!OSHelper.IsAdministrator()) {
166+
if (OSHelper.IsRunningAsUwp()) {
167167
chkOffline.Enabled = false;
168168
chkOffline.Checked = false;
169169

@@ -177,7 +177,7 @@ public MainForm() {
177177
chkMsUpd.Checked = agent.IsActive() && agent.TestService(WuAgent.MsUpdGuid);
178178

179179
// Note: when running in the UWP sandbox we cant write the real registry even as admins
180-
if (!OperatingSystemHelper.IsAdministrator() || OperatingSystemHelper.IsRunningAsUwp())
180+
if (!OSHelper.IsAdministrator() || OSHelper.IsRunningAsUwp())
181181
foreach (Control ctl in gbxAutoUpdate.Controls)
182182
ctl.Enabled = false;
183183

@@ -283,9 +283,9 @@ private void InitializeTheme() {
283283
var item = new ToolStripRadioButtonMenuItem(title, null, onClick);
284284
themeMenuItem.DropDownItems.Add(item);
285285
return item;
286-
},
287-
OnThemeCurrentChanged,
288-
Program.IniReadValue("Root", "Theme", ""), "winUpdateMiniTool.themes");
286+
},
287+
OnThemeCurrentChanged,
288+
Program.IniReadValue("Root", "Theme"), "winUpdateMiniTool.themes");
289289
currentItem?.PerformClick();
290290
Theme.Current.Apply(this);
291291
}
@@ -299,13 +299,13 @@ protected override void WndProc(ref Message m) {
299299
WindowState = FormWindowState.Normal;
300300
Activate();
301301
BringToFront();
302-
WinApiHelper.SetForegroundWindow(this.Handle);
302+
WinApiHelper.SetForegroundWindow(Handle);
303303
}
304304
else {
305305
base.WndProc(ref m);
306306
}
307307
}
308-
308+
309309
private void siteToolStripMenuItem_Click(object sender, EventArgs e) {
310310
Updater.VisitAppSite();
311311
}
@@ -339,7 +339,7 @@ private void OnTimedEvent(object source, EventArgs e) {
339339
var daysDue = GetAutoUpdateDue();
340340
if (daysDue != 0 && !agent.IsBusy()) {
341341
// ensure we only start a check when user is not doing anything
342-
var idleTime = OperatingSystemHelper.GetIdleTime();
342+
var idleTime = OSHelper.GetIdleTime();
343343
if (idleDelay * 60 < idleTime) {
344344
AppLog.Line("Starting automatic search for updates.");
345345
updateNow = true;
@@ -636,7 +636,7 @@ private void UpdateState() {
636636
var isValid = agent.IsValid();
637637
var isValid2 = isValid || chkManual.Checked;
638638

639-
var admin = OperatingSystemHelper.IsAdministrator() || !OperatingSystemHelper.IsRunningAsUwp();
639+
var admin = OSHelper.IsAdministrator() || !OSHelper.IsRunningAsUwp();
640640

641641
var enable = agent.IsActive() && !busy;
642642
btnSearch.Enabled = enable;
@@ -778,14 +778,14 @@ private void restoreDefaults_Click(object sender, EventArgs e) {
778778
chkOld.Checked = false;
779779
chkMsUpd.Checked = false;
780780
chkBlockMS.Checked = false;
781-
781+
782782
chkDisableAU.Checked = false;
783783
radDefault.Checked = true;
784-
784+
785785
chkHideWU.Checked = false;
786786
chkStore.Checked = false;
787787
chkDrivers.Checked = true;
788-
788+
789789
dlAutoCheck.SelectedIndex = 0;
790790

791791
MessageBox.Show("Default settings restored.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
@@ -850,42 +850,42 @@ private void btnHistory_CheckedChanged(object sender, EventArgs e) {
850850
private void btnSearch_Click(object sender, EventArgs e) {
851851
if (!agent.IsActive() || agent.IsBusy())
852852
return;
853-
var ret = chkOffline.Checked
854-
? agent.SearchForUpdates(chkDownload.Checked, chkOld.Checked)
853+
var ret = chkOffline.Checked
854+
? agent.SearchForUpdates(chkDownload.Checked, chkOld.Checked)
855855
: agent.SearchForUpdates(dlSource.Text, chkOld.Checked);
856856
ShowResult(WuAgent.AgentOperation.CheckingUpdates, ret);
857857
}
858858

859859
private void btnDownload_Click(object sender, EventArgs e) {
860-
if (!chkManual.Checked && !OperatingSystemHelper.IsAdministrator()) {
860+
if (!chkManual.Checked && !OSHelper.IsAdministrator()) {
861861
MessageBox.Show("Administrator privileges are required in order to download updates using windows update services. Use 'Manual' download instead.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
862862
return;
863863
}
864864

865865
if (!agent.IsActive() || agent.IsBusy())
866866
return;
867-
var ret = chkManual.Checked
868-
? agent.DownloadUpdatesManually(GetUpdates())
867+
var ret = chkManual.Checked
868+
? agent.DownloadUpdatesManually(GetUpdates())
869869
: agent.DownloadUpdates(GetUpdates());
870870
ShowResult(WuAgent.AgentOperation.DownloadingUpdates, ret);
871871
}
872872

873873
private void btnInstall_Click(object sender, EventArgs e) {
874-
if (!OperatingSystemHelper.IsAdministrator()) {
874+
if (!OSHelper.IsAdministrator()) {
875875
MessageBox.Show("Administrator privileges are required in order to install updates.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
876876
return;
877877
}
878878

879879
if (!agent.IsActive() || agent.IsBusy())
880880
return;
881-
var ret = chkManual.Checked
882-
? agent.DownloadUpdatesManually(GetUpdates(), true)
881+
var ret = chkManual.Checked
882+
? agent.DownloadUpdatesManually(GetUpdates(), true)
883883
: agent.DownloadUpdates(GetUpdates(), true);
884884
ShowResult(WuAgent.AgentOperation.InstallingUpdates, ret);
885885
}
886886

887887
private void btnUnInstall_Click(object sender, EventArgs e) {
888-
if (!OperatingSystemHelper.IsAdministrator()) {
888+
if (!OSHelper.IsAdministrator()) {
889889
MessageBox.Show("Administrator privileges are required in order to remove updates.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
890890
return;
891891
}
@@ -978,7 +978,7 @@ private void OnUpdates(object sender, WuAgent.UpdatesArgs args) {
978978
if (args.Found) // if (agent.CurOperation() == WuAgent.AgentOperation.CheckingUpdates)
979979
{
980980
lastCheck = DateTime.Now;
981-
SetConfig("LastCheck", lastCheck.ToString());
981+
SetConfig("LastCheck", lastCheck.ToString(CultureInfo.InvariantCulture));
982982
SwitchList(UpdateLists.PendingUpdates);
983983
}
984984
else {
@@ -1207,7 +1207,7 @@ private void chkAutoRun_CheckedChanged(object sender, EventArgs e) {
12071207
return;
12081208
if (chkAutoRun.CheckState == CheckState.Indeterminate)
12091209
return;
1210-
if (OperatingSystemHelper.IsRunningAsUwp()) {
1210+
if (OSHelper.IsRunningAsUwp()) {
12111211
if (chkAutoRun.CheckState == CheckState.Checked) {
12121212
mSuspendUpdate = true;
12131213
chkAutoRun.CheckState = CheckState.Indeterminate;
@@ -1398,7 +1398,7 @@ private enum UpdateLists {
13981398

13991399
// Implements the manual sorting of items by columns.
14001400
private class ListViewItemComparer : IComparer {
1401-
private int col = 0;
1401+
private int col;
14021402
private int inv = 1;
14031403

14041404
public int Compare(object x, object y) {
@@ -1436,7 +1436,7 @@ private void selectUIFontToolStripMenuItem_Click(object sender, EventArgs e) {
14361436
fontDialog.Font = Font;
14371437
if (fontDialog.ShowDialog() != DialogResult.OK)
14381438
return;
1439-
if (fontDialog.Font == Font)
1439+
if (Equals(fontDialog.Font, Font))
14401440
return;
14411441
Font = fontDialog.Font;
14421442
SetConfig("UIFont", $"{Font.Name};{Font.Size};{(int)Font.Style}");
@@ -1480,10 +1480,10 @@ private void RestorePosition() {
14801480
bool hasW = int.TryParse(GetConfig("Window_Width"), out var w);
14811481
bool hasH = int.TryParse(GetConfig("Window_Height"), out var h);
14821482
bool hasAll = hasX && hasY && hasW && hasH;
1483-
1483+
14841484
Rectangle bounds = new(x, y, w, h);
14851485
bool isVisible = Screen.AllScreens.Any(s => s.WorkingArea.IntersectsWith(bounds));
1486-
1486+
14871487
if (hasAll && isVisible) {
14881488
StartPosition = FormStartPosition.Manual;
14891489
Bounds = bounds;

winUpdateMiniTool/MsUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace winUpdateMiniTool;
77
/// <summary>
88
/// Represents a Microsoft Update with various attributes and states.
99
/// </summary>
10-
public class MsUpdate {
10+
internal class MsUpdate {
1111
/// <summary>
1212
/// Enumeration for update attributes.
1313
/// </summary>

winUpdateMiniTool/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static void Main(string[] mainArgs) {
5151
AppLog.Line("{0}, Version v{1}", Updater.ApplicationTitle, Updater.CurrentVersion);
5252
AppLog.Line("This Tool is Open Source under the GNU General Public License, Version 3\r\n");
5353

54-
if (!OperatingSystemHelper.IsCompatible(false, out var errorMessage, out var fixAction)) {
54+
if (!OSHelper.IsCompatible(false, out var errorMessage, out var fixAction)) {
5555
if (fixAction != null) {
5656
if (MessageBox.Show(errorMessage, Updater.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
5757
fixAction?.Invoke();
@@ -70,15 +70,15 @@ private static void Main(string[] mainArgs) {
7070
return;
7171
}
7272

73-
if (!OperatingSystemHelper.IsAdministrator() && !OperatingSystemHelper.IsDebugging()) {
73+
if (!OSHelper.IsAdministrator() && !OSHelper.IsDebugging()) {
7474
Console.WriteLine(@"Trying to get admin privileges...");
7575

7676
if (SkipUacRun()) {
7777
Application.Exit();
7878
return;
7979
}
8080

81-
if (!OperatingSystemHelper.IsRunningAsUwp()) {
81+
if (!OSHelper.IsRunningAsUwp()) {
8282
Console.WriteLine(@"Trying to start with 'runas'...");
8383
// Restart program and run as admin
8484
var exeName = Process.GetCurrentProcess().MainModule?.FileName;
@@ -371,7 +371,7 @@ public static bool SkipUacEnable(bool isEnable) {
371371
return false;
372372

373373
// Note: if we run as UWP we need to adjust the file permissions for this workaround to work
374-
if (OperatingSystemHelper.IsRunningAsUwp()) {
374+
if (OSHelper.IsRunningAsUwp()) {
375375
if (!FileOps.TakeOwn(exePath))
376376
return false;
377377

winUpdateMiniTool/WuAgent.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using WUApiLib;
1111
using StringCollection = System.Collections.Specialized.StringCollection;
1212

13-
//this is required to use the Interfaces given by microsoft.
13+
//this is required to use the Interfaces given by microsoft.
1414
namespace winUpdateMiniTool;
1515

1616
internal class WuAgent {
@@ -329,12 +329,12 @@ private RetCodes SearchForUpdates() {
329329
mCallback = new UpdateCallback(this);
330330

331331
AppLog.Line("Searching for updates");
332-
//for the above search criteria refer to
332+
//for the above search criteria refer to
333333
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
334334
try {
335335
//string query = "(IsInstalled = 0 and IsHidden = 0) or (IsInstalled = 1 and IsHidden = 0) or (IsHidden = 1)";
336336
//string query = "(IsInstalled = 0 and IsHidden = 0) or (IsInstalled = 1 and IsHidden = 0) or (IsHidden = 1) or (IsInstalled = 0 and IsHidden = 0 and DeploymentAction='OptionalInstallation') or (IsInstalled = 1 and IsHidden = 0 and DeploymentAction='OptionalInstallation') or (IsHidden = 1 and DeploymentAction='OptionalInstallation')";
337-
var query = OperatingSystemHelper.IsWindows7OrLower
337+
var query = OSHelper.IsWindows7OrLower
338338
? "(IsInstalled = 0 and IsHidden = 0) or (IsInstalled = 1 and IsHidden = 0) or (IsHidden = 1)"
339339
: "(IsInstalled = 0 and IsHidden = 0 and DeploymentAction=*) or (IsInstalled = 1 and IsHidden = 0 and DeploymentAction=*) or (IsHidden = 1 and DeploymentAction=*)";
340340
mSearchJob = mUpdateSearcher.BeginSearch(query, mCallback, null);
@@ -1008,13 +1008,13 @@ private class UpdateCallback(WuAgent agent) : ISearchCompletedCallback, IDownloa
10081008
IDownloadCompletedCallback, IInstallationProgressChangedCallback, IInstallationCompletedCallback {
10091009
// Implementation of IDownloadCompletedCallback interface...
10101010
public void Invoke(IDownloadJob downloadJob, IDownloadCompletedCallbackArgs callbackArgs) {
1011-
// !!! warning this function is invoked from a different thread !!!
1011+
// !!! warning this function is invoked from a different thread !!!
10121012
agent.mDispatcher.Invoke(() => { agent.OnUpdatesDownloaded(downloadJob, downloadJob.AsyncState); });
10131013
}
10141014

10151015
// Implementation of IDownloadProgressChangedCallback interface...
10161016
public void Invoke(IDownloadJob downloadJob, IDownloadProgressChangedCallbackArgs callbackArgs) {
1017-
// !!! warning this function is invoced from a different thread !!!
1017+
// !!! warning this function is invoced from a different thread !!!
10181018
agent.mDispatcher.Invoke(() => {
10191019
agent.OnProgress(downloadJob.Updates.Count, callbackArgs.Progress.PercentComplete,
10201020
callbackArgs.Progress.CurrentUpdateIndex + 1,
@@ -1025,15 +1025,15 @@ public void Invoke(IDownloadJob downloadJob, IDownloadProgressChangedCallbackArg
10251025

10261026
// Implementation of IInstallationCompletedCallback interface...
10271027
public void Invoke(IInstallationJob installationJob, IInstallationCompletedCallbackArgs callbackArgs) {
1028-
// !!! warning this function is invoced from a different thread !!!
1028+
// !!! warning this function is invoced from a different thread !!!
10291029
agent.mDispatcher.Invoke(() => {
10301030
agent.OnInstalationCompleted(installationJob, installationJob.AsyncState);
10311031
});
10321032
}
10331033

10341034
// Implementation of IInstallationProgressChangedCallback interface...
10351035
public void Invoke(IInstallationJob installationJob, IInstallationProgressChangedCallbackArgs callbackArgs) {
1036-
// !!! warning this function is invoced from a different thread !!!
1036+
// !!! warning this function is invoced from a different thread !!!
10371037
agent.mDispatcher.Invoke(() => {
10381038
agent.OnProgress(installationJob.Updates.Count, callbackArgs.Progress.PercentComplete,
10391039
callbackArgs.Progress.CurrentUpdateIndex + 1,
@@ -1044,7 +1044,7 @@ public void Invoke(IInstallationJob installationJob, IInstallationProgressChange
10441044

10451045
// Implementation of ISearchCompletedCallback interface...
10461046
public void Invoke(ISearchJob searchJob, ISearchCompletedCallbackArgs e) {
1047-
// !!! warning this function is invoced from a different thread !!!
1047+
// !!! warning this function is invoced from a different thread !!!
10481048
agent.mDispatcher.Invoke(() => { agent.OnUpdatesFound(searchJob); });
10491049
}
10501050
}

winUpdateMiniTool/winUpdateMiniTool.csproj

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,27 @@
7171
<PrivateAssets>all</PrivateAssets>
7272
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
7373
</PackageReference>
74+
<PackageReference Include="Obfuscar">
75+
<Version>2.2.50</Version>
76+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
77+
<PrivateAssets>all</PrivateAssets>
78+
</PackageReference>
7479
</ItemGroup>
7580
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Release' ">
7681
<Exec Command="for %%f in ($(OutDir)\*) do if not %%~xf==.exe del /S /Q &quot;%%f&quot;" />
77-
</Target>
78-
</Project>
82+
</Target>
83+
<Target Name="GenerateObfuscarConfig" AfterTargets="PreBuildEvent" Condition="'$(Configuration)' == 'Release' OR '$(Configuration)' == 'ReleaseLite' ">
84+
<WriteLinesToFile File="$(TargetDir)obfuscar.xml" Overwrite="true" Lines="
85+
&lt;?xml version='1.0' encoding='utf-8' ?&gt;
86+
&lt;Obfuscator&gt;
87+
&lt;Var name='InPath' value='$(TargetDir)' /&gt;
88+
&lt;Var name='OutPath' value='.\Obfuscated' /&gt;
89+
&lt;Var name='HidePrivateApi' value='true' /&gt;
90+
&lt;Module file='$(TargetDir)$(TargetFileName)' /&gt;
91+
&lt;/Obfuscator&gt;
92+
" />
93+
</Target>
94+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' OR '$(Configuration)' == 'ReleaseLite' ">
95+
<PostBuildEvent>$(Obfuscar) obfuscar.xml</PostBuildEvent>
96+
</PropertyGroup>
97+
</Project>

0 commit comments

Comments
 (0)