2020-03-24 20:53:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2020-09-10 20:05:44 +02:00
|
|
|
|
//using CC_Functions.Misc;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
using UpToolLib.DataStructures;
|
|
|
|
|
|
|
|
|
|
namespace UpToolLib.Tool
|
|
|
|
|
{
|
|
|
|
|
public static class RepoManagement
|
|
|
|
|
{
|
|
|
|
|
public static void FetchRepos()
|
|
|
|
|
{
|
|
|
|
|
XmlTool.FixXml();
|
|
|
|
|
XElement meta = XDocument.Load(PathTool.InfoXml).Element("meta");
|
|
|
|
|
List<XElement> tmpAppsList = new List<XElement>();
|
|
|
|
|
List<string> repArr = meta.Element("Repos").Elements("Repo").Select(s => s.Element("Link").Value).Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (i < repArr.Count)
|
|
|
|
|
{
|
2020-08-31 18:58:15 +02:00
|
|
|
|
string status = "Initializing";
|
2020-03-24 20:53:23 +01:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#endif
|
2020-04-08 16:05:09 +02:00
|
|
|
|
ExternalFunctionalityManager.Instance.Log($"[{i + 1}] Loading {repArr[i]}");
|
2020-08-31 18:58:15 +02:00
|
|
|
|
status = $"Loading {repArr[i]}";
|
2020-09-10 20:05:44 +02:00
|
|
|
|
XDocument repo = XDocument.Load(new Uri(repArr[i]).AbsoluteUri);
|
2020-08-31 18:58:15 +02:00
|
|
|
|
status = $"Extracting repolinks";
|
2020-03-24 20:53:23 +01:00
|
|
|
|
repArr.AddRange(repo.Element("repo").Elements("repolink").Select(s => s.Value)
|
|
|
|
|
.Where(s => !repArr.Contains(s)));
|
2020-08-31 18:58:15 +02:00
|
|
|
|
status = $"Extracting apps";
|
2020-04-08 16:05:09 +02:00
|
|
|
|
XElement[] tmpApparray = repo.Element("repo").Elements("app").Where(app =>
|
2020-03-24 20:53:23 +01:00
|
|
|
|
!tmpAppsList.Any(a => a.Element("ID").Value == app.Element("ID").Value) ||
|
|
|
|
|
!tmpAppsList
|
|
|
|
|
.Where(a => a.Element("ID").Value == app.Element("ID").Value).Any(a =>
|
|
|
|
|
GetVer(a.Element("Version")) >= app.Element("Version").GetVer())).ToArray()
|
|
|
|
|
.Concat(repo.Element("repo").Elements("applink")
|
|
|
|
|
.Select(s =>
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
ExternalFunctionalityManager.Instance.Log($"- Loading {s.Value}");
|
2020-08-31 18:58:15 +02:00
|
|
|
|
status = $"Fetching app data tag: {s.Value}";
|
2020-09-10 20:05:44 +02:00
|
|
|
|
XElement ret = XDocument.Load(new Uri(s.Value).AbsoluteUri)
|
2020-08-31 18:58:15 +02:00
|
|
|
|
.Element("app");
|
|
|
|
|
return ret;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}))
|
|
|
|
|
.ToArray();
|
2020-08-31 18:58:15 +02:00
|
|
|
|
status = $"Creating app cache";
|
2020-04-08 16:05:09 +02:00
|
|
|
|
foreach (XElement app in tmpApparray)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{
|
|
|
|
|
//"Sanity check"
|
|
|
|
|
Version.Parse(app.Element("Version").Value);
|
|
|
|
|
Guid.Parse(app.Element("ID").Value);
|
|
|
|
|
//Create XElement
|
|
|
|
|
tmpAppsList.Add(new XElement("App",
|
|
|
|
|
new XElement("Name", app.Element("Name").Value),
|
|
|
|
|
new XElement("Description", app.Element("Description").Value),
|
|
|
|
|
new XElement("Version", app.Element("Version").Value),
|
|
|
|
|
new XElement("ID", app.Element("ID").Value),
|
2020-09-10 20:05:44 +02:00
|
|
|
|
new XElement("File", new Uri(app.Element("File").Value).ToString()),
|
2020-03-24 20:53:23 +01:00
|
|
|
|
new XElement("Hash", app.Element("Hash").Value)
|
|
|
|
|
));
|
|
|
|
|
if (app.Element("MainFile") != null)
|
|
|
|
|
tmpAppsList.Last().Add(new XElement("MainFile", app.Element("MainFile").Value));
|
|
|
|
|
if (app.Element("Icon") != null)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
tmpAppsList.Last()
|
2020-03-24 22:43:48 +01:00
|
|
|
|
.Add(new XElement("Icon",
|
2020-04-08 16:05:09 +02:00
|
|
|
|
ExternalFunctionalityManager.Instance.FetchImageB64(
|
2020-09-10 20:05:44 +02:00
|
|
|
|
new Uri(app.Element("Icon").Value))));
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
// ignored
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
2020-04-08 19:16:14 +02:00
|
|
|
|
tmpAppsList.Last().Add(new XElement("Platform",
|
|
|
|
|
app.Element("Platform") == null ||
|
|
|
|
|
!new[] {GlobalVariables.Posix, GlobalVariables.Windows}.Contains(app.Element("Platform")
|
|
|
|
|
.Value)
|
|
|
|
|
? GlobalVariables.CurrentPlatform
|
|
|
|
|
: app.Element("Platform").Value));
|
2020-04-08 16:05:09 +02:00
|
|
|
|
XElement app1 = app;
|
|
|
|
|
if (tmpAppsList.Count(a => a.Element("ID").Value == app1.Element("ID").Value) > 1)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
tmpAppsList.Where(a => a.Element("ID").Value == app.Element("ID").Value).Reverse()
|
|
|
|
|
.Skip(1)
|
|
|
|
|
.ToList().ForEach(a => tmpAppsList.Remove(a));
|
|
|
|
|
}
|
|
|
|
|
#if !DEBUG
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
ExternalFunctionalityManager.Instance.OkDialog(
|
2020-08-31 18:58:15 +02:00
|
|
|
|
$"Failed to load repo: {repArr[i]}{Environment.NewLine}Last status was: {status}{Environment.NewLine}{e}");
|
2020-03-24 20:53:23 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
tmpAppsList.Sort((x, y) =>
|
|
|
|
|
string.Compare(x.Element("Name").Value, y.Element("Name").Value, StringComparison.Ordinal));
|
|
|
|
|
if (meta.Element("LocalRepo") == null)
|
|
|
|
|
meta.Add(new XElement("LocalRepo"));
|
|
|
|
|
XElement repos = meta.Element("LocalRepo");
|
|
|
|
|
repos.RemoveNodes();
|
|
|
|
|
tmpAppsList.ForEach(app => repos.Add(app));
|
|
|
|
|
meta.Save(PathTool.InfoXml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Version GetVer(this XElement el) =>
|
|
|
|
|
int.TryParse(el.Value, out int i) ? new Version(0, 0, 0, i) : Version.Parse(el.Value);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-24 22:43:48 +01:00
|
|
|
|
/// Load the repository cache
|
2020-03-24 20:53:23 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="errorHandler">Function to call on an exception, will ask the user whether he wants to quit</param>
|
|
|
|
|
public static void GetReposFromDisk()
|
|
|
|
|
{
|
|
|
|
|
GlobalVariables.Apps.Clear();
|
|
|
|
|
string xml = PathTool.InfoXml;
|
|
|
|
|
XDocument.Load(xml).Element("meta").Element("LocalRepo").Elements().ToList().ForEach(app =>
|
|
|
|
|
{
|
2020-04-08 16:32:16 +02:00
|
|
|
|
if (app.Element("Platform").Value != GlobalVariables.CurrentPlatform) return;
|
2020-03-24 20:53:23 +01:00
|
|
|
|
Guid id = Guid.Parse(app.Element("ID").Value);
|
|
|
|
|
string locInPath = PathTool.GetInfoPath(id);
|
|
|
|
|
XElement locIn = File.Exists(locInPath) ? XDocument.Load(locInPath).Element("app") : app;
|
|
|
|
|
if (int.TryParse(app.Element("Version").Value, out _))
|
2020-04-08 16:05:09 +02:00
|
|
|
|
app.Element("Version").Value = GlobalVariables.MinimumVer.ToString();
|
2020-03-24 20:53:23 +01:00
|
|
|
|
GlobalVariables.Apps.Add(id, new App(
|
|
|
|
|
locIn.Element("Name").Value,
|
|
|
|
|
locIn.Element("Description").Value,
|
|
|
|
|
Version.Parse(app.Element("Version").Value),
|
|
|
|
|
app.Element("File").Value,
|
|
|
|
|
false,
|
|
|
|
|
app.Element("Hash").Value,
|
|
|
|
|
id,
|
|
|
|
|
Color.White,
|
|
|
|
|
app.Element("Icon") == null
|
2020-04-08 16:05:09 +02:00
|
|
|
|
? ExternalFunctionalityManager.Instance.GetDefaultIcon()
|
|
|
|
|
: ExternalFunctionalityManager.Instance.ImageFromB64(app.Element("Icon").Value),
|
2020-03-24 20:53:23 +01:00
|
|
|
|
locIn.Element("MainFile") != null || app.Element("MainFile") != null,
|
|
|
|
|
locIn.Element("MainFile") == null
|
|
|
|
|
? app.Element("MainFile") == null ? "" : app.Element("MainFile").Value
|
|
|
|
|
: locIn.Element("MainFile").Value
|
|
|
|
|
));
|
|
|
|
|
});
|
2020-04-08 16:05:09 +02:00
|
|
|
|
Directory.GetDirectories(PathTool.AppsPath)
|
2020-03-24 20:53:23 +01:00
|
|
|
|
.Where(s => Guid.TryParse(Path.GetFileName(s), out Guid guid) &&
|
|
|
|
|
!GlobalVariables.Apps.ContainsKey(guid)).ToList().ForEach(s =>
|
|
|
|
|
{
|
|
|
|
|
Guid tmp = Guid.Parse(Path.GetFileName(s));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
XElement data = XDocument.Load(PathTool.GetInfoPath(tmp)).Element("app");
|
|
|
|
|
GlobalVariables.Apps.Add(tmp,
|
2020-04-08 18:41:57 +02:00
|
|
|
|
new App($"(local) {data.Element("Name").Value}", data.Element("Description").Value,
|
2020-04-08 16:05:09 +02:00
|
|
|
|
GlobalVariables.MinimumVer, "", true, "", tmp, Color.Red,
|
|
|
|
|
ExternalFunctionalityManager.Instance.GetDefaultIcon(),
|
2020-03-24 20:53:23 +01:00
|
|
|
|
data.Element("MainFile") != null,
|
|
|
|
|
data.Element("MainFile") == null ? "" : data.Element("MainFile").Value));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-04-08 16:05:09 +02:00
|
|
|
|
if (ExternalFunctionalityManager.Instance.YesNoDialog(
|
2020-03-24 22:43:48 +01:00
|
|
|
|
$@"An error occured while loading this local repo:
|
2020-03-24 20:53:23 +01:00
|
|
|
|
{e.Message}
|
|
|
|
|
Do you want to exit? Otherwise the folder will be deleted, possibly causeing problems later.", false))
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
else
|
|
|
|
|
Directory.Delete(PathTool.GetAppPath(tmp), true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|