uptime-kuma/extra/exe-builder/Program.cs

162 lines
5.0 KiB
C#
Raw Normal View History

2022-10-06 04:08:10 +00:00
using System;
using System.Collections.Generic;
2022-10-06 16:16:07 +00:00
using System.Diagnostics;
2022-10-06 04:08:10 +00:00
using System.Drawing;
2022-10-08 08:23:11 +00:00
using System.IO;
2022-10-06 04:08:10 +00:00
using System.Linq;
using System.Reflection;
2022-10-07 10:38:14 +00:00
using System.Runtime.InteropServices;
2022-10-06 04:08:10 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
2022-10-06 04:08:10 +00:00
using UptimeKuma.Properties;
namespace UptimeKuma {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
2022-10-07 10:38:14 +00:00
static void Main(string[] args) {
2022-10-06 04:08:10 +00:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new UptimeKumaApplicationContext());
}
}
public class UptimeKumaApplicationContext : ApplicationContext
{
const string appName = "Uptime Kuma";
2022-10-06 04:08:10 +00:00
private NotifyIcon trayIcon;
2022-10-06 16:16:07 +00:00
private Process process;
2022-10-06 04:08:10 +00:00
private MenuItem runWhenStarts;
private RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
2022-10-06 04:08:10 +00:00
public UptimeKumaApplicationContext()
{
trayIcon = new NotifyIcon();
runWhenStarts = new MenuItem("Run when system starts", RunWhenStarts);
runWhenStarts.Checked = registryKey.GetValue(appName) != null;
2022-10-06 04:08:10 +00:00
trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
trayIcon.ContextMenu = new ContextMenu(new MenuItem[] {
2022-10-07 10:38:14 +00:00
new("Open", Open),
//new("Debug Console", DebugConsole),
runWhenStarts,
2022-10-07 10:38:14 +00:00
new("Check for Update...", CheckForUpdate),
new("Visit GitHub...", VisitGitHub),
new("About", About),
new("Exit", Exit),
2022-10-06 04:08:10 +00:00
});
2022-10-07 10:38:14 +00:00
trayIcon.MouseDoubleClick += new MouseEventHandler(Open);
2022-10-06 04:08:10 +00:00
trayIcon.Visible = true;
2022-10-06 16:16:07 +00:00
if (Directory.Exists("core") && Directory.Exists("node") && Directory.Exists("core/node_modules") && Directory.Exists("core/dist")) {
2022-10-08 08:23:11 +00:00
// Go go go
StartProcess();
} else {
DownloadFiles();
}
}
void DownloadFiles() {
var form = new DownloadForm();
form.Closed += Exit;
form.Show();
}
private void RunWhenStarts(object sender, EventArgs e) {
if (registryKey == null) {
MessageBox.Show("Error: Unable to set startup registry key.");
return;
}
if (runWhenStarts.Checked) {
registryKey.DeleteValue(appName, false);
runWhenStarts.Checked = false;
} else {
registryKey.SetValue(appName, Application.ExecutablePath);
runWhenStarts.Checked = true;
}
}
2022-10-08 08:23:11 +00:00
void StartProcess() {
2022-10-07 10:38:14 +00:00
var startInfo = new ProcessStartInfo {
FileName = "node/node.exe",
Arguments = "server/server.js --data-dir=\"../data/\"",
RedirectStandardOutput = false,
RedirectStandardError = false,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = "core"
};
2022-10-06 16:16:07 +00:00
process = new Process();
process.StartInfo = startInfo;
process.EnableRaisingEvents = true;
2022-10-08 08:23:11 +00:00
process.Exited += ProcessExited;
2022-10-07 10:38:14 +00:00
2022-10-06 16:16:07 +00:00
try {
process.Start();
2022-10-07 10:38:14 +00:00
//Open(null, null);
2022-10-06 16:16:07 +00:00
} catch (Exception e) {
MessageBox.Show("Startup failed: " + e.Message, "Uptime Kuma Error");
}
2022-10-06 04:08:10 +00:00
}
2022-10-06 16:16:07 +00:00
void Open(object sender, EventArgs e) {
Process.Start("http://localhost:3001");
}
2022-10-07 10:38:14 +00:00
void DebugConsole(object sender, EventArgs e) {
}
2022-10-06 16:16:07 +00:00
void CheckForUpdate(object sender, EventArgs e) {
2022-10-07 10:38:14 +00:00
Process.Start("https://github.com/louislam/uptime-kuma/releases");
}
2022-10-06 16:16:07 +00:00
2022-10-07 10:38:14 +00:00
void VisitGitHub(object sender, EventArgs e)
{
Process.Start("https://github.com/louislam/uptime-kuma");
2022-10-06 04:08:10 +00:00
}
void About(object sender, EventArgs e)
{
MessageBox.Show("Uptime Kuma Windows Runtime v1.0.0" + Environment.NewLine + "© 2023 Louis Lam", "Info");
2022-10-06 04:08:10 +00:00
}
2022-10-06 16:16:07 +00:00
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
2022-10-08 08:23:11 +00:00
process?.Kill();
Application.Exit();
2022-10-07 10:38:14 +00:00
}
void ProcessExited(object sender, EventArgs e) {
if (process.ExitCode != 0) {
var line = "";
while (!process.StandardOutput.EndOfStream)
{
line += process.StandardOutput.ReadLine();
}
MessageBox.Show("Uptime Kuma exited unexpectedly. Exit code: " + process.ExitCode + " " + line);
}
trayIcon.Visible = false;
2022-10-06 16:16:07 +00:00
Application.Exit();
2022-10-06 04:08:10 +00:00
}
2022-10-07 10:38:14 +00:00
2022-10-06 04:08:10 +00:00
}
}