Skip to content

Commit

Permalink
Update README + bug fixes
Browse files Browse the repository at this point in the history
- Revert to using `net` for service start/stop since `sc` didn't wait for the service to be running and would cause issues if the service crashed during startup

- Don't bother uninstalling the old YAMDCC service during an update, since we're most likely installing into the same directory as where the old service is installed

- Add more update error output during download/extract progress

- Detect if service is running before attempting to stop it during an update, and don't restart the service if it wasn't running before the update

- Other small optimisations
  • Loading branch information
Sparronator9999 committed Jan 13, 2025
1 parent e30e82d commit 3bc2eff
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ details.

This project makes use of the following third-party libraries:

- [Markdig](https://github.com/xoofx/markdig) to parse release changelogs.
- [Json.NET (Newtonsoft.Json)](https://www.newtonsoft.com/json) to parse obtained release manifests from GitHub.
- [Marked .NET](https://github.com/tomlm/MarkedNet) to parse release changelogs.
- [My fork of Named Pipe Wrapper](https://github.com/Sparronator9999/NamedPipeWrapper) for
communication between the service and UI program (called `YAMDCC.IPC` in the source files).
- [Octokit](https://github.com/octokit/octokit.net) to obtain the latest release assets and changelog.
- [Task Scheduler Managed Wrapper](https://github.com/dahall/taskscheduler) to schedule automatic update checks.
- [WinRing0](https://github.com/QCute/WinRing0) for low-level hardware access required to
read/write the EC.
26 changes: 24 additions & 2 deletions YAMDCC.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public static bool UninstallService(string svcExe)
/// </returns>
public static bool StartService(string svcName)
{
return RunCmd("sc.exe", $"start {svcName}") is 0 or 1056;
return RunCmd("net", $"start {svcName}") == 0;
}

/// <summary>
Expand All @@ -323,7 +323,7 @@ public static bool StartService(string svcName)
/// </returns>
public static bool StopService(string svcName)
{
return RunCmd("sc.exe", $"stop {svcName}") is 0 or 1062;
return RunCmd("net", $"stop {svcName}") == 0;
}

/// <summary>
Expand All @@ -343,6 +343,28 @@ public static bool ServiceExists(string svcName)
return ServiceController.GetServices().Any(s => s.ServiceName == svcName);
}

/// <summary>
/// Checks to see if the specified service
/// is running or pending start on the computer.
/// </summary>
/// <param name="svcName">
/// The service name, as shown in <c>services.msc</c>
/// (NOT to be confused with its display name).
/// </param>
/// <returns>
/// <see langword="true"/> if the service is
/// running, otherwise <see langword="false"/>
/// </returns>
public static bool ServiceRunning(string svcName)
{
using (ServiceController service = new(svcName))
{
return service.Status
is ServiceControllerStatus.Running
or ServiceControllerStatus.StartPending;
}
}

private static void DeleteInstallUtilLogs()
{
foreach (string file in Directory.GetFiles(".", "*.InstallLog", SearchOption.TopDirectoryOnly))
Expand Down
18 changes: 6 additions & 12 deletions YAMDCC.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,10 @@ private static void InstallUpdate(
{
ProgressDialog dlg = new("Installing YAMDCC update...", (e) =>
{
// uninstall the old YAMDCC service
// TODO: detect if YAMDCC service is already uninstalled
if (Utils.StopService("yamdccsvc"))
{
if (!Utils.UninstallService($"{destPath}\\yamdccsvc"))
{
Utils.ShowError("Failed to uninstall YAMDCC service!");
}
}
else
bool svcRunning = Utils.ServiceRunning("yamdccsvc");

// stop the YAMDCC service if it's running
if (svcRunning && !Utils.StopService("yamdccsvc"))
{
Utils.ShowError("Failed to stop YAMDCC service!");
}
Expand Down Expand Up @@ -113,8 +107,8 @@ private static void InstallUpdate(
dir.MoveTo(Path.Combine(destPath, dir.Name));
}

// install the new YAMDCC service
if (Utils.InstallService($"{destPath}\\yamdccsvc"))
// restart the YAMDCC service if it was running before the update
if (svcRunning)
{
Utils.StartService("yamdccsvc");
}
Expand Down
28 changes: 26 additions & 2 deletions YAMDCC.Updater/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Continue?</value>
<value>Click "Check for updates" to get the latest updates and changelog.</value>
</data>
<data name="errNoRelease" xml:space="preserve">
<value>**ERROR: failed to get latest release info!**
<value>**Failed to get latest release info!**

This could possibly mean one of the following:

Expand All @@ -143,7 +143,7 @@ This could possibly mean one of the following:
<data name="errCheckUpdate" xml:space="preserve">
<value>**An error occurred during the update check.**

First, make sure you're connected to the internet and that your firewall allows connections from this program to `github.com`.
Make sure you're connected to the internet and that your firewall allows connections from this program to `github.com`.

If the below error message mentions an API rate limit was exceeded, try again later.

Expand All @@ -168,4 +168,28 @@ Error details:
<data name="InstallPrompt" xml:space="preserve">
<value>Click "Install update", then click "Yes" at the UAC prompt to install the YAMDCC update.</value>
</data>
<data name="errDownload" xml:space="preserve">
<value>**An error occurred while downloading the update.**

If the below error message mentions an API rate limit was exceeded, try again later.

If the below error is mentions an I/O or security error, try re-running the update as an Administrator, then try updating again.

Error details:

```
{0}
```</value>
</data>
<data name="errExtract" xml:space="preserve">
<value>**An error occurred while extracting the downloaded update.**

If the below error is mentions an I/O or security error, try re-running the update as an Administrator, then try updating again.

Error details:

```
{0}
```</value>
</data>
</root>
31 changes: 15 additions & 16 deletions YAMDCC.Updater/UpdateForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void wbChangelog_Navigating(object sender, WebBrowserNavigatingEventArgs
{
string url = e.Url.ToString();

if (url.StartsWith("yamdcc:", StringComparison.OrdinalIgnoreCase))
if (url.StartsWith("yamdcc", StringComparison.OrdinalIgnoreCase))
{
switch (url.Remove(0, url.IndexOf(':') + 1))
{
Expand All @@ -89,15 +89,13 @@ private void wbChangelog_Navigating(object sender, WebBrowserNavigatingEventArgs
}

// open external links in user's web browser
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
Process.Start(url);
e.Cancel = true;
}
}

// TODO: better error handling while downloading and installing update
private async void btnUpdate_Click(object sender, EventArgs e)
{
if ("update".Equals(btnUpdate.Tag) && Release is not null)
Expand All @@ -124,10 +122,7 @@ await Updater.DownloadUpdateAsync(
}
catch (HttpRequestException ex)
{
// re-enable update button to allow retry
btnUpdate.Enabled = true;
btnUpdate.Text = "Retry update";
SetProgress(0, $"ERROR: Failed to download YAMDCC: {ex.Message}");
UpdateError(ex, "failed to download update", "errDownload");
}

try
Expand All @@ -137,10 +132,7 @@ await Updater.DownloadUpdateAsync(
}
catch (Exception ex)
{
// re-enable update button to allow retry
btnUpdate.Enabled = true;
btnUpdate.Text = "Retry update";
SetProgress(0, $"ERROR: Failed to extract update: {ex.Message}");
UpdateError(ex, "failed to extract update", "errExtract");
}

// delete old YAMDCC install from previous update if it exists
Expand Down Expand Up @@ -219,10 +211,7 @@ private async void CheckUpdate()
}
catch (HttpRequestException ex)
{
SetProgress(0, $"ERROR: {(ex.InnerException is WebException ex2 ? ex2.Message : ex.Message)}");
wbChangelog.DocumentText = GetHtml(Strings.GetString(
"errCheckUpdate", ex));
btnUpdate.Enabled = true;
UpdateError(ex, "failed to check for updates", "errCheckUpdate");
btnOptions.Enabled = true;
return;
}
Expand Down Expand Up @@ -287,6 +276,16 @@ private void UpdateAvailable()
}
}

private void UpdateError(Exception ex, string shortMsg, string longMsg)
{
SetProgress(0, $"ERROR: {shortMsg}: {(ex.InnerException is WebException ex2 ? ex2.Message : ex.Message)}");
wbChangelog.DocumentText = GetHtml(Strings.GetString(
longMsg, ex));
btnUpdate.Text = "Retry update";
// re-enable update button to allow retry
btnUpdate.Enabled = true;
}

private void DownloadProgress(long bytesReceived, long fileSize)
{
SetProgress((int)(bytesReceived * 100 / fileSize),
Expand Down

0 comments on commit 3bc2eff

Please sign in to comment.