git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1199 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
chrisf
2007-07-13 13:06:41 +00:00
parent 0293544f89
commit 37b0d0cb57
11 changed files with 165 additions and 248 deletions

View File

@@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace OpenRa.FileFormats
{
public static class BitmapBuilder
{
public static Bitmap FromBytes(byte[] imageBytes, Size size, Palette pal)
{
Bitmap bitmap = new Bitmap(size.Width, size.Height);
for (int x = 0; x < size.Width; x++)
for (int y = 0; y < size.Height; y++)
bitmap.SetPixel(x, y, pal.GetColor(imageBytes[x + size.Width * y]));
return bitmap;
}
}
}

View File

@@ -19,65 +19,69 @@ namespace OpenRa.FileFormats
public readonly TileReference[ , ] MapTiles = new TileReference[ 128, 128 ];
public readonly List<TreeReference> Trees = new List<TreeReference>();
public Map( IniFile file )
static string Truncate( string s, int maxLength )
{
IniSection basic = file.GetSection( "Basic" );
Title = basic.GetValue( "Name", "(null)" );
IniSection map = file.GetSection( "Map" );
Theater = map.GetValue( "Theater", "TEMPERATE" );
XOffset = int.Parse( map.GetValue( "X", "0" ) );
YOffset = int.Parse( map.GetValue( "Y", "0" ) );
Width = int.Parse( map.GetValue( "Width", "0" ) );
Height = int.Parse( map.GetValue( "Height", "0" ) );
MemoryStream ms = ReadMapPack( file );
UnpackTileData( ms );
ReadTrees( file );
return s.Length <= maxLength ? s : s.Substring(0,maxLength );
}
static MemoryStream ReadMapPack( IniFile file )
public string TileSuffix { get { return "." + Truncate(Theater, 3); } }
public Map(IniFile file)
{
IniSection mapPackSection = file.GetSection( "MapPack" );
IniSection basic = file.GetSection("Basic");
Title = basic.GetValue("Name", "(null)");
IniSection map = file.GetSection("Map");
Theater = Truncate(map.GetValue("Theater", "TEMPERATE"), 8);
XOffset = int.Parse(map.GetValue("X", "0"));
YOffset = int.Parse(map.GetValue("Y", "0"));
Width = int.Parse(map.GetValue("Width", "0"));
Height = int.Parse(map.GetValue("Height", "0"));
UnpackTileData(ReadMapPack(file));
ReadTrees(file);
}
static MemoryStream ReadMapPack(IniFile file)
{
IniSection mapPackSection = file.GetSection("MapPack");
StringBuilder sb = new StringBuilder();
for( int i = 1 ; ; i++ )
for (int i = 1; ; i++)
{
string line = mapPackSection.GetValue( i.ToString(), null );
if( line == null )
string line = mapPackSection.GetValue(i.ToString(), null);
if (line == null)
break;
sb.Append( line.Trim() );
sb.Append(line.Trim());
}
byte[] data = Convert.FromBase64String( sb.ToString() );
byte[] data = Convert.FromBase64String(sb.ToString());
List<byte[]> chunks = new List<byte[]>();
BinaryReader reader = new BinaryReader( new MemoryStream( data ) );
BinaryReader reader = new BinaryReader(new MemoryStream(data));
try
{
while( true )
while (true)
{
uint length = reader.ReadUInt32() & 0xdfffffff;
byte[] dest = new byte[ 8192 ];
byte[] src = reader.ReadBytes( (int)length );
byte[] dest = new byte[8192];
byte[] src = reader.ReadBytes((int)length);
int actualLength = Format80.DecodeInto( new MemoryStream( src ), dest );
int actualLength = Format80.DecodeInto(new MemoryStream(src), dest);
chunks.Add( dest );
chunks.Add(dest);
}
}
catch( EndOfStreamException ) { }
catch (EndOfStreamException) { }
MemoryStream ms = new MemoryStream();
foreach( byte[] chunk in chunks )
ms.Write( chunk, 0, chunk.Length );
foreach (byte[] chunk in chunks)
ms.Write(chunk, 0, chunk.Length);
ms.Position = 0;
@@ -95,33 +99,26 @@ namespace OpenRa.FileFormats
void UnpackTileData( MemoryStream ms )
{
for( int i = 0 ; i < 128 ; i++ )
{
for( int j = 0 ; j < 128 ; j++ )
{
MapTiles[ j, i ].tile = ReadByte( ms );
MapTiles[ j, i ].tile |= (ushort)( ReadByte( ms ) << 8 );
}
}
for( int i = 0 ; i < 128 ; i++ )
{
for( int j = 0 ; j < 128 ; j++ )
{
MapTiles[ j, i ].image = ReadByte( ms );
if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff )
MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 );
}
}
}
void ReadTrees( IniFile file )
{
IniSection terrain = file.GetSection( "TERRAIN" );
foreach( KeyValuePair<string, string> kv in terrain )
{
int xy = int.Parse( kv.Key );
Trees.Add( new TreeReference( xy % 128, xy / 128, kv.Value ) );
}
Trees.Add( new TreeReference( int.Parse( kv.Key ), kv.Value ) );
}
}
@@ -130,10 +127,7 @@ namespace OpenRa.FileFormats
public ushort tile;
public byte image;
public override int GetHashCode()
{
return tile.GetHashCode() ^ image.GetHashCode();
}
public override int GetHashCode() { return tile.GetHashCode() ^ image.GetHashCode(); }
public override bool Equals(object obj)
{
@@ -144,15 +138,8 @@ namespace OpenRa.FileFormats
return (r.image == image && r.tile == tile);
}
public static bool operator ==(TileReference a, TileReference b)
{
return a.Equals(b);
}
public static bool operator !=(TileReference a, TileReference b)
{
return !a.Equals(b);
}
public static bool operator ==(TileReference a, TileReference b) { return a.Equals(b); }
public static bool operator !=(TileReference a, TileReference b) { return !a.Equals(b); }
}
public struct TreeReference
@@ -161,10 +148,10 @@ namespace OpenRa.FileFormats
public readonly int Y;
public readonly string Image;
public TreeReference( int x, int y, string image )
public TreeReference(int xy, string image)
{
X = x;
Y = y;
X = xy % 128;
Y = xy / 128;
Image = image;
}
}

View File

@@ -35,7 +35,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BitmapBuilder.cs" />
<Compile Include="Blowfish.cs" />
<Compile Include="Format40.cs" />
<Compile Include="Format80.cs" />

View File

@@ -10,7 +10,7 @@ namespace OpenRa.FileFormats
{
public readonly List<byte[]> TileBitmapBytes = new List<byte[]>();
public Terrain( Stream stream, Palette pal )
public Terrain( Stream stream )
{
int Width, Height, XDim, YDim, NumTiles;

View File

@@ -11,7 +11,7 @@ namespace OpenRa.FileFormats
public readonly Dictionary<ushort, Terrain> tiles = new Dictionary<ushort, Terrain>();
public readonly Package MixFile;
public TileSet( Package mixFile, string suffix, Palette pal )
public TileSet( Package mixFile, string suffix )
{
MixFile = mixFile;
StreamReader tileIdFile = File.OpenText( "../../../tileSet.til" );
@@ -32,7 +32,7 @@ namespace OpenRa.FileFormats
{
Stream s = mixFile.GetContent(string.Format(pattern, i + 1));
if (!tiles.ContainsKey((ushort)(start + i)))
tiles.Add((ushort)(start + i), new Terrain(s, pal));
tiles.Add((ushort)(start + i), new Terrain(s));
}
catch { }
}