2020-03-24 20:53:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UpToolLib.DataStructures;
|
|
|
|
|
|
|
|
|
|
namespace UpToolLib.Tool
|
|
|
|
|
{
|
|
|
|
|
public static class AppExtras
|
|
|
|
|
{
|
|
|
|
|
public static Process RunApp(App app) =>
|
|
|
|
|
Process.Start(
|
|
|
|
|
new ProcessStartInfo
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
FileName = Path.Combine(app.DataPath, app.MainFile),
|
|
|
|
|
WorkingDirectory = app.DataPath
|
2020-03-24 20:53:23 +01:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-24 21:27:41 +01:00
|
|
|
|
public static void Update(App app, bool overwrite)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Remove(app, overwrite);
|
|
|
|
|
AppInstall.Install(app, overwrite);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Remove(App app, bool deleteAll)
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
string tmp = PathTool.TempPath;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
if (Directory.Exists(tmp))
|
|
|
|
|
Directory.Delete(tmp, true);
|
|
|
|
|
Directory.CreateDirectory(tmp);
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if (File.Exists(Path.Combine(app.AppPath, "package.zip")))
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
ZipFile.ExtractToDirectory(Path.Combine(app.AppPath, "package.zip"), tmp);
|
|
|
|
|
/*Process.Start(new ProcessStartInfo
|
2020-03-24 21:27:41 +01:00
|
|
|
|
{
|
|
|
|
|
FileName = "cmd.exe",
|
|
|
|
|
Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"",
|
2020-04-08 16:05:09 +02:00
|
|
|
|
WorkingDirectory = Path.Combine(app.AppPath, "app"),
|
2020-03-24 21:27:41 +01:00
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
2020-04-08 16:05:09 +02:00
|
|
|
|
}).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, "Remove.sh")) ? 1 : 2;
|
|
|
|
|
ProcessStartInfo prc = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = key switch
|
|
|
|
|
{
|
|
|
|
|
0 => "cmd.exe",
|
|
|
|
|
1 => "bash",
|
|
|
|
|
2 => "wine",
|
|
|
|
|
_ => throw new Exception()
|
|
|
|
|
},
|
|
|
|
|
WorkingDirectory = Path.Combine(app.AppPath, "app"),
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
|
|
|
};
|
|
|
|
|
foreach (string s in key switch
|
|
|
|
|
{
|
|
|
|
|
0 => new[] {"/C", $"{Path.Combine(tmp, "Remove.bat")}"},
|
|
|
|
|
1 => new[] {Path.Combine(tmp, "Remove.sh")},
|
|
|
|
|
2 => new[] {"cmd", "/C", $"{Path.Combine(tmp, "Remove.bat")}"},
|
|
|
|
|
_ => throw new Exception()
|
|
|
|
|
})
|
|
|
|
|
prc.ArgumentList.Add(s);
|
|
|
|
|
Process.Start(prc)?.WaitForExit();
|
|
|
|
|
if (!deleteAll) CheckDirecory(Path.Combine(tmp, "Data"), app.DataPath);
|
2020-03-24 21:27:41 +01:00
|
|
|
|
Directory.Delete(tmp, true);
|
|
|
|
|
}
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if (File.Exists(app.InfoPath))
|
|
|
|
|
File.Delete(app.InfoPath);
|
|
|
|
|
if (File.Exists(Path.Combine(app.AppPath, "package.zip")))
|
|
|
|
|
File.Delete(Path.Combine(app.AppPath, "package.zip"));
|
2020-04-08 19:16:14 +02:00
|
|
|
|
if (deleteAll || Directory.Exists(app.DataPath) &&
|
|
|
|
|
Directory.GetFiles(app.DataPath).Length + Directory.GetDirectories(app.DataPath).Length ==
|
|
|
|
|
0)
|
2020-04-08 16:05:09 +02:00
|
|
|
|
Directory.Delete(app.AppPath, true);
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CheckDirecory(string tmp, string app)
|
|
|
|
|
{
|
2020-03-24 21:57:52 +01:00
|
|
|
|
foreach (string file in Directory.GetFiles(tmp))
|
|
|
|
|
{
|
|
|
|
|
string tmp1 = Path.Combine(app, Path.GetFileName(file));
|
|
|
|
|
if (File.Exists(tmp1))
|
|
|
|
|
File.Delete(tmp1);
|
|
|
|
|
}
|
2020-03-24 20:53:23 +01:00
|
|
|
|
foreach (string directory in Directory.GetDirectories(tmp))
|
|
|
|
|
CheckDirecory(directory, Path.Combine(app, Path.GetFileName(directory)));
|
2020-03-24 21:57:52 +01:00
|
|
|
|
if (Directory.Exists(app) && Directory.GetFiles(app).Length + Directory.GetDirectories(app).Length == 0)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Directory.Delete(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static App[] FindApps(string identifier)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<Guid, App> tmp = GlobalVariables.Apps;
|
|
|
|
|
IEnumerable<KeyValuePair<Guid, App>> tmp1 = tmp.Where(s => s.Key.ToString().StartsWith(identifier));
|
|
|
|
|
tmp1 = tmp1.Concat(tmp.Where(s => s.Value.Name.Contains(identifier)));
|
|
|
|
|
tmp1 = tmp1.Concat(tmp.Where(s => s.Value.Description.Contains(identifier)));
|
|
|
|
|
return tmp1.Select(s => s.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|