-
Notifications
You must be signed in to change notification settings - Fork 809
Expand file tree
/
Copy pathSerializableBundle.cs
More file actions
79 lines (66 loc) · 3.39 KB
/
SerializableBundle.cs
File metadata and controls
79 lines (66 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Text.Json.Nodes;
using UniGetUI.PackageEngine.Serializable;
using UniGetUI.Core.Data;
namespace UniGetUI.PackageEngine.Classes.Serializable
{
public class SerializableBundle: SerializableComponent<SerializableBundle>
{
public const double ExpectedVersion = 3;
public const string IncompatMessage = "Incompatible packages cannot be installed from UniGetUI, " +
"either because they came from a local source (for example Local PC)" +
"or because the package manager was unavailable. " +
"Nevertheless, they have been listed here for logging purposes.";
public double export_version { get; set; } = 3;
public List<SerializablePackage> packages { get; set; } = [];
public string incompatible_packages_info { get; set; } = IncompatMessage;
public List<SerializableIncompatiblePackage> incompatible_packages { get; set; } = [];
public override SerializableBundle Copy()
{
var _packages = new List<SerializablePackage>();
var _incompatPackages = new List<SerializableIncompatiblePackage>();
foreach(var package in this.packages)
_packages.Add(package.Copy());
foreach(var incompatPackage in this.incompatible_packages)
_incompatPackages.Add(incompatPackage.Copy());
return new()
{
export_version = this.export_version,
packages = _packages,
incompatible_packages_info = this.incompatible_packages_info,
incompatible_packages = _incompatPackages
};
}
public override void LoadFromJson(JsonNode data)
{
this.export_version = data[nameof(export_version)]?.GetVal<double>() ?? 0;
this.incompatible_packages_info = data[nameof(incompatible_packages_info)]?.GetVal<string>() ?? IncompatMessage;
this.packages = new List<SerializablePackage>();
this.incompatible_packages = new List<SerializableIncompatiblePackage>();
foreach (JsonNode? pkg in data[nameof(packages)]?.AsArray2() ?? new())
{
if (pkg is null) throw new InvalidDataException("JsonNode? pkg was null, when it shouldn't");
packages.Add(new SerializablePackage(pkg));
}
foreach (JsonNode? inc_pkg in data[nameof(incompatible_packages)]?.AsArray2() ?? new())
{
if (inc_pkg is null) throw new InvalidDataException("JsonNode? inc_pkg was null, when it shouldn't");
incompatible_packages.Add(new SerializableIncompatiblePackage(inc_pkg));
}
}
public override JsonNode AsJsonNode()
{
JsonObject obj = new();
obj.Add(nameof(export_version), export_version);
obj.Add(nameof(packages), new JsonArray(packages.Select(p => p.AsJsonNode()).ToArray()));
obj.Add(nameof(incompatible_packages_info), incompatible_packages_info);
obj.Add(nameof(incompatible_packages), new JsonArray(incompatible_packages.Select(p => p.AsJsonNode()).ToArray()));
return obj;
}
public SerializableBundle() : base()
{
}
public SerializableBundle(JsonNode data) : base(data)
{
}
}
}