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

282 lines
11 KiB
C#
Raw Normal View History

2019-09-08 21:39:04 +02:00
using System;
using System.Drawing;
using System.Windows.Forms;
using UpTool2.Properties;
using System.IO;
using System.Diagnostics;
using System.IO.Compression;
2019-09-29 16:19:57 +02:00
using Microsoft.VisualBasic;
2019-11-14 18:53:00 +01:00
using System.Reflection;
using System.Runtime.InteropServices;
2019-11-12 21:23:14 +01:00
#if DEBUG
using System.Threading;
using System.Linq;
#endif
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
public partial class MainForm : Form
{
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
2019-11-11 16:06:15 +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)
{
2019-11-11 15:48:49 +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
}
2019-10-20 15:18:21 +02:00
private void Action_remove_Click(object sender, EventArgs e)
{
try
{
2019-11-11 15:48:49 +01:00
string app = GlobalVariables.dir + @"\Apps\" + ((App)action_remove.Tag).ID.ToString();
string tmp = GlobalVariables.dir + @"\tmp";
2019-10-20 15:18:21 +02:00
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
ZipFile.ExtractToDirectory(app + @"\package.zip", tmp);
Process.Start(new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C \"" + tmp + "\\Remove.bat\"", WorkingDirectory = app + @"\app" }).WaitForExit();
Directory.Delete(tmp, true);
Directory.Delete(app, true);
2019-11-11 15:48:49 +01:00
if (GlobalVariables.relE)
2019-10-20 15:18:21 +02:00
reloadElements();
}
catch (Exception e1)
{
2019-11-11 15:48:49 +01:00
if (!GlobalVariables.relE)
2019-10-20 15:18:21 +02:00
throw;
MessageBox.Show(e1.ToString(), "Removal failed");
}
}
2019-09-29 16:19:57 +02:00
private void controls_upload_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
if (searchPackageDialog.ShowDialog() == DialogResult.OK)
{
Guid ID = Guid.NewGuid();
2019-11-11 16:06:15 +01:00
while (GlobalVariables.apps.ContainsKey(ID) || Directory.Exists(GlobalVariables.getAppPath(ID)))
2019-09-29 16:19:57 +02:00
ID = Guid.NewGuid();
2019-11-12 21:23:14 +01:00
App appI = new App(Interaction.InputBox("Name:"), "Locally installed package, removal only", GlobalVariables.minimumVer, "", true, "", ID, Color.Red, Resources.C_64.ToBitmap(), false, "");
2019-11-11 16:06:15 +01:00
AppInstall.installZip(searchPackageDialog.FileName, appI);
2019-09-29 16:19:57 +02:00
}
#if !DEBUG
}
catch (Exception e1)
{
2019-11-11 15:48:49 +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
}
2019-10-20 15:18:21 +02:00
void reloadElements()
{
//remove
toolTip.RemoveAll();
clearSelection();
infoPanel_Title.Invalidate();
infoPanel_Description.Invalidate();
int F = sidebarPanel.Controls.Count;
for (int i = 0; i < F; i++)
{
sidebarPanel.Controls[0].Dispose();
}
2019-11-11 15:48:49 +01:00
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");
2019-11-11 15:48:49 +01:00
RepoManagement.getReposFromDisk();
2019-11-11 15:23:14 +01:00
int availableUpdates = 0;
2019-11-11 15:48:49 +01:00
foreach (App app in GlobalVariables.apps.Values)
2019-10-20 15:18:21 +02:00
{
Panel sidebarIcon = new Panel();
sidebarIcon.Tag = app;
sidebarIcon.BackColor = app.color;
sidebarIcon.Size = new Size(70, 70);
sidebarIcon.BackgroundImage = app.icon;
sidebarIcon.BackgroundImageLayout = ImageLayout.Stretch;
2019-11-12 21:23:14 +01:00
bool updatable = (!app.local) && ((app.status & Status.Updatable) == Status.Updatable);
2019-10-20 18:25:00 +02:00
sidebarIcon.Click += (object sender, EventArgs e) =>
{
2019-10-20 15:18:21 +02:00
infoPanel_Title.Text = app.name;
infoPanel_Title.ForeColor = app.local ? Color.Red : Color.Black;
infoPanel_Description.Text = app.description;
action_install.Tag = app;
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;
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++;
2019-10-20 15:18:21 +02:00
toolTip.SetToolTip(sidebarIcon, app.name);
sidebarPanel.Controls.Add(sidebarIcon);
}
updateSidebarV(null, null);
2019-11-11 15:23:14 +01:00
Text = "UpTool2 " + ((availableUpdates == 0) ? "(All up-to-date)" : "(" + availableUpdates.ToString() + " Updates)");
2019-10-20 15:18:21 +02:00
}
2019-11-09 20:51:53 +01:00
private void Action_run_Click(object sender, EventArgs e)
{
Console.WriteLine(new string('-', 10));
2019-11-11 15:48:49 +01:00
Console.WriteLine(GlobalVariables.getDataPath((App)action_run.Tag));
2019-11-09 20:51:53 +01:00
Console.WriteLine("\\");
Console.WriteLine(((App)action_run.Tag).mainFile);
2019-11-11 15:48:49 +01:00
Console.WriteLine(GlobalVariables.getDataPath((App)action_run.Tag));
2019-11-09 20:51:53 +01:00
_ = Process.Start(
2019-11-11 14:41:11 +01:00
new ProcessStartInfo
{
2019-11-11 15:48:49 +01:00
FileName = GlobalVariables.getDataPath((App)action_run.Tag) + "\\" + ((App)action_run.Tag).mainFile,
WorkingDirectory = GlobalVariables.getDataPath((App)action_run.Tag)
2019-11-09 20:51:53 +01:00
});
}
2019-10-20 15:18:21 +02:00
private void Action_update_Click(object sender, EventArgs e)
{
try
{
2019-11-11 15:48:49 +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");
}
reloadElements();
2019-11-11 15:48:49 +01:00
GlobalVariables.relE = true;
2019-10-20 15:18:21 +02:00
}
2019-10-20 18:25:00 +02:00
private void Controls_reload_Click(object sender, EventArgs e)
{
2019-11-11 15:48:49 +01:00
RepoManagement.fetchRepos();
2019-10-20 18:25:00 +02:00
reloadElements();
}
2019-10-21 15:33:09 +02:00
private void Controls_settings_Click(object sender, EventArgs e) => new SettingsForms().ShowDialog();
2019-09-10 10:02:24 +02:00
void clearSelection()
{
action_install.Enabled = false;
action_remove.Enabled = false;
action_update.Enabled = false;
action_run.Enabled = false;
infoPanel_Title.Text = "";
infoPanel_Description.Text = "";
}
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";
string _tmp_file = Path.GetTempFileName();
2019-11-14 18:53:00 +01:00
File.WriteAllText(_tmp_file, string.Join("\r\n\r\n", GlobalVariables.apps.Values.Select(app => app.ToString()).Concat(new string[] { "Assembly version: " + Assembly.GetExecutingAssembly().GetName().Version.ToString() }).ToArray()));
2019-11-09 20:51:53 +01:00
new Thread(() => {
Process.Start("notepad", _tmp_file).WaitForExit();
File.Delete(_tmp_file);
}).Start();
2019-09-10 10:02:24 +02:00
}
2019-11-09 20:51:53 +01:00
else
{
#endif
2019-11-11 14:41:11 +01:00
Enum.TryParse(filterBox.SelectedValue.ToString(), out Status status);
for (int i = 0; i < sidebarPanel.Controls.Count; i++)
{
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;
}
clearSelection();
2019-11-09 20:51:53 +01:00
#if DEBUG
}
#endif
2019-09-10 10:02:24 +02:00
}
2019-10-20 15:18:21 +02:00
public MainForm()
{
2019-11-11 16:06:15 +01:00
GlobalVariables.reloadElements = reloadElements;
2019-10-20 15:18:21 +02:00
InitializeComponent();
filterBox.DataSource = Enum.GetValues(typeof(Status));
2019-10-21 16:12:07 +02:00
if (Program.online)
2019-11-11 15:48:49 +01:00
RepoManagement.fetchRepos();
2019-10-21 16:12:07 +02:00
else
{
MessageBox.Show("Starting in offline mode!");
controls_reload.Enabled = false;
filterBox.Enabled = false;
filterBox.SelectedIndex = 2;
}
2019-10-20 15:18:21 +02:00
reloadElements();
2019-11-11 15:48:49 +01:00
if (!Directory.Exists(GlobalVariables.appsPath))
Directory.CreateDirectory(GlobalVariables.appsPath);
2019-10-20 15:18:21 +02:00
}
private void MainForm_Load(object sender, EventArgs e)
{
Program.splash.Hide();
BringToFront();
}
2019-11-14 18:53:00 +01:00
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-12-20 13:29:13 +01:00
}
2019-11-14 18:53:00 +01:00
static DateTime GetBuildDateTime(Assembly assembly)
{
var path = assembly.GetName().CodeBase;
if (File.Exists(path))
{
var buffer = new byte[Math.Max(Marshal.SizeOf(typeof(_IMAGE_FILE_HEADER)), 4)];
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
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);
}
var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
try
{
var coffHeader = (_IMAGE_FILE_HEADER)Marshal.PtrToStructure(pinnedBuffer.AddrOfPinnedObject(), typeof(_IMAGE_FILE_HEADER));
return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1) + new TimeSpan(coffHeader.TimeDateStamp * TimeSpan.TicksPerSecond));
}
finally
{
pinnedBuffer.Free();
}
}
return new DateTime();
}
private void MainForm_HelpRequested(object sender, HelpEventArgs hlpevent)
{
DateTime buildTime = GetBuildDateTime(Assembly.GetExecutingAssembly());
MessageBox.Show("UpTool2 by CC24\r\nVersion: " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\r\nBuild Date: " + buildTime.ToString("dd.MM.yyyy"), "UpTool2");
}
2019-09-08 21:39:04 +02:00
}
}