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/UpToolLib/DataStructures/App.cs

82 lines
2.9 KiB
C#
Raw Normal View History

2019-11-11 15:48:49 +01:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
2020-03-24 20:53:23 +01:00
using UpToolLib.Tool;
2020-02-28 12:59:59 +01:00
using static System.Environment;
2019-11-11 15:48:49 +01:00
2020-03-24 20:53:23 +01:00
namespace UpToolLib.DataStructures
2019-11-11 15:48:49 +01:00
{
public struct App : IEquatable<App>
{
2020-02-28 14:26:16 +01:00
public readonly string Name;
public readonly string Description;
public readonly Version Version;
public readonly string File;
public readonly bool Local;
public readonly string Hash;
2020-03-24 20:53:23 +01:00
public readonly Guid Id;
2020-02-28 14:26:16 +01:00
public Color Color;
2020-03-24 20:53:23 +01:00
public readonly object Icon;
2020-02-28 14:26:16 +01:00
public readonly bool Runnable;
public readonly string MainFile;
2019-11-11 15:48:49 +01:00
2020-02-28 14:26:16 +01:00
public App(string name, string description, Version version, string file, bool local, string hash, Guid iD,
2020-03-24 20:53:23 +01:00
Color color, object icon, bool runnable, string mainFile)
2019-11-11 15:48:49 +01:00
{
2020-02-28 14:26:16 +01:00
Name = name ?? throw new ArgumentNullException(nameof(name));
Description = description ?? throw new ArgumentNullException(nameof(description));
Version = version;
File = file ?? throw new ArgumentNullException(nameof(file));
Local = local;
Hash = hash ?? throw new ArgumentNullException(nameof(hash));
2020-03-24 20:53:23 +01:00
Id = iD;
2020-02-28 14:26:16 +01:00
Color = color;
Icon = icon ?? throw new ArgumentNullException(nameof(icon));
Runnable = runnable;
MainFile = mainFile ?? throw new ArgumentNullException(nameof(mainFile));
2019-11-11 15:48:49 +01:00
}
public Status Status
2019-11-11 15:48:49 +01:00
{
2020-02-28 14:26:16 +01:00
get
{
if (!System.IO.File.Exists(InfoPath))
return Status.NotInstalled;
if (Version.TryParse(XDocument.Load(InfoPath).Element("app").Element("Version").Value,
2020-02-28 14:26:16 +01:00
out Version ver) && ver >= Version)
return Local ? Status.Installed | Status.Local : Status.Installed;
return Status.Installed | Status.Updatable;
2019-11-11 15:48:49 +01:00
}
}
public override bool Equals(object obj) => obj is App app && Equals(app);
2020-02-28 12:59:59 +01:00
2020-03-24 20:53:23 +01:00
public bool Equals(App other) => Id.Equals(other.Id);
2020-02-28 12:59:59 +01:00
2020-03-24 20:53:23 +01:00
public override int GetHashCode() => 1213502048 + EqualityComparer<Guid>.Default.GetHashCode(Id);
2020-02-28 12:59:59 +01:00
2020-02-28 14:26:16 +01:00
public override string ToString() => $@"Name: {Name}
2020-02-28 12:59:59 +01:00
Description:
{string.Join(NewLine, Description.Split('\n').Select(s => { if (s.EndsWith("\r")) s.Remove(s.Length - 1, 1); return $"> {s}"; }))}
2020-02-28 14:26:16 +01:00
Version: {Version}
File: {File}
2020-03-24 20:53:23 +01:00
Local: {Local}
2020-02-28 14:26:16 +01:00
Hash: {Hash}
2020-03-24 20:53:23 +01:00
ID: {Id}
Color: {Color.ToKnownColor()}
2020-02-28 14:26:16 +01:00
Runnable: {Runnable}
MainFile: {MainFile}
Status: {Status}
2020-02-28 12:59:59 +01:00
Object Hash Code: {GetHashCode()}";
2019-11-11 15:48:49 +01:00
public static bool operator ==(App left, App right) => left.Equals(right);
2020-02-28 12:59:59 +01:00
2019-11-11 15:48:49 +01:00
public static bool operator !=(App left, App right) => !(left == right);
2020-02-28 12:59:59 +01:00
public string AppPath => PathTool.GetAppPath(Id);
public string DataPath => PathTool.GetDataPath(Id);
public string InfoPath => PathTool.GetInfoPath(Id);
2019-11-11 15:48:49 +01:00
}
2020-02-28 12:59:59 +01:00
}