fixed GSA writing, added always-edit button and search in file and for file

This commit is contained in:
2026-07-21 15:50:52 +02:00
parent cbf0891ba4
commit d171c34236
42 changed files with 538 additions and 61 deletions

View File

@ -1,36 +1,62 @@
using System.Xml.Linq;
using System.Text;
using GsaEditor.Core.Models;
namespace GsaEditor.Core.IO;
/// <summary>
/// Writes a .idx NML index file from a <see cref="GsaArchive"/>.
/// The index file is XML-compatible and lists each entry with its alias, compression method,
/// sizes, and data offset for fast lookup without a full sequential scan.
/// The NML format is the engine's own tag syntax (NOT standard XML):
/// <code>
/// &lt;Version=1.0&gt;
/// &lt;Index=
/// &lt;Entry=
/// &lt;ID="path/in/archive"&gt;
/// &lt;CompLen=123&gt;
/// &lt;Len=456&gt;
/// &lt;Offset=789&gt;
/// &lt;Method="Zlib"&gt;
/// &gt;
/// &gt;
/// </code>
/// Tab indentation, LF line endings, no BOM. Method is "Zlib" or "Raw".
/// The engine parses this file to locate entries, so the format must match exactly.
/// </summary>
public static class GsaIndexWriter
{
/// <summary>
/// The engine's bootstrap script is loaded by sequential archive scan and is
/// deliberately absent from the shipped index files, so it is skipped here too.
/// </summary>
private const string BootstrapAlias = "bootstrap.txt";
/// <summary>
/// Writes the index file for the given archive to the specified path.
/// Entry offsets must be up to date (i.e. call after <see cref="GsaWriter.Write(GsaArchive, string)"/>).
/// </summary>
/// <param name="archive">The archive whose entries will be indexed.</param>
/// <param name="filePath">The output .idx file path.</param>
public static void Write(GsaArchive archive, string filePath)
{
var doc = new XDocument(
new XElement("Index",
archive.Entries.Select(e =>
new XElement("Entry",
new XElement("Id", e.Alias),
new XElement("Method", e.IsCompressed ? 1 : 0),
new XElement("Len", e.OriginalLength),
new XElement("CompLen", e.CompressedLength),
new XElement("Offset", e.DataOffset)
)
)
)
);
var sb = new StringBuilder();
sb.Append("<Version=1.0>\n");
sb.Append("<Index=\n");
doc.Save(filePath);
foreach (var e in archive.Entries)
{
if (string.Equals(e.Alias, BootstrapAlias, StringComparison.OrdinalIgnoreCase))
continue;
sb.Append("\t<Entry=\n");
sb.Append("\t\t<ID=\"").Append(e.Alias).Append("\">\n");
sb.Append("\t\t<CompLen=").Append(e.CompressedLength).Append(">\n");
sb.Append("\t\t<Len=").Append(e.OriginalLength).Append(">\n");
sb.Append("\t\t<Offset=").Append(e.DataOffset).Append(">\n");
sb.Append("\t\t<Method=\"").Append(e.IsCompressed ? "Zlib" : "Raw").Append("\">\n");
sb.Append("\t>\n");
}
sb.Append(">\n");
File.WriteAllText(filePath, sb.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}
}