Skip to content

Commit

Permalink
ConfigApp: Several workshop-related changes (#3706)
Browse files Browse the repository at this point in the history
- Moved settings file handler to a separate class
- Made an option to open text and sound files for preview
- Improved search: now it supports individual file names, script names
and script IDs. Found files are highlighted in the settings window.

---------

Co-authored-by: Reguas <[email protected]>
  • Loading branch information
Regynate and Regynate authored Jan 31, 2025
1 parent 53b281c commit 1b48a8e
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 151 deletions.
2 changes: 1 addition & 1 deletion ConfigApp/EffectsTreeMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ICommand OnConfigureCommand
{
get => new TreeMenuItemAction(OnConfigureClick);
}

public TreeMenuItem(string text, TreeMenuItem? parent = null)
{
Text = text;
Expand Down
41 changes: 28 additions & 13 deletions ConfigApp/Tabs/WorkshopTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using ConfigApp.Workshop;
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -66,24 +67,33 @@ private void HandleWorkshopSubmissionsSearchFilter()
var view = CollectionViewSource.GetDefaultView(m_WorkshopSubmissionItems);
view.Filter = (submissionItem) =>
{
if (submissionItem is not WorkshopSubmissionItem item || transformedText is null)
if (submissionItem is not WorkshopSubmissionItem item || transformedText is null || transformedText == "")
return true;

var texts = new string?[]
foreach (var term in item.SearchTerms)
{
item.Name,
item.Author,
item.Description
};

foreach (var text in texts)
{
if (text is not null && text.ToLower().Contains(transformedText))
if (term.Term.ToLower().Contains(transformedText))
return true;
};
}

return false;
};

foreach (var item in m_WorkshopSubmissionItems)
{
item.HighlightedFiles.Clear();
if (transformedText is not null && transformedText != "")
{
foreach (var term in item.SearchTerms)
{
if (term.Term.ToLower().Contains(transformedText))
{
if (term.IsInFile)
item.HighlightedFiles.Add(term.FileName);
}
}
}
}
}

private void ParseWorkshopSubmissionsFile(byte[] compressedFileContent)
Expand Down Expand Up @@ -124,9 +134,8 @@ T getDataItem<T>(dynamic item, T defaultValue)
return;
}

var submissionItem = new WorkshopSubmissionItem()
var submissionItem = new WorkshopSubmissionItem(id)
{
Id = id,
Name = getDataItem<string>(submissionData.name, "No Name"),
Author = getDataItem<string>(submissionData.author, "No Author"),
Description = getDataItem<string>(submissionData.description, "No Description"),
Expand All @@ -135,6 +144,8 @@ T getDataItem<T>(dynamic item, T defaultValue)
Sha256 = sha256,
};

submissionItem.UpdateSearchTerms();

// Remote submissions are fetched before local ones so this submission only exists locally
if (isLocal)
{
Expand Down Expand Up @@ -268,6 +279,10 @@ private async void OnRefreshClick(object sender, RoutedEventArgs eventArgs)
var button = (Button)sender;

button.IsEnabled = false;
foreach (var item in m_WorkshopSubmissionItems)
{
item.Refresh();
}
await ForceRefreshWorkshopContentFromRemote();
button.IsEnabled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
</Grid.RowDefinitions>
<TextBlock x:Name="files_info_text" Text="Disable any files from loading by unchecking the corresponding checkbox" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="0" Margin="6,0" />
<TreeView x:Name="files_tree_view" Background="{x:Null}" BorderBrush="White" VerticalAlignment="Stretch" Grid.Row="1">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsColored}" Value="True">
<Setter Property="Background" Value="#FFF6F653" />
</DataTrigger>
<DataTrigger Binding="{Binding IsColored}" Value="False">
<Setter Property="Background" Value="Transparent" />
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:TreeMenuItem}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Height="20">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Windows;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;

namespace ConfigApp
{
Expand All @@ -8,15 +10,39 @@ public enum WorkshopEditDialogMode
Install
}

public enum WorkshopSubmissionFileType
{
Script,
Sound,
Text,
Undefined
}

public class WorkshopSubmissionFile : IComparable<WorkshopSubmissionFile>
{
public string Name { get; private set; }
public bool IsEnabled { get; private set; }
public WorkshopSubmissionFileType Type { get; private set; }
public EffectData? EffectData { get; private set; }

public WorkshopSubmissionFile(string name, bool enabled, EffectData? effectData = null)
{
Name = name;
switch (name[^4..])
{
case ".lua":
Type = WorkshopSubmissionFileType.Script;
break;
case ".mp3":
Type = WorkshopSubmissionFileType.Sound;
break;
case ".txt":
Type = WorkshopSubmissionFileType.Text;
break;
default:
Type = WorkshopSubmissionFileType.Undefined;
break;
}
IsEnabled = enabled;
EffectData = effectData;
}
Expand Down Expand Up @@ -50,7 +76,7 @@ public partial class WorkshopEditDialog : Window

private readonly WorkshopEditDialogMode m_DialogMode;

public WorkshopEditDialog(List<WorkshopSubmissionFile> files, WorkshopEditDialogMode dialogMode)
public WorkshopEditDialog(List<WorkshopSubmissionFile> files, WorkshopEditDialogMode dialogMode, string? path = null, List<string>? highlightedFiles = null)
{
InitializeComponent();

Expand All @@ -66,17 +92,17 @@ public WorkshopEditDialog(List<WorkshopSubmissionFile> files, WorkshopEditDialog
button_save_or_no.Content = "No";
}

TreeMenuItem generateItem(string text, TreeMenuItem? parent = null)
TreeMenuItem generateItem(string text, TreeMenuItem? parent = null, bool showCheckbox = true)
{
var item = new TreeMenuItem(text, parent);
if (m_DialogMode == WorkshopEditDialogMode.Install)
if (m_DialogMode == WorkshopEditDialogMode.Install || !showCheckbox)
item.CheckBoxVisiblity = Visibility.Collapsed;
return item;
}

var luaParentItem = generateItem("Scripts");
var mp3ParentItem = generateItem("Sounds");
var txtParentItem = generateItem("Text Files");
var txtParentItem = generateItem("Text Files", showCheckbox: false);

var parentFolderItems = new Dictionary<string, TreeMenuItem>();

Expand All @@ -91,19 +117,15 @@ TreeMenuItem generateItem(string text, TreeMenuItem? parent = null)
var pathFragments = (pathName.StartsWith("sounds\\") ? pathName[7..] : pathName).Split('\\');

TreeMenuItem targetItem;
bool isConfigurable = false;
switch (pathName[^4..])
switch (file.Type)
{
case ".lua":
case WorkshopSubmissionFileType.Script:
targetItem = luaParentItem;
isConfigurable = true;
break;
case ".mp3":
case WorkshopSubmissionFileType.Sound:
targetItem = mp3ParentItem;
break;
case ".txt":
if (m_DialogMode != WorkshopEditDialogMode.Install)
continue;
case WorkshopSubmissionFileType.Text:
targetItem = txtParentItem;
break;
default:
Expand Down Expand Up @@ -137,23 +159,43 @@ TreeMenuItem generateItem(string text, TreeMenuItem? parent = null)
}
}

var menuItem = generateItem(pathFragments.Last(), targetItem);
var menuItem = generateItem(pathFragments.Last(), targetItem, file.Type != WorkshopSubmissionFileType.Text);
var fileState = new WorkshopSubmissionFileState(menuItem, pathName, file.EffectData);
menuItem.ForceConfigHidden = m_DialogMode != WorkshopEditDialogMode.Edit || !isConfigurable;
menuItem.ForceConfigHidden = m_DialogMode != WorkshopEditDialogMode.Edit;
menuItem.OnConfigureClick = () =>
{
var effectConfig = new EffectConfig(null, fileState.EffectData, new Effects.EffectInfo()
if (file.Type == WorkshopSubmissionFileType.Script)
{
Name = pathName,
IsTimed = true
});
effectConfig.ShowDialog();
var effectConfig = new EffectConfig(null, fileState.EffectData, new Effects.EffectInfo()
{
Name = pathName,
IsTimed = true
});
effectConfig.ShowDialog();

if (!effectConfig.IsSaved)
return;
if (!effectConfig.IsSaved)
return;

fileState.EffectData = effectConfig.GetNewData();
fileState.EffectData = effectConfig.GetNewData();
}
else
{
try
{
System.Diagnostics.Process.Start(new ProcessStartInfo(path is not null ? path.Replace('/', '\\') + pathName : pathName) { UseShellExecute = true });
}
catch (Win32Exception)
{
MessageBox.Show("Error: File not found", "ChaosModV", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
};

if (highlightedFiles?.Contains(pathName) ?? false)
{
menuItem.IsColored = true;
}

targetItem.AddChild(menuItem);
FileStates.Add(fileState);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Windows;
using System.Windows.Input;

namespace ConfigApp
namespace ConfigApp.Workshop
{
public class WorkshopInfoHandler : ICommand
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Newtonsoft.Json.Linq;
using ZstdSharp;

namespace ConfigApp
namespace ConfigApp.Workshop
{
public class WorkshopInstallHandler : ICommand
{
Expand Down Expand Up @@ -173,7 +173,6 @@ string getFileSha256(byte[] buffer)

var fileStream = new MemoryStream(fileContent);
if (isFileCompressed)
{
try
{
var decompressor = new Decompressor();
Expand All @@ -185,7 +184,6 @@ string getFileSha256(byte[] buffer)
// File content is not (zstd) compressed even though compressed = yes?
// Skip decompression
}
}

try
{
Expand Down
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions ConfigApp/Workshop/WorkshopSettingsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;
using System.Windows;
using System.Windows.Input;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ConfigApp.Workshop
{
public class WorkshopSettingsHandler : ICommand
{
public event EventHandler? CanExecuteChanged = null;

private readonly WorkshopSubmissionItem m_SubmissionItem;
private readonly WorkshopSubmissionFileHandler m_FileHandler;

public WorkshopSettingsHandler(WorkshopSubmissionItem submissionItem, WorkshopSubmissionFileHandler fileHandler)
{
m_SubmissionItem = submissionItem;
m_FileHandler = fileHandler;
}

public bool CanExecute(object? parameter)
{
return true;
}

public void Execute(object? parameter)
{
List<WorkshopSubmissionFile> files;

try
{
m_FileHandler.ReloadFiles();
files = m_FileHandler.GetSubmissionFiles();
m_SubmissionItem.UpdateSearchTerms();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ChaosModV", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

var editWindow = new WorkshopEditDialog(files, WorkshopEditDialogMode.Edit, m_FileHandler.SubmissionDirectory, m_SubmissionItem.HighlightedFiles);
editWindow.ShowDialog();

try
{
m_FileHandler.SetSettings(editWindow.FileStates);
m_SubmissionItem.UpdateSearchTerms();
}
catch (Exception)
{
MessageBox.Show("Error while saving settings! Check that workshop folder has write permissions", "ChaosModV", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
Loading

0 comments on commit 1b48a8e

Please sign in to comment.