From b2e743f6abb761214d03db83045b07054e8e996f Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sun, 26 Jul 2020 13:18:21 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E5=90=8D=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=81=A8Mod=E3=81=8B=E3=82=89=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92=E6=8A=BD=E5=87=BA=E3=81=99=E3=82=8B?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibX4/FileSystem/CatFile.cs | 63 ++++++++++++++----- LibX4/FileSystem/CatFileLoader.cs | 53 +++++++++++----- LibX4/FileSystem/IFileLoader.cs | 20 ++++++ ...LangageResolver.cs => LanguageResolver.cs} | 23 ++++--- .../Export/Equipment/EquipmentExporter.cs | 4 +- .../Export/Equipment/EquipmentTypeExporter.cs | 4 +- .../Export/Module/ModuleExporter.cs | 4 +- .../Export/Module/ModuleTypeExporter.cs | 4 +- .../Export/Other/MapExporter.cs | 4 +- .../Export/Other/SizeExporter.cs | 4 +- .../Export/Other/TransportTypeExporter.cs | 4 +- .../Export/Race/FactionExporter.cs | 4 +- .../Export/Race/RaceExporter.cs | 4 +- .../Export/Ware/WareExporter.cs | 4 +- .../Export/Ware/WareGroupExporter.cs | 4 +- .../Export/Ware/WareProductionExporter.cs | 4 +- .../ExportWindow/DataExportModel.cs | 2 +- 17 files changed, 145 insertions(+), 64 deletions(-) create mode 100644 LibX4/FileSystem/IFileLoader.cs rename LibX4/Lang/{LangageResolver.cs => LanguageResolver.cs} (88%) diff --git a/LibX4/FileSystem/CatFile.cs b/LibX4/FileSystem/CatFile.cs index de67ce19..c5b7fb15 100644 --- a/LibX4/FileSystem/CatFile.cs +++ b/LibX4/FileSystem/CatFile.cs @@ -15,13 +15,13 @@ public class CatFile /// /// バニラのファイル /// - private readonly CatFileLoader _VanillaFile; + private readonly IFileLoader _VanillaFile; /// /// Modのファイル /// - private readonly Dictionary _ModFiles = new Dictionary(); + private readonly Dictionary _ModFiles = new Dictionary(); /// @@ -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) { @@ -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; @@ -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; @@ -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); + } + + + /// + /// Modの言語ファイルを連結 + /// + /// ファイルストリーム + /// 言語 XDocument + 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")) { @@ -189,11 +219,10 @@ public XDocument OpenLangXml(string filePath) } } } - - return ret ?? throw new FileNotFoundException(filePath); } + /// /// indexファイルに記載されているxmlを開く /// diff --git a/LibX4/FileSystem/CatFileLoader.cs b/LibX4/FileSystem/CatFileLoader.cs index e63e05f1..80d89f1d 100644 --- a/LibX4/FileSystem/CatFileLoader.cs +++ b/LibX4/FileSystem/CatFileLoader.cs @@ -8,8 +8,14 @@ namespace LibX4.FileSystem /// /// catファイル読み込み用クラス /// - class CatFileLoader + class CatFileLoader : IFileLoader { + /// + /// catやModファイルがあるルートディレクトリ + /// + public string RootDir { get; } + + /// /// まだロードされていないCat/Datファイルの絶対パス /// @@ -36,6 +42,8 @@ private static readonly Regex _CatFileRecordParser /// Cat/Datファイルを探すディレクトリの絶対パス public CatFileLoader(string dirPath) { + RootDir = dirPath; + var archivePaths = Directory.EnumerateFiles(dirPath, "*.cat") // ファイル名にsigを含むCatファイルは除外 .Where(p => !Path.GetFileName(p).Contains("sig")) @@ -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); + } } diff --git a/LibX4/FileSystem/IFileLoader.cs b/LibX4/FileSystem/IFileLoader.cs new file mode 100644 index 00000000..663e91a7 --- /dev/null +++ b/LibX4/FileSystem/IFileLoader.cs @@ -0,0 +1,20 @@ +using System.IO; + +namespace LibX4.FileSystem +{ + interface IFileLoader + { + /// + /// Mod等のルートディレクトリ + /// + public string RootDir { get; } + + + /// + /// ファイルを開く + /// + /// ファイルパス + /// ファイルのMemoryStream、該当ファイルが無かった場合はnull + public MemoryStream? OpenFile(string filePath); + } +} diff --git a/LibX4/Lang/LangageResolver.cs b/LibX4/Lang/LanguageResolver.cs similarity index 88% rename from LibX4/Lang/LangageResolver.cs rename to LibX4/Lang/LanguageResolver.cs index 9b52b960..e8db3392 100644 --- a/LibX4/Lang/LangageResolver.cs +++ b/LibX4/Lang/LanguageResolver.cs @@ -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 { /// /// X4の言語フィールド文字列を解決するクラス /// - public class LangageResolver + public class LanguageResolver { #region メンバ /// @@ -55,7 +54,7 @@ public class LangageResolver /// コンストラクタ /// /// catファイルオブジェクト - public LangageResolver(CatFile catFile) + public LanguageResolver(CatFile catFile) { _CatFile = catFile; } @@ -67,16 +66,24 @@ public LangageResolver(CatFile catFile) /// 言語ID public void LoadLangFile(int langID) { - var langFilePath = $"t/0001-l{langID,3:D3}.xml"; + LoadLangFile($"t/0001-l{langID,3:D3}.xml"); + } + + /// + /// 言語ファイル読み込み + /// + /// + 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); } diff --git a/X4_DataExporterWPF/Export/Equipment/EquipmentExporter.cs b/X4_DataExporterWPF/Export/Equipment/EquipmentExporter.cs index fd0fae29..9cbb34d4 100644 --- a/X4_DataExporterWPF/Export/Equipment/EquipmentExporter.cs +++ b/X4_DataExporterWPF/Export/Equipment/EquipmentExporter.cs @@ -29,7 +29,7 @@ class EquipmentExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -38,7 +38,7 @@ class EquipmentExporter : IExporter /// catファイルオブジェクト /// ウェア情報xml /// 言語解決用オブジェクト - public EquipmentExporter(CatFile catFile, XDocument waresXml, LangageResolver resolver) + public EquipmentExporter(CatFile catFile, XDocument waresXml, LanguageResolver resolver) { _CatFile = catFile; _WaresXml = waresXml; diff --git a/X4_DataExporterWPF/Export/Equipment/EquipmentTypeExporter.cs b/X4_DataExporterWPF/Export/Equipment/EquipmentTypeExporter.cs index fb995093..f8c1e958 100644 --- a/X4_DataExporterWPF/Export/Equipment/EquipmentTypeExporter.cs +++ b/X4_DataExporterWPF/Export/Equipment/EquipmentTypeExporter.cs @@ -13,14 +13,14 @@ public class EquipmentTypeExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// /// コンストラクタ /// /// 言語解決用オブジェクト - public EquipmentTypeExporter(LangageResolver resolver) + public EquipmentTypeExporter(LanguageResolver resolver) { _Resolver = resolver; } diff --git a/X4_DataExporterWPF/Export/Module/ModuleExporter.cs b/X4_DataExporterWPF/Export/Module/ModuleExporter.cs index e77476c8..46bdc2b1 100644 --- a/X4_DataExporterWPF/Export/Module/ModuleExporter.cs +++ b/X4_DataExporterWPF/Export/Module/ModuleExporter.cs @@ -28,7 +28,7 @@ public class ModuleExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -37,7 +37,7 @@ public class ModuleExporter : IExporter /// catファイルオブジェクト /// ウェア情報xml /// 言語解決用オブジェクト - public ModuleExporter(CatFile catFile, XDocument waresXml, LangageResolver resolver) + public ModuleExporter(CatFile catFile, XDocument waresXml, LanguageResolver resolver) { _CatFile = catFile; _WaresXml = waresXml; diff --git a/X4_DataExporterWPF/Export/Module/ModuleTypeExporter.cs b/X4_DataExporterWPF/Export/Module/ModuleTypeExporter.cs index ef52f55a..d021a248 100644 --- a/X4_DataExporterWPF/Export/Module/ModuleTypeExporter.cs +++ b/X4_DataExporterWPF/Export/Module/ModuleTypeExporter.cs @@ -14,14 +14,14 @@ public class ModuleTypeExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// /// コンストラクタ /// /// 言語解決用オブジェクト - public ModuleTypeExporter(LangageResolver resolver) + public ModuleTypeExporter(LanguageResolver resolver) { _Resolver = resolver; } diff --git a/X4_DataExporterWPF/Export/Other/MapExporter.cs b/X4_DataExporterWPF/Export/Other/MapExporter.cs index 57ff15f5..bfbe8025 100644 --- a/X4_DataExporterWPF/Export/Other/MapExporter.cs +++ b/X4_DataExporterWPF/Export/Other/MapExporter.cs @@ -20,7 +20,7 @@ class MapExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -28,7 +28,7 @@ class MapExporter : IExporter /// /// catファイル /// 言語解決用オブジェクト - public MapExporter(CatFile catFile, LangageResolver resolver) + public MapExporter(CatFile catFile, LanguageResolver resolver) { _MapXml = catFile.OpenXml("libraries/mapdefaults.xml"); _Resolver = resolver; diff --git a/X4_DataExporterWPF/Export/Other/SizeExporter.cs b/X4_DataExporterWPF/Export/Other/SizeExporter.cs index 8b5dfa7c..a607594d 100644 --- a/X4_DataExporterWPF/Export/Other/SizeExporter.cs +++ b/X4_DataExporterWPF/Export/Other/SizeExporter.cs @@ -13,14 +13,14 @@ class SizeExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver Resolver; + private readonly LanguageResolver Resolver; /// /// コンストラクタ /// /// 言語解決用オブジェクト - public SizeExporter(LangageResolver resolver) + public SizeExporter(LanguageResolver resolver) { Resolver = resolver; } diff --git a/X4_DataExporterWPF/Export/Other/TransportTypeExporter.cs b/X4_DataExporterWPF/Export/Other/TransportTypeExporter.cs index e7ec9492..15b8e868 100644 --- a/X4_DataExporterWPF/Export/Other/TransportTypeExporter.cs +++ b/X4_DataExporterWPF/Export/Other/TransportTypeExporter.cs @@ -13,14 +13,14 @@ class TransportTypeExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver Resolver; + private readonly LanguageResolver Resolver; /// /// コンストラクタ /// /// 言語解決用オブジェクト - public TransportTypeExporter(LangageResolver resolver) + public TransportTypeExporter(LanguageResolver resolver) { Resolver = resolver; } diff --git a/X4_DataExporterWPF/Export/Race/FactionExporter.cs b/X4_DataExporterWPF/Export/Race/FactionExporter.cs index 0c279186..b15e5cd3 100644 --- a/X4_DataExporterWPF/Export/Race/FactionExporter.cs +++ b/X4_DataExporterWPF/Export/Race/FactionExporter.cs @@ -23,7 +23,7 @@ public class FactionExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -31,7 +31,7 @@ public class FactionExporter : IExporter /// /// catファイル /// 言語解決用オブジェクト - public FactionExporter(CatFile catFile, LangageResolver resolver) + public FactionExporter(CatFile catFile, LanguageResolver resolver) { _FactionsXml = catFile.OpenXml("libraries/factions.xml"); diff --git a/X4_DataExporterWPF/Export/Race/RaceExporter.cs b/X4_DataExporterWPF/Export/Race/RaceExporter.cs index 63990f94..3c17f14b 100644 --- a/X4_DataExporterWPF/Export/Race/RaceExporter.cs +++ b/X4_DataExporterWPF/Export/Race/RaceExporter.cs @@ -22,7 +22,7 @@ public class RaceExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -30,7 +30,7 @@ public class RaceExporter : IExporter /// /// catファイル /// 言語解決用オブジェクト - public RaceExporter(CatFile catFile, LangageResolver resolver) + public RaceExporter(CatFile catFile, LanguageResolver resolver) { _RaceXml = catFile.OpenXml("libraries/races.xml"); diff --git a/X4_DataExporterWPF/Export/Ware/WareExporter.cs b/X4_DataExporterWPF/Export/Ware/WareExporter.cs index 36a37cef..39cf4e9c 100644 --- a/X4_DataExporterWPF/Export/Ware/WareExporter.cs +++ b/X4_DataExporterWPF/Export/Ware/WareExporter.cs @@ -19,7 +19,7 @@ public class WareExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -27,7 +27,7 @@ public class WareExporter : IExporter /// /// ウェア情報xml /// 言語解決用オブジェクト - public WareExporter(XDocument waresXml, LangageResolver resolver) + public WareExporter(XDocument waresXml, LanguageResolver resolver) { _WaresXml = waresXml; _Resolver = resolver; diff --git a/X4_DataExporterWPF/Export/Ware/WareGroupExporter.cs b/X4_DataExporterWPF/Export/Ware/WareGroupExporter.cs index 3cee87fa..aa6e66c0 100644 --- a/X4_DataExporterWPF/Export/Ware/WareGroupExporter.cs +++ b/X4_DataExporterWPF/Export/Ware/WareGroupExporter.cs @@ -23,7 +23,7 @@ public class WareGroupExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -31,7 +31,7 @@ public class WareGroupExporter : IExporter /// /// catファイル /// 言語解決用オブジェクト - public WareGroupExporter(CatFile catFile, LangageResolver resolver) + public WareGroupExporter(CatFile catFile, LanguageResolver resolver) { _WareGroupXml = catFile.OpenXml("libraries/waregroups.xml"); diff --git a/X4_DataExporterWPF/Export/Ware/WareProductionExporter.cs b/X4_DataExporterWPF/Export/Ware/WareProductionExporter.cs index a1a46145..898699a6 100644 --- a/X4_DataExporterWPF/Export/Ware/WareProductionExporter.cs +++ b/X4_DataExporterWPF/Export/Ware/WareProductionExporter.cs @@ -22,7 +22,7 @@ public class WareProductionExporter : IExporter /// /// 言語解決用オブジェクト /// - private readonly LangageResolver _Resolver; + private readonly LanguageResolver _Resolver; /// @@ -30,7 +30,7 @@ public class WareProductionExporter : IExporter /// /// ウェア情報xml /// 言語解決用オブジェクト - public WareProductionExporter(XDocument waresXml, LangageResolver resolver) + public WareProductionExporter(XDocument waresXml, LanguageResolver resolver) { _WaresXml = waresXml; _Resolver = resolver; diff --git a/X4_DataExporterWPF/ExportWindow/DataExportModel.cs b/X4_DataExporterWPF/ExportWindow/DataExportModel.cs index 82d5a11b..6311d74c 100644 --- a/X4_DataExporterWPF/ExportWindow/DataExportModel.cs +++ b/X4_DataExporterWPF/ExportWindow/DataExportModel.cs @@ -67,7 +67,7 @@ public void Export(IProgress<(int currentStep, int maxSteps)> progless, string i using var trans = conn.BeginTransaction(); - var resolver = new LangageResolver(catFile); + var resolver = new LanguageResolver(catFile); // 英語をデフォルトにする resolver.LoadLangFile(44);