Improve toml reading logic

If there's an error reading the toml, give an error rather than showing the crash dialog.
This commit is contained in:
haven1433 2022-10-02 00:16:15 -05:00
parent 0f3748419b
commit 6de8951d6a
2 changed files with 17 additions and 8 deletions

View File

@ -163,8 +163,8 @@ namespace HavenSoft.HexManiac.Core.Models {
currentItemNote = cleanLine.Split("'''")[1];
} else if (cleanLine.StartsWith("Image = '''")) {
currentItemImage = cleanLine.Split("'''")[1];
} else if (cleanLine.StartsWith("Destination = '''")) {
currentItemDestination = cleanLine.Split("'''")[1];
} else if (cleanLine.StartsWith("Destination =")) {
currentItemDestination = cleanLine.Split("=")[1].Trim('\'', ' ');
}
if (cleanLine.StartsWith("ApplicationVersion = '''")) {
@ -378,26 +378,26 @@ namespace HavenSoft.HexManiac.Core.Models {
if (currentItem == "[[List]]") {
if (currentItemName == null) throw new ArgumentNullException("The Metadata file has a list that didn't specify a name!");
if (currentItemChildren == null) throw new ArgumentNullException("The Metadata file has a list that didn't specify any children!");
if (currentItemChildren == null) throw new ArgumentNullException($"List {currentItemName} didn't specify any children!");
lists.Add(new StoredList(currentItemName, currentItemChildren, currentItemHash));
}
if (currentItem == "[[UnmappedConstant]]") {
if (currentItemName == null) throw new ArgumentNullException("The Metadata file has an unmapped constant that didn't specify a name!");
if (currentItemValue == -1) throw new ArgumentNullException("The Metadata file has an unmapped constant that didn't specify a value!");
if (currentItemValue == -1) throw new ArgumentNullException($"Unmapped Constant {currentItemName} didn't specify a value!");
unmappedConstants.Add(new StoredUnmappedConstant(currentItemName, currentItemValue));
}
if (currentItem == "[[GotoShortcut]]") {
if (currentItemName == null) throw new ArgumentNullException("The Metadata file has a Goto Shortcut that didn't specify a Name!");
if (currentItemImage == null) throw new ArgumentNullException("The Metadata file has a Goto Shortcut that didn't specify an Image!");
if (currentItemDestination == null) throw new ArgumentNullException("The Metadata file has a Goto Shortcut that didn't specify a Destination!");
if (currentItemImage == null) throw new ArgumentNullException($"Goto Shortcut {currentItemName} didn't specify an Image!");
if (currentItemDestination == null) throw new ArgumentNullException($"Goto Shortcut {currentItemName} didn't specify a Destination!");
gotoShortcuts.Add(new StoredGotoShortcut(currentItemName, currentItemImage, currentItemDestination));
}
if (currentItem == "[[TableGroup]]") {
if (currentItemName == null) throw new ArgumentNullException("The Metadata file has a TableGroup that didn't specify a name!");
if (currentItemChildren == null) throw new ArgumentNullException("The Metadat file has a TableGroup that didn't specify any children!");
if (currentItemChildren == null) throw new ArgumentNullException($"TableGroup {currentItemName} didn't specify any children!");
tableGroups.Add(new(currentItemName, currentItemChildren, currentItemHash));
}

View File

@ -709,7 +709,16 @@ namespace HavenSoft.HexManiac.Core.ViewModels {
if (allowLoadingMetadata) {
metadataText = fileSystem.MetadataFor(file.Name) ?? new string[0];
}
var metadata = new StoredMetadata(metadataText);
StoredMetadata metadata;
try {
metadata = new StoredMetadata(metadataText);
} catch (ArgumentNullException nullEx) {
ErrorMessage = nullEx.Message;
return;
} catch (ArgumentOutOfRangeException rangeEx) {
ErrorMessage = rangeEx.Message;
return;
}
IDataModel model = file.Name.ToLower().EndsWith(".gba") ?
new HardcodeTablesModel(Singletons, file.Contents, metadata) :
new PokemonModel(file.Contents, metadata, Singletons);