This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpTool2/MainForm.cs

322 lines
12 KiB
C#
Raw Normal View History

2019-09-08 21:39:04 +02:00
using System;
2020-02-28 14:26:16 +01:00
using System.Diagnostics;
2019-09-08 21:39:04 +02:00
using System.Drawing;
using System.IO;
using System.IO.Compression;
2019-11-14 18:53:00 +01:00
using System.Reflection;
using System.Runtime.InteropServices;
2020-02-28 14:26:16 +01:00
using System.Windows.Forms;
using UpTool2.DataStructures;
using UpTool2.Properties;
2020-02-28 12:59:59 +01:00
using UpTool2.Tool;
2019-11-12 21:23:14 +01:00
#if DEBUG
using System.Threading;
using System.Linq;
2019-11-12 21:23:14 +01:00
#endif
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
public partial class MainForm : Form
{
2020-02-28 14:26:16 +01:00
public MainForm()
{
GlobalVariables.ReloadElements = ReloadElements;
InitializeComponent();
filterBox.DataSource = Enum.GetValues(typeof(Status));
if (Program.Online)
{
RepoManagement.FetchRepos();
}
else
{
MessageBox.Show("Starting in offline mode!");
controls_reload.Enabled = false;
filterBox.Enabled = false;
filterBox.SelectedIndex = 2;
}
ReloadElements();
if (!Directory.Exists(PathTool.appsPath))
Directory.CreateDirectory(PathTool.appsPath);
}
2019-09-08 21:39:04 +02:00
private void Action_install_Click(object sender, EventArgs e)
{
2019-09-29 16:19:57 +02:00
#if !DEBUG
2019-09-08 21:39:04 +02:00
try
{
2019-09-29 16:19:57 +02:00
#endif
2020-02-28 14:26:16 +01:00
AppInstall.Install((App) action_install.Tag);
2019-09-29 16:19:57 +02:00
#if !DEBUG
2019-09-08 21:39:04 +02:00
}
catch (Exception e1)
{
2020-02-28 14:31:52 +01:00
if (!GlobalVariables.RelE)
2019-09-08 21:39:04 +02:00
throw;
MessageBox.Show(e1.ToString(), "Install failed");
}
2019-09-29 16:19:57 +02:00
#endif
}
2020-02-28 12:59:59 +01:00
2019-10-20 15:18:21 +02:00
private void Action_remove_Click(object sender, EventArgs e)
{
try
{
2020-02-28 14:26:16 +01:00
string app = ((App) action_remove.Tag).appPath;
2020-02-28 12:59:59 +01:00
string tmp = PathTool.tempPath;
2019-10-20 15:18:21 +02:00
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
2020-02-28 12:59:59 +01:00
ZipFile.ExtractToDirectory(Path.Combine(app, "package.zip"), tmp);
2020-02-28 14:26:16 +01:00
Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe", Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"",
WorkingDirectory = Path.Combine(app, "app")
}).WaitForExit();
2019-10-20 15:18:21 +02:00
Directory.Delete(tmp, true);
Directory.Delete(app, true);
2020-02-28 14:26:16 +01:00
if (GlobalVariables.RelE)
ReloadElements();
2019-10-20 15:18:21 +02:00
}
catch (Exception e1)
{
2020-02-28 14:26:16 +01:00
if (!GlobalVariables.RelE)
2019-10-20 15:18:21 +02:00
throw;
MessageBox.Show(e1.ToString(), "Removal failed");
}
}
2020-02-28 12:59:59 +01:00
2019-09-29 16:19:57 +02:00
private void controls_upload_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
2019-09-29 16:19:57 +02:00
#endif
2020-02-28 14:26:16 +01:00
if (searchPackageDialog.ShowDialog() != DialogResult.OK)
return;
Guid id = Guid.NewGuid();
while (GlobalVariables.Apps.ContainsKey(id) || Directory.Exists(PathTool.GetAppPath(id)))
id = Guid.NewGuid();
App appI = new App(AppNameDialog.Show(), "Locally installed package, removal only",
2020-02-28 14:26:16 +01:00
GlobalVariables.minimumVer, "", true, "", id, Color.Red, Resources.C_64.ToBitmap(), false, "");
AppInstall.InstallZip(searchPackageDialog.FileName, appI);
2019-09-29 16:19:57 +02:00
#if !DEBUG
}
catch (Exception e1)
{
2020-02-28 14:31:52 +01:00
if (!GlobalVariables.RelE)
2019-09-29 16:19:57 +02:00
throw;
MessageBox.Show(e1.ToString(), "Install failed");
}
2019-11-09 23:46:49 +01:00
#endif
2019-09-08 21:39:04 +02:00
}
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void ReloadElements()
2019-10-20 15:18:21 +02:00
{
//remove
toolTip.RemoveAll();
2020-02-28 14:26:16 +01:00
ClearSelection();
2019-10-20 15:18:21 +02:00
infoPanel_Title.Invalidate();
infoPanel_Description.Invalidate();
int F = sidebarPanel.Controls.Count;
2020-02-28 14:26:16 +01:00
for (int i = 0; i < F; i++) sidebarPanel.Controls[0].Dispose();
GlobalVariables.Apps.Clear();
2019-10-20 15:18:21 +02:00
//add
toolTip.SetToolTip(controls_settings, "Settings");
toolTip.SetToolTip(controls_reload, "Refresh repositories");
toolTip.SetToolTip(controls_upload, "Install package from disk");
toolTip.SetToolTip(filterBox, "Filter");
toolTip.SetToolTip(action_install, "Install");
toolTip.SetToolTip(action_remove, "Remove");
toolTip.SetToolTip(action_update, "Update");
toolTip.SetToolTip(action_run, "Run");
2020-02-28 14:26:16 +01:00
RepoManagement.GetReposFromDisk();
2019-11-11 15:23:14 +01:00
int availableUpdates = 0;
2020-02-28 14:26:16 +01:00
foreach (App app in GlobalVariables.Apps.Values)
2019-10-20 15:18:21 +02:00
{
Panel sidebarIcon = new Panel();
sidebarIcon.Tag = app;
2020-02-28 14:26:16 +01:00
sidebarIcon.BackColor = app.Color;
2019-10-20 15:18:21 +02:00
sidebarIcon.Size = new Size(70, 70);
2020-02-28 14:26:16 +01:00
sidebarIcon.BackgroundImage = app.Icon;
2019-10-20 15:18:21 +02:00
sidebarIcon.BackgroundImageLayout = ImageLayout.Stretch;
2020-02-28 14:26:16 +01:00
bool updatable = !app.Local && (app.status & Status.Updatable) == Status.Updatable;
sidebarIcon.Click += (sender, e) =>
2019-10-20 18:25:00 +02:00
{
2020-02-28 14:26:16 +01:00
infoPanel_Title.Text = app.Name;
infoPanel_Title.ForeColor = app.Local ? Color.Red : Color.Black;
infoPanel_Description.Text = app.Description;
2019-10-20 15:18:21 +02:00
action_install.Tag = app;
2020-02-28 14:26:16 +01:00
action_install.Enabled = !(app.Local || Directory.Exists(app.appPath));
2019-10-20 15:18:21 +02:00
action_remove.Tag = app;
action_remove.Enabled = Directory.Exists(app.appPath);
2019-10-20 15:18:21 +02:00
action_update.Tag = app;
2019-11-12 21:23:14 +01:00
action_update.Enabled = updatable;
2019-10-20 15:18:21 +02:00
action_run.Tag = app;
2020-02-28 14:26:16 +01:00
action_run.Enabled = !app.Local && app.Runnable && Directory.Exists(app.appPath);
2019-10-20 15:18:21 +02:00
};
2019-11-12 21:23:14 +01:00
if (updatable)
2019-11-11 15:23:14 +01:00
availableUpdates++;
2020-02-28 14:26:16 +01:00
toolTip.SetToolTip(sidebarIcon, app.Name);
2019-10-20 15:18:21 +02:00
sidebarPanel.Controls.Add(sidebarIcon);
}
2020-02-28 14:26:16 +01:00
UpdateSidebarV(null, null);
Text =
$"UpTool2 {(availableUpdates == 0 ? "(All up-to-date)" : $"({availableUpdates.ToString()} Updates)")}";
2019-10-20 15:18:21 +02:00
}
2020-02-28 12:59:59 +01:00
2019-11-09 20:51:53 +01:00
private void Action_run_Click(object sender, EventArgs e)
{
Console.WriteLine(new string('-', 10));
2020-02-28 14:26:16 +01:00
Console.WriteLine(((App) action_run.Tag).dataPath);
2019-11-09 20:51:53 +01:00
Console.WriteLine("\\");
2020-02-28 14:26:16 +01:00
Console.WriteLine(((App) action_run.Tag).MainFile);
Console.WriteLine(((App) action_run.Tag).dataPath);
string path = Path.Combine(((App) action_run.Tag).dataPath, ((App) action_run.Tag).MainFile);
try
{
Process.Start(
new ProcessStartInfo
{
FileName = path,
WorkingDirectory = ((App) action_run.Tag).dataPath
});
}
catch (Exception e1)
{
MessageBox.Show(e1
#if DEBUG
+ $"{Environment.NewLine}File was: {path}"
#endif
2020-02-28 14:26:16 +01:00
, "Failed to start!");
}
2019-11-09 20:51:53 +01:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 15:18:21 +02:00
private void Action_update_Click(object sender, EventArgs e)
{
try
{
2020-02-28 14:26:16 +01:00
GlobalVariables.RelE = false;
2019-10-20 15:18:21 +02:00
Action_remove_Click(sender, e);
Action_install_Click(sender, e);
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString(), "Install failed");
}
2020-02-28 14:26:16 +01:00
ReloadElements();
GlobalVariables.RelE = true;
2019-10-20 15:18:21 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 18:25:00 +02:00
private void Controls_reload_Click(object sender, EventArgs e)
{
2020-02-28 14:26:16 +01:00
RepoManagement.FetchRepos();
ReloadElements();
2019-10-20 18:25:00 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-21 15:33:09 +02:00
private void Controls_settings_Click(object sender, EventArgs e) => new SettingsForms().ShowDialog();
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void ClearSelection()
2019-09-10 10:02:24 +02:00
{
action_install.Enabled = false;
action_remove.Enabled = false;
action_update.Enabled = false;
action_run.Enabled = false;
infoPanel_Title.Text = "";
infoPanel_Description.Text = "";
}
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
private void UpdateSidebarV(object sender, EventArgs e)
2019-09-09 17:50:33 +02:00
{
2019-11-09 20:51:53 +01:00
#if DEBUG
if (searchBox.Text == "!DEBUG:PRINT!")
2019-09-09 17:50:33 +02:00
{
2019-11-09 20:51:53 +01:00
searchBox.Text = "!DEBUG:PRINT";
2020-02-28 14:26:16 +01:00
string tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile,
string.Join("\r\n\r\n",
GlobalVariables.Apps.Values.Select(app => app.ToString()).Concat(new[]
{"Assembly version: " + Assembly.GetExecutingAssembly().GetName().Version}).ToArray()));
2020-02-28 12:59:59 +01:00
new Thread(() =>
{
2020-02-28 14:26:16 +01:00
Process.Start("notepad", tmpFile).WaitForExit();
File.Delete(tmpFile);
2019-11-09 20:51:53 +01:00
}).Start();
2019-09-10 10:02:24 +02:00
}
2019-11-09 20:51:53 +01:00
else
{
#endif
2020-02-28 12:59:59 +01:00
Enum.TryParse(filterBox.SelectedValue.ToString(), out Status status);
for (int i = 0; i < sidebarPanel.Controls.Count; i++)
{
2020-02-28 14:26:16 +01:00
Panel sidebarIcon = (Panel) sidebarPanel.Controls[i];
App app = (App) sidebarIcon.Tag;
sidebarIcon.Visible = app.Name.Contains(searchBox.Text) &&
((int) app.status & (int) (Program.Online ? status : Status.Installed)) != 0;
2020-02-28 12:59:59 +01:00
}
2020-02-28 14:26:16 +01:00
ClearSelection();
2019-11-09 20:51:53 +01:00
#if DEBUG
}
#endif
2019-09-10 10:02:24 +02:00
}
2020-02-28 12:59:59 +01:00
2019-10-20 15:18:21 +02:00
private void MainForm_Load(object sender, EventArgs e)
{
2020-02-28 14:26:16 +01:00
Program.Splash.Hide();
2019-10-20 15:18:21 +02:00
BringToFront();
}
2019-11-14 18:53:00 +01:00
2020-02-28 12:59:59 +01:00
private static DateTime GetBuildDateTime(Assembly assembly)
2019-11-14 18:53:00 +01:00
{
2020-02-28 14:26:16 +01:00
string path = assembly.GetName().CodeBase;
2019-11-14 18:53:00 +01:00
if (File.Exists(path))
{
2020-02-28 14:26:16 +01:00
byte[] buffer = new byte[Math.Max(Marshal.SizeOf(typeof(_IMAGE_FILE_HEADER)), 4)];
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
2019-11-14 18:53:00 +01:00
{
fileStream.Position = 0x3C;
fileStream.Read(buffer, 0, 4);
fileStream.Position = BitConverter.ToUInt32(buffer, 0); // COFF header offset
fileStream.Read(buffer, 0, 4); // "PE\0\0"
fileStream.Read(buffer, 0, buffer.Length);
}
2020-02-28 14:26:16 +01:00
GCHandle pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
2019-11-14 18:53:00 +01:00
try
{
2020-02-28 14:26:16 +01:00
_IMAGE_FILE_HEADER coffHeader =
(_IMAGE_FILE_HEADER) Marshal.PtrToStructure(pinnedBuffer.AddrOfPinnedObject(),
typeof(_IMAGE_FILE_HEADER));
2019-11-14 18:53:00 +01:00
2020-02-28 14:26:16 +01:00
return TimeZone.CurrentTimeZone.ToLocalTime(
new DateTime(1970, 1, 1) + new TimeSpan(coffHeader.TimeDateStamp * TimeSpan.TicksPerSecond));
2019-11-14 18:53:00 +01:00
}
finally
{
pinnedBuffer.Free();
}
}
return new DateTime();
}
private void MainForm_HelpRequested(object sender, HelpEventArgs hlpevent)
{
DateTime buildTime = GetBuildDateTime(Assembly.GetExecutingAssembly());
2020-02-28 12:59:59 +01:00
_ = MessageBox.Show($@"UpTool2 by CC24
2020-02-28 14:26:16 +01:00
Version: {Assembly.GetExecutingAssembly().GetName().Version}
Build Date: {buildTime:dd.MM.yyyy}", "UpTool2");
2020-02-28 12:59:59 +01:00
hlpevent.Handled = true;
2019-11-14 18:53:00 +01:00
}
2020-02-28 14:26:16 +01:00
private struct _IMAGE_FILE_HEADER
{
public ushort Machine;
public ushort NumberOfSections;
public uint TimeDateStamp;
public uint PointerToSymbolTable;
public uint NumberOfSymbols;
public ushort SizeOfOptionalHeader;
public ushort Characteristics;
}
2019-09-08 21:39:04 +02:00
}
2020-02-28 12:59:59 +01:00
}