Added auto-update checker

This commit is contained in:
Serge
2023-05-15 22:11:27 +02:00
parent fd3a139c47
commit 4015e0a7f7
6 changed files with 108 additions and 36 deletions

View File

@@ -2,6 +2,7 @@
using Starlight.Communication; using Starlight.Communication;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Globalization; using System.Globalization;
using System.Management; using System.Management;
using System.Text; using System.Text;
@@ -343,11 +344,17 @@ namespace Starlight.AnimeMatrix
public void PresentClock() public void PresentClock()
{ {
int second = DateTime.Now.Second; int second = DateTime.Now.Second;
string time;
if (CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern.Contains("H")) if (CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern.Contains("H"))
PresentTextDiagonal(DateTime.Now.ToString(" H" + ((second % 2 == 0) ? ":" : " ") + "mm")); time = DateTime.Now.ToString("H" + ((second % 2 == 0) ? ":" : " ") + "mm");
else else
PresentTextDiagonal(DateTime.Now.ToString("h" + ((second % 2 == 0) ? ":" : " ") + "mmtt")); time = DateTime.Now.ToString("h" + ((second % 2 == 0) ? ":" : " ") + "mmtt");
if (_model == AnimeType.GA401)
PresentText(time);
else
PresentTextDiagonal(time);
} }
public void PresentText(string text1, string text2 = "") public void PresentText(string text1, string text2 = "")
@@ -359,14 +366,14 @@ namespace Starlight.AnimeMatrix
g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias;
using (Font font = new Font("Arial", 24F, GraphicsUnit.Pixel)) using (Font font = new Font("Consolas", 24F, FontStyle.Regular, GraphicsUnit.Pixel))
{ {
SizeF textSize = g.MeasureString(text1, font); SizeF textSize = g.MeasureString(text1, font);
g.DrawString(text1, font, Brushes.White, (MaxColumns * 3 - textSize.Width) + 3, -3); g.DrawString(text1, font, Brushes.White, (MaxColumns * 3 - textSize.Width) + 3, -4);
} }
if (text2.Length > 0) if (text2.Length > 0)
using (Font font = new Font("Arial", 18F, GraphicsUnit.Pixel)) using (Font font = new Font("Consolas", 18F, GraphicsUnit.Pixel))
{ {
SizeF textSize = g.MeasureString(text2, font); SizeF textSize = g.MeasureString(text2, font);
g.DrawString(text2, font, Brushes.White, (MaxColumns * 3 - textSize.Width) + 1, 25); g.DrawString(text2, font, Brushes.White, (MaxColumns * 3 - textSize.Width) + 1, 25);
@@ -437,6 +444,24 @@ namespace Starlight.AnimeMatrix
Clear(); Clear();
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
string familyName;
string familyList = "";
FontFamily[] fontFamilies;
// Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families;
int count = fontFamilies.Length;
for (int j = 0; j < count; ++j)
{
familyName = fontFamilies[j].Name;
familyList = familyList + familyName;
familyList = familyList + ", ";
}
int maxX = (int)Math.Sqrt(MaxRows * MaxRows + MaxColumns * MaxColumns); int maxX = (int)Math.Sqrt(MaxRows * MaxRows + MaxColumns * MaxColumns);
using (Bitmap bmp = new Bitmap(maxX, MaxRows)) using (Bitmap bmp = new Bitmap(maxX, MaxRows))
@@ -446,10 +471,10 @@ namespace Starlight.AnimeMatrix
g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias;
using (Font font = new Font("Calibri", 13F, GraphicsUnit.Pixel)) using (Font font = new Font("Consolas", 13F, FontStyle.Regular, GraphicsUnit.Pixel))
{ {
SizeF textSize = g.MeasureString(text, font); SizeF textSize = g.MeasureString(text, font);
g.DrawString(text, font, Brushes.White, 4, 0); g.DrawString(text, font, Brushes.White, 4, 1);
} }
} }

View File

@@ -11,7 +11,7 @@ public class AmdGpuControl : IGpuControl {
public bool IsNvidia => false; public bool IsNvidia => false;
public string FullName => _internalDiscreteAdapter!.AdapterName;
public AmdGpuControl() { public AmdGpuControl() {
if (!Adl2.Load()) if (!Adl2.Load())
return; return;

View File

@@ -3,6 +3,7 @@
public interface IGpuControl : IDisposable { public interface IGpuControl : IDisposable {
bool IsNvidia { get; } bool IsNvidia { get; }
bool IsValid { get; } bool IsValid { get; }
public string FullName { get; }
int? GetCurrentTemperature(); int? GetCurrentTemperature();
int? GetGpuUse(); int? GetGpuUse();
} }

View File

@@ -51,6 +51,7 @@ public static class HardwareControl
try try
{ {
int? gpuUse = GpuControl?.GetGpuUse(); int? gpuUse = GpuControl?.GetGpuUse();
Logger.WriteLine("GPU usage: " + GpuControl?.FullName + " " + gpuUse + "%");
if (gpuUse is not null) return (int)gpuUse; if (gpuUse is not null) return (int)gpuUse;
} }
catch (Exception ex) catch (Exception ex)
@@ -113,16 +114,12 @@ public static class HardwareControl
} }
} }
public static bool IsUsedGPU(int threshold = 20) public static bool IsUsedGPU(int threshold = 10)
{ {
int use = GetGpuUse(); if (GetGpuUse() > threshold)
Logger.WriteLine("GPU usage: " + use);
if (use > threshold)
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
use = GetGpuUse(); return (GetGpuUse() > threshold);
Logger.WriteLine("GPU usage: " + use);
return (use > threshold);
} }
return false; return false;
} }
@@ -136,7 +133,7 @@ public static class HardwareControl
return null; return null;
} }
public static void RecreateGpuControlWithDelay(int delay = 3) public static void RecreateGpuControlWithDelay(int delay = 5)
{ {
// Re-enabling the discrete GPU takes a bit of time, // Re-enabling the discrete GPU takes a bit of time,
// so a simple workaround is to refresh again after that happens // so a simple workaround is to refresh again after that happens

View File

@@ -267,24 +267,25 @@ namespace GHelper
labelModel.Anchor = AnchorStyles.Top | AnchorStyles.Right; labelModel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
labelModel.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); labelModel.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
labelModel.ForeColor = SystemColors.ControlDark; labelModel.ForeColor = SystemColors.ControlDark;
labelModel.Location = new Point(291, 119); labelModel.Location = new Point(380, 119);
labelModel.Margin = new Padding(8, 0, 8, 0); labelModel.Margin = new Padding(8, 0, 8, 0);
labelModel.Name = "labelModel"; labelModel.Name = "labelModel";
labelModel.Size = new Size(492, 32); labelModel.Size = new Size(400, 32);
labelModel.TabIndex = 38; labelModel.TabIndex = 38;
labelModel.TextAlign = ContentAlignment.TopRight; labelModel.TextAlign = ContentAlignment.TopRight;
// //
// labelVersion // labelVersion
// //
labelVersion.AutoSize = true; labelVersion.AutoSize = false;
labelVersion.Font = new Font("Segoe UI", 9F, FontStyle.Underline, GraphicsUnit.Point); labelVersion.Font = new Font("Segoe UI", 9F, FontStyle.Underline, GraphicsUnit.Point);
labelVersion.ForeColor = SystemColors.ControlDark; labelVersion.ForeColor = SystemColors.ControlDark;
labelVersion.Location = new Point(25, 119); labelVersion.Location = new Point(25, 119);
labelVersion.Margin = new Padding(8, 0, 8, 0); labelVersion.Margin = new Padding(8, 0, 8, 0);
labelVersion.Name = "labelVersion"; labelVersion.Name = "labelVersion";
labelVersion.Size = new Size(44, 32); labelVersion.Size = new Size(300, 32);
labelVersion.TabIndex = 37; labelVersion.TabIndex = 37;
labelVersion.Text = "v.0"; labelVersion.Text = "v.0";
labelVersion.Cursor = Cursors.Hand;
// //
// labelBattery // labelBattery
// //

View File

@@ -3,9 +3,13 @@ using GHelper.Gpu;
using Starlight.AnimeMatrix; using Starlight.AnimeMatrix;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO.Compression;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Timers; using System.Timers;
using System.Windows.Forms;
using System.Xml.Linq;
using Tools; using Tools;
namespace GHelper namespace GHelper
@@ -293,11 +297,16 @@ namespace GHelper
var url = config.GetProperty("assets")[0].GetProperty("browser_download_url").ToString(); var url = config.GetProperty("assets")[0].GetProperty("browser_download_url").ToString();
var gitVersion = new Version(tag); var gitVersion = new Version(tag);
var appVersion = new Version(Assembly.GetExecutingAssembly().GetName().Version.ToString()); var appVersion = new Version(Assembly.GetExecutingAssembly().GetName().Version.ToString());
//appVersion = new Version("0.50.0.0");
if (gitVersion.CompareTo(appVersion) > 0) int newer = gitVersion.CompareTo(appVersion);
if (newer > 0)
{ {
SetVersionLabel(Properties.Strings.DownloadUpdate + ": " + tag, url); SetVersionLabel(Properties.Strings.DownloadUpdate + ": " + tag, url);
DialogResult dialogResult = MessageBox.Show(Properties.Strings.DownloadUpdate + ": G-Helper " + tag + "?", "Update", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes) AutoUpdate(url);
} }
else else
{ {
@@ -314,6 +323,59 @@ namespace GHelper
} }
void SetVersionLabel(string label, string url = null)
{
BeginInvoke(delegate
{
labelVersion.Text = label;
if (url is not null)
{
this.versionUrl = url;
labelVersion.ForeColor = Color.Red;
}
});
}
public async void AutoUpdate(string requestUri)
{
Uri uri = new Uri(requestUri);
string filename = Path.GetFileName(uri.LocalPath);
string exeLocation = Application.ExecutablePath;
string exeDir = Path.GetDirectoryName(exeLocation);
string zipLocation = exeDir + "\\" + filename;
using (WebClient client = new WebClient())
{
client.DownloadFile(uri, zipLocation);
}
var cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.FileName = "powershell";
cmd.StartInfo.Arguments = $"Start-Sleep -Seconds 1; Expand-Archive {zipLocation} -DestinationPath {exeDir} -Force; {exeLocation}";
cmd.Start();
Debug.WriteLine(requestUri);
Debug.WriteLine(zipLocation);
Application.Exit();
return;
}
private void LabelVersion_Click(object? sender, EventArgs e)
{
if (versionUrl.Contains(".zip"))
AutoUpdate(versionUrl);
else
Process.Start(new ProcessStartInfo(versionUrl) { UseShellExecute = true });
}
private static void TrayIcon_MouseMove(object? sender, MouseEventArgs e) private static void TrayIcon_MouseMove(object? sender, MouseEventArgs e)
{ {
Program.settingsForm.RefreshSensors(); Program.settingsForm.RefreshSensors();
@@ -419,22 +481,8 @@ namespace GHelper
} }
void SetVersionLabel(string label, string url = null)
{
labelVersion.Text = label;
if (url is not null)
{
this.versionUrl = url;
labelVersion.ForeColor = Color.Red;
}
}
private void LabelVersion_Click(object? sender, EventArgs e)
{
Process.Start(new ProcessStartInfo(versionUrl) { UseShellExecute = true });
}
private void CheckStartup_CheckedChanged(object? sender, EventArgs e) private void CheckStartup_CheckedChanged(object? sender, EventArgs e)
{ {