Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Maple2.File.IO/IXmlSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Xml;

namespace Maple2.File.IO {
// Where a parser reads its XML files from. Paths are archive-relative, such as
// "table/Server/constants.xml". M2dReader serves them from a packed archive and
// XmlDirectorySource from an unpacked folder. Other implementations, such as one backed by a
// database, only have to return the same text for the same path.
public interface IXmlSource {
// The file's text with any UTF-8 byte order mark removed.
string GetString(string path);

XmlReader GetXmlReader(string path);
}
}
6 changes: 5 additions & 1 deletion Maple2.File.IO/M2dReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Maple2.File.IO.Crypto.Stream;

namespace Maple2.File.IO {
public class M2dReader : IDisposable {
public class M2dReader : IXmlSource, IDisposable {
private readonly FileStream m2dFile;
private readonly object m2dLock = new();
public readonly IReadOnlyList<PackFileEntry> Files;
Expand Down Expand Up @@ -67,6 +67,10 @@ public string GetString(PackFileEntry entry) {
return result;
}

string IXmlSource.GetString(string path) => GetString(GetEntry(path));

XmlReader IXmlSource.GetXmlReader(string path) => GetXmlReader(GetEntry(path));

private byte[] DecryptEntry(PackFileEntry entry) {
try {
return CryptoManager.DecryptData(entry.FileHeader, m2dFile, m2dLock);
Expand Down
43 changes: 43 additions & 0 deletions Maple2.File.IO/XmlDirectorySource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text;
using System.Xml;

namespace Maple2.File.IO {
// Reads XML from an unpacked folder laid out like the archive, so a change to the source XML can
// be parsed without repacking. For Server.m2d the root is the folder that holds "table/Server/".
public class XmlDirectorySource : IXmlSource {
private readonly string root;

public XmlDirectorySource(string root) {
this.root = Path.GetFullPath(root);
if (!Directory.Exists(this.root)) {
throw new DirectoryNotFoundException($"XML source folder does not exist: {this.root}");
}
}

public string GetString(string path) {
string result = Encoding.UTF8.GetString(System.IO.File.ReadAllBytes(Resolve(path)));
if (result.Length > 0 && result[0] == '') {
return result.Substring(1);
}
return result;
}

// Read into memory first, as M2dReader does, because parsers do not dispose the reader and
// an open file handle would otherwise outlive the parse.
public XmlReader GetXmlReader(string path) {
return XmlReader.Create(new MemoryStream(System.IO.File.ReadAllBytes(Resolve(path))));
}

private string Resolve(string path) {
string full = Path.GetFullPath(Path.Combine(root, path));
string prefix = root.EndsWith(Path.DirectorySeparatorChar.ToString()) ? root : root + Path.DirectorySeparatorChar;
if (!full.StartsWith(prefix, StringComparison.Ordinal)) {
throw new ArgumentException($"Path resolves outside the source folder: {path}", nameof(path));
}
if (!System.IO.File.Exists(full)) {
throw new FileNotFoundException($"XML source has no file {path}", full);
}
return full;
}
}
}
2 changes: 1 addition & 1 deletion Maple2.File.Parser/Maple2.File.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageTags>MapleStory2, File, Parser, m2d, xml</PackageTags>
<!-- Use following lines to write the generated files to disk. -->
<EmitCompilerGeneratedFiles Condition=" '$(Configuration)' == 'Debug' ">true</EmitCompilerGeneratedFiles>
<PackageVersion>2.4.41</PackageVersion>
<PackageVersion>2.4.42</PackageVersion>
<TargetFramework>net8.0</TargetFramework>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Loading
Loading