Skip to content

feat: LSX (XML) parsing typesafe#19

Merged
revblaze merged 4 commits into
mainfrom
feat-lsxparsing-typesafe
Jan 9, 2024
Merged

feat: LSX (XML) parsing typesafe#19
revblaze merged 4 commits into
mainfrom
feat-lsxparsing-typesafe

Conversation

@revblaze

@revblaze revblaze commented Jan 8, 2024

Copy link
Copy Markdown
Owner

Alongside creating a backup of each modsettings.lsx file, we also need to parse its contents for attribute values. We not only need to do this on first backup, but every backup henceforth due to the possibility that BG3 may update these values with future patches. Finally, we also need to replicate the GustavDev embedded attributes within the ModuleShortDesc as its values may change as well–this goes especially for associated values to attribute IDs UUID and Version64.

Input sample default modsettings.lsx from BG3 version 4.1.1.4251417.

<?xml version="1.0" encoding="UTF-8"?>
<save>
    <version major="4" minor="4" revision="0" build="300"/>
    <region id="ModuleSettings">
        <node id="root">
            <children>
                <node id="ModOrder"/>
                <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="UUID" type="FixedString" value="28ac9ce2-2aba-8cda-b3b5-6e922f71b6b8"/>
                            <attribute id="Version64" type="int64" value="36028797018963968"/>
                        </node>
                    </children>
                </node>
            </children>
        </node>
    </region>
</save>

We will create our own XMLAttributes structure to store these values:

struct XMLAttributes {
  var version: Version
  var moduleShortDesc: ModuleShortDesc
  
  struct Version {
    var majorString: String
    var minorString: String
    var revisionString: String
    var buildString: String
  }
  
  struct ModuleShortDesc {
    var folder: Attribute
    var md5: Attribute
    var name: Attribute
    var uuid: Attribute
    var version64: Attribute
    
    struct Attribute {
      var typeString: String
      var valueString: String
    }
  }
}

Our LsxParserDelegate class will then extract this data, storing them into (kinda) "type-safe(ish)" variables. From there, we can call them as such to help re-generate the modsettings.lsx file anew:

XML version Header

let majorVersion = xmlAttrs.version.majorString
let minorVersion = xmlAttrs.version.minorString
let revisionVersion = xmlAttrs.version.revisionString
let buildVersion = xmlAttrs.version.buildString

let versionXmlString = 
"""
<version major="\(majorVersion)" minor="\(minorVersion)" revision="\(revisionVersion)" build="\(buildVersion)"/>
"""

print(versionXmlString)

Output:

<version major="4" minor="4" revision="0" build="300"/>

XML ModuleShortDesc Child Nodes

let gustavDevGeneratedAttributes = 
"""
<attribute id="Folder" type="\(gustavDevModule.folder.typeString)" value="\(gustavDevModule.folder.valueString)"/>
<attribute id="MD5" type="\(gustavDevModule.md5.typeString)" value="\(gustavDevModule.md5.valueString)"/>
<attribute id="Name" type="\(gustavDevModule.name.typeString)" value="\(gustavDevModule.name.valueString)"/>
<attribute id="UUID" type="\(gustavDevModule.uuid.typeString)" value="\(gustavDevModule.uuid.valueString)"/>
<attribute id="Version64" type="\(gustavDevModule.version64.typeString)" value="\(gustavDevModule.version64.valueString)"/>
"""

print(gustavDevGeneratedAttributes)

Output:

<attribute id="Folder" type="LSString" value="GustavDev"/>
<attribute id="MD5" type="LSString" value=""/>
<attribute id="Name" type="LSString" value="GustavDev"/>
<attribute id="UUID" type="FixedString" value="28ac9ce2-2aba-8cda-b3b5-6e922f71b6b8"/>
<attribute id="Version64" type="int64" value="36028797018963968"/>

@revblaze revblaze left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed.

@revblaze revblaze left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested.

@revblaze revblaze left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

@revblaze revblaze merged commit 0f44205 into main Jan 9, 2024
@revblaze revblaze deleted the feat-lsxparsing-typesafe branch January 9, 2024 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant