Skip to content

Commit

Permalink
Cleanup code and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jun 26, 2024
1 parent ac6422d commit 5e987a1
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 152 deletions.
4 changes: 0 additions & 4 deletions src/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Totk.ZStdTool"
xmlns:sty="using:FluentAvalonia.Styling">
<Application.DataTemplates>
<local:ViewLocator />
</Application.DataTemplates>

<Application.Styles>
<sty:FluentAvaloniaTheme />
</Application.Styles>
Expand Down
1 change: 0 additions & 1 deletion src/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Totk.ZStdTool;

public partial class App : Application
{
public static TotkConfig Config { get; } = TotkConfig.Load();
public static TopLevel? VisualRoot { get; private set; }

public override void Initialize()
Expand Down
6 changes: 2 additions & 4 deletions src/CommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public static void Compress(string input, Dictionary<char, string> flags)
Directory.CreateDirectory(directory);
}

using FileStream fs = File.Create(output);
fs.Write(ZStdHelper.Compress(input, useDictionaries));
ZstdHelper.Compress(input, output, useDictionaries);
}

public static void Decompress(string input, Dictionary<char, string> flags)
Expand All @@ -90,7 +89,6 @@ public static void Decompress(string input, Dictionary<char, string> flags)
Directory.CreateDirectory(directory);
}

using FileStream fs = File.Create(output);
fs.Write(ZStdHelper.Decompress(input));
ZstdHelper.Decompress(input, output);
}
}
93 changes: 49 additions & 44 deletions src/Helpers/ZStdHelper.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
using SarcLibrary;
using ZstdSharp;
using CommunityToolkit.HighPerformance.Buffers;
using TotkCommon;

namespace Totk.ZStdTool.Helpers;

public class ZStdHelper
public static class ZstdHelper
{
private static readonly Decompressor _defaultDecompressor = new();
private static readonly Decompressor _commonDecompressor = new();
private static readonly Decompressor _mapDecompressor = new();
private static readonly Decompressor _packDecompressor = new();
private static readonly Compressor _defaultCompressor = new(16);
private static readonly Compressor _commonCompressor = new(16);
private static readonly Compressor _mapCompressor = new(16);
private static readonly Compressor _packCompressor = new(16);

static ZStdHelper()
public static int GetDictioanryId(this string path, bool useDictionaries)
{
Span<byte> data = _commonDecompressor.Unwrap(File.ReadAllBytes(TotkConfig.ZsDicPath));
SarcFile sarc = SarcFile.FromBinary(data.ToArray());
return useDictionaries switch {
false => -1,
true => path.EndsWith(".rsizetable") || path.EndsWith("ZsDic.pack") ? -1 :
path.EndsWith(".bcett.byml") ? 3 :
path.EndsWith(".pack") ? 2 : 1
};
}

public static void Decompress(string file, string output)
{
using SpanOwner<byte> decompressed = Decompress(file);
using FileStream fs = File.Create(output);
fs.Write(decompressed.Span);
}

_commonDecompressor.LoadDictionary(sarc["zs.zsdic"]);
_mapDecompressor.LoadDictionary(sarc["bcett.byml.zsdic"]);
_packDecompressor.LoadDictionary(sarc["pack.zsdic"]);
public static SpanOwner<byte> Decompress(string file)
{
using FileStream fs = File.OpenRead(file);
int size = Convert.ToInt32(fs.Length);
using SpanOwner<byte> buffer = SpanOwner<byte>.Allocate(size);
fs.Read(buffer.Span);

_commonCompressor.LoadDictionary(sarc["zs.zsdic"]);
_mapCompressor.LoadDictionary(sarc["bcett.byml.zsdic"]);
_packCompressor.LoadDictionary(sarc["pack.zsdic"]);
size = Zstd.GetDecompressedSize(buffer.Span);
SpanOwner<byte> decompressed = SpanOwner<byte>.Allocate(size);
TotkCommon.Totk.Zstd.Decompress(buffer.Span, decompressed.Span);
return decompressed;
}

public static Span<byte> Decompress(string file)
public static void Compress(string file, string output, bool useDictionaries)
{
Span<byte> src = File.ReadAllBytes(file);
return
file.EndsWith(".bcett.byml.zs") ? _mapDecompressor.Unwrap(src) :
file.EndsWith(".pack.zs") ? _packDecompressor.Unwrap(src) :
file.EndsWith(".rsizetable.zs") ? _defaultDecompressor.Unwrap(src) :
_commonDecompressor.Unwrap(src);
using SpanOwner<byte> compressed = Compress(file, useDictionaries);
using FileStream fs = File.Create(output);
fs.Write(compressed.Span);
}
public static Span<byte> Compress(string file, bool useDictionaries)

public static SpanOwner<byte> Compress(string file, bool useDictionaries)
{
Span<byte> src = File.ReadAllBytes(file);
return
!useDictionaries ? _defaultCompressor.Wrap(src) :
file.EndsWith(".bcett.byml") ? _mapCompressor.Wrap(src) :
file.EndsWith(".pack") ? _packCompressor.Wrap(src) :
file.EndsWith(".rsizetable") ? _defaultCompressor.Wrap(src) :
_commonCompressor.Wrap(src);
using FileStream fs = File.OpenRead(file);
int size = Convert.ToInt32(fs.Length);
using SpanOwner<byte> buffer = SpanOwner<byte>.Allocate(size);
fs.Read(buffer.Span);

size = Zstd.GetDecompressedSize(buffer.Span);
SpanOwner<byte> decompressed = SpanOwner<byte>.Allocate(size);
TotkCommon.Totk.Zstd.Decompress(buffer.Span, decompressed.Span);
return decompressed;
}

public static void DecompressFolder(string path, string output, bool recursive, Action<int>? setCount = null, Action<int>? updateCount = null)
Expand All @@ -54,33 +61,31 @@ public static void DecompressFolder(string path, string output, bool recursive,
setCount?.Invoke(files.Length);

for (int i = 0; i < files.Length; i++) {
var file = files[i];
Span<byte> data = Decompress(file);
string file = files[i];
using SpanOwner<byte> data = Decompress(file);

string outputFile = Path.Combine(output, Path.GetRelativePath(path, file.Remove(file.Length - 3, 3)));
Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!);
using FileStream fs = File.Create(outputFile);
fs.Write(data);
fs.Write(data.Span);

updateCount?.Invoke(i + 1);
}
}

public static void CompressFolder(string path, string output, bool recursive, Action<int>? setCount = null, Action<int>? updateCount = null, bool useDictionaries = true)
{
string[] files = Directory.EnumerateFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Where(path => Path.GetExtension(path) != ".zs" && Path.GetFileName(path) != "ZsDic.pack")
.ToArray();
string[] files = Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
setCount?.Invoke(files.Length);

for (int i = 0; i < files.Length; i++) {
string file = files[i];
Span<byte> data = Compress(file, useDictionaries);
using SpanOwner<byte> data = Compress(file, useDictionaries);

string outputFile = Path.Combine(output, Path.GetRelativePath(path, $"{file}.zs"));
Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!);
using FileStream fs = File.Create(outputFile);
fs.Write(data);
fs.Write(data.Span);

updateCount?.Invoke(i + 1);
}
Expand Down
17 changes: 9 additions & 8 deletions src/Totk.ZStdTool.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<!--Avalonia doesen't support TrimMode=link currently,but we are working on that https://github.com/AvaloniaUI/Avalonia/issues/6892 -->
<TrimMode>copyused</TrimMode>
Expand All @@ -28,14 +28,15 @@
<TrimmableAssembly Include="Avalonia.Themes.Default" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0-preview7" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0-preview7" />
<PackageReference Include="Avalonia" Version="11.0.11" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.11" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview7" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview7" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.0-preview7" />
<PackageReference Include="SarcLibrary" Version="2.0.2" />
<PackageReference Include="ZstdSharp.Port" Version="0.7.1" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.11" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.11" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.5" />
<PackageReference Include="SarcLibrary" Version="3.1.3" />
<PackageReference Include="TotkCommon" Version="1.2.7" />
<PackageReference Include="ZstdSharp.Port" Version="0.8.1" />
</ItemGroup>
<ItemGroup>
<Using Include="ReactiveUI" />
Expand Down
38 changes: 0 additions & 38 deletions src/TotkConfig.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/ViewLocator.cs

This file was deleted.

20 changes: 9 additions & 11 deletions src/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public async Task Decompress()
if (await dialog.ShowDialog() is string path) {
StartLoading(1);

using FileStream fs = File.Create(path);
fs.Write(ZStdHelper.Decompress(FilePath));
ZstdHelper.Decompress(FilePath, path);

UpdateCount(1);

Expand Down Expand Up @@ -130,8 +129,7 @@ public async Task Compress()
string outputFile = $"{Path.GetFileName(FilePath)}.zs";
BrowserDialog dialog = new(BrowserMode.SaveFile, "Save Compressed File", $"Zstd Compressed File:*.zs|Any File:*.*", outputFile, "save");
if (await dialog.ShowDialog() is string path) {
using FileStream fs = File.Create(path);
fs.Write(ZStdHelper.Compress(FilePath, UseDictionaries));
ZstdHelper.Compress(FilePath, path, UseDictionaries);

ContentDialog dlg = new() {
Content = $"File compressed to '{path}'",
Expand Down Expand Up @@ -172,7 +170,7 @@ public async Task DecompressFolder()
BrowserDialog dialog = new(BrowserMode.OpenFolder, "Output Folder", "save-fld");
if (await dialog.ShowDialog() is string path && Directory.Exists(path)) {
StartLoading();
await Task.Run(() => ZStdHelper.DecompressFolder(FolderPath, path, DecompressRecursive, SetCount, UpdateCount));
await Task.Run(() => ZstdHelper.DecompressFolder(FolderPath, path, DecompressRecursive, SetCount, UpdateCount));

ContentDialog dlg = new() {
Content = $"Folder Decompressed to '{path}'",
Expand Down Expand Up @@ -211,7 +209,7 @@ public async Task CompressFolder()
BrowserDialog dialog = new(BrowserMode.OpenFolder, "Output Folder", "save-fld");
if (await dialog.ShowDialog() is string path && Directory.Exists(path)) {
StartLoading();
await Task.Run(() => ZStdHelper.CompressFolder(FolderPath, path, DecompressRecursive, SetCount, UpdateCount, UseDictionaries));
await Task.Run(() => ZstdHelper.CompressFolder(FolderPath, path, DecompressRecursive, SetCount, UpdateCount, UseDictionaries));

ContentDialog dlg = new() {
Content = $"Folder compressed to '{path}'",
Expand Down Expand Up @@ -249,7 +247,7 @@ public static async Task ShowSettings()
Spacing = 10,
Children = {
new TextBox {
Text = App.Config.GamePath,
Text = TotkCommon.Totk.Config.GamePath,
Watermark = "Game Path",
UseFloatingWatermark = true,
},
Expand All @@ -264,14 +262,14 @@ public static async Task ShowSettings()

if (await dlg.ShowAsync() == ContentDialogResult.Primary) {
var stack = (dlg.Content as ScrollViewer)!.Content as StackPanel;
App.Config.GamePath = (stack!.Children[0] as TextBox)!.Text!;
App.Config.Save();
TotkCommon.Totk.Config.GamePath = (stack!.Children[0] as TextBox)!.Text!;
TotkCommon.Totk.Config.Save();
}
}

public async Task<bool> CheckConfig()
public static async Task<bool> CheckConfig()
{
if (File.Exists(TotkConfig.ZsDicPath)) {
if (File.Exists(TotkCommon.Totk.Config.ZsDicPath)) {
return true;
}

Expand Down
34 changes: 17 additions & 17 deletions src/Views/ShellView.axaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Window x:Class="Totk.ZStdTool.Views.ShellView"
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"
Title="Totk ZStd Tool"
Width="580"
Height="350"
MinWidth="580"
MinHeight="350"
MaxWidth="650"
MaxHeight="450"
d:DesignHeight="450"
d:DesignWidth="800"
Icon="/Assets/icon.ico"
mc:Ignorable="d">
<ui:AppWindow x:Class="Totk.ZStdTool.Views.ShellView"
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:ui="using:FluentAvalonia.UI.Windowing"
Title="Totk zStandard Tool"
Width="580"
Height="350"
MinWidth="580"
MinHeight="350"
MaxWidth="650"
MaxHeight="450"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<Grid Margin="20" RowDefinitions="Auto,*,Auto">
<StackPanel>
Expand Down Expand Up @@ -93,4 +93,4 @@
</StackPanel>
</Border>
</Grid>
</Window>
</ui:AppWindow>
9 changes: 8 additions & 1 deletion src/Views/ShellView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using FluentAvalonia.UI.Windowing;

namespace Totk.ZStdTool.Views;
public partial class ShellView : Window
public partial class ShellView : AppWindow
{
public ShellView()
{
InitializeComponent();

Bitmap bitmap = new(AssetLoader.Open(new Uri("avares://Totk.ZStdTool/Assets/icon.ico")));
Icon = bitmap.CreateScaledBitmap(new(48, 48), BitmapInterpolationMode.HighQuality);

FileNameEntry.AddHandler(DragDrop.DropEvent, DragDropEvent);
FolderNameEntry.AddHandler(DragDrop.DropEvent, DragDropEvent);
}
Expand Down

0 comments on commit 5e987a1

Please sign in to comment.