Skip to content

Commit

Permalink
クラス名の修正とModからデータを抽出する処理の修正
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot1210 committed Jul 26, 2020
1 parent bc4bdbf commit b2e743f
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 64 deletions.
63 changes: 46 additions & 17 deletions LibX4/FileSystem/CatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class CatFile
/// <summary>
/// バニラのファイル
/// </summary>
private readonly CatFileLoader _VanillaFile;
private readonly IFileLoader _VanillaFile;


/// <summary>
/// Modのファイル
/// </summary>
private readonly Dictionary<string, CatFileLoader> _ModFiles = new Dictionary<string, CatFileLoader>();
private readonly Dictionary<string, IFileLoader> _ModFiles = new Dictionary<string, IFileLoader>();


/// <summary>
Expand Down Expand Up @@ -74,9 +74,9 @@ public MemoryStream OpenFile(string filePath)
}

// バニラのデータに見つからない場合、Modのデータを探しに行く
foreach (var catFile in _ModFiles.Values)
foreach (var fileLoader in _ModFiles.Values)
{
using var ret = catFile.OpenFile(filePath);
using var ret = fileLoader.OpenFile(filePath);

if (ret != null)
{
Expand Down Expand Up @@ -109,7 +109,7 @@ public XDocument OpenXml(string filePath)
}

// Modのxmlを連結
foreach (var (modPath, catFile) in _ModFiles)
foreach (var (modPath, fileLoader) in _ModFiles)
{
var targetPath = filePath;

Expand All @@ -119,7 +119,7 @@ public XDocument OpenXml(string filePath)
targetPath = targetPath.Substring(modPath.Length + 1);
}

using var ms = catFile.OpenFile(targetPath);
using var ms = fileLoader.OpenFile(targetPath);
if (ms == null)
{
continue;
Expand Down Expand Up @@ -158,21 +158,51 @@ public XDocument OpenLangXml(string filePath)
}

// Modのxmlを連結
foreach (var catFile in _ModFiles.Values)
foreach (var loader in _ModFiles.Values)
{
using var ms = catFile.OpenFile(filePath);
if (ms == null)
{
continue;
}
ConcatModLangFile(loader, "t/0001.xml", ref ret);
ConcatModLangFile(loader, filePath, ref ret);
}

if (ret == null)
return ret ?? throw new FileNotFoundException(filePath);
}


/// <summary>
/// Modの言語ファイルを連結
/// </summary>
/// <param name="stream">ファイルストリーム</param>
/// <param name="ret">言語 XDocument</param>
private void ConcatModLangFile(IFileLoader loader, string filePath, ref XDocument? ret)
{
using var ms = loader.OpenFile(filePath);

// 言語ファイルが無ければ何もしない
if (ms == null)
{
return;
}

// バニラに言語ファイルが無いか?
if (ret == null)
{
// バニラに言語ファイルが無い場合
ret = XDocument.Load(ms);
}
else
{
// バニラに言語ファイルが有る場合、Modの言語ファイルを連結
var src = XDocument.Load(ms);

// Modの言語ファイルは差分ファイルか?
if (src.Root.Name.LocalName == "diff")
{
ret = XDocument.Load(ms);
// 差分ファイルの場合、パッチ処理に差分を適用させる
_XMLPatcher.MergeXML(ret, src);
}
else
{
var src = XDocument.Load(ms);
// 差分ファイルでない場合、xmlの内容を連結する

foreach (var elm in src.XPathSelectElements("language/page"))
{
Expand All @@ -189,11 +219,10 @@ public XDocument OpenLangXml(string filePath)
}
}
}

return ret ?? throw new FileNotFoundException(filePath);
}



/// <summary>
/// indexファイルに記載されているxmlを開く
/// </summary>
Expand Down
53 changes: 39 additions & 14 deletions LibX4/FileSystem/CatFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ namespace LibX4.FileSystem
/// <summary>
/// catファイル読み込み用クラス
/// </summary>
class CatFileLoader
class CatFileLoader : IFileLoader
{
/// <summary>
/// catやModファイルがあるルートディレクトリ
/// </summary>
public string RootDir { get; }


/// <summary>
/// まだロードされていないCat/Datファイルの絶対パス
/// </summary>
Expand All @@ -36,6 +42,8 @@ private static readonly Regex _CatFileRecordParser
/// <param name="dirPath">Cat/Datファイルを探すディレクトリの絶対パス</param>
public CatFileLoader(string dirPath)
{
RootDir = dirPath;

var archivePaths = Directory.EnumerateFiles(dirPath, "*.cat")
// ファイル名にsigを含むCatファイルは除外
.Where(p => !Path.GetFileName(p).Contains("sig"))
Expand All @@ -60,19 +68,36 @@ public CatFileLoader(string dirPath)
public MemoryStream? OpenFile(string filePath)
{
var entry = GetCatEntry(filePath);
if (entry == null) return null;

using var fs = new FileStream(
entry.DatFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
entry.FileSize
);
fs.Seek(entry.Offset, SeekOrigin.Begin);
var buff = new byte[entry.FileSize];
fs.Read(buff, 0, buff.Length);
return new MemoryStream(buff, false);
if (entry != null)
{
using var fs = new FileStream(
entry.DatFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
entry.FileSize
);
var buff = new byte[entry.FileSize];
fs.Seek(entry.Offset, SeekOrigin.Begin);
fs.Read(buff, 0, buff.Length);

return new MemoryStream(buff, false);
}
else
{
var path = Path.Combine(RootDir, filePath);
if (!File.Exists(path))
{
return null;
}

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);

var buff = new byte[fs.Length];
fs.Read(buff, 0, buff.Length);

return new MemoryStream(buff, false);
}
}


Expand Down
20 changes: 20 additions & 0 deletions LibX4/FileSystem/IFileLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.IO;

namespace LibX4.FileSystem
{
interface IFileLoader
{
/// <summary>
/// Mod等のルートディレクトリ
/// </summary>
public string RootDir { get; }


/// <summary>
/// ファイルを開く
/// </summary>
/// <param name="filePath">ファイルパス</param>
/// <returns>ファイルのMemoryStream、該当ファイルが無かった場合はnull</returns>
public MemoryStream? OpenFile(string filePath);
}
}
23 changes: 15 additions & 8 deletions LibX4/Lang/LangageResolver.cs → LibX4/Lang/LanguageResolver.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using LibX4.FileSystem;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Xml.XPath;
using LibX4.FileSystem;

namespace LibX4.Lang
{
/// <summary>
/// X4の言語フィールド文字列を解決するクラス
/// </summary>
public class LangageResolver
public class LanguageResolver
{
#region メンバ
/// <summary>
Expand Down Expand Up @@ -55,7 +54,7 @@ public class LangageResolver
/// コンストラクタ
/// </summary>
/// <param name="catFile">catファイルオブジェクト</param>
public LangageResolver(CatFile catFile)
public LanguageResolver(CatFile catFile)
{
_CatFile = catFile;
}
Expand All @@ -67,16 +66,24 @@ public LangageResolver(CatFile catFile)
/// <param name="langID">言語ID</param>
public void LoadLangFile(int langID)
{
var langFilePath = $"t/0001-l{langID,3:D3}.xml";
LoadLangFile($"t/0001-l{langID,3:D3}.xml");
}


/// <summary>
/// 言語ファイル読み込み
/// </summary>
/// <param name="fileName"></param>
public void LoadLangFile(string fileName)
{
// ロード済みなら何もしない
if (_LangTrees.ContainsKey(langFilePath))
if (_LangTrees.ContainsKey(fileName))
{
return;
}

_LangTrees.Add(langFilePath, _CatFile.OpenLangXml(langFilePath));
_Langages.Push(langFilePath);
_LangTrees.Add(fileName, _CatFile.OpenLangXml(fileName));
_Langages.Push(fileName);
}


Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Equipment/EquipmentExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EquipmentExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver _Resolver;
private readonly LanguageResolver _Resolver;


/// <summary>
Expand All @@ -38,7 +38,7 @@ class EquipmentExporter : IExporter
/// <param name="catFile">catファイルオブジェクト</param>
/// <param name="waresXml">ウェア情報xml</param>
/// <param name="resolver">言語解決用オブジェクト</param>
public EquipmentExporter(CatFile catFile, XDocument waresXml, LangageResolver resolver)
public EquipmentExporter(CatFile catFile, XDocument waresXml, LanguageResolver resolver)
{
_CatFile = catFile;
_WaresXml = waresXml;
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Equipment/EquipmentTypeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class EquipmentTypeExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver _Resolver;
private readonly LanguageResolver _Resolver;


/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="resolver">言語解決用オブジェクト</param>
public EquipmentTypeExporter(LangageResolver resolver)
public EquipmentTypeExporter(LanguageResolver resolver)
{
_Resolver = resolver;
}
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Module/ModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ModuleExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver _Resolver;
private readonly LanguageResolver _Resolver;


/// <summary>
Expand All @@ -37,7 +37,7 @@ public class ModuleExporter : IExporter
/// <param name="catFile">catファイルオブジェクト</param>
/// <param name="waresXml">ウェア情報xml</param>
/// <param name="resolver">言語解決用オブジェクト</param>
public ModuleExporter(CatFile catFile, XDocument waresXml, LangageResolver resolver)
public ModuleExporter(CatFile catFile, XDocument waresXml, LanguageResolver resolver)
{
_CatFile = catFile;
_WaresXml = waresXml;
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Module/ModuleTypeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class ModuleTypeExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver _Resolver;
private readonly LanguageResolver _Resolver;


/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="resolver">言語解決用オブジェクト</param>
public ModuleTypeExporter(LangageResolver resolver)
public ModuleTypeExporter(LanguageResolver resolver)
{
_Resolver = resolver;
}
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Other/MapExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class MapExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver _Resolver;
private readonly LanguageResolver _Resolver;


/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="catFile">catファイル</param>
/// <param name="resolver">言語解決用オブジェクト</param>
public MapExporter(CatFile catFile, LangageResolver resolver)
public MapExporter(CatFile catFile, LanguageResolver resolver)
{
_MapXml = catFile.OpenXml("libraries/mapdefaults.xml");
_Resolver = resolver;
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Other/SizeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class SizeExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver Resolver;
private readonly LanguageResolver Resolver;


/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="resolver">言語解決用オブジェクト</param>
public SizeExporter(LangageResolver resolver)
public SizeExporter(LanguageResolver resolver)
{
Resolver = resolver;
}
Expand Down
4 changes: 2 additions & 2 deletions X4_DataExporterWPF/Export/Other/TransportTypeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class TransportTypeExporter : IExporter
/// <summary>
/// 言語解決用オブジェクト
/// </summary>
private readonly LangageResolver Resolver;
private readonly LanguageResolver Resolver;


/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="resolver">言語解決用オブジェクト</param>
public TransportTypeExporter(LangageResolver resolver)
public TransportTypeExporter(LanguageResolver resolver)
{
Resolver = resolver;
}
Expand Down
Loading

0 comments on commit b2e743f

Please sign in to comment.