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

Add support for "Groups" tag in Info.xml #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions src/definitions/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DefaultFomodDocumentConfig, FomodDocumentConfig } from "./lib/FomodDocu
import { ElementObjectMap, XmlRepresentation } from "./lib/XmlRepresentation";

export interface FomodInfoData {
[key: string]: string|undefined;
[key: string]: string|string[]|undefined;

/** MO2 reads and displays this field in the installer dialog. Vortex reads but does not use it. */
Name?: string;
Expand All @@ -21,6 +21,9 @@ export interface FomodInfoData {
/** MO2 reads, displays, and potentially stores this field. There are no restrictions on its value. */
Version?: string;

/** An array of strings (categories) */
Groups?: string[];

}

export const DefaultInfoSchema = 'https://fomod.bellcube.dev/schemas/Info.xsd';
Expand Down Expand Up @@ -54,7 +57,17 @@ export class FomodInfo extends XmlRepresentation<boolean> {
if (value === undefined) continue;

const child = document.createElement(key);
child.textContent = value;

if (Array.isArray(value)) {
for (const subChildValue of value) {
// Append "element" tag to child, with the value
const subChild = document.createElement('element');
subChild.textContent = subChildValue;
child.appendChild(subChild);
}
} else {
child.textContent = value;
}
Comment on lines +60 to +70
Copy link
Owner

Choose a reason for hiding this comment

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

It may be worth investigating how existing tooling and mod managers handle multi-child elements in Info.xml. I have little knowledge on the subject personally, though the Fomod Creation Tool may be a good place to look if memory serves. I suspect we'll end up creating a class for more complex representations sometime down the line, even if just to handle the element name.


element.appendChild(child);
}
Expand All @@ -80,7 +93,20 @@ export class FomodInfo extends XmlRepresentation<boolean> {

for (const child of element.children) {
if (child.textContent === null) continue;
data[child.tagName] = child.textContent;

if (child.children.length > 0) {
const filteredChildren: string[] = [];

for (const subchild of child.children) {
if (typeof subchild.textContent === 'string') {
filteredChildren.push(subchild.textContent);
}
}

data[child.tagName] = filteredChildren;
} else {
data[child.tagName] = child.textContent;
}
}

const obj = new FomodInfo(data);
Expand Down
8 changes: 8 additions & 0 deletions tests/parse-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ describe('Info.xml Got Parsed', () => {
<Id>1</Id>
<Website>https://example.com</Website>
<Version>1.0.0</Version>
<Groups>
<element>category 1</element>
<element>testGroup2</element>
</Groups>
</fomod>`, {contentType: 'text/xml'}).window.document;

const infoObj = parseInfoDoc(infoDoc);
Expand Down Expand Up @@ -189,6 +193,10 @@ describe('Info.xml Got Parsed', () => {
expect(infoObj.data.Version).toBe('1.0.0');
});

test('Groups Is Correct', () => {
expect(infoObj.data.Groups).toStrictEqual(['category 1', 'testGroup2']);
});

describe('Converted Back Into Element Properly', () => {
const newElement = infoObj.asElement(new JSDOM('<config />', {contentType: 'text/xml'}).window.document);

Expand Down