Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant archive_postsperpage - assumed 'archive_postsperpage' (this will throw an Error in a future version of PHP) in ..../archive/index.php on line 456
C# Map Reader [Archive] - Forums

PDA

View Full Version : C# Map Reader



XtrmJash
12-23-2013, 12:38 AM
So I had to read map files today, and it was a right pain in the arse to fix some software to do it. I did it, and here's the sauce. Use as you wish.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.IO;

namespace MapParser
{
class Program
{
public struct Colour // This is for the minimap shit, dunno where to put it so I dumped it here.
{
public int r; // RED
public int g; // GREEN
public int b; // BLUE
} // FUUUUUUUUUUUUUUUUUUUUUU

public static Colour[] colourlist;
public static Colour[] speedcolourlist;

public static Colour getClr(int r, int g, int b)
{
Colour Clr = new Colour();
Clr.r = r;
Clr.g = g;
Clr.b = b;
return Clr;
}

public static byte[] ReadFile(string filePath)
{
byte[] buffer;
using (FileStream fileStream = new FileStream(Environment.ExpandEnvironmentVariables( filePath), FileMode.Open, FileAccess.Read))
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read

// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
return buffer;
}

public static void fillSpeedColourList()
{
speedcolourlist = new Colour[256];
for (int i = 0; i < 256; i++)
{
speedcolourlist[i] = getClr(i, i, i);
}
}

public static void fillColourList()
{
colourlist = new Colour[256];
colourlist[0] = getClr(0, 0, 0); // Undiscovered / tar / void / inner cave walls
colourlist[12] = getClr(0, 102, 0); // tree / bush
colourlist[24] = getClr(0, 204, 0); // grass or stone ground
colourlist[30] = getClr(0, 255, 0); // old swamp
colourlist[40] = getClr(51, 0, 204); // water
colourlist[86] = getClr(102, 102, 102); // mountain / rock
colourlist[114] = getClr(153, 51, 0); // cave wall
colourlist[121] = getClr(153, 102, 51); // cave mud
colourlist[129] = getClr(153, 153, 153); // normal floor / path
colourlist[179] = getClr(204, 255, 255); // ice wall
colourlist[186] = getClr(255, 51, 0); // wall
colourlist[192] = getClr(255, 102, 0); // lava
colourlist[207] = getClr(255, 204, 153); // sand
colourlist[210] = getClr(255, 255, 0); // portal (or stairs etc)
colourlist[215] = getClr(255, 255, 255); // snow
colourlist[255] = getClr(150, 0, 255); // unknown but appeared once apparently
colourlist[140] = getClr(153, 255, 102); // some noob retard fag claimed this was changed from swamp color
}

public static Bitmap getMapFile(string fileName)
{
Bitmap bmp = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
fillColourList();
byte[] fileContents = ReadFile(Environment.ExpandEnvironmentVariables(fi leName));
int index = 0;
Rectangle rect = new Rectangle();
rect.Height = 256;
rect.Width = 256;
rect.X = 0;
rect.Y = 0;
Colour color = new Colour();
color = colourlist[0];
Graphics.FromImage(bmp).FillRectangle(new SolidBrush(Color.FromArgb(color.r, color.g, color.b)), rect);
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 256; y++)
{
Colour clr;
clr = colourlist[fileContents[index]];
if (!(clr.r == color.r && clr.g == color.g && clr.b == color.b))
{
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((byte)255, (byte)clr.r, (byte)clr.g, (byte)clr.b));
}
index++;
}
}
return bmp;
}

static void Main(string[] args)
{
string[] files = Directory.GetFiles(Environment.ExpandEnvironmentVa riables("%APPDATA%\\Tibia\\Automap\\"));

int index = 0;

foreach (string fl in files)
{
Bitmap bmp = getMapFile(fl);
bmp.Save("C:\\Users\\Josh\\Desktop\\" + index.ToString() + ".bmp");
index++;
}
}
}
}


Should prove useful to at least one person out there. This generates a bitmap for EVERY file in your automap directory, and dumps it to the file path specified at the end of the source.

Provided with no implied warranty etc, entirely free of charge and all that shit.

A side note, you may not be able to access the Bitmap class by default, if so, right click your PROJECT (not solution) in the solution explorer, select Add -> Reference, and find System.Drawing in the list of classes, check the box next to it and click OK.

Enjoy :)

XtrmJash
12-23-2013, 01:22 AM
Looks like I'm on a roll tonight. Just managed to knock out a similar script to create a file big enough for your entire floor 7 map to be dumped (into just one single bitmap, could it be any more awesome?!)

Again, free to use it as you wish, try not to nag about support.

Edit: Just realised that actually I set the width and height to 7x256 when they should both be 8x256, although if some of your map files are missing, you will fail hard on this. I've added a few changes to mine, won't upload but basically they are checking that the file exists before attempting to read it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.IO;

namespace MapParser
{
class Program
{
public struct Colour // This is for the minimap shit, dunno where to put it so I dumped it here.
{
public int r; // RED
public int g; // GREEN
public int b; // BLUE
} // FUUUUUUUUUUUUUUUUUUUUUU

public static Colour[] colourlist;
public static Colour[] speedcolourlist;

public static Colour getClr(int r, int g, int b)
{
Colour Clr = new Colour();
Clr.r = r;
Clr.g = g;
Clr.b = b;
return Clr;
}

public static byte[] ReadFile(string filePath)
{
byte[] buffer;
using (FileStream fileStream = new FileStream(Environment.ExpandEnvironmentVariables( filePath), FileMode.Open, FileAccess.Read))
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read

// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
return buffer;
}

public static void fillSpeedColourList()
{
speedcolourlist = new Colour[256];
for (int i = 0; i < 256; i++)
{
speedcolourlist[i] = getClr(i, i, i);
}
}

public static void fillColourList()
{
colourlist = new Colour[256];
colourlist[0] = getClr(0, 0, 0); // Undiscovered / tar / void / inner cave walls
colourlist[12] = getClr(0, 102, 0); // tree / bush
colourlist[24] = getClr(0, 204, 0); // grass or stone ground
colourlist[30] = getClr(0, 255, 0); // old swamp
colourlist[40] = getClr(51, 0, 204); // water
colourlist[86] = getClr(102, 102, 102); // mountain / rock
colourlist[114] = getClr(153, 51, 0); // cave wall
colourlist[121] = getClr(153, 102, 51); // cave mud
colourlist[129] = getClr(153, 153, 153); // normal floor / path
colourlist[179] = getClr(204, 255, 255); // ice wall
colourlist[186] = getClr(255, 51, 0); // wall
colourlist[192] = getClr(255, 102, 0); // lava
colourlist[207] = getClr(255, 204, 153); // sand
colourlist[210] = getClr(255, 255, 0); // portal (or stairs etc)
colourlist[215] = getClr(255, 255, 255); // snow
colourlist[255] = getClr(150, 0, 255); // unknown but appeared once apparently
colourlist[140] = getClr(153, 255, 102); // some noob retard fag claimed this was changed from swamp color
}

public static Bitmap getMapFile(string fileName)
{
Bitmap bmp = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
fillColourList();
byte[] fileContents = ReadFile(Environment.ExpandEnvironmentVariables(fi leName));
int index = 0;
Rectangle rect = new Rectangle();
rect.Height = 256;
rect.Width = 256;
rect.X = 0;
rect.Y = 0;
Colour color = new Colour();
color = colourlist[0];
Graphics.FromImage(bmp).FillRectangle(new SolidBrush(Color.FromArgb(color.r, color.g, color.b)), rect);
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 256; y++)
{
Colour clr;
clr = colourlist[fileContents[index]];
if (!(clr.r == color.r && clr.g == color.g && clr.b == color.b))
{
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((byte)255, (byte)clr.r, (byte)clr.g, (byte)clr.b));
}
index++;
}
}
return bmp;
}

static void Main(string[] args)
{
string[] files = Directory.GetFiles(Environment.ExpandEnvironmentVa riables("%APPDATA%\\Tibia\\Automap\\"));

FileStream fst = new FileStream("C:\\Users\\Josh\\Desktop\\Output.txt", FileMode.Create);
foreach (string fl in files)
{
Console.WriteLine("fl[43+7] " + fl[43+7]);
Console.WriteLine("fl[43+8] " + fl[43+8]);
Console.WriteLine("---------------");

if (fl[43+7] == '0' && fl[43+8] == '7')
{

foreach (char c in fl)
{
fst.Write(BitConverter.GetBytes(c), 0, 1);
fst.Flush();
}
fst.Write(BitConverter.GetBytes('\n'), 0, 1);
}
}
fst.Close();

Bitmap bmp = new Bitmap(256 * 7, 256 * 7, System.Drawing.Imaging.PixelFormat.Format32bppArgb );

Graphics g = Graphics.FromImage(bmp);

for (int x = 0; x < 7; x++)
{
for (int y = 0; y < 7; y++)
{
// Draw bitmap at x = x+124, y = y+121 onto position x*256, y*256
// Get the name of the file to draw
StringBuilder sb = new StringBuilder();
sb.Append(Environment.ExpandEnvironmentVariables("%APPDATA%\\Tibia\\Automap\\"));
sb.Append(Convert.ToString(124 + x));
sb.Append(Convert.ToString(121 + y));
sb.Append("07.map");
// And draw the file to our graphics object
g.DrawImage(getMapFile(sb.ToString()), x*256, y*256);
}
}
// Save the graphics object, pushing changes to bmp
g.Save();
// Save the bmp object to file
bmp.Save("C:\\Users\\Josh\\Desktop\\LargeBMP.bmp");

Console.ReadLine();
}
}
}


Slightly easier to share the output of this one, so here it is from a full map folder I just downloaded from a rather hacky looking website:

http://i.imgur.com/sp8hZv6.png

XtrmJash
12-23-2013, 01:35 AM
OK, last one from me tonight I think. This basically gives me what I originally started writing this software for, that is a grid guided map which I can use to analyse the memory structure of the .map file once it has been loaded into Tibia. So here is the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.IO;

namespace MapParser
{
class Program
{
public struct Colour // This is for the minimap shit, dunno where to put it so I dumped it here.
{
public int r; // RED
public int g; // GREEN
public int b; // BLUE
} // FUUUUUUUUUUUUUUUUUUUUUU

public static Colour[] colourlist;
public static Colour[] speedcolourlist;

public static Colour getClr(int r, int g, int b)
{
Colour Clr = new Colour();
Clr.r = r;
Clr.g = g;
Clr.b = b;
return Clr;
}

public static byte[] ReadFile(string filePath)
{
byte[] buffer;
using (FileStream fileStream = new FileStream(Environment.ExpandEnvironmentVariables( filePath), FileMode.Open, FileAccess.Read))
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read

// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
return buffer;
}

public static void fillSpeedColourList()
{
speedcolourlist = new Colour[256];
for (int i = 0; i < 256; i++)
{
speedcolourlist[i] = getClr(i, i, i);
}
}

public static void fillColourList()
{
colourlist = new Colour[256];
colourlist[0] = getClr(0, 0, 0); // Undiscovered / tar / void / inner cave walls
colourlist[12] = getClr(0, 102, 0); // tree / bush
colourlist[24] = getClr(0, 204, 0); // grass or stone ground
colourlist[30] = getClr(0, 255, 0); // old swamp
colourlist[40] = getClr(51, 0, 204); // water
colourlist[86] = getClr(102, 102, 102); // mountain / rock
colourlist[114] = getClr(153, 51, 0); // cave wall
colourlist[121] = getClr(153, 102, 51); // cave mud
colourlist[129] = getClr(153, 153, 153); // normal floor / path
colourlist[179] = getClr(204, 255, 255); // ice wall
colourlist[186] = getClr(255, 51, 0); // wall
colourlist[192] = getClr(255, 102, 0); // lava
colourlist[207] = getClr(255, 204, 153); // sand
colourlist[210] = getClr(255, 255, 0); // portal (or stairs etc)
colourlist[215] = getClr(255, 255, 255); // snow
colourlist[255] = getClr(150, 0, 255); // unknown but appeared once apparently
colourlist[140] = getClr(153, 255, 102); // some noob retard fag claimed this was changed from swamp color
}

public static Bitmap getMapFile(string fileName)
{
Bitmap bmp = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
fillColourList();
byte[] fileContents = ReadFile(Environment.ExpandEnvironmentVariables(fi leName));
int index = 0;
Rectangle rect = new Rectangle();
rect.Height = 256;
rect.Width = 256;
rect.X = 0;
rect.Y = 0;
Colour color = new Colour();
color = colourlist[0];
Graphics.FromImage(bmp).FillRectangle(new SolidBrush(Color.FromArgb(color.r, color.g, color.b)), rect);
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 256; y++)
{
Colour clr;
clr = colourlist[fileContents[index]];
if (!(clr.r == color.r && clr.g == color.g && clr.b == color.b))
{
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((byte)255, (byte)clr.r, (byte)clr.g, (byte)clr.b));
}
index++;
}
}
return bmp;
}

static void Main(string[] args)
{
string[] files = Directory.GetFiles(Environment.ExpandEnvironmentVa riables("%APPDATA%\\Tibia\\Automap\\"));

FileStream fst = new FileStream("C:\\Users\\Josh\\Desktop\\Output.txt", FileMode.Create);
foreach (string fl in files)
{
Console.WriteLine("fl[43+7] " + fl[43+7]);
Console.WriteLine("fl[43+8] " + fl[43+8]);
Console.WriteLine("---------------");

if (fl[43+7] == '0' && fl[43+8] == '7')
{

foreach (char c in fl)
{
fst.Write(BitConverter.GetBytes(c), 0, 1);
fst.Flush();
}
fst.Write(BitConverter.GetBytes('\n'), 0, 1);
}
}
fst.Close();

Bitmap bmp = new Bitmap(256 * 8 + 8, 256 * 8 + 8, System.Drawing.Imaging.PixelFormat.Format32bppArgb );

Graphics g = Graphics.FromImage(bmp);

for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
// Draw bitmap at x = x+124, y = y+121 onto position x*256, y*256
// Get the name of the file to draw
StringBuilder sb = new StringBuilder();
sb.Append(Environment.ExpandEnvironmentVariables("%APPDATA%\\Tibia\\Automap\\"));
sb.Append(Convert.ToString(124 + x));
sb.Append(Convert.ToString(121 + y));
sb.Append("07.map");
// And draw the file to our graphics object
if (File.Exists(sb.ToString()))
g.DrawImage(getMapFile(sb.ToString()), x * 256 + x, y * 256 + y);
else
g.FillRectangle(Brushes.Black, x * 256 + x, y * 256 + y, 256, 256);
}
}
// Save the graphics object, pushing changes to bmp
g.Save();
// Save the bmp object to file
bmp.Save("C:\\Users\\Josh\\Desktop\\LargeBMP.bmp");

Console.ReadLine();
}
}
}


And here is the output image:

http://i.imgur.com/mQSHbts.png

jo3bingham
12-25-2013, 08:50 PM
Nice work. Replace SetPixel() with LockBits()/UnlockBits() for a nice performance boost.

astra
12-26-2013, 02:56 PM
Awesome, thanks guys :)

XtrmJash
12-26-2013, 09:55 PM
Awesome, thanks guys :)

Hope you find it useful mate :)


Nice work. Replace SetPixel() with LockBits()/UnlockBits() for a nice performance boost.

Thanks for the tip Jo3, I won't be updating this code as I only actually need the output image, but if anyone wants assistance making that change I will do so.

poopsiedoodle
01-05-2014, 03:42 PM
menz y r saving to c:\users\josh???////forwardslash

euehuehueheuhueheuheuehueheuheueheuheueheuheuheuhe ueheuheuehuehueheuheuehueheuheuhueheuh

XtrmJash
01-17-2014, 09:27 PM
menz y r saving to c:\users\josh???////forwardslash

euehuehueheuhueheuheuehueheuheueheuheueheuheuheuhe ueheuheuehuehueheuheuehueheuheuhueheuh

Why not? :D It's sample code, if someone wants to use it they should learn how it works, that's what it's all about! Learning!

poopsiedoodle
01-18-2014, 09:01 PM
I was kidding. Lel.