using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Nodes; using Artemis.Core; using Artemis.Storage.Entities.Workshop; namespace Artemis.WebClient.Workshop.Models; public class InstalledEntry { private Dictionary _metadata = new(); internal InstalledEntry(EntryEntity entity) { Entity = entity; Load(); } public InstalledEntry(IEntrySummary entry, IRelease release) { Entity = new EntryEntity(); EntryId = entry.Id; EntryType = entry.EntryType; Author = entry.Author; Name = entry.Name; InstalledAt = DateTimeOffset.Now; ReleaseId = release.Id; ReleaseVersion = release.Version; } public long EntryId { get; set; } public EntryType EntryType { get; set; } public string Author { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; public long ReleaseId { get; set; } public string ReleaseVersion { get; set; } = string.Empty; public DateTimeOffset InstalledAt { get; set; } internal EntryEntity Entity { get; } internal void Load() { EntryId = Entity.EntryId; EntryType = (EntryType) Entity.EntryType; Author = Entity.Author; Name = Entity.Name; ReleaseId = Entity.ReleaseId; ReleaseVersion = Entity.ReleaseVersion; InstalledAt = Entity.InstalledAt; _metadata = Entity.Metadata != null ? new Dictionary(Entity.Metadata) : new Dictionary(); } internal void Save() { Entity.EntryId = EntryId; Entity.EntryType = (int) EntryType; Entity.Author = Author; Entity.Name = Name; Entity.ReleaseId = ReleaseId; Entity.ReleaseVersion = ReleaseVersion; Entity.InstalledAt = InstalledAt; Entity.Metadata = new Dictionary(_metadata); } /// /// Gets the metadata value associated with the specified key. /// /// The key of the value to get. /// When this method returns, contains the value associated with the specified key, if the key is found and of type ; /// otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. /// The type of the value. /// if the metadata contains an element with the specified key; otherwise, . public bool TryGetMetadata(string key, [NotNullWhen(true)] out T? value) { if (!_metadata.TryGetValue(key, out JsonNode? element)) { value = default; return false; } value = element.GetValue(); return value != null; } /// /// Sets metadata with the provided key to the provided value. /// /// The key of the value to set /// The value to set. public void SetMetadata(string key, object value) { _metadata[key] = JsonSerializer.SerializeToNode(value) ?? throw new InvalidOperationException(); } /// /// Removes metadata with the provided key. /// /// The key of the metadata to remove /// if the element is successfully found and removed; otherwise, . public bool RemoveMetadata(string key) { return _metadata.Remove(key); } /// /// Returns the directory info of the entry, where any files would be stored if applicable. /// /// The directory info of the directory. public DirectoryInfo GetDirectory() { return new DirectoryInfo(Path.Combine(Constants.WorkshopFolder, $"{EntryId}-{StringUtilities.UrlFriendly(Name)}")); } /// /// Returns the directory info of a release of this entry, where any files would be stored if applicable. /// /// The release to use, if none provided the current release is used. /// The directory info of the directory. public DirectoryInfo GetReleaseDirectory(IRelease? release = null) { return new DirectoryInfo(Path.Combine(GetDirectory().FullName, StringUtilities.UrlFriendly(release?.Version ?? ReleaseVersion))); } /// /// Applies the provided release to the installed entry. /// /// The release to apply. public void ApplyRelease(IRelease release) { ReleaseId = release.Id; ReleaseVersion = release.Version; InstalledAt = DateTimeOffset.UtcNow; } }