Skip to content

Commit

Permalink
fix: fix issue that fail to open legal TAS file.
Browse files Browse the repository at this point in the history
- switch zlib library from zlib.net to DotNetZip because old one can not handle zlib-compressed file properly.
- bump version to 1.2
  • Loading branch information
yyc12345 committed Jul 26, 2024
1 parent e8fc96e commit 96f6136
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 36 deletions.
6 changes: 3 additions & 3 deletions BallanceTASEditor/BallanceTASEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.9.1.8\lib\net20\Ionic.Zip.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand All @@ -57,9 +60,6 @@
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="zlib.net, Version=1.0.3.0, Culture=neutral, PublicKeyToken=47d7877cb3620160">
<HintPath>..\packages\zlib.net.1.0.4.0\lib\zlib.net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
Expand Down
65 changes: 47 additions & 18 deletions BallanceTASEditor/Core/ZlibUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BallanceTASEditor.Core.TASStruct;
using BallanceTASEditor.Core.TASStruct;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -12,34 +12,63 @@ public class ZlibUtil {
public static void CompressTAS(LinkedList<FrameData> mem, FileStream file) {
file.Write(BitConverter.GetBytes(mem.Count * ConstValue.FRAMEDATA_SIZE), 0, 4);

var zo = new zlib.ZOutputStream(file, 9);
var node = mem.First;
while (node != null) {
zo.Write(BitConverter.GetBytes(node.Value.deltaTime), 0, 4);
zo.Write(BitConverter.GetBytes(node.Value.keystates), 0, 4);
node = node.Next;
using (var zo = new Ionic.Zlib.ZlibStream(file, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.Level9, true)) {
var node = mem.First;
while (node != null) {
zo.Write(BitConverter.GetBytes(node.Value.deltaTime), 0, 4);
zo.Write(BitConverter.GetBytes(node.Value.keystates), 0, 4);
node = node.Next;
}
zo.Close();
}
zo.finish();
zo.Close();

//var zo = new zlib.ZOutputStream(file, 9);
//var node = mem.First;
//while (node != null) {
// zo.Write(BitConverter.GetBytes(node.Value.deltaTime), 0, 4);
// zo.Write(BitConverter.GetBytes(node.Value.keystates), 0, 4);
// node = node.Next;
//}
//zo.finish();
//zo.Close();
}

public static void DecompressTAS(LinkedList<FrameData> ls, FileStream file) {
var lengthTemp = new byte[4];
var mem = new MemoryStream();
file.Read(lengthTemp, 0, 4);
Int32 expectedLength = BitConverter.ToInt32(lengthTemp, 0);
long expectedCount = expectedLength / ConstValue.FRAMEDATA_SIZE;

var zo = new zlib.ZOutputStream(mem);
CopyStream(file, zo);
zo.finish();
using (var mem = new MemoryStream()) {
using (var zo = new Ionic.Zlib.ZlibStream(mem, Ionic.Zlib.CompressionMode.Decompress, true)) {
CopyStream(file, zo);
zo.Close();
}

mem.Seek(0, SeekOrigin.Begin);
for(long i = 0; i < expectedCount; i++) {
ls.AddLast(new FrameData(mem));
mem.Seek(0, SeekOrigin.Begin);
for (long i = 0; i < expectedCount; i++) {
ls.AddLast(new FrameData(mem));
}
mem.Close();
}
mem.Close();
zo.Close();

//mem.Seek(0, SeekOrigin.Begin);
//for (long i = 0; i < expectedCount; i++) {
// ls.AddLast(new FrameData(mem));
//}
//mem.Close();
//zo.Close();

//var zo = new zlib.ZOutputStream(mem);
//CopyStream(file, zo);
//zo.finish();

//mem.Seek(0, SeekOrigin.Begin);
//for (long i = 0; i < expectedCount; i++) {
// ls.AddLast(new FrameData(mem));
//}
//mem.Close();
//zo.Close();
}

public static void CopyStream(Stream origin, Stream target) {
Expand Down
4 changes: 2 additions & 2 deletions BallanceTASEditor/Language/CHS.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BallanceTASEditor.Language"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
Expand Down Expand Up @@ -62,7 +62,7 @@
<sys:String x:Key="code_Shared_ProgramName">Ballance TAS 编辑器</sys:String>

<sys:String x:Key="code_MainWindow_Menu_Help_About" xml:space="preserve">基于 MIT 开源许可证发布
版本:1.1 stable
版本:1.2 stable
程序:yyc12345.
图标设计:plAer_2</sys:String>
<sys:String x:Key="code_MainWindow_Closing">文件未关闭。您想直接退出吗?所有修改不会被保存。</sys:String>
Expand Down
4 changes: 2 additions & 2 deletions BallanceTASEditor/Language/DefaultLanguage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BallanceTASEditor.Language"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
Expand Down Expand Up @@ -62,7 +62,7 @@
<sys:String x:Key="code_Shared_ProgramName">Ballance TAS Editor</sys:String>

<sys:String x:Key="code_MainWindow_Menu_Help_About" xml:space="preserve">Under MIT License
Version: 1.1 stable
Version: 1.2 stable
Program: yyc12345.
Icon design: plAer_2</sys:String>
<sys:String x:Key="code_MainWindow_Closing">File is not closed. Do you want to just quit? All changes will not be saved.</sys:String>
Expand Down
4 changes: 2 additions & 2 deletions BallanceTASEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="BallanceTASEditor.MainWindow"
<Window x:Class="BallanceTASEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down Expand Up @@ -262,7 +262,7 @@
</StatusBarItem>

<StatusBarItem DockPanel.Dock="Right" HorizontalAlignment="Right">
<TextBlock Text="v1.1 stable" Foreground="Gray" FontStyle="Italic"/>
<TextBlock Text="v1.2 stable" Foreground="Gray" FontStyle="Italic"/>
</StatusBarItem>
</StatusBar>

Expand Down
22 changes: 14 additions & 8 deletions BallanceTASEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BallanceTASEditor.Core;
using BallanceTASEditor.Core;
using BallanceTASEditor.Core.TASStruct;
using BallanceTASEditor.UI;
using System;
Expand Down Expand Up @@ -90,8 +90,8 @@ private void funcMenu_Help_ReportBugs(object sender, RoutedEventArgs e) {
}

private void funcMenu_Help_About(object sender, RoutedEventArgs e) {
MessageBox.Show(I18NProcessor.GetI18N("code_MainWindow_Menu_Help_About"),
I18NProcessor.GetI18N("code_Shared_ProgramName"),
MessageBox.Show(I18NProcessor.GetI18N("code_MainWindow_Menu_Help_About"),
I18NProcessor.GetI18N("code_Shared_ProgramName"),
MessageBoxButton.OK, MessageBoxImage.Information);
}

Expand Down Expand Up @@ -248,7 +248,7 @@ private void funcBtn_FastMoveNext(object sender, RoutedEventArgs e) {
private void funcWindow_KeyUp(object sender, KeyEventArgs e) {
if (mFile == null || mViewer == null) return;

switch(e.Key) {
switch (e.Key) {
case Key.A:
mSlider.MoveSliderManually(true, true, mViewer.GetItemCountInPage());
break;
Expand Down Expand Up @@ -280,7 +280,7 @@ private void funcWindow_MouseWheel(object sender, MouseWheelEventArgs e) {
// normally move
mSlider.MoveSliderManually(true, false, mViewer.GetItemCountInPage());
}

} else if (e.Delta < 0) {
// wheel down
if (KeyboardState.IsKeyPressed(KeyboardState.VirtualKeyStates.VK_SHIFT)) {
Expand Down Expand Up @@ -309,8 +309,7 @@ private void funcDrop_DragEnter(object sender, DragEventArgs e) {
var arr = (System.Array)e.Data.GetData(DataFormats.FileDrop);
if (arr.Length != 1) e.Effects = DragDropEffects.None;
else e.Effects = DragDropEffects.Link;
}
else e.Effects = DragDropEffects.None;
} else e.Effects = DragDropEffects.None;
}

#endregion
Expand All @@ -331,15 +330,22 @@ private void ApplyConfigureManager() {
}

private void OpenFile(string file) {
#if DEBUG
#else
try {
#endif
mFile = new TASFile(file);

#if DEBUG
#else
} catch {
MessageBox.Show(I18NProcessor.GetI18N("code_MainWindow_Menu_File_Open_Fail"),
I18NProcessor.GetI18N("code_Shared_ProgramName"),
I18NProcessor.GetI18N("code_Shared_ProgramName"),
MessageBoxButton.OK, MessageBoxImage.Error);
mFile = null;
return;
}
#endif

mViewer = new TASViewer(mFile, mSlider, mFlow);

Expand Down
2 changes: 1 addition & 1 deletion BallanceTASEditor/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.9.1.8" targetFramework="net45" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
<package id="zlib.net" version="1.0.4.0" targetFramework="net40" />
</packages>

0 comments on commit 96f6136

Please sign in to comment.