Changed config format from xml to toml

This commit is contained in:
strongleong 2022-02-13 00:09:27 +11:00
parent 73ccf99efc
commit 02d9df8160
3 changed files with 72 additions and 75 deletions

View file

@ -27,33 +27,37 @@ For now mod for Scrapland is a *.sm file that basically is a zip arhive with fol
| Filename | Description | | Filename | Description |
|--------------------|----------------------------------------------| |--------------------|----------------------------------------------|
| icon.png | Icon for mod that will show up in mod loader | | icon.png | Icon for mod that will show up in mod loader |
| config.xml | Information about mod | | config.toml | Information about mod |
| <filename\>.packed | Container with all mod game assets | | <filename\>.packed | Container with all mod game assets |
### meta.ini sample ### config.toml sample
```xml ```toml
<?xml version="1.0" encoding="UTF-8"?> title = "Mod title"
<ScrapMod> description = "Mod description"
<Title>Mod Title</Title> category = "Mod category"
<Description>Mod Desciption</Description> version = "1.0"
requiredLauncher = "1.0"
requiredGame = "1.0"
<Category>Category</Category> authors = [
<Version>1.0</Version> { name = "Author 1" },
<RequiredLauncher>1.0</RequiredLauncher> { name = "Author 2" }
<RequiredGame>1.1</RequiredGame> ]
<Author name="Author1" website="https://example.com" /> [[credits]]
<Author name="Author2" /> group = "Group 1"
credits = [
<Credits group="Mod author"> { name = "Author 1" },
<Credit name="Author1" /> { name = "Author 2" },
</Credits> { name = "Author 3" }
<Credits group="Some credit" > ]
<Credit name="Credit1" />
<Credit name="Credit2" /> [[credits]]
<Credit name="Credit3" /> group = "Group 2"
</Credits> credits = [
</ScrapMod> { name = "Author 3" },
{ name = "Author 4" }
]
``` ```
## TODO: ## TODO:

View file

@ -6,6 +6,8 @@ using System.Xml;
using Ionic.Zip; using Ionic.Zip;
using Tommy;
namespace ScrapModLoader namespace ScrapModLoader
{ {
public class ScrapMod public class ScrapMod
@ -25,7 +27,7 @@ namespace ScrapModLoader
public ScrapMod(String path) public ScrapMod(String path)
{ {
ModPath = path; ModPath = path;
Name = Path.GetFileNameWithoutExtension(path); Name = String.Empty;
Description = String.Empty; Description = String.Empty;
Icon = new BitmapImage(); Icon = new BitmapImage();
Checked = false; Checked = false;
@ -105,7 +107,7 @@ namespace ScrapModLoader
using (ZipFile zipFile = ZipFile.Read(path)) using (ZipFile zipFile = ZipFile.Read(path))
{ {
Byte[] iconBuffer = ExtractFromZip(zipFile, "icon.png"); Byte[] iconBuffer = ExtractFromZip(zipFile, "icon.png");
Byte[] confBuffer = ExtractFromZip(zipFile, "config.xml"); Byte[] confBuffer = ExtractFromZip(zipFile, "config.toml");
LoadIcon(iconBuffer); LoadIcon(iconBuffer);
LoadConfig(confBuffer); LoadConfig(confBuffer);
} }
@ -115,7 +117,7 @@ namespace ScrapModLoader
{ {
ZipEntry? entry = zip[entry_path]; ZipEntry? entry = zip[entry_path];
if (entry == null) if (entry == null)
throw new FileFormatException($"No '{entry}' in {Name} found"); throw new FileFormatException($"No '{entry_path}' in {Name} found");
Byte[] buffer = new Byte[entry.UncompressedSize]; Byte[] buffer = new Byte[entry.UncompressedSize];
using (MemoryStream zipStream = new MemoryStream(buffer)) using (MemoryStream zipStream = new MemoryStream(buffer))
@ -138,63 +140,53 @@ namespace ScrapModLoader
private void LoadConfig(Byte[] buffer) private void LoadConfig(Byte[] buffer)
{ {
using (MemoryStream sourceStream = new MemoryStream(buffer)) using (MemoryStream sourceStream = new MemoryStream(buffer))
using (StreamReader reader = new StreamReader(sourceStream))
{ {
XmlDocument xmlDocument = new XmlDocument(); TomlTable config = TOML.Parse(reader);
xmlDocument.Load(sourceStream);
ParseModInfoFromXml(xmlDocument);
ParseAuthorsFromXml(xmlDocument);
ParseCreditsFromXml(xmlDocument);
}
}
private void ParseModInfoFromXml(XmlDocument xmlDocument) CheckConfig(config);
{
Description = xmlDocument.GetElementsByTagName("Description").Item(0)?.InnerText ??
throw new FileFormatException("No 'Description' tag in 'config.xml'");
Category = xmlDocument.GetElementsByTagName("Category").Item(0)?.InnerText ??
throw new FileFormatException("No 'Category' tag in 'config.xml'");
Version = xmlDocument.GetElementsByTagName("Version").Item(0)?.InnerText ??
throw new FileFormatException("No 'Version' tag in 'config.xml'");
RequiredLauncher = xmlDocument.GetElementsByTagName("RequiredLauncher").Item(0)?.InnerText ??
throw new FileFormatException("No 'RequiredLauncher' tag in 'config.xml'");
RequiredGame = xmlDocument.GetElementsByTagName("RequiredGame").Item(0)?.InnerText ??
throw new FileFormatException("No 'RequiredGame' tag in 'config.xml'");
}
private void ParseAuthorsFromXml(XmlDocument xmlDocument) Name = config["title"];
{ Description = config["description"];
XmlNodeList authors = xmlDocument.GetElementsByTagName("Author"); Category = config["category"];
foreach (XmlNode author in authors) Version = config["version"];
{ RequiredLauncher = config["requiredLauncher"];
XmlAttribute? nameAttr = author.Attributes?["name"]; RequiredGame = config["requiredGame"];
if (nameAttr == null)
throw new FileFormatException("No 'name' attribute in 'Author' tag in 'config.xml'");
Authors.Add(nameAttr.InnerText);
}
}
private void ParseCreditsFromXml(XmlDocument xmlDocument) foreach (TomlNode author in config["authors"])
{ Authors.Add(author["name"]);
XmlNodeList credits = xmlDocument.GetElementsByTagName("Credits");
foreach (XmlNode credit in credits)
{
List<String> entries = new List<String>();
XmlAttribute? groupAttr = credit.Attributes?["group"]; foreach (TomlNode credit in config["credits"])
if (groupAttr == null)
throw new FileFormatException("No 'group' attribute in 'Credits' tag in 'config.xml'");
foreach (XmlNode entry in credit)
{ {
XmlAttribute? nameAttr = entry.Attributes?["name"]; List<String> entries = new List<String>();
if (nameAttr == null)
throw new FileFormatException("No 'name' attribute in 'Author' tag in 'config.xml'");
entries.Add(nameAttr.InnerText); foreach (TomlNode entry in credit["credits"])
entries.Add(entry["name"]);
Credits.Add(credit["group"], entries);
} }
Credits.Add(groupAttr.InnerText, entries);
} }
} }
private static void CheckConfig(TomlTable config)
{
if (!config.HasKey("title"))
throw new FileFormatException("No 'title' key in 'config.toml'");
if (!config.HasKey("description"))
throw new FileFormatException("No 'description' key in 'config.toml'");
if (!config.HasKey("category"))
throw new FileFormatException("No 'category' key in 'config.toml'");
if (!config.HasKey("version"))
throw new FileFormatException("No 'version' key in 'config.toml'");
if (!config.HasKey("requiredLauncher"))
throw new FileFormatException("No 'name' key in 'config.toml'");
if (!config.HasKey("requiredGame"))
throw new FileFormatException("No 'requiredGame' key in 'config.toml'");
}
} }
} }

View file

@ -16,6 +16,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetZip" Version="1.16.0" /> <PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="Tommy" Version="3.1.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>