@@ -104,6 +104,25 @@ uint32_t Checksum
104104 public List < TileElement > [ , ] ? TileElementMap { get ; set ; }
105105 byte [ ] OriginalTileElementData { get ; set ; } = [ ] ;
106106
107+ public ( int Width , int Height ) GetMapSize ( )
108+ => GetMapSize ( SaveDetails , ScenarioOptions ) ;
109+
110+ public static ( int Width , int Height ) GetMapSize ( SaveDetails saveDetails , ScenarioOptions scenarioOptions )
111+ {
112+ if ( saveDetails != null )
113+ {
114+ return ( saveDetails . MapSizeX , saveDetails . MapSizeY ) ;
115+ }
116+ else if ( scenarioOptions != null )
117+ {
118+ return ( scenarioOptions . MapSizeX , scenarioOptions . MapSizeY ) ;
119+ }
120+ else
121+ {
122+ return ( Limits . kMapColumnsVanilla , Limits . kMapRowsVanilla ) ;
123+ }
124+ }
125+
107126 public IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
108127 => [ ] ;
109128
@@ -215,6 +234,8 @@ public static S5File Read(ReadOnlySpan<byte> data)
215234 byte [ ] tileElementData = [ ] ;
216235 IGameState gameState ;
217236
237+ var mapSize = GetMapSize ( saveDetails , scenarioOptions ) ;
238+
218239 if ( header . Type == S5FileType . Scenario )
219240 {
220241 var gameStateA = SawyerStreamReader . ReadChunk < GameStateScenarioA > ( ref data ) ;
@@ -228,7 +249,7 @@ public static S5File Read(ReadOnlySpan<byte> data)
228249 if ( gameStateA . GameStateFlags . HasFlag ( GameStateFlags . TileManagerLoaded ) )
229250 {
230251 tileElementData = SawyerStreamReader . ReadChunkCore ( ref data ) . ToArray ( ) ;
231- ( tileElements , tileElementMap ) = ParseTileElements ( tileElementData ) ;
252+ ( tileElements , tileElementMap ) = ParseTileElements ( tileElementData , mapSize . Width , mapSize . Height ) ;
232253 }
233254 }
234255 else
@@ -237,7 +258,7 @@ public static S5File Read(ReadOnlySpan<byte> data)
237258 FixState ( ) ;
238259
239260 tileElementData = SawyerStreamReader . ReadChunkCore ( ref data ) . ToArray ( ) ;
240- ( tileElements , tileElementMap ) = ParseTileElements ( tileElementData ) ;
261+ ( tileElements , tileElementMap ) = ParseTileElements ( tileElementData , mapSize . Width , mapSize . Height ) ;
241262 }
242263
243264 var checksum = BitConverter . ToUInt32 ( data [ 0 ..4 ] ) ;
@@ -249,12 +270,12 @@ public static S5File Read(ReadOnlySpan<byte> data)
249270 static void FixState ( )
250271 { }
251272
252- static ( List < TileElement > , List < TileElement > [ , ] ) ParseTileElements ( ReadOnlySpan < byte > tileElementData )
273+ static ( List < TileElement > , List < TileElement > [ , ] ) ParseTileElements ( ReadOnlySpan < byte > tileElementData , int mapWidth , int mapHeight )
253274 {
254275 var numTileElements = tileElementData . Length / TileElement . StructLength ;
255276
256277 List < TileElement > tileElements = [ ] ;
257- var tileElementMap = new List < TileElement > [ Limits . kMapColumns , Limits . kMapRows ] ;
278+ var tileElementMap = new List < TileElement > [ mapWidth , mapHeight ] ;
258279
259280 var x = 0 ;
260281 var y = 0 ;
@@ -275,12 +296,12 @@ static void FixState()
275296
276297 if ( el . IsLast ( ) )
277298 {
278- if ( x == Limits . kMapColumns - 1 )
299+ if ( x == mapWidth - 1 )
279300 {
280- y = ( y + 1 ) % Limits . kMapRows ;
301+ y = ( y + 1 ) % mapHeight ;
281302 }
282303
283- x = ( x + 1 ) % Limits . kMapColumns ;
304+ x = ( x + 1 ) % mapWidth ;
284305 }
285306
286307 // el.IsLast() indicates its the last element on that tile
0 commit comments