Skip to content

Commit

Permalink
ConfigApp: Preserve local-only submissions in submission items list
Browse files Browse the repository at this point in the history
Allows viewing and removing installed submissions without reliance on remote server
  • Loading branch information
pongo1231 committed Oct 30, 2023
1 parent 8605e64 commit d2734f7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 52 deletions.
125 changes: 78 additions & 47 deletions ConfigApp/Tabs/WorkshopTab.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -55,6 +56,74 @@ private void HandleWorkshopSubmissionsSearchFilter()

private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent)
{
void submitWorkshopSubmissionData(dynamic submissionData, bool isLocal)
{
T getDataItem<T>(dynamic item, T defaultValue)
{
try
{
return item;
}
catch (RuntimeBinderException)
{
return defaultValue;
}
}

var id = getDataItem<string>(submissionData.id, string.Empty);
if (string.IsNullOrEmpty(id))
{
return;
}

var version = getDataItem<string>(submissionData.version, string.Empty);
if (string.IsNullOrEmpty(version))
{
return;
}

var lastUpdated = getDataItem<int>(submissionData.lastupdated, 0);
var sha256 = getDataItem<string>(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<string>(submissionData.name, "No Name"),
Author = getDataItem<string>(submissionData.author, "No Author"),
Description = getDataItem<string>(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);
Expand All @@ -68,28 +137,11 @@ private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent)
foreach (var submissionObject in json["submissions"].ToObject<Dictionary<string, dynamic>>())
{
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);
}
}

Expand All @@ -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<dynamic>();
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)
{
Expand Down
3 changes: 1 addition & 2 deletions ConfigApp/WorkshopSettingsDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Shared;
using System.Windows;
using System.Windows;

namespace ConfigApp
{
Expand Down
1 change: 0 additions & 1 deletion ConfigApp/WorkshopSettingsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Input;

Expand Down
5 changes: 3 additions & 2 deletions ConfigApp/WorkshopSubmissionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +49,7 @@ public SubmissionInstallState InstallState
{
case SubmissionInstallState.NotInstalled:
InstallButtonText = "Install";
InstallButtonEnabled = true;
InstallButtonEnabled = !IsAlien;
SettingsButtonVisibility = Visibility.Hidden;
break;
case SubmissionInstallState.Installed:
Expand All @@ -62,7 +63,7 @@ public SubmissionInstallState InstallState
break;
case SubmissionInstallState.UpdateAvailable:
InstallButtonText = "Update";
InstallButtonEnabled = true;
InstallButtonEnabled = !IsAlien;
SettingsButtonVisibility = Visibility.Visible;
break;
case SubmissionInstallState.Removing:
Expand Down

0 comments on commit d2734f7

Please sign in to comment.