Browse Source

Render .resources files as inline string + object DataGrids

Replace the comment-line dump in ResourcesFileTreeNode.Decompile with two
AvaloniaUI DataGrid views — ResourceStringTable for string entries (Name/Value)
and ResourceObjectTable for everything else (Name/Value/Type, including
ResourceSerializedObject and primitive-typed values). otherEntries collection
plus SerializedObjectRepresentation row model are added on the node so
ResX export (next commit) can iterate the same shape. Test pins the behaviour:
a synthetic .resources stream produces three UIElements (Save button + two
grids) where the prior implementation produced one.

Assisted-by: Claude:claude-opus-4-7:Claude Code
pull/3755/head
Siegfried Pammer 2 months ago
parent
commit
a616c0613b
  1. 47
      ILSpy.Tests/Resources/ResourceFactoryTests.cs
  2. 39
      ILSpy/Controls/ResourceObjectTable.axaml
  3. 57
      ILSpy/Controls/ResourceObjectTable.axaml.cs
  4. 35
      ILSpy/Controls/ResourceStringTable.axaml
  5. 41
      ILSpy/Controls/ResourceStringTable.axaml.cs
  6. 34
      ILSpy/TreeNodes/ResourcesFileTreeNode.cs

47
ILSpy.Tests/Resources/ResourceFactoryTests.cs

@ -16,15 +16,24 @@ @@ -16,15 +16,24 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using AwesomeAssertions;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
using ILSpy;
using ILSpy.AppEnv;
using ILSpy.Languages;
using ILSpy.TextView;
using ILSpy.TreeNodes;
using ILSpy.Views;
@ -73,4 +82,42 @@ public class ResourceFactoryTests @@ -73,4 +82,42 @@ public class ResourceFactoryTests
var node = ResourceEntryNode.Create(new ByteArrayResource("data.bin", new byte[] { 1, 2, 3 }));
node.GetType().Should().Be(typeof(ResourceTreeNode));
}
[AvaloniaTest]
public void Resources_File_Decompile_Emits_UIElements_For_String_And_Object_Tables()
{
EnsureComposition();
var node = (ResourcesFileTreeNode)ResourceEntryNode.Create(
new ByteArrayResource("strings.resources", BuildResources(
new (string, object)[] {
("stringKey", "hello"),
("anotherKey", "world"),
("intKey", 42),
("doubleKey", 3.14),
})));
node.EnsureLazyChildren();
var output = new AvaloniaEditTextOutput();
// Use the active language so WriteCommentLine works without reaching into MEF state.
var language = AppComposition.Current.GetExport<LanguageService>().CurrentLanguage;
node.Decompile(language, output, new DecompilationOptions());
// Two grids (string + object) on top of the inherited Save button = 3 UI elements.
// The previous implementation emitted comment lines and only the inherited Save button,
// so this drives adding two AddUIElement(...) calls in ResourcesFileTreeNode.Decompile.
output.UIElements.Should().HaveCount(3,
".resources should render a string table + object table inline alongside the Save button");
var realised = output.UIElements.Select(kv => kv.Value.Value).ToList();
realised.Should().ContainItemsAssignableTo<Control>();
}
static byte[] BuildResources((string Key, object Value)[] entries)
{
var ms = new MemoryStream();
using (var writer = new ResourceWriter(ms))
{
foreach (var (k, v) in entries)
writer.AddResource(k, v);
}
return ms.ToArray();
}
}

39
ILSpy/Controls/ResourceObjectTable.axaml

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="240"
x:Class="ILSpy.Controls.ResourceObjectTable">
<Grid Margin="5,0,0,0" RowDefinitions="Auto,*">
<TextBlock Grid.Row="0"
Text="Other entries:"
FontFamily="Segoe UI"
FontWeight="Bold"
FontSize="14"
Margin="0,4,0,4" />
<DataGrid Grid.Row="1"
Name="EntriesGrid"
AutoGenerateColumns="False"
IsReadOnly="True"
CanUserResizeColumns="True"
GridLinesVisibility="Horizontal"
HeadersVisibility="Column"
RowHeight="22"
x:CompileBindings="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Width="220"
Binding="{Binding Key}"
CanUserSort="True" />
<DataGridTextColumn Header="Value"
Width="*"
Binding="{Binding Value}"
CanUserSort="True" />
<DataGridTextColumn Header="Type"
Width="240"
Binding="{Binding Type}"
CanUserSort="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>

57
ILSpy/Controls/ResourceObjectTable.axaml.cs

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Collections.Generic;
using global::Avalonia.Controls;
namespace ILSpy.Controls
{
/// <summary>
/// Tabular view of non-string entries inside a <c>.resources</c> file (ints, doubles,
/// serialized objects, …). Used as an inline UI element from
/// <see cref="TreeNodes.ResourcesFileTreeNode"/>'s decompilation output.
/// </summary>
public partial class ResourceObjectTable : UserControl
{
public ResourceObjectTable()
{
InitializeComponent();
}
public ResourceObjectTable(IEnumerable<SerializedObjectRepresentation> entries) : this()
{
EntriesGrid.ItemsSource = entries;
}
}
/// <summary>Row model for <see cref="ResourceObjectTable"/>.</summary>
public sealed class SerializedObjectRepresentation
{
public SerializedObjectRepresentation(string key, string type, string value)
{
Key = key;
Type = type;
Value = value;
}
public string Key { get; }
public string Type { get; }
public string Value { get; }
}
}

35
ILSpy/Controls/ResourceStringTable.axaml

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="240"
x:Class="ILSpy.Controls.ResourceStringTable">
<Grid Margin="5,0,0,0" RowDefinitions="Auto,*">
<TextBlock Grid.Row="0"
Text="String table:"
FontFamily="Segoe UI"
FontWeight="Bold"
FontSize="14"
Margin="0,4,0,4" />
<DataGrid Grid.Row="1"
Name="EntriesGrid"
AutoGenerateColumns="False"
IsReadOnly="True"
CanUserResizeColumns="True"
GridLinesVisibility="Horizontal"
HeadersVisibility="Column"
RowHeight="22"
x:CompileBindings="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Width="220"
Binding="{Binding Key}"
CanUserSort="True" />
<DataGridTextColumn Header="Value"
Width="*"
Binding="{Binding Value}"
CanUserSort="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>

41
ILSpy/Controls/ResourceStringTable.axaml.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Collections.Generic;
using global::Avalonia.Controls;
namespace ILSpy.Controls
{
/// <summary>
/// Tabular view of the string entries inside a <c>.resources</c> file. Used as an inline
/// UI element from <see cref="TreeNodes.ResourcesFileTreeNode"/>'s decompilation output.
/// </summary>
public partial class ResourceStringTable : UserControl
{
public ResourceStringTable()
{
InitializeComponent();
}
public ResourceStringTable(IEnumerable<KeyValuePair<string, string>> entries) : this()
{
EntriesGrid.ItemsSource = entries;
}
}
}

34
ILSpy/TreeNodes/ResourcesFileTreeNode.cs

@ -27,7 +27,9 @@ using ICSharpCode.Decompiler.Metadata; @@ -27,7 +27,9 @@ using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpyX.Abstractions;
using ILSpy.Controls;
using ILSpy.Languages;
using ILSpy.TextView;
namespace ILSpy.TreeNodes
{
@ -52,8 +54,10 @@ namespace ILSpy.TreeNodes @@ -52,8 +54,10 @@ namespace ILSpy.TreeNodes
public sealed class ResourcesFileTreeNode : ResourceTreeNode
{
readonly List<KeyValuePair<string, string>> stringTableEntries = new();
readonly List<SerializedObjectRepresentation> otherEntries = new();
public IReadOnlyList<KeyValuePair<string, string>> StringTableEntries => stringTableEntries;
public IReadOnlyList<SerializedObjectRepresentation> OtherEntries => otherEntries;
public ResourcesFileTreeNode(Resource r) : base(r)
{
@ -90,6 +94,18 @@ namespace ILSpy.TreeNodes @@ -90,6 +94,18 @@ namespace ILSpy.TreeNodes
case MemoryStream ms:
Children.Add(ResourceEntryNode.Create(entry.Key, ms.ToArray()));
break;
case null:
otherEntries.Add(new SerializedObjectRepresentation(entry.Key, "null", string.Empty));
break;
case ResourceSerializedObject so:
otherEntries.Add(new SerializedObjectRepresentation(entry.Key, so.TypeName ?? string.Empty, "<serialized>"));
break;
default:
otherEntries.Add(new SerializedObjectRepresentation(
entry.Key,
entry.Value.GetType().FullName ?? entry.Value.GetType().Name,
entry.Value.ToString() ?? string.Empty));
break;
}
}
@ -97,12 +113,20 @@ namespace ILSpy.TreeNodes @@ -97,12 +113,20 @@ namespace ILSpy.TreeNodes
{
EnsureLazyChildren();
base.Decompile(language, output, options);
if (stringTableEntries.Count == 0)
if (output is not ISmartTextOutput smart)
return;
output.WriteLine();
language.WriteCommentLine(output, "string table:");
foreach (var pair in stringTableEntries)
language.WriteCommentLine(output, $" {pair.Key} = {pair.Value}");
if (stringTableEntries.Count > 0)
{
output.WriteLine();
smart.AddUIElement(() => new ResourceStringTable(stringTableEntries));
output.WriteLine();
}
if (otherEntries.Count > 0)
{
output.WriteLine();
smart.AddUIElement(() => new ResourceObjectTable(otherEntries));
output.WriteLine();
}
}
}
}

Loading…
Cancel
Save