I normally have no problem with this, but this particular problem has been bugging me for 2 days now. |:
The following function can't read the 2D array in the file because Width and Height are being reported as being 41thousand-something and 2 respectively;
/// <summary>
/// Loads and returns a new Map instance from a given Flexible Level Data file.
/// </summary>
/// <param name="Path">The path to the file, including filename.</param>
/// <returns>The new Map instance.</returns>
public static Map FromFile(string Path)
{
BinaryReader Reader = new BinaryReader(File.Open(Path, FileMode.Open));
ASCIIEncoding Encoding = new ASCIIEncoding();
string Signature = Encoding.GetString(Reader.ReadBytes(3));
if (Signature != "FLD")
throw new Exception("Tried loading a non-leveldata file in Map.FromFile()!");
int Version = Reader.ReadInt32();
if (Version != 1)
throw new Exception("Tried loading FLD file with wrong version in Map.FromFile()!");
int PreviousLevel = Reader.ReadInt32();
int NextLevel = Reader.ReadInt32();
string StrPreviousLevel = PreviousLevel.ToString() + ".fld";
string StrNextLevel = NextLevel.ToString() + ".fld";
int Width = Reader.ReadInt32();
int Height = Reader.ReadInt32();
MessageBox.Show("Width: " + Width.ToString() + " Height: " + Height.ToString());
int[,] Grid = new int[Width, Height];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
int Tile = Reader.ReadInt32();
if (Tile > 0)
Grid[x, y] = Tile / 2;
else
Grid[x, y] = Tile;
}
}
Reader.Close();
return new Map(Width, Height, Grid, StrNextLevel);
}
I've checked the file that's written by my level editor in Hex Workshop, and the values of Width and Height are -supposed- to be 64 and 64. But they're not being read as such!!
Here's the writing function:
/// <summary>
/// Saves the currently generated map to a FLD level file.
/// </summary>
/// <param name="Path">The path of the file to write tp, including name.
/// Will be created if it doesn't already exist.</param>
public void ToFile(string Path, string PreviousLvl, string NextLvl)
{
BinaryWriter Writer = new BinaryWriter(File.Create(Path));
ASCIIEncoding Encoding = new ASCIIEncoding();
//Signature - Flexible Level Data
Writer.Write(Encoding.GetBytes("FLD"));
//Version
Writer.Write((int)1);
//Previous Level
Writer.Write((int)int.Parse(PreviousLvl));
//Next Level
Writer.Write((int)int.Parse(NextLvl));
//Level Width
Writer.Write((int)mapWidth);
//Level Height
Writer.Write((int)mapHeight);
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
Writer.Write((int)(grid[x, y ] * 2));
}
}
Writer.Close();
}
What's wrong here? There must be something I'm not seeing. Am I blind? o_O
Edit: If anyone's wondering, the error I'm getting in the reading function is, obviously, 'Cannot read beyond the end of the stream'.