more changes

This commit is contained in:
CreepyCrafter24 2019-11-11 16:06:15 +01:00
parent 2453b05f1c
commit 8490c54df2
5 changed files with 114 additions and 62 deletions

View File

@ -64,5 +64,8 @@ namespace UpTool2
public override string ToString() => "Name: " + name + "\r\nDescription:\r\n" + string.Join("\r\n", description.Split('\n').Select(s => { if (s.EndsWith("\r")) s.Remove(s.Length - 1, 1); return "> " + s; })) + "\r\nVersion: " + version + "\r\nFile: " + file + "\r\nLocal: " + local.ToString() + "\r\nHash: " + hash + "\r\nID: " + ID.ToString() + "\r\nColor: " + color.ToKnownColor().ToString() + "\r\nRunnable: " + runnable + "\r\nMainFile: " + mainFile + "\r\nStatus: " + status.ToString() + "\r\nObject Hash Code: " + GetHashCode();
public static bool operator ==(App left, App right) => left.Equals(right);
public static bool operator !=(App left, App right) => !(left == right);
public string appPath => GlobalVariables.getAppPath(this);
public string dataPath => GlobalVariables.getDataPath(this);
public string infoPath => GlobalVariables.getInfoPath(this);
}
}

105
UpTool2/AppInstall.cs Normal file
View File

@ -0,0 +1,105 @@
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace UpTool2
{
static class AppInstall
{
public static void Install(App appI)
{
string app = "";
string tmp = "";
try
{
app = GlobalVariables.dir + @"\Apps\" + appI.ID.ToString();
tmp = GlobalVariables.dir + @"\tmp";
if (Directory.Exists(""))
Directory.Delete("", true);
Directory.CreateDirectory("");
if (Directory.Exists(app))
Directory.Delete(app, true);
Directory.CreateDirectory(app);
if (new DownloadDialog(appI.file, app + @"\package.zip").ShowDialog() != DialogResult.OK)
throw new Exception("Download failed");
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
string pkghash = BitConverter.ToString(sha256.ComputeHash(File.ReadAllBytes(app + @"\package.zip"))).Replace("-", string.Empty).ToUpper();
if (pkghash != appI.hash.ToUpper())
throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkghash + "\r\nOnline: " + appI.hash.ToUpper());
}
completeInstall(app, appI);
}
catch
{
if (Directory.Exists(app))
Directory.Delete(app, true);
throw;
}
finally
{
if (tmp != "" && Directory.Exists(tmp))
Directory.Delete(tmp, true);
}
}
public static void installZip(string zipPath, App meta)
{
string app = "";
string tmp = "";
try
{
app = meta.appPath;
Directory.CreateDirectory(app);
tmp = GlobalVariables.dir + @"\tmp";
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
File.Copy(zipPath, app + @"\package.zip");
completeInstall(app, meta);
}
catch
{
if (Directory.Exists(app))
Directory.Delete(app, true);
throw;
}
finally
{
if (tmp != "" && Directory.Exists(tmp))
Directory.Delete(tmp, true);
}
}
static void completeInstall(string app, App appI)
{
#if !DEBUG
try
{
#endif
string tmp = GlobalVariables.dir + @"\tmp";
ZipFile.ExtractToDirectory(app + @"\package.zip", tmp);
Directory.Move(tmp + @"\Data", app + @"\app");
if (appI.runnable)
new XElement("app", new XElement("Name", appI.name), new XElement("Description", appI.description), new XElement("Version", appI.version), new XElement("MainFile", appI.mainFile)).Save(app + @"\info.xml");
else
new XElement("app", new XElement("Name", appI.name), new XElement("Description", appI.description), new XElement("Version", appI.version)).Save(app + @"\info.xml");
Process.Start(new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C \"" + tmp + "\\Install.bat\"", WorkingDirectory = app + @"\app", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }).WaitForExit();
if (GlobalVariables.relE)
GlobalVariables.reloadElements.Invoke();
#if !DEBUG
}
catch { throw; }
#endif
}
}
}

View File

@ -17,5 +17,6 @@ namespace UpTool2
public static string getDataPath(Guid app) => getAppPath(app) + @"\app";
public static string getInfoPath(Guid app) => getAppPath(app) + "\\info.xml";
public static bool relE = true;
public static Action reloadElements;
}
}

View File

@ -15,42 +15,20 @@ namespace UpTool2
{
private void Action_install_Click(object sender, EventArgs e)
{
string app = "";
string tmp = "";
#if !DEBUG
try
{
#endif
App appI = (App)action_install.Tag;
app = GlobalVariables.dir + @"\Apps\" + appI.ID.ToString();
tmp = GlobalVariables.dir + @"\tmp";
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
if (Directory.Exists(app))
Directory.Delete(app, true);
Directory.CreateDirectory(app);
if (new DownloadDialog(appI.file, app + @"\package.zip").ShowDialog() != DialogResult.OK)
throw new Exception("Download failed");
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
string pkghash = BitConverter.ToString(sha256.ComputeHash(File.ReadAllBytes(app + @"\package.zip"))).Replace("-", string.Empty).ToUpper();
if (pkghash != appI.hash.ToUpper())
throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkghash + "\r\nOnline: " + appI.hash.ToUpper());
}
completeInstall(app, appI);
AppInstall.Install((App)action_install.Tag);
#if !DEBUG
}
catch (Exception e1)
{
if (!GlobalVariables.relE)
throw;
if (Directory.Exists(app))
Directory.Delete(app, true);
MessageBox.Show(e1.ToString(), "Install failed");
}
#endif
Directory.Delete(tmp, true);
}
private void Action_remove_Click(object sender, EventArgs e)
{
@ -77,8 +55,6 @@ namespace UpTool2
}
private void controls_upload_Click(object sender, EventArgs e)
{
string app = "";
string tmp = "";
#if !DEBUG
try
{
@ -86,20 +62,10 @@ namespace UpTool2
if (searchPackageDialog.ShowDialog() == DialogResult.OK)
{
Guid ID = Guid.NewGuid();
app = GlobalVariables.dir + @"\Apps\" + ID.ToString();
while (Directory.Exists(app))
{
while (GlobalVariables.apps.ContainsKey(ID) || Directory.Exists(GlobalVariables.getAppPath(ID)))
ID = Guid.NewGuid();
app = GlobalVariables.dir + @"\Apps\" + ID.ToString();
}
App appI = new App(Interaction.InputBox("Name:"), "Locally installed package, removal only", -1, "", true, "", ID, Color.Red, Resources.C_64.ToBitmap(), false, "");
Directory.CreateDirectory(app);
tmp = GlobalVariables.dir + @"\tmp";
if (Directory.Exists(tmp))
Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp);
File.Copy(searchPackageDialog.FileName, app + @"\package.zip");
completeInstall(app, appI);
AppInstall.installZip(searchPackageDialog.FileName, appI);
}
#if !DEBUG
}
@ -107,33 +73,8 @@ namespace UpTool2
{
if (!GlobalVariables.relE)
throw;
if (Directory.Exists(app))
Directory.Delete(app, true);
MessageBox.Show(e1.ToString(), "Install failed");
}
#endif
if (tmp != "" && Directory.Exists(tmp))
Directory.Delete(tmp, true);
}
void completeInstall(string app, App appI)
{
#if !DEBUG
try
{
#endif
string tmp = GlobalVariables.dir + @"\tmp";
ZipFile.ExtractToDirectory(app + @"\package.zip", tmp);
Directory.Move(tmp + @"\Data", app + @"\app");
if (appI.runnable)
new XElement("app", new XElement("Name", appI.name), new XElement("Description", appI.description), new XElement("Version", appI.version), new XElement("MainFile", appI.mainFile)).Save(app + @"\info.xml");
else
new XElement("app", new XElement("Name", appI.name), new XElement("Description", appI.description), new XElement("Version", appI.version)).Save(app + @"\info.xml");
Process.Start(new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C \"" + tmp + "\\Install.bat\"", WorkingDirectory = app + @"\app", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }).WaitForExit();
if (GlobalVariables.relE)
reloadElements();
#if !DEBUG
}
catch { throw; }
#endif
}
void reloadElements()
@ -267,6 +208,7 @@ namespace UpTool2
}
public MainForm()
{
GlobalVariables.reloadElements = reloadElements;
InitializeComponent();
filterBox.DataSource = Enum.GetValues(typeof(Status));
if (Program.online)

View File

@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App.cs" />
<Compile Include="AppInstall.cs" />
<Compile Include="DownloadDialog.cs">
<SubType>Form</SubType>
</Compile>