Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BG3: Add support for serializing and deserializing modsettings.lsx Load Order file #2149

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement ModSettings.lsx bg3 load order serialization + tests
  • Loading branch information
Al12rs committed Oct 9, 2024

Verified

This commit was signed with the committer’s verified signature.
commit 45004797690924ca3d7efd72250025d97c608819
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using System.Xml;

namespace NexusMods.Games.Larian.BaldursGate3.Utils.PakParsing;
Expand Down Expand Up @@ -35,32 +36,19 @@ public struct ModuleShortDesc
/// </summary>
public static string SerializeModuleShortDesc(ModuleShortDesc moduleShortDesc)
{
var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
};

using var stringWriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true}))
using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
{
xmlWriter.WriteStartElement("node");
xmlWriter.WriteAttributeString("id", "ModuleShortDesc");

WriteAttribute(xmlWriter, "Folder", "LSString", moduleShortDesc.Folder);
WriteAttribute(xmlWriter, "MD5", "LSString", moduleShortDesc.Md5);
WriteAttribute(xmlWriter, "Name", "LSString", moduleShortDesc.Name);
WriteAttribute(xmlWriter, "PublishHandle", "uint64", moduleShortDesc.PublishHandle);
WriteAttribute(xmlWriter, "UUID", "guid", moduleShortDesc.Uuid);
WriteAttribute(xmlWriter, "Version64", "int64", moduleShortDesc.Version);

xmlWriter.WriteEndElement();
ModsettingsFileWriter.WriteModuleShortDesc(xmlWriter, moduleShortDesc);
}

return stringWriter.ToString();

static void WriteAttribute(XmlWriter xmlWriter, string id, string type, string value)
{
xmlWriter.WriteStartElement("attribute");
xmlWriter.WriteAttributeString("id", id);
xmlWriter.WriteAttributeString("type", type);
xmlWriter.WriteAttributeString("value", value);
xmlWriter.WriteEndElement();
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Text;
using System.Xml;

namespace NexusMods.Games.Larian.BaldursGate3.Utils.PakParsing;

public static class ModsettingsFileWriter
{
public static string SerializeModsettingsLoadOrder(List<LsxXmlFormat.ModuleShortDesc> moduleShortDescs)
{
var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
};

using var stringWriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("save");

WriteVersion(xmlWriter);
WriteRegion(xmlWriter, moduleShortDescs);

xmlWriter.WriteEndElement(); // save
xmlWriter.WriteEndDocument();
}

return stringWriter.ToString();
}

private static void WriteVersion(XmlWriter xmlWriter)
{
xmlWriter.WriteStartElement("version");
xmlWriter.WriteAttributeString("major", "4");
xmlWriter.WriteAttributeString("minor", "7");
xmlWriter.WriteAttributeString("revision", "1");
xmlWriter.WriteAttributeString("build", "200");
xmlWriter.WriteEndElement();
}

private static void WriteRegion(XmlWriter xmlWriter, List<LsxXmlFormat.ModuleShortDesc> moduleShortDescs)
{
xmlWriter.WriteStartElement("region");
xmlWriter.WriteAttributeString("id", "ModuleSettings");

xmlWriter.WriteStartElement("node");
xmlWriter.WriteAttributeString("id", "root");

xmlWriter.WriteStartElement("children");
xmlWriter.WriteStartElement("node");
xmlWriter.WriteAttributeString("id", "Mods");

xmlWriter.WriteStartElement("children");

// Add default GustavDev entry
WriteModuleShortDesc(xmlWriter, new LsxXmlFormat.ModuleShortDesc
{
Folder = "GustavDev",
Name = "GustavDev",
PublishHandle = "0",
Version = "36028797018963968",
Uuid = "28ac9ce2-2aba-8cda-b3b5-6e922f71b6b8",
Md5 = ""
});

// Add pak mod entries
foreach (var moduleShortDesc in moduleShortDescs)
{
WriteModuleShortDesc(xmlWriter, moduleShortDesc);
}

xmlWriter.WriteEndElement(); // children
xmlWriter.WriteEndElement(); // node Mods
xmlWriter.WriteEndElement(); // children
xmlWriter.WriteEndElement(); // node root
xmlWriter.WriteEndElement(); // region
}

internal static void WriteModuleShortDesc(XmlWriter xmlWriter, LsxXmlFormat.ModuleShortDesc moduleShortDesc)
{
xmlWriter.WriteStartElement("node");
xmlWriter.WriteAttributeString("id", "ModuleShortDesc");

WriteAttribute(xmlWriter, "Folder", "LSString", moduleShortDesc.Folder);
WriteAttribute(xmlWriter, "MD5", "LSString", moduleShortDesc.Md5);
WriteAttribute(xmlWriter, "Name", "LSString", moduleShortDesc.Name);
WriteAttribute(xmlWriter, "PublishHandle", "uint64", moduleShortDesc.PublishHandle);
WriteAttribute(xmlWriter, "UUID", "guid", moduleShortDesc.Uuid);
WriteAttribute(xmlWriter, "Version64", "int64", moduleShortDesc.Version);

xmlWriter.WriteEndElement();
}

private static void WriteAttribute(XmlWriter xmlWriter, string id, string type, string value)
{
xmlWriter.WriteStartElement("attribute");
xmlWriter.WriteAttributeString("id", id);
xmlWriter.WriteAttributeString("type", type);
xmlWriter.WriteAttributeString("value", value);
xmlWriter.WriteEndElement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-16"?>
<save>
<version major="4" minor="7" revision="1" build="200" />
<region id="ModuleSettings">
<node id="root">
<children>
<node id="Mods">
<children>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="GustavDev" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="GustavDev" />
<attribute id="PublishHandle" type="uint64" value="0" />
<attribute id="UUID" type="guid" value="28ac9ce2-2aba-8cda-b3b5-6e922f71b6b8" />
<attribute id="Version64" type="int64" value="36028797018963968" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="AllItems" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="AllItems" />
<attribute id="PublishHandle" type="uint64" value="" />
<attribute id="UUID" type="guid" value="44c1adf4-9b2e-47e9-b20b-c090d837b98a" />
<attribute id="Version64" type="int64" value="1" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="Carry Weight Increased 9000" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="Carry Weight Increased 9000" />
<attribute id="PublishHandle" type="uint64" value="" />
<attribute id="UUID" type="guid" value="92af0ebd-a04c-4e9c-8306-b947aac4c127" />
<attribute id="Version64" type="int64" value="1" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="Dnd5rBardCollegeOfSwordsHomebrew_fb035e66-c916-8b33-a4cd-af40b86e4813" />
<attribute id="MD5" type="LSString" value="92ae5c2df98777c464300fb9c7a5e6d2" />
<attribute id="Name" type="LSString" value="DnD 5R Bard - College of Swords (Homebrew)" />
<attribute id="PublishHandle" type="uint64" value="4343434" />
<attribute id="UUID" type="guid" value="fb035e66-c916-8b33-a4cd-af40b86e4813" />
<attribute id="Version64" type="int64" value="36028797018963969" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="MetamagicExtendedQuickened2SP" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="MetamagicExtendedQuickened2SP" />
<attribute id="PublishHandle" type="uint64" value="" />
<attribute id="UUID" type="guid" value="46ad1e3b-34fa-4c89-967f-296b2e7dff80" />
<attribute id="Version64" type="int64" value="144115188075855872" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="MoreSpellSlotsAndFeats" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="MoreSpellSlotsAndFeats" />
<attribute id="PublishHandle" type="uint64" value="" />
<attribute id="UUID" type="guid" value="82ec86b3-8c32-4adb-b10c-789b68de3245" />
<attribute id="Version64" type="int64" value="36028799166447617" />
</node>
<node id="ModuleShortDesc">
<attribute id="Folder" type="LSString" value="WaypointInsideEmeraldGrove" />
<attribute id="MD5" type="LSString" value="" />
<attribute id="Name" type="LSString" value="Waypoint Inside Emerald Grove" />
<attribute id="PublishHandle" type="uint64" value="" />
<attribute id="UUID" type="guid" value="e342ee75-f7c9-4aeb-b6de-403991578337" />
<attribute id="Version64" type="int64" value="72057598332895232" />
</node>
</children>
</node>
</children>
</node>
</region>
</save>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using NexusMods.Games.Larian.BaldursGate3.Utils.PakParsing;
using NexusMods.Paths;

namespace NexusMods.Games.Larian.Tests.BaldursGate3;

public class BG3ModsettingsFileWriterTests
{
private readonly IFileSystem _fs;

public BG3ModsettingsFileWriterTests(IFileSystem fs)
{
_fs = fs;
}

[Fact]
public async Task SerializeModsettingsLoadOrder_VerifyTest()
{
var pakFolderPath = _fs.GetKnownPath(KnownPath.EntryDirectory).Combine("BaldursGate3/Resources/PakFiles/");
var pakFiles = Directory.GetFiles(pakFolderPath.ToString(), "*.pak");

var moduleShortDescs = new List<LsxXmlFormat.ModuleShortDesc>();

foreach (var pakFilePath in pakFiles)
{
await using var pakFileStream = File.OpenRead(pakFilePath);
LsxXmlFormat.MetaFileData metaFileData;
try
{
metaFileData = PakFileParser.ParsePakMeta(pakFileStream);
}
catch (InvalidDataException)
{
// Skip malformed pak test files
continue;
}
moduleShortDescs.Add(metaFileData.ModuleShortDesc);
}

// Sort the list by Name
moduleShortDescs = moduleShortDescs.OrderBy(m => m.Name).ToList();

var modsettingsString = ModsettingsFileWriter.SerializeModsettingsLoadOrder(moduleShortDescs);
await Verify(modsettingsString).UseParameters(pakFolderPath);
}
}