From d2734f79a0fe6937da42b540ab4cc81faa4acde0 Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Mon, 30 Oct 2023 18:35:07 +0000 Subject: [PATCH] ConfigApp: Preserve local-only submissions in submission items list Allows viewing and removing installed submissions without reliance on remote server --- ConfigApp/Tabs/WorkshopTab.cs | 125 ++++++++++++++--------- ConfigApp/WorkshopSettingsDialog.xaml.cs | 3 +- ConfigApp/WorkshopSettingsHandler.cs | 1 - ConfigApp/WorkshopSubmissionItem.cs | 5 +- 4 files changed, 82 insertions(+), 52 deletions(-) diff --git a/ConfigApp/Tabs/WorkshopTab.cs b/ConfigApp/Tabs/WorkshopTab.cs index f87cef823..ac4a78874 100644 --- a/ConfigApp/Tabs/WorkshopTab.cs +++ b/ConfigApp/Tabs/WorkshopTab.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Microsoft.CSharp.RuntimeBinder; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -55,6 +56,74 @@ private void HandleWorkshopSubmissionsSearchFilter() private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent) { + void submitWorkshopSubmissionData(dynamic submissionData, bool isLocal) + { + T getDataItem(dynamic item, T defaultValue) + { + try + { + return item; + } + catch (RuntimeBinderException) + { + return defaultValue; + } + } + + var id = getDataItem(submissionData.id, string.Empty); + if (string.IsNullOrEmpty(id)) + { + return; + } + + var version = getDataItem(submissionData.version, string.Empty); + if (string.IsNullOrEmpty(version)) + { + return; + } + + var lastUpdated = getDataItem(submissionData.lastupdated, 0); + var sha256 = getDataItem(submissionData.sha256, string.Empty); + + var duplicateSubmissionItem = m_WorkshopSubmissionItems.FirstOrDefault((submissionItem) => { return submissionItem.Id == id; }); + if (duplicateSubmissionItem != null) + { + if (!isLocal) + { + return; + } + + if (duplicateSubmissionItem.Version != version || duplicateSubmissionItem.LastUpdated != lastUpdated || duplicateSubmissionItem.Sha256 != sha256) + { + duplicateSubmissionItem.InstallState = WorkshopSubmissionItem.SubmissionInstallState.UpdateAvailable; + } + else + { + duplicateSubmissionItem.InstallState = WorkshopSubmissionItem.SubmissionInstallState.Installed; + } + } + + var submissionItem = new WorkshopSubmissionItem() + { + Id = id, + Name = getDataItem(submissionData.name, "No Name"), + Author = getDataItem(submissionData.author, "No Author"), + Description = getDataItem(submissionData.description, "No Description"), + Version = $"v{version}", + LastUpdated = lastUpdated, + Sha256 = sha256, + }; + + // Remote submissions are fetched before local ones so this submission only exists locally + if (isLocal) + { + submissionItem.InstallState = WorkshopSubmissionItem.SubmissionInstallState.Installed; + submissionItem.IsAlien = true; + } + + m_WorkshopSubmissionItems.Add(submissionItem); + } + { var decompressor = new Decompressor(); var decompressed = decompressor.Unwrap(compressedFileContent); @@ -68,28 +137,11 @@ private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent) foreach (var submissionObject in json["submissions"].ToObject>()) { var submissionId = submissionObject.Key; - var submissionData = submissionObject.Value; - - // Submission has no data - if (submissionData.lastupdated == 0) - { - continue; - } - WorkshopSubmissionItem submission = new WorkshopSubmissionItem(); - submission.Id = submissionId; - submission.Name = submissionData.name; - submission.Author = submissionData.author; - submission.Description = submissionData.description; - if (submission.Description.Length == 0) - { - submission.Description = "No Description"; - } - submission.Version = $"v{submissionData.version}"; - submission.LastUpdated = submissionData.lastupdated; - submission.Sha256 = submissionData.sha256; + var submissionData = submissionObject.Value; + submissionData.id = submissionId; - m_WorkshopSubmissionItems.Add(submission); + submitWorkshopSubmissionData(submissionData, false); } } @@ -109,35 +161,14 @@ private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent) { var json = JObject.Parse(File.ReadAllText($"{directory}/metadata.json")); - var version = (string)json["version"]; - var lastUpdated = (int)json["lastupdated"]; - var sha256 = (string)json["sha256"]; - - var foundSubmissionItem = m_WorkshopSubmissionItems.FirstOrDefault((submissionItem) => { return submissionItem.Id == id; }); - if (foundSubmissionItem == null) + var submissionData = json.ToObject(); + if (submissionData == null) { - var result = MessageBox.Show($"Local submission \"{id}\" does not exist remotely. Remove submission?", "ChaosModV", MessageBoxButton.YesNo, MessageBoxImage.Warning); - if (result == MessageBoxResult.Yes) - { - try - { - Directory.Delete(directory, true); - } - catch (Exception) - { - MessageBox.Show($"Couldn't access \"{directory}\". Try deleting it manually!", "ChaosModV", MessageBoxButton.OK, MessageBoxImage.Error); - } - } continue; } - else if (foundSubmissionItem.Version != version || foundSubmissionItem.LastUpdated != lastUpdated || foundSubmissionItem.Sha256 != sha256) - { - foundSubmissionItem.InstallState = WorkshopSubmissionItem.SubmissionInstallState.UpdateAvailable; - } - else - { - foundSubmissionItem.InstallState = WorkshopSubmissionItem.SubmissionInstallState.Installed; - } + submissionData.id = id; + + submitWorkshopSubmissionData(submissionData, true); } catch (Exception exception) when (exception is JsonException || exception is ZstdException) { diff --git a/ConfigApp/WorkshopSettingsDialog.xaml.cs b/ConfigApp/WorkshopSettingsDialog.xaml.cs index 994f578a3..75c80243c 100644 --- a/ConfigApp/WorkshopSettingsDialog.xaml.cs +++ b/ConfigApp/WorkshopSettingsDialog.xaml.cs @@ -1,5 +1,4 @@ -using Shared; -using System.Windows; +using System.Windows; namespace ConfigApp { diff --git a/ConfigApp/WorkshopSettingsHandler.cs b/ConfigApp/WorkshopSettingsHandler.cs index 8632dba03..29737f947 100644 --- a/ConfigApp/WorkshopSettingsHandler.cs +++ b/ConfigApp/WorkshopSettingsHandler.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; using System.Windows; using System.Windows.Input; diff --git a/ConfigApp/WorkshopSubmissionItem.cs b/ConfigApp/WorkshopSubmissionItem.cs index 3022d5a2a..e4b6b0114 100644 --- a/ConfigApp/WorkshopSubmissionItem.cs +++ b/ConfigApp/WorkshopSubmissionItem.cs @@ -22,6 +22,7 @@ public class WorkshopSubmissionItem : INotifyPropertyChanged public int LastUpdated { get; set; } public string Sha256 { get; set; } public BitmapSource SubmissionIcon { get; set; } + public bool IsAlien { get; set; } = false; // In order for sorting public enum SubmissionInstallState @@ -48,7 +49,7 @@ public SubmissionInstallState InstallState { case SubmissionInstallState.NotInstalled: InstallButtonText = "Install"; - InstallButtonEnabled = true; + InstallButtonEnabled = !IsAlien; SettingsButtonVisibility = Visibility.Hidden; break; case SubmissionInstallState.Installed: @@ -62,7 +63,7 @@ public SubmissionInstallState InstallState break; case SubmissionInstallState.UpdateAvailable: InstallButtonText = "Update"; - InstallButtonEnabled = true; + InstallButtonEnabled = !IsAlien; SettingsButtonVisibility = Visibility.Visible; break; case SubmissionInstallState.Removing: