Skip to content

Commit

Permalink
Fix crash when loading a terrain generator preset that contained a no…
Browse files Browse the repository at this point in the history
…nexistent TileSet
  • Loading branch information
Rampastring committed Aug 28, 2023
1 parent fcd2e94 commit bc7370c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/TSMapEditor/Mutations/Classes/TerrainGenerationMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ public static TerrainGeneratorConfiguration FromConfigSection(IniSection section

var terrainTypeGroup = TerrainGeneratorTerrainTypeGroup.FromConfigString(terrainTypes, value);
if (terrainTypeGroup == null)
continue;

terrainTypeGroups.Add(terrainTypeGroup);
{
Logger.Log($"Failed to load terrain type group #{i} from terrain generator configuration section {section.SectionName}. User preset: {isUserConfiguration}");
}
else
{
terrainTypeGroups.Add(terrainTypeGroup);
}

i++;
}
Expand All @@ -110,9 +114,13 @@ public static TerrainGeneratorConfiguration FromConfigSection(IniSection section

var tileGroup = TerrainGeneratorTileGroup.FromConfigString(tilesets, value);
if (tileGroup == null)
continue;

tileGroups.Add(tileGroup);
{
Logger.Log($"Failed to load tile group #{i} from terrain generator configuration section {section.SectionName}. User preset: {isUserConfiguration}");
}
else
{
tileGroups.Add(tileGroup);
}

i++;
}
Expand All @@ -126,9 +134,13 @@ public static TerrainGeneratorConfiguration FromConfigSection(IniSection section

var overlayGroup = TerrainGeneratorOverlayGroup.FromConfigString(overlayTypes, value);
if (overlayGroup == null)
continue;

overlayGroups.Add(overlayGroup);
{
Logger.Log($"Failed to load overlay group #{i} from terrain generator configuration section {section.SectionName}. User preset: {isUserConfiguration}");
}
else
{
overlayGroups.Add(overlayGroup);
}

i++;
}
Expand All @@ -142,9 +154,13 @@ public static TerrainGeneratorConfiguration FromConfigSection(IniSection section

var smudgeGroup = TerrainGeneratorSmudgeGroup.FromConfigString(smudgeTypes, value);
if (smudgeGroup == null)
continue;

smudgeGroups.Add(smudgeGroup);
{
Logger.Log($"Failed to load smudge group #{i} from terrain generator configuration section {section.SectionName}. User preset: {isUserConfiguration}");
}
else
{
smudgeGroups.Add(smudgeGroup);
}

i++;
}
Expand Down Expand Up @@ -201,7 +217,7 @@ public class TerrainGeneratorTileGroup
{
public TerrainGeneratorTileGroup(TileSet tileSet, List<int> tileIndicesInSet, double openChance, double overlapChance)
{
TileSet = tileSet;
TileSet = tileSet ?? throw new ArgumentNullException(nameof(tileSet));
TileIndicesInSet = tileIndicesInSet;
OpenChance = openChance;
OverlapChance = overlapChance;
Expand Down Expand Up @@ -233,6 +249,9 @@ public static TerrainGeneratorTileGroup FromConfigString(List<TileSet> allTileSe
double openChance = Conversions.DoubleFromString(parts[0], 0.0);
double overlapChance = Conversions.DoubleFromString(parts[1], 0.0);
var tileSet = allTileSets.Find(ts => ts.AllowToPlace && ts.LoadedTileCount > 0 && ts.SetName == parts[2]);
if (tileSet == null)
return null;

List<int> tileIndices = null;
if (parts.Length > 3)
{
Expand Down
1 change: 1 addition & 0 deletions src/TSMapEditor/Settings/TerrainGeneratorUserPresets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Load()
break;

var config = TerrainGeneratorConfiguration.FromConfigSection(iniSection, map.Rules, map.TheaterInstance.Theater, true);

if (config != null)
configurations.Add(config);
else
Expand Down

0 comments on commit bc7370c

Please sign in to comment.