Skip to content

Commit

Permalink
ファイル一覧出力処理を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
logue committed Oct 2, 2021
1 parent d214e17 commit 74a3219
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 61 deletions.
7 changes: 2 additions & 5 deletions NgsPacker.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31702.278
# Visual Studio Version 17
VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NgsPacker", "NgsPacker\NgsPacker.csproj", "{1469A306-6324-4E7D-B435-C0DA536C3D9F}"
ProjectSection(ProjectDependencies) = postProject
{B6748219-A0E7-46BB-AE38-5343961CB36A} = {B6748219-A0E7-46BB-AE38-5343961CB36A}
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1890804D-C1D4-4DC0-B739-4F87BC075825}"
ProjectSection(SolutionItems) = preProject
Expand Down
11 changes: 10 additions & 1 deletion NgsPacker/Interfaces/IZamboniService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
// -----------------------------------------------------------------------

using System.Collections.Generic;
using System.Threading.Tasks;
using zamboni;

Expand All @@ -17,7 +18,7 @@ public interface IZamboniService
/// </summary>
/// <param name="inputPath">パックしたいファイルのディレクトリ</param>
/// <param name="recursive">サブディレクトリを再帰的に含めるか</param>
/// <param name="compress">圧縮する(非推奨)</param>
/// <param name="compress">圧縮する</param>
/// <param name="forceUnencrypted">暗号化する(非推奨)</param>
/// <returns>パックしたファイルのバイナリ</returns>
public byte[] Pack(string inputPath, bool recursive, bool compress = false, bool forceUnencrypted = false);
Expand All @@ -26,10 +27,18 @@ public interface IZamboniService
/// 指定されたファイルをアンパックする
/// </summary>
/// <param name="inputPath">入力ファイルのパス</param>
/// <param name="outputPath">出力先のパス</param>
/// <param name="sepalate">グループ1と2で分ける</param>
/// <returns>解凍したファイルのバイナリ</returns>
public void Unpack(string inputPath, string outputPath = null, bool sepalate = false);

/// <summary>
/// ファイル一覧を取得
/// </summary>
/// <param name="inputPath">解析するdataディレクトリ</param>
/// <returns></returns>
public List<string> Filelist(string inputPath);

/// <summary>
/// Iceファイルを読み込む.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions NgsPacker/NgsPacker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageProjectUrl>https://logue.dev/NgsPacker</PackageProjectUrl>
<RepositoryUrl>git@github.com:logue/NgsPacker.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Version>1.0.1</Version>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
Expand Down
113 changes: 72 additions & 41 deletions NgsPacker/Services/ZamboniService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NgsPacker.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -139,17 +140,17 @@ public void Unpack(string inputPath, string outputPath = null, bool sepalate = f
// 出力先のディレクトリ作成
if (!Directory.Exists(destinaton))
{
Directory.CreateDirectory(destinaton);
_ = Directory.CreateDirectory(destinaton);
}
if (sepalate)
{
if (!Directory.Exists(destinaton + Path.DirectorySeparatorChar + "group1"))
{
Directory.CreateDirectory(destinaton + Path.DirectorySeparatorChar + "group1");
_ = Directory.CreateDirectory(destinaton + Path.DirectorySeparatorChar + "group1");
}
if (!Directory.Exists(destinaton + Path.DirectorySeparatorChar + "group2"))
{
Directory.CreateDirectory(destinaton + Path.DirectorySeparatorChar + "group2");
_ = Directory.CreateDirectory(destinaton + Path.DirectorySeparatorChar + "group2");
}
}

Expand All @@ -162,56 +163,86 @@ public void Unpack(string inputPath, string outputPath = null, bool sepalate = f

using BinaryWriter writer = new(new FileStream(fileName, FileMode.Create));
writer.Write(model.Content);
writer.Close();
}
}

/// <summary>
/// 指定されたファイルをアンパックする(非同期)
/// ファイル一覧を取得
/// </summary>
/// <param name="inputPath">入力ファイルのパス</param>
/// <param name="sepalate">グループ1と2で分ける</param>
public async void UnpackAsync(string inputPath, string outputPath = null, bool sepalate = false)
/// <param name="inputPath">解析するdataディレクトリ</param>
/// <returns></returns>
public List<string> Filelist(string inputPath)
{
if (string.IsNullOrEmpty(outputPath))
List<string> ret = new();
string[] entries = Directory.EnumerateFiles(inputPath, "*.*", SearchOption.AllDirectories).ToArray();
Debug.WriteLine("Entries: ", entries.Length);
foreach (string path in entries)
{
outputPath = Directory.GetCurrentDirectory();
}
// Iceファイルを読み込む
IceFile iceFile = LoadIceFile(inputPath);

// 出力先のディレクトリ
string destinaton = outputPath + Path.DirectorySeparatorChar + Path.GetFileName(inputPath) + "_ext" + Path.DirectorySeparatorChar;

// グループ1
List<IceEntryModel> groupOneFiles = IceToFilelist(iceFile.groupOneFiles, true);
// グループ2
List<IceEntryModel> groupTwoFiles = IceToFilelist(iceFile.groupTwoFiles);


// 出力できるファイルがない場合
if (groupOneFiles.Count == 0 && groupTwoFiles.Count == 0)
{
throw new ZamboniException($"Neither group1 nor group2 was dumped from {Path.GetFileName(inputPath)}.");
}


foreach (IceEntryModel model in groupOneFiles.Concat(groupTwoFiles))
{
if (!sepalate)
if (path.Contains("."))
{
await File.WriteAllBytesAsync(destinaton + model.FileName, model.Content);
continue;
}

if (model.Group == IceGroupEnum.GROUP1)
Debug.WriteLine(path);
// Iceファイルを読み込む
using (FileStream fileStream = File.OpenRead(path))
{
await File.WriteAllBytesAsync(destinaton + "group1" + Path.DirectorySeparatorChar + model.FileName, model.Content);
}
else
{
await File.WriteAllBytesAsync(destinaton + "group2" + Path.DirectorySeparatorChar + model.FileName, model.Content);
byte[] numArray = new byte[4];
_ = fileStream.Read(numArray, 0, 4);
_ = fileStream.Seek(0L, SeekOrigin.Begin);


string str1 = Encoding.ASCII.GetString(numArray).Replace("\0", "");
if (str1 != "ICE")
{
fileStream.Close();
// ICEファイルでない
continue;
}

ret.Add(Path.GetFileName(path) + "\t\t\t" + str1);

// Iceファイルをパースする
IceFile iceFile = IceFile.LoadIceFile(fileStream);

if (iceFile == null)
{
fileStream.Close();
// パース不能
continue;
}

// グループ1のファイルをパース
if (iceFile.groupOneFiles != null)
{
ret.Add("\tGroup 1:");
byte[][] groupOneFiles = iceFile.groupOneFiles;
for (int index = 0; index < groupOneFiles.Length; ++index)
{
int int32 = BitConverter.ToInt32(groupOneFiles[index], 16);
string str2 = Encoding.ASCII.GetString(groupOneFiles[index], 64, int32).TrimEnd(new char[1]);
ret.Add("\t\t" + str2);
}
}

// グループ2のファイルをパース
if (iceFile.groupOneFiles != null)
{
ret.Add("\tGroup 2:");
byte[][] groupTwoFiles = iceFile.groupTwoFiles;
for (int index = 0; index < groupTwoFiles.Length; ++index)
{
int int32 = BitConverter.ToInt32(groupTwoFiles[index], 16);
string str2 = Encoding.ASCII.GetString(groupTwoFiles[index], 64, int32).TrimEnd(new char[1]);
ret.Add("\t\t" + str2);
}

}
}
ret.Add("");
ret.Add("");
}
return ret;
}

/// <summary>
Expand Down Expand Up @@ -277,7 +308,7 @@ public async Task<IceFile> LoadIceFileAsync(string inputPath)
/// </summary>
/// <param name="data">解凍済みのIceのデータストリーム</param>
/// <returns></returns>
private List<IceEntryModel> IceToFilelist(byte[][] data, bool isGroupOne = false)
public List<IceEntryModel> IceToFilelist(byte[][] data, bool isGroupOne = false)
{
List<IceEntryModel> fileList = new();
for (int index = 0; index < data.Length; ++index)
Expand Down
103 changes: 91 additions & 12 deletions NgsPacker/ViewModels/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Prism.Commands;
using Prism.Mvvm;
using SourceChord.FluentWPF;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -79,6 +78,7 @@ public HomePageViewModel(ILocalizerService localizerService, IZamboniService zam
{
PackCommand = new DelegateCommand(ExecutePackCommand);
UnpackCommand = new DelegateCommand(ExecuteUnpackCommand);
ExportFilelistCommand = new DelegateCommand(ExecuteExportFilelistCommand);

IsCompress = true;

Expand Down Expand Up @@ -124,7 +124,6 @@ private async void ExecutePackCommand()
#if !DEBUG
try
{
#endif
InProgress = true;
ProgressText = LocalizerService.GetLocalizedString("PackingText");
Task task = await Task.Run(async () =>
Expand All @@ -136,24 +135,34 @@ private async void ExecutePackCommand()
return Task.CompletedTask;
});
InProgress = false;
#if !DEBUG
}
catch (Exception ex)
{
_ = AcrylicMessageBox.Show(System.Windows.Application.Current.MainWindow, ex.Message, LocalizerService.GetLocalizedString("ErrorTitleText"));
return;
}
#endif
#else
InProgress = true;
ProgressText = LocalizerService.GetLocalizedString("PackingText");
Task task = await Task.Run(async () =>
{
// Iceで圧縮(結構重い)
byte[] iceStream = ZamboniService.Pack(folder.Path, IsCompress, IsCrypt);
await File.WriteAllBytesAsync(saveFileDialog.FileName, iceStream);

return Task.CompletedTask;
});
InProgress = false;
#endif

// 完了通知
if (Properties.Settings.Default.NotifyComplete)
{
// トースト通知
new ToastContentBuilder()
.AddText(LocalizerService.GetLocalizedString("PackText"))
.AddText(LocalizerService.GetLocalizedString("CompleteText"))
.Show();
.AddText(LocalizerService.GetLocalizedString("PackText"))
.AddText(LocalizerService.GetLocalizedString("CompleteText"))
.Show();
}
else
{
Expand Down Expand Up @@ -194,7 +203,6 @@ private async void ExecuteUnpackCommand()
#if !DEBUG
try
{
#endif
ProgressText = LocalizerService.GetLocalizedString("UnpackingText");
InProgress = true;
Task task = await Task.Run(async () =>
Expand All @@ -203,22 +211,30 @@ private async void ExecuteUnpackCommand()
return Task.CompletedTask;
});
InProgress = false;
#if !DEBUG
}
catch (Exception ex)
{
_ = AcrylicMessageBox.Show(System.Windows.Application.Current.MainWindow, ex.Message, LocalizerService.GetLocalizedString("ErrorTitleText"));
return;
}
#else
ProgressText = LocalizerService.GetLocalizedString("UnpackingText");
InProgress = true;
Task task = await Task.Run(async () =>
{
ZamboniService.Unpack(openFileDialog.FileName, folder.Path, IsSepareteByGroup);
return Task.CompletedTask;
});
InProgress = false;
#endif
// 完了通知
if (Properties.Settings.Default.NotifyComplete)
{
// トースト通知
new ToastContentBuilder()
.AddText(LocalizerService.GetLocalizedString("UnpackText"))
.AddText(LocalizerService.GetLocalizedString("CompleteText"))
.Show();
.AddText(LocalizerService.GetLocalizedString("UnpackText"))
.AddText(LocalizerService.GetLocalizedString("CompleteText"))
.Show();
}
else
{
Expand All @@ -227,6 +243,69 @@ private async void ExecuteUnpackCommand()
}
}

/// <summary>
/// ファイル一覧を出力
/// </summary>
private async void ExecuteExportFilelistCommand()
{
// フォルダ選択ダイアログ
FolderPickerEx picker = new();

// 出力先ファイルダイアログを表示
Windows.Storage.StorageFolder folder = picker.PickSingleFolder();

if (folder == null)
{
return;
}

// ファイル保存ダイアログ
using SaveFileDialog saveFileDialog = new()
{
Title = LocalizerService.GetLocalizedString("SaveAsDialogText"),
FileName = "list.csv"
};

// ダイアログを表示
DialogResult dialogResult = saveFileDialog.ShowDialog();
if (dialogResult != DialogResult.OK)
{
// キャンセルされたので終了
return;
}

#if !DEBUG
try
{
InProgress = true;
await File.WriteAllTextAsync(saveFileDialog.FileName, string.Join("\r\n", ZamboniService.Filelist(folder.Path)));
InProgress = false;
}
catch (Exception ex)
{
_ = AcrylicMessageBox.Show(System.Windows.Application.Current.MainWindow, ex.Message, LocalizerService.GetLocalizedString("ErrorTitleText"));
return;
}
#else
InProgress = true;
await File.WriteAllTextAsync(saveFileDialog.FileName, string.Join("\r\n", ZamboniService.Filelist(folder.Path)));
InProgress = false;
#endif

// 完了通知
if (Properties.Settings.Default.NotifyComplete)
{
// トースト通知
new ToastContentBuilder()
.AddText(LocalizerService.GetLocalizedString("ExportFileListText"))
.AddText(LocalizerService.GetLocalizedString("CompleteText"))
.Show();
}
else
{
_ = AcrylicMessageBox.Show(System.Windows.Application.Current.MainWindow,
LocalizerService.GetLocalizedString("ExportFileListText"), LocalizerService.GetLocalizedString("CompleteText"));
}
}
}
}
Loading

0 comments on commit 74a3219

Please sign in to comment.