Skip to content

Commit

Permalink
code errors fix for reading demo + fix for bad bitmaps inside a map h…
Browse files Browse the repository at this point in the history
  • Loading branch information
HarpyWar committed Oct 23, 2014
1 parent f277442 commit 9c5390c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 65 deletions.
27 changes: 13 additions & 14 deletions nfklib/NDemo/NFKDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public DemoItem Read(string fileName)

public DemoItem Read(Stream stream)
{
byte[] data;
using (var br = new BinaryReader(stream, Encoding.ASCII))
{
// check if header is bad
Expand All @@ -50,27 +51,25 @@ public DemoItem Read(Stream stream)
// gzip compressed data
var gzdata = br.ReadBytes((int)stream.Length - 8);

var data = Helper.BZDecompress(gzdata);

data = Helper.BZDecompress(gzdata);
}
#if DEBUG
File.WriteAllBytes("rawdemoandmap.ndm", data);
File.WriteAllBytes("rawdemoandmap.ndm", data);
#endif
using (var ms = new MemoryStream(data))
using (var ms = new MemoryStream(data))
{
using (var mbr = new BinaryReader(ms))
{
demo.Map = Map.Read(ms);

// rewind
ms.Seek(-Marshal.SizeOf(typeof(TMapEntry)), SeekOrigin.Current);

demo.Map = Map.Read(mbr);
#if DEBUG
var pos = mbr.BaseStream.Position; // remember
int datasize = (int)(mbr.BaseStream.Length - mbr.BaseStream.Position) - 1;
var pos = ms.Position; // remember
int datasize = (int)(ms.Length - ms.Position) - 1;
byte[] data2 = new byte[datasize];
mbr.BaseStream.Read(data2, 0, datasize);
ms.Read(data2, 0, datasize);
File.WriteAllBytes("rawdemo.ndm", data2);
mbr.BaseStream.Seek(pos, SeekOrigin.Begin); // restore
ms.Seek(pos, SeekOrigin.Begin); // restore
#endif
demo = Read(ms);
demo = Read(mbr);
}
}
return demo;
Expand Down
141 changes: 90 additions & 51 deletions nfklib/NMap/NFKMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,70 +60,79 @@ public MapItem Read(Stream stream)
{
using (var br = new BinaryReader(stream, Encoding.ASCII))
{
// map header
map.Header = br.BaseStream.ReadStruct<THeader>();
return Read(br);
}
}

public MapItem Read(BinaryReader br)
{
// map header
map.Header = br.BaseStream.ReadStruct<THeader>();

map.Bricks = new byte[map.Header.MapSizeX][];
// read bricks (start at pos 154)
for (int y = 0; y < map.Header.MapSizeY; y++)
map.Bricks = new byte[map.Header.MapSizeX][];
// read bricks (start at pos 154)
for (int y = 0; y < map.Header.MapSizeY; y++)
{
for (int x = 0; x < map.Header.MapSizeX; x++)
{
for (int x = 0; x < map.Header.MapSizeX; x++)
{
if (map.Bricks[x] == null)
map.Bricks[x] = new byte[map.Header.MapSizeY];
map.Bricks[x][y] = br.ReadByte();
}
if (map.Bricks[x] == null)
map.Bricks[x] = new byte[map.Header.MapSizeY];
map.Bricks[x][y] = br.ReadByte();
}
}

map.Objects = new TMapObj[map.Header.numobj];
// read objects
for (int i = 0; i < map.Header.numobj; i++)
map.Objects[i] = br.BaseStream.ReadStruct<TMapObj>();
map.Objects = new TMapObj[map.Header.numobj];
// read objects
for (int i = 0; i < map.Header.numobj; i++)
map.Objects[i] = br.BaseStream.ReadStruct<TMapObj>();

// read pal and loc blocks
while (br.BaseStream.Length > br.BaseStream.Position)
// read pal and loc blocks
while (br.BaseStream.Length > br.BaseStream.Position)
{
var entry = br.BaseStream.ReadStruct<TMapEntry>();

// palette
if (new string(entry.EntryType).EndsWith("pal"))
{
var entry = br.BaseStream.ReadStruct<TMapEntry>();
map.PaletteEntry = entry;

// palette
if (new string(entry.EntryType).EndsWith("pal"))
{
map.PaletteEntry = entry;

var palette_data = br.ReadBytes(entry.DataSize);
// map nested in demo is not compressed
var palette_bin = (new string(map.Header.ID) == MapInDemoHeader)
? palette_data
: Helper.BZDecompress(palette_data);

map.Palette = new Bitmap(new MemoryStream(palette_bin));
if (entry.Reserved6 == 1)
{
// set transparent color
var color = Color.FromArgb(entry.Reserved5);
map.Palette.MakeTransparent(color);
}
}
// locations
else if (new string(entry.EntryType).EndsWith("loc"))
var palette_data = br.ReadBytes(entry.DataSize);
// map nested in demo is not compressed
var palette_bin = (new string(map.Header.ID) == MapInDemoHeader)
? palette_data
: Helper.BZDecompress(palette_data);

fixBitmapBin(ref palette_data);

map.Palette = new Bitmap(new MemoryStream(palette_bin));

if (entry.Reserved6 == 1)
{
var loc_count = entry.DataSize / Marshal.SizeOf(typeof(TLocationText));
map.Locations = new TLocationText[loc_count];
map.LocationEntry = entry;
for (var i = 0; i < loc_count; i++)
{
map.Locations[i] = br.BaseStream.ReadStruct<TLocationText>();
}
// set transparent color
var color = Color.FromArgb(entry.Reserved5);
map.Palette.MakeTransparent(color);
}
// end of map
else
}
// locations
else if (new string(entry.EntryType).EndsWith("loc"))
{
var loc_count = entry.DataSize / Marshal.SizeOf(typeof(TLocationText));
map.Locations = new TLocationText[loc_count];
map.LocationEntry = entry;
for (var i = 0; i < loc_count; i++)
{
break;
map.Locations[i] = br.BaseStream.ReadStruct<TLocationText>();
}
}
}
// end of map
else
{
// restore position to correctly read demo
br.BaseStream.Seek(-(Marshal.SizeOf(typeof(TMapEntry))), SeekOrigin.Current);
break;
}
}
return map;

}

public void Write(string fileName)
Expand Down Expand Up @@ -185,5 +194,35 @@ public void Write(BinaryWriter bw)
}
}
}

/// <summary>
/// https://github.com/HarpyWar/nfkmap-viewer/wiki/BMP-картинка-палитры
/// </summary>
/// <param name="data"></param>
private void fixBitmapBin(ref byte[] data)
{
int dataSize = data.Length;
int bitmapDataSize;

using (var ms = new MemoryStream(data))
{
using (var br = new BinaryReader(ms))
{
// read bitmap data length
ms.Seek(0x22, SeekOrigin.Begin);
bitmapDataSize = br.ReadInt32();

using (var bw = new BinaryWriter(ms))
{
// write whole data size
bw.Seek(0x02, SeekOrigin.Begin);
bw.Write(dataSize);
// write fixed data offset
bw.Seek(0x0A, SeekOrigin.Begin);
bw.Write(dataSize - bitmapDataSize);
}
}
}
}
}
}

0 comments on commit 9c5390c

Please sign in to comment.