Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
-Add Convert Folder to VP tool
-Fix filenames were missing the initial "_" character is they had them
  • Loading branch information
Shivansps committed Jan 30, 2024
1 parent 459c43c commit 812ce18
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 5 deletions.
3 changes: 2 additions & 1 deletion VP.NET.GUI/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class Settings
public string? ToolLastVPDecompressionDestinationPath { get; set; } = null;
public string? ToolLastVPCompressionOpenPath { get; set; } = null;
public string? ToolLastVPCompressionDestinationPath { get; set; } = null;

public string? ToolLastFolderToVPFolderPath { get; set; } = null;
public string? ToolLastFolderToVPVPSavePath { get; set; } = null;
public void Load()
{
try
Expand Down
27 changes: 26 additions & 1 deletion VP.NET.GUI/Models/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;

using System.Linq;
using System.Threading.Tasks;

namespace VP.NET.GUI.Models
{
Expand Down Expand Up @@ -50,5 +51,29 @@ public static string FormatBytes(long bytes)
return string.Empty;
}
}

/// <summary>
/// Gets the complete size of all files in a folder and subdirectories in bytes
/// </summary>
/// <param name="folderPath"></param>
/// <returns>size in bytes or 0 if failed</returns>
public static async Task<long> GetSizeOfFolderInBytes(string folderPath)
{
return await Task<long>.Run(() =>
{
try
{
if (Directory.Exists(folderPath))
{
return Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories).Sum(fileInfo => new FileInfo(fileInfo).Length);
}
}
catch (Exception ex)
{
Log.Add(Log.LogSeverity.Warning, "KnUtils.GetSizeOfFolderInBytes", ex);
}
return 0;
});
}
}
}
158 changes: 158 additions & 0 deletions VP.NET.GUI/ViewModels/FolderToVPViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using VP.NET.GUI.Models;
using VP.NET.GUI.Views;

namespace VP.NET.GUI.ViewModels
{
public partial class FolderToVPViewModel : ViewModelBase
{
private CancellationTokenSource? cancellationTokenSource = null;
[ObservableProperty]
internal int progressMax = 1;
[ObservableProperty]
internal int progressCurrent = 0;
[ObservableProperty]
internal string progressFilename = string.Empty;
[ObservableProperty]
internal string folderPath = string.Empty;
[ObservableProperty]
internal string folderSize = string.Empty;
[ObservableProperty]
internal string vPPath = string.Empty;
[ObservableProperty]
internal bool buttonsEnabled = true;
[ObservableProperty]
internal bool compressVP = false;
[ObservableProperty]
internal bool canCreate = false;

public FolderToVPViewModel()
{

}

private async void GetFolderSize()
{
try
{
FolderSize = "";
if (Directory.Exists(FolderPath))
{
FolderSize = "Estimated Size: " + Utils.FormatBytes(await Utils.GetSizeOfFolderInBytes(FolderPath));
}
}
catch (Exception ex)
{
Log.Add(Log.LogSeverity.Error, "FolderToVPViewModel.GetFolderSize()", ex);
}
}

public async void Start()
{
await Task.Run(async () => {
cancellationTokenSource = new CancellationTokenSource();
Dispatcher.UIThread.Invoke(() => {
ButtonsEnabled = false;
ProgressCurrent = 0;
ProgressMax = 1;
ProgressFilename = string.Empty;
});
if (Directory.Exists(FolderPath))
{
var vp = new VPContainer();
if (CompressVP)
{
vp.EnableCompression();
VPPath = Path.ChangeExtension(VPPath, ".vpc");
}
else
{
vp.DisableCompression();
VPPath = Path.ChangeExtension(VPPath, ".vp");
}
vp.AddFolderToRoot(FolderPath);
await vp.SaveAsAsync(VPPath, progressCallback, cancellationTokenSource).ConfigureAwait(false);
if(File.Exists(VPPath))
{
FolderSize = "Final Size: " + Utils.FormatBytes(new FileInfo(VPPath).Length);
}
}
Dispatcher.UIThread.Invoke(() => {
ButtonsEnabled = true;
cancellationTokenSource = null;
});
}).ConfigureAwait(false);
}

internal void Cancel()
{
cancellationTokenSource?.Cancel();
}

internal void progressCallback(string name, int max)
{
Dispatcher.UIThread.Invoke(() => {
ProgressFilename = "_"+name;//visual hack
ProgressMax = max;
ProgressCurrent++;
});
}

public async void BrowseFolder()
{
FolderPickerOpenOptions options = new FolderPickerOpenOptions();
options.Title = "Select the source folder";
if (MainWindowViewModel.Settings.ToolLastFolderToVPFolderPath != null)
{
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(MainWindowViewModel.Settings.ToolLastFolderToVPFolderPath);
}
var result = await MainWindow.Instance!.StorageProvider.OpenFolderPickerAsync(options);

if (result != null && result.Count > 0)
{
var newPath = (await result[0].GetParentAsync())?.Path.LocalPath;
if (MainWindowViewModel.Settings.ToolLastFolderToVPFolderPath != newPath)
{
MainWindowViewModel.Settings.ToolLastFolderToVPFolderPath = newPath;
MainWindowViewModel.Settings.Save();
}
FolderPath = result[0].Path.LocalPath;
CanCreate = FolderPath.Trim().Length > 0 && VPPath.Trim().Length > 0;
GetFolderSize();
}
}

public async void BrowseFile()
{
FilePickerSaveOptions options = new FilePickerSaveOptions();
options.Title = "Destination VP name";
if (MainWindowViewModel.Settings.ToolLastFolderToVPVPSavePath != null)
{
options.SuggestedStartLocation = await MainWindow.Instance!.StorageProvider.TryGetFolderFromPathAsync(MainWindowViewModel.Settings.ToolLastFolderToVPVPSavePath);
options.DefaultExtension = ".vp";
options.FileTypeChoices = new List<FilePickerFileType> { new("VP (*.vp)") { Patterns = new[] { "*.vp" } } };
}
var result = await MainWindow.Instance!.StorageProvider.SaveFilePickerAsync(options);

if (result != null)
{
var newPath = (await result.GetParentAsync())?.Path.LocalPath;
if (MainWindowViewModel.Settings.ToolLastFolderToVPVPSavePath != newPath)
{
MainWindowViewModel.Settings.ToolLastFolderToVPVPSavePath = newPath;
MainWindowViewModel.Settings.Save();
}
VPPath = result.Path.LocalPath;
VPPath = Path.ChangeExtension(VPPath, ".vp");
CanCreate = FolderPath.Trim().Length > 0 && VPPath.Trim().Length > 0;
}
}
}
}
8 changes: 8 additions & 0 deletions VP.NET.GUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,13 @@ internal void CompressVPs()
_ = dialog.ShowDialog<MultiToolView?>(MainWindow.Instance!);
dataContext.CompressVPCs();
}

internal void FolderToVP()
{
var dialog = new FolderToVPView();
var dataContext = new FolderToVPViewModel();
dialog.DataContext = dataContext;
_ = dialog.ShowDialog<FolderToVPView?>(MainWindow.Instance!);
}
}
}
2 changes: 1 addition & 1 deletion VP.NET.GUI/ViewModels/VpFileEntryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public VpFileEntryViewModel(VPFile vpFile)
{
try
{
Name = vpFile.info.name;
Name = "_"+vpFile.info.name; //visual hack
FileDate = VPTime.GetDateFromUnixTimeStamp(vpFile.info.timestamp).ToString();
if (vpFile.compressionInfo.header.HasValue && vpFile.type == VPFileType.File)
{
Expand Down
46 changes: 46 additions & 0 deletions VP.NET.GUI/Views/FolderToVPView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<Window 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="800" d:DesignHeight="450"
x:Class="VP.NET.GUI.Views.FolderToVPView"
xmlns:vm="using:VP.NET.GUI.ViewModels"
Icon="/Assets/vpneticon.ico"
Width="600"
MinWidth="600"
Height="470"
MinHeight="470"
WindowStartupLocation="CenterOwner"
Title="Folder to VP">

<Design.DataContext>
<vm:FolderToVPViewModel/>
</Design.DataContext>

<Grid RowDefinitions="Auto, Auto, Auto, Auto, Auto, Auto, Auto, Auto, Auto,Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock Grid.Row="0" TextWrapping="Wrap" Margin="10">This tool creates a VP out of a folder. The selected folder should have the "data" folder inside of it. DO NOT select the "data" folder itself as that will add all folders inside of it to the VP root and thats is not the correct VP structure. Remember that the max VP size is 2GB, if you select "compress" files will be compressed while added to the vp and the final size may be lower than the one indicated here.</TextBlock>

<Label Grid.Row="1" Margin="5">Source Folder</Label>
<WrapPanel Grid.Row="2" HorizontalAlignment="Center" IsEnabled="{Binding ButtonsEnabled}" Margin="10">
<TextBox MinWidth="400" Text="{Binding FolderPath}"></TextBox>
<Button Content="Browse" Command="{Binding BrowseFolder}"></Button>
</WrapPanel>

<Label Grid.Row="3" Margin="5">Destination VP</Label>
<WrapPanel HorizontalAlignment="Center" Grid.Row="4" IsEnabled="{Binding ButtonsEnabled}" Margin="10">
<TextBox MinWidth="400" Text="{Binding VPPath}"></TextBox>
<Button Content="Browse" Command="{Binding BrowseFile}"></Button>
</WrapPanel>
<CheckBox IsEnabled="{Binding ButtonsEnabled}" HorizontalAlignment="Center" Grid.Row="5" Margin="5" IsChecked="{Binding CompressVP}">Compress VP (Creates a .vpc)</CheckBox>

<ProgressBar Grid.Row="6" Minimum="0" Maximum="{Binding ProgressMax}" Value="{Binding ProgressCurrent}" Margin="20,10,20,10" Height="20" ShowProgressText="True"></ProgressBar>
<Label Grid.Row="7" Content="{Binding ProgressFilename}" HorizontalContentAlignment="Center"></Label>
<StackPanel Margin="5" HorizontalAlignment="Center" Grid.Row="8">
<Button Width="100" HorizontalContentAlignment="Center" IsVisible="{Binding ButtonsEnabled}" IsEnabled="{Binding CanCreate}" Content="Make VP" Command="{Binding Start}"></Button>
<Button Width="100" HorizontalContentAlignment="Center" IsVisible="{Binding !ButtonsEnabled}" Command="{Binding Cancel}">Cancel</Button>
</StackPanel>

<Label Content="{Binding FolderSize}" Grid.Row="9" HorizontalAlignment="Center" Margin="5"></Label>
</Grid>

</Window>
13 changes: 13 additions & 0 deletions VP.NET.GUI/Views/FolderToVPView.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace VP.NET.GUI.Views;

public partial class FolderToVPView : Window
{
public FolderToVPView()
{
InitializeComponent();
}
}
6 changes: 4 additions & 2 deletions VP.NET.GUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="480"
x:Class="VP.NET.GUI.Views.MainWindow"
Icon="/Assets/vpneticon.ico"
Title="VP.NET GUI v0.0.1"
Title="VP.NET GUI v0.0.2"
Width="800"
Height="480">

Expand Down Expand Up @@ -39,7 +39,9 @@
<MenuItem Header="Decompress VP (.vpc) files" Command="{Binding DecompressVPs}"></MenuItem>

<MenuItem Header="Compress VP (.vp) files" Command="{Binding CompressVPs}"></MenuItem>


<MenuItem Header="Convert folder to VP" Command="{Binding FolderToVP}"></MenuItem>

</MenuItem>
</Menu>

Expand Down

0 comments on commit 812ce18

Please sign in to comment.