Reject invalid terrain.png (non square, non power of 2) instead of crashing. (Thanks 123DMWM)

This commit is contained in:
UnknownShadow200 2016-05-06 13:19:11 +10:00
parent ca0b9cc038
commit f63ca316d7
4 changed files with 16 additions and 7 deletions

View File

@ -34,9 +34,16 @@ namespace ClassicalSharp {
TerrainAtlas1D.UpdateState( TerrainAtlas );
}
public void ChangeTerrainAtlas( Bitmap newAtlas ) {
LoadAtlas( newAtlas );
public bool ChangeTerrainAtlas( Bitmap atlas ) {
bool pow2 = Utils.IsPowerOf2( atlas.Width ) && Utils.IsPowerOf2( atlas.Height );
if( !pow2 || atlas.Width != atlas.Height ) {
Chat.Add( "&cCurrent texture pack has an invalid terrain.png" );
Chat.Add( "&cWidth and length must be the same, and also powers of two." );
return false;
}
LoadAtlas( atlas );
Events.RaiseTerrainAtlasChanged();
return true;
}
public void Run() { window.Run(); }

View File

@ -134,7 +134,7 @@ namespace ClassicalSharp {
Utils.LogDebug( "Converting terrain atlas to 32bpp image" );
game.Drawer2D.ConvertTo32Bpp( ref bmp );
}
game.ChangeTerrainAtlas( bmp );
if( !game.ChangeTerrainAtlas( bmp ) ) { bmp.Dispose(); return; }
TextureCache.AddToCache( item.Url, bmp );
TextureCache.AddETagToCache( item.Url, item.ETag, game.ETags );
} else if( Is304Status( item.WebEx ) ) {
@ -143,7 +143,7 @@ namespace ClassicalSharp {
ExtractDefault();
} else if( item.Url != game.World.TextureUrl ) {
game.Animations.Dispose();
game.ChangeTerrainAtlas( bmp );
if( !game.ChangeTerrainAtlas( bmp ) ) { bmp.Dispose(); return; }
}
if( bmp != null ) game.World.TextureUrl = item.Url;

View File

@ -35,8 +35,8 @@ namespace ClassicalSharp.TexturePack {
public void Tick( double delta ) {
if( animations.Count == 0 ) return;
if( fastBmp == null ) {
game.Chat.Add( "&cCurrent texture pack specifies it uses animations,");
game.Chat.Add( "&cbut it is missing animations.png");
game.Chat.Add( "&cCurrent texture pack specifies it uses animations," );
game.Chat.Add( "&cbut is missing animations.png" );
animations.Clear();
return;
}

View File

@ -43,7 +43,9 @@ namespace ClassicalSharp.TexturePack {
switch( filename ) {
case "terrain.png":
game.ChangeTerrainAtlas( Platform.ReadBmp( stream ) ); break;
Bitmap atlas = Platform.ReadBmp( stream );
if( !game.ChangeTerrainAtlas( atlas ) ) atlas.Dispose();
break;
case "chicken.png":
UpdateTexture( ref cache.ChickenTexId, stream, false ); break;
case "creeper.png":