Skip to content

Commit

Permalink
Fix exporter (#162)
Browse files Browse the repository at this point in the history
* refactoring

* データ抽出オプションを修正

* テストに合格するようにした
  • Loading branch information
Ocelot1210 authored Jan 29, 2023
1 parent fce3ac4 commit c4f65df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
8 changes: 5 additions & 3 deletions LibX4/FileSystem/CatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private static readonly Regex _ParseModRegex
/// コンストラクタ
/// </summary>
/// <param name="gameRoot">X4インストール先ディレクトリパス</param>
/// <param name="option">cat ファイルの読み込みオプション</param>
/// <exception cref="DependencyResolutionException">Mod の依存関係の解決に失敗した場合</exception>
public CatFile(string gameRoot, CatLoadOption option = CatLoadOption.All)
{
Expand All @@ -92,7 +93,7 @@ public CatFile(string gameRoot, CatLoadOption option = CatLoadOption.All)
}
}

var modInfo = GetModInfo(gameRoot);
var modInfo = GetModInfo(gameRoot, option);

_loadedMods = new HashSet<string>(modInfo.Select(x => $"extensions/{Path.GetFileName(x.Directory)}".Replace('\\', '/')));

Expand All @@ -111,8 +112,9 @@ public CatFile(string gameRoot, CatLoadOption option = CatLoadOption.All)
/// Mod の情報を <see cref="IReadOnlyList{ModInfo}"/> で返す
/// </summary>
/// <param name="gameRoot">X4 インストール先ディレクトリパス</param>
/// <param name="option">cat ファイルの読み込みオプション</param>
/// <returns>Mod の情報を表す <see cref="IReadOnlyList{ModInfo}"/></returns>
private static IReadOnlyList<ModInfo> GetModInfo(string gameRoot)
private static IReadOnlyList<ModInfo> GetModInfo(string gameRoot, CatLoadOption option)
{
var entensionsPath = Path.Combine(gameRoot, "extensions");

Expand All @@ -127,7 +129,7 @@ private static IReadOnlyList<ModInfo> GetModInfo(string gameRoot)

var unloadedMods = Directory.GetDirectories(entensionsPath)
.Select(x => new ModInfo(userContentXml, x))
.Where(x => x.Enabled)
.Where(x => x.CanLoad(option))
.OrderBy(x => x.Name)
.ToList();

Expand Down
18 changes: 18 additions & 0 deletions LibX4/FileSystem/ModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public ModInfo(XDocument? userContentXml, string modDirPath)
}


/// <summary>
/// Mod を読み込めるか判定する
/// </summary>
/// <param name="option">cat ファイルの読み込みオプション</param>
/// <returns>Mod を読み込んでも良い場合、<c>true;</c> それ以外の場合 <c>false;</c></returns>
/// <exception cref="NotImplementedException"><paramref name="option"/>が無効な場合</exception>
public bool CanLoad(CatLoadOption option)
{
return Enabled && option switch
{
CatLoadOption.All => true,
CatLoadOption.Official => Author == "Egosoft GmbH",
CatLoadOption.Vanilla => false,
_ => throw new NotImplementedException()
};
}


/// <summary>
/// Mod が有効化されているかを取得する
/// </summary>
Expand Down
11 changes: 8 additions & 3 deletions X4_DataExporterWPF/ExportWindow/DataExportModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class DataExportModel
/// <summary>
/// 言語一覧を更新
/// </summary>
public static async Task<(bool success, IReadOnlyList<LangComboboxItem> languages)> GetLanguages(string inDirPath, Window owner)
/// <param name="x4Dir">X4のインストール先フォルダ</param>
/// <param name="catLoadOption">cat ファイルの読み込みオプション</param>
/// <param name="owner">親ウィンドウハンドル(メッセージボックス表示用)</param>
public static async Task<(bool success, IReadOnlyList<LangComboboxItem> languages)> GetLanguages(string x4Dir, CatLoadOption catLoadOption, Window owner)
{
try
{
var catFiles = new CatFile(inDirPath);
var catFiles = new CatFile(x4Dir, catLoadOption);
var xml = await catFiles.OpenXmlAsync("libraries/languages.xml", CancellationToken.None);
var languages = xml.XPathSelectElements("/languages/language")
.Select(x => (ID: x.Attribute("id")?.Value, Name: x.Attribute("name")?.Value))
Expand Down Expand Up @@ -60,6 +63,7 @@ await owner.Dispatcher.BeginInvoke((Action)(() =>
/// 抽出実行
/// </summary>
/// <param name="inDirPath">入力元フォルダパス</param>
/// <param name="catLoadOption">cat ファイルの読み込みオプション</param>
/// <param name="outFilePath">出力先ファイルパス</param>
/// <param name="language">選択された言語</param>
/// <param name="owner">親ウィンドウハンドル(メッセージボックス表示用)</param>
Expand All @@ -68,12 +72,13 @@ public static async Task Export(
IProgress<(int currentStep, int maxSteps)> progress,
IProgress<(int currentStep, int maxSteps)> progressSub,
string inDirPath,
CatLoadOption catLoadOption,
string outFilePath,
LangComboboxItem language,
Window owner
)
{
var catFile = new CatFile(inDirPath);
var catFile = new CatFile(inDirPath, catLoadOption);

// 抽出に失敗した場合、例外設定で「Common Languate Runtime Exceptions」にチェックを入れるとどこで例外が発生したか分かる
try
Expand Down
3 changes: 2 additions & 1 deletion X4_DataExporterWPF/ExportWindow/DataExportViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private async Task UpdateLangList(string x4InstallDirectory)
_unableToGetLanguages.Value = false;
Languages.ClearOnScheduler();

var (success, languages) = await Task.Run(async () => await DataExportModel.GetLanguages(x4InstallDirectory, _ownerWindow));
var (success, languages) = await Task.Run(async () => await DataExportModel.GetLanguages(x4InstallDirectory, CatLoadOption.Value, _ownerWindow));
_unableToGetLanguages.Value = !success;
Languages.AddRangeOnScheduler(languages);

Expand Down Expand Up @@ -251,6 +251,7 @@ await Task.Run(() => DataExportModel.Export(
progress,
progressSub,
InDirPath.Value,
CatLoadOption.Value,
_outFilePath,
SelectedLanguage.Value,
_ownerWindow
Expand Down

0 comments on commit c4f65df

Please sign in to comment.