2020-02-28 12:59:59 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Xml.Linq;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
using UpToolLib.DataStructures;
|
2020-02-28 12:59:59 +01:00
|
|
|
|
|
2020-03-24 20:53:23 +01:00
|
|
|
|
namespace UpToolLib.Tool
|
2020-02-28 12:59:59 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
public static class AppInstall
|
2020-02-28 12:59:59 +01:00
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
/// <summary>
|
2020-03-24 22:43:48 +01:00
|
|
|
|
/// Install an application
|
2020-03-24 20:53:23 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appI">The app to install</param>
|
2020-03-24 21:27:41 +01:00
|
|
|
|
/// <param name="force">Set to true to overwrite all old data</param>
|
|
|
|
|
public static void Install(App appI, bool force)
|
2020-02-28 12:59:59 +01:00
|
|
|
|
{
|
|
|
|
|
string app = "";
|
|
|
|
|
string tmp = "";
|
|
|
|
|
#if !DEBUG
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#endif
|
2020-04-08 16:05:09 +02:00
|
|
|
|
app = appI.AppPath;
|
|
|
|
|
tmp = PathTool.TempPath;
|
2020-03-16 15:53:14 +01:00
|
|
|
|
if (Directory.Exists(tmp))
|
|
|
|
|
Directory.Delete(tmp, true);
|
|
|
|
|
Directory.CreateDirectory(tmp);
|
2020-03-24 21:27:41 +01:00
|
|
|
|
if (force)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(app))
|
|
|
|
|
Directory.Delete(app, true);
|
|
|
|
|
Directory.CreateDirectory(app);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(app))
|
|
|
|
|
Directory.CreateDirectory(app);
|
|
|
|
|
}
|
2020-04-08 16:05:09 +02:00
|
|
|
|
(bool dlSuccess, byte[] dlData) = ExternalFunctionalityManager.Instance.Download(new Uri(appI.File));
|
2020-03-24 20:53:23 +01:00
|
|
|
|
if (!dlSuccess)
|
2020-03-16 15:53:14 +01:00
|
|
|
|
throw new Exception("Download failed");
|
|
|
|
|
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
|
|
|
|
{
|
2020-03-24 20:53:23 +01:00
|
|
|
|
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dlData)).Replace("-", string.Empty)
|
2020-03-16 15:53:14 +01:00
|
|
|
|
.ToUpper();
|
|
|
|
|
if (pkgHash != appI.Hash.ToUpper())
|
|
|
|
|
throw new Exception($@"The hash is not equal to the one stored in the repo:
|
2020-02-28 14:26:16 +01:00
|
|
|
|
Package: {pkgHash}
|
|
|
|
|
Online: {appI.Hash.ToUpper()}");
|
2020-03-16 15:53:14 +01:00
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
File.WriteAllBytes(Path.Combine(app, "package.zip"), dlData);
|
2020-03-24 21:27:41 +01:00
|
|
|
|
CompleteInstall(appI, force);
|
2020-02-28 12:59:59 +01:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(app))
|
|
|
|
|
Directory.Delete(app, true);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
#endif
|
2020-03-16 15:53:14 +01:00
|
|
|
|
if (tmp != "" && Directory.Exists(tmp))
|
|
|
|
|
Directory.Delete(tmp, true);
|
2020-02-28 12:59:59 +01:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
public static void InstallZip(string zipPath, App meta, bool force)
|
2020-02-28 12:59:59 +01:00
|
|
|
|
{
|
|
|
|
|
string app = "";
|
|
|
|
|
string tmp = "";
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
app = meta.AppPath;
|
2020-02-28 12:59:59 +01:00
|
|
|
|
Directory.CreateDirectory(app);
|
2020-04-08 16:05:09 +02:00
|
|
|
|
tmp = PathTool.TempPath;
|
2020-02-28 12:59:59 +01:00
|
|
|
|
if (Directory.Exists(tmp))
|
|
|
|
|
Directory.Delete(tmp, true);
|
|
|
|
|
Directory.CreateDirectory(tmp);
|
2020-02-28 14:26:16 +01:00
|
|
|
|
File.Copy(zipPath, Path.Combine(app, "package.zip"));
|
2020-03-24 21:27:41 +01:00
|
|
|
|
CompleteInstall(meta, force);
|
2020-02-28 12:59:59 +01:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(app))
|
|
|
|
|
Directory.Delete(app, true);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (tmp != "" && Directory.Exists(tmp))
|
|
|
|
|
Directory.Delete(tmp, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 20:16:33 +01:00
|
|
|
|
//Use
|
|
|
|
|
//PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Hello World')"
|
|
|
|
|
//for message boxes
|
2020-04-08 16:05:09 +02:00
|
|
|
|
private static void CompleteInstall(App app, bool force) => CompleteInstall(app.AppPath, app.Name,
|
2020-03-24 22:43:48 +01:00
|
|
|
|
app.Description, app.Version, app.MainFile, force);
|
2020-02-28 12:59:59 +01:00
|
|
|
|
|
2020-03-24 22:43:48 +01:00
|
|
|
|
private static void CompleteInstall(string appPath, string name, string description, Version version,
|
|
|
|
|
string mainFile, bool force)
|
2020-02-28 12:59:59 +01:00
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
string tmp = PathTool.TempPath;
|
2020-02-28 14:26:16 +01:00
|
|
|
|
ZipFile.ExtractToDirectory(Path.Combine(appPath, "package.zip"), tmp);
|
2020-03-24 21:27:41 +01:00
|
|
|
|
if (force)
|
|
|
|
|
Directory.Move(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app"));
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-24 23:14:40 +01:00
|
|
|
|
CopyAll(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app"));
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Directory.Delete(Path.Combine(tmp, "Data"), true);
|
|
|
|
|
}
|
2020-02-28 14:26:16 +01:00
|
|
|
|
XElement el = new XElement("app", new XElement("Name", name), new XElement("Description", description),
|
|
|
|
|
new XElement("Version", version));
|
2020-02-28 12:59:59 +01:00
|
|
|
|
if (mainFile != null)
|
|
|
|
|
el.Add(new XElement(new XElement("MainFile", mainFile)));
|
|
|
|
|
el.Save(Path.Combine(appPath, "info.xml"));
|
2020-04-08 16:05:09 +02:00
|
|
|
|
/*if (new[] { PlatformID.Xbox, PlatformID.Win32S, PlatformID.Win32Windows, PlatformID.Win32NT, PlatformID.WinCE }.Contains(Environment.OSVersion.Platform))
|
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
ArgumentList = {"/C", $"{Path.Combine(tmp, "Install.bat")}"},
|
|
|
|
|
WorkingDirectory = Path.Combine(appPath, "app"),
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
|
|
|
}).WaitForExit();
|
|
|
|
|
else if (File.Exists(Path.Combine(tmp, "Install.sh")))
|
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "bash",
|
|
|
|
|
ArgumentList = {Path.Combine(tmp, "Install.sh")},
|
|
|
|
|
WorkingDirectory = Path.Combine(appPath, "app"),
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
|
|
|
}).WaitForExit();
|
|
|
|
|
else
|
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "wine",
|
|
|
|
|
ArgumentList = {"cmd", "/C", $"{Path.Combine(tmp, "Install.bat")}"},
|
|
|
|
|
WorkingDirectory = Path.Combine(appPath, "app"),
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
|
|
|
}).WaitForExit();*/
|
2020-04-08 16:32:16 +02:00
|
|
|
|
int key = PlatformCheck.IsWindows ? 0 :
|
2020-04-08 16:05:09 +02:00
|
|
|
|
File.Exists(Path.Combine(tmp, "Install.sh")) ? 1 : 2;
|
|
|
|
|
ProcessStartInfo prc = new ProcessStartInfo
|
2020-02-28 14:26:16 +01:00
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
FileName = key switch
|
|
|
|
|
{
|
|
|
|
|
0 => "cmd.exe",
|
|
|
|
|
1 => "bash",
|
|
|
|
|
2 => "wine",
|
|
|
|
|
_ => throw new Exception()
|
|
|
|
|
},
|
2020-03-20 13:03:44 +01:00
|
|
|
|
WorkingDirectory = Path.Combine(appPath, "app"),
|
|
|
|
|
CreateNoWindow = true,
|
2020-02-28 14:26:16 +01:00
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
2020-04-08 16:05:09 +02:00
|
|
|
|
};
|
|
|
|
|
foreach (string s in key switch
|
|
|
|
|
{
|
|
|
|
|
0 => new[] {"/C", $"{Path.Combine(tmp, "Install.bat")}"},
|
|
|
|
|
1 => new[] {Path.Combine(tmp, "Install.sh")},
|
|
|
|
|
2 => new[] {"cmd", "/C", $"{Path.Combine(tmp, "Install.bat")}"},
|
|
|
|
|
_ => throw new Exception()
|
|
|
|
|
})
|
|
|
|
|
prc.ArgumentList.Add(s);
|
|
|
|
|
Process.Start(prc)?.WaitForExit();
|
2020-02-28 12:59:59 +01:00
|
|
|
|
}
|
2020-03-24 22:43:48 +01:00
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
private static void CopyAll(string source, string target)
|
|
|
|
|
{
|
2020-03-24 22:43:48 +01:00
|
|
|
|
if (string.Equals(Path.GetFullPath(source), Path.GetFullPath(target),
|
|
|
|
|
StringComparison.CurrentCultureIgnoreCase))
|
2020-03-24 21:27:41 +01:00
|
|
|
|
return;
|
|
|
|
|
if (!Directory.Exists(target))
|
|
|
|
|
Directory.CreateDirectory(target);
|
2020-03-24 22:43:48 +01:00
|
|
|
|
foreach (string file in Directory.GetFiles(source))
|
|
|
|
|
File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
|
2020-03-24 21:27:41 +01:00
|
|
|
|
foreach (string dir in Directory.GetDirectories(source))
|
|
|
|
|
CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
|
|
|
|
|
}
|
2020-02-28 12:59:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|