Skip to content

Commit

Permalink
ImportGen2Map: Fix imports of malformed maps.
Browse files Browse the repository at this point in the history
Fixes #21126
  • Loading branch information
tinix0 committed Nov 14, 2023
1 parent 9a5f5f9 commit caf2905
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
37 changes: 25 additions & 12 deletions OpenRA.Game/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,19 +776,10 @@ public byte[] SavePreview()

if (Grid.MaximumTerrainHeight > 0)
{
// The minimap is drawn in cell space, so we need to
// unproject the PPos bounds to find the MPos boundaries.
// This matches the calculation in RadarWidget that is used ingame
for (var x = Bounds.Left; x < Bounds.Right; x++)
{
var allTop = Unproject(new PPos(x, Bounds.Top));
var allBottom = Unproject(new PPos(x, Bounds.Bottom));
if (allTop.Count > 0)
top = Math.Min(top, allTop.MinBy(uv => uv.V).V);
(top, bottom) = GetCellSpaceBounds();

if (allBottom.Count > 0)
bottom = Math.Max(bottom, allBottom.MaxBy(uv => uv.V).V);
}
if (top == int.MaxValue || bottom == int.MinValue)
throw new InvalidDataException("The map has invalid boundaries");
}
else
{
Expand Down Expand Up @@ -864,6 +855,28 @@ public byte[] SavePreview()
return png.Save();
}

public (int Top, int Bottom) GetCellSpaceBounds()
{
var top = int.MaxValue;
var bottom = int.MinValue;

// The minimap is drawn in cell space, so we need to
// unproject the PPos bounds to find the MPos boundaries.
// This matches the calculation in RadarWidget that is used ingame
for (var x = Bounds.Left; x < Bounds.Right; x++)
{
var allTop = Unproject(new PPos(x, Bounds.Top));
var allBottom = Unproject(new PPos(x, Bounds.Bottom));
if (allTop.Count > 0)
top = Math.Min(top, allTop.MinBy(uv => uv.V).V);

if (allBottom.Count > 0)
bottom = Math.Max(bottom, allBottom.MaxBy(uv => uv.V).V);
}

return (top, bottom);
}

public bool Contains(CPos cell)
{
if (Grid.Type == MapGridType.RectangularIsometric)
Expand Down
25 changes: 23 additions & 2 deletions OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,30 @@ protected virtual void SetInteractableBounds(Map map, int[] iniBounds)
// Calculate Bounds edge tile coordinates.
// Reduce bottom and right by 1 because map.SetBounds() increases them.
var topLeft = new PPos(left, top);
var bottomRight = new PPos(playableAreaWidth + left - 1, playableAreaHeight + unknownHeightPadding + top - 1);

map.SetBounds(topLeft, bottomRight);
if (map.Height.Max() != 0)
{
// Workaround: Some custom TS maps have invalid boundaries, with no padding area at the bottom
// The boundary needs to be reduced until the projections are valid
bool hasValidBoundaries;
var paddingOffset = 1;
do
{
var bottomRight = new PPos(playableAreaWidth + left - 1, playableAreaHeight + unknownHeightPadding + top - paddingOffset);
map.SetBounds(topLeft, bottomRight);

var (topBound, bottomBound) = map.GetCellSpaceBounds();

hasValidBoundaries = topBound != int.MaxValue && bottomBound != int.MinValue;
paddingOffset++;
}
while (!hasValidBoundaries);
}
else
{
var bottomRight = new PPos(playableAreaWidth + left - 1, playableAreaHeight + unknownHeightPadding + top - 1);
map.SetBounds(topLeft, bottomRight);
}
}

protected virtual bool TryHandleOverlayToActorInner(CPos cell, byte[] overlayPack, CellLayer<int> overlayIndex, byte overlayType, out ActorReference actorReference)
Expand Down
7 changes: 6 additions & 1 deletion OpenRA.Mods.Common/FileFormats/IniFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public void Load(Stream s)
{
case ';': break;
case '[': currentSection = ProcessSection(line); break;
default: ProcessEntry(line, currentSection); break;
default:
// Skip everything before the first section
if (currentSection != null)
ProcessEntry(line, currentSection);

break;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Utility/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"profiles": {
"OpenRA.Utility": {
"commandName": "Project",
"commandLineArgs": "all --docs {DEV_VERSION}",
"commandLineArgs": "ts --import-ts-map \"E:\\Downloads\\Failed\\trauma.map\"",
"environmentVariables": {
"ENGINE_DIR": ".."
}
}
}
}
}

0 comments on commit caf2905

Please sign in to comment.