~ruther/NosTale-PacketLogger

fd54708c709cc2d62d08f99fa7ada101bdb3802f — František Boháček 2 years ago 1659baf
feat: add packet document that loads packet providers
8 files changed, 171 insertions(+), 12 deletions(-)

M src/PacketLogger/ViewModels/LogTabViewModel.cs
A src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs
R src/PacketLogger/Views/{LogFilterTab.axaml => LogFilterTabView.axaml}
R src/PacketLogger/Views/{LogFilterTab.axaml.cs => LogFilterTabView.axaml.cs}
R src/PacketLogger/Views/{LogTab.axaml => LogTabView.axaml}
R src/PacketLogger/Views/{LogTab.axaml.cs => LogTabView.axaml.cs}
A src/PacketLogger/Views/PacketLogDocumentView.axaml
A src/PacketLogger/Views/PacketLogDocumentView.axaml.cs
M src/PacketLogger/ViewModels/LogTabViewModel.cs => src/PacketLogger/ViewModels/LogTabViewModel.cs +7 -4
@@ 29,8 29,11 @@ public class LogTabViewModel : ViewModelBase, IDisposable
    /// <summary>
    /// Initializes a new instance of the <see cref="LogTabViewModel"/> class.
    /// </summary>
    public LogTabViewModel()
    /// <param name="packetProvider">The packet provider.</param>
    public LogTabViewModel(IPacketProvider packetProvider)
    {
        Provider = packetProvider;

        var dynamicFilter = this.WhenValueChanged(@this => @this.CurrentFilter)
            .Select
            (


@@ 58,12 61,12 @@ public class LogTabViewModel : ViewModelBase, IDisposable

        CopyPackets = ReactiveCommand.CreateFromObservable<IList, Unit>
        (
            (l) => Observable.StartAsync
            list => Observable.StartAsync
            (
                async () =>
                {
                    var clipboardString = string.Join
                        ('\n', l.OfType<PacketInfo>().Select(x => x.PacketString));
                        ('\n', list.OfType<PacketInfo>().Select(x => x.PacketString));
                    await Application.Current!.Clipboard!.SetTextAsync(clipboardString);
                }
            )


@@ 123,7 126,7 @@ public class LogTabViewModel : ViewModelBase, IDisposable
    /// <summary>
    /// Gets packet provider.
    /// </summary>
    public IPacketProvider Provider { get; } = new DummyPacketProvider();
    public IPacketProvider Provider { get; }

    /// <summary>
    /// Gets whether the pane is open.

A src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs => src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs +105 -0
@@ 0,0 1,105 @@
//
//  PacketLogDocumentViewModel.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Dock.Model.Mvvm.Controls;
using PacketLogger.Models.Packets;
using ReactiveUI;

namespace PacketLogger.ViewModels;

/// <inheritdoc />
public class PacketLogDocumentViewModel : Document, INotifyPropertyChanged
{
    /// <summary>
    /// Initializes a new instance of the <see cref="PacketLogDocumentViewModel"/> class.
    /// </summary>
    public PacketLogDocumentViewModel()
    {
        OpenDummy = ReactiveCommand.CreateFromTask
        (
            () => Task.Run(() =>
            {
                Loading = true;
                Name = "Dummy";
                LogViewModel = new LogTabViewModel(new DummyPacketProvider());
                Loaded = true;
            })
        );
        OpenFile = ReactiveCommand.CreateFromTask
        (
            async () =>
            {
                var mainWindow = (App.Current!.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)
                    ?.MainWindow;
                var result = await new OpenFileDialog()
                {
                    AllowMultiple = false,
                    InitialFileName = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName
                }.ShowAsync(mainWindow!);

                if (result is null || result.Length == 0)
                {
                    return;
                }
                Loading = true;

                var path = result[0];
                var provider = new FilePacketProvider(path);

                var openResult = await provider.Open();
                if (!openResult.IsSuccess)
                {
                    Console.WriteLine("Could not open the file.");
                    return;
                }

                LogViewModel = new LogTabViewModel(provider);
                Loaded = true;
                Loading = false;
            }
        );
    }

    /// <summary>
    /// Gets or sets the name of the tab.
    /// </summary>
    public string Name { get; set; } = "New tab";

    /// <summary>
    /// Gets whether the document is currently being loaded.
    /// </summary>
    public bool Loading { get; private set; } = false;

    /// <summary>
    /// Gets whether a document has been loaded.
    /// </summary>
    public bool Loaded { get; private set; }

    /// <summary>
    /// Gets the log tab view model.
    /// </summary>
    public LogTabViewModel? LogViewModel { get; private set; }

    /// <summary>
    /// Gets command for opening a dummy.
    /// </summary>
    public ReactiveCommand<Unit, Unit> OpenDummy { get; }

    /// <summary>
    /// Gets command for opening a file.
    /// </summary>
    public ReactiveCommand<Unit, Unit> OpenFile { get; }
}
\ No newline at end of file

R src/PacketLogger/Views/LogFilterTab.axaml => src/PacketLogger/Views/LogFilterTabView.axaml +1 -1
@@ 4,7 4,7 @@
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:PacketLogger.ViewModels"
             mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="450"
             x:Class="PacketLogger.Views.LogFilterTab">
             x:Class="PacketLogger.Views.LogFilterTabView">
    <Design.DataContext>
        <vm:LogFilterTabViewModel />
    </Design.DataContext>

R src/PacketLogger/Views/LogFilterTab.axaml.cs => src/PacketLogger/Views/LogFilterTabView.axaml.cs +4 -4
@@ 1,5 1,5 @@
//
//  LogFilterTab.axaml.cs
//  LogFilterTabView.axaml.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.


@@ 14,12 14,12 @@ using PropertyChanged;
namespace PacketLogger.Views;

[DoNotNotify]
public partial class LogFilterTab : UserControl
public partial class LogFilterTabView : UserControl
{
    /// <summary>
    /// Initializes a new instance of the <see cref="LogFilterTab"/> class.
    /// Initializes a new instance of the <see cref="LogFilterTabView"/> class.
    /// </summary>
    public LogFilterTab()
    public LogFilterTabView()
    {
        InitializeComponent();
        this.FindControl<ComboBox>("FilterType").Items = Enum.GetValues<FilterCreator.FilterType>();

R src/PacketLogger/Views/LogTab.axaml => src/PacketLogger/Views/LogTabView.axaml +2 -2
@@ 41,14 41,14 @@
                                Recv
                            </TabItem.Header>

                            <views:LogFilterTab DataContext="{Binding RecvFilter}" />
                            <ContentControl DataContext="{Binding RecvFilter}" />
                        </TabItem>
                        <TabItem>
                            <TabItem.Header>
                                Send
                            </TabItem.Header>

                            <views:LogFilterTab DataContext="{Binding SendFilter}" />
                            <ContentControl DataContext="{Binding SendFilter}" />
                        </TabItem>
                    </TabControl>
                </Grid>

R src/PacketLogger/Views/LogTab.axaml.cs => src/PacketLogger/Views/LogTabView.axaml.cs +1 -1
@@ 1,5 1,5 @@
//
//  LogTab.axaml.cs
//  LogTabView.axaml.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

A src/PacketLogger/Views/PacketLogDocumentView.axaml => src/PacketLogger/Views/PacketLogDocumentView.axaml +22 -0
@@ 0,0 1,22 @@
<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"
             xmlns:views="clr-namespace:PacketLogger.Views"
             xmlns:viewModels="clr-namespace:PacketLogger.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="PacketLogger.Views.PacketLogDocumentView">
    <Design.DataContext>
        <viewModels:PacketLogDocumentViewModel />
    </Design.DataContext>
    <Grid>
        <Grid IsVisible="{Binding !Loaded}">
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                <Button Content="Open File" IsEnabled="{Binding !Loading}" Command="{Binding OpenFile}"></Button>
                <Button Content="Open Dummy" IsEnabled="{Binding !Loading}" Command="{Binding OpenDummy}"></Button>
            </StackPanel>
        </Grid>

        <ContentControl IsVisible="{Binding Loaded}" Content="{Binding  LogViewModel}" />
    </Grid>
</UserControl>
\ No newline at end of file

A src/PacketLogger/Views/PacketLogDocumentView.axaml.cs => src/PacketLogger/Views/PacketLogDocumentView.axaml.cs +29 -0
@@ 0,0 1,29 @@
//
//  PacketLogDocumentView.axaml.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using PropertyChanged;

namespace PacketLogger.Views;

[DoNotNotify]
public partial class PacketLogDocumentView : UserControl
{
    /// <summary>
    /// Initializes a new instance of the <see cref="PacketLogDocumentView"/> class.
    /// </summary>
    public PacketLogDocumentView()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }
}
\ No newline at end of file

Do not follow this link