Use using constructs on disposable io objects in LLRaw to ensure they are always closed even if an exception is thrown.

inv-download
Justin Clark-Casey (justincc) 2015-02-28 00:41:11 +00:00
parent 06a52b43df
commit 4717132b82
1 changed files with 111 additions and 113 deletions

View File

@ -74,12 +74,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename) public ITerrainChannel LoadFile(string filename)
{ {
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
ITerrainChannel retval = LoadStream(s);
s.Close(); ITerrainChannel channel;
return retval; using (FileStream s = file.Open(FileMode.Open, FileAccess.Read))
channel = LoadStream(s);
return channel;
} }
public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
@ -87,62 +88,62 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
BinaryReader bs = new BinaryReader(s);
int currFileYOffset = fileHeight - 1; using (FileStream s = file.Open(FileMode.Open, FileAccess.Read))
using (BinaryReader bs = new BinaryReader(s))
// if our region isn't on the first Y section of the areas to be landscaped, then
// advance to our section of the file
while (currFileYOffset > offsetY)
{ {
// read a whole strip of regions int currFileYOffset = fileHeight - 1;
int heightsToRead = sectionHeight * (fileWidth * sectionWidth);
bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels
currFileYOffset--;
}
// got to the Y start offset within the file of our region // if our region isn't on the first Y section of the areas to be landscaped, then
// so read the file bits associated with our region // advance to our section of the file
int y; while (currFileYOffset > offsetY)
// for each Y within our Y offset
for (y = sectionHeight - 1; y >= 0; y--)
{
int currFileXOffset = 0;
// if our region isn't the first X section of the areas to be landscaped, then
// advance the stream to the X start pos of our section in the file
// i.e. eat X upto where we start
while (currFileXOffset < offsetX)
{ {
bs.ReadBytes(sectionWidth * 13); // read a whole strip of regions
currFileXOffset++; int heightsToRead = sectionHeight * (fileWidth * sectionWidth);
bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels
currFileYOffset--;
} }
// got to our X offset, so write our regions X line // got to the Y start offset within the file of our region
int x; // so read the file bits associated with our region
for (x = 0; x < sectionWidth; x++) int y;
{
// Read a strip and continue
retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0);
bs.ReadBytes(11);
}
// record that we wrote it
currFileXOffset++;
// if our region isn't the last X section of the areas to be landscaped, then // for each Y within our Y offset
// advance the stream to the end of this Y column for (y = sectionHeight - 1; y >= 0; y--)
while (currFileXOffset < fileWidth)
{ {
// eat the next regions x line int currFileXOffset = 0;
bs.ReadBytes(sectionWidth * 13); //The 13 channels again
// if our region isn't the first X section of the areas to be landscaped, then
// advance the stream to the X start pos of our section in the file
// i.e. eat X upto where we start
while (currFileXOffset < offsetX)
{
bs.ReadBytes(sectionWidth * 13);
currFileXOffset++;
}
// got to our X offset, so write our regions X line
int x;
for (x = 0; x < sectionWidth; x++)
{
// Read a strip and continue
retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0);
bs.ReadBytes(11);
}
// record that we wrote it
currFileXOffset++; currFileXOffset++;
// if our region isn't the last X section of the areas to be landscaped, then
// advance the stream to the end of this Y column
while (currFileXOffset < fileWidth)
{
// eat the next regions x line
bs.ReadBytes(sectionWidth * 13); //The 13 channels again
currFileXOffset++;
}
} }
} }
bs.Close();
s.Close();
return retval; return retval;
} }
@ -158,90 +159,88 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension); TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension);
BinaryReader bs = new BinaryReader(s); using (BinaryReader bs = new BinaryReader(s))
int y;
for (y = 0; y < retval.Height; y++)
{ {
int x; int y;
for (x = 0; x < retval.Width; x++) for (y = 0; y < retval.Height; y++)
{ {
retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); int x;
bs.ReadBytes(11); // Advance the stream to next bytes. for (x = 0; x < retval.Width; x++)
{
retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0);
bs.ReadBytes(11); // Advance the stream to next bytes.
}
} }
} }
bs.Close();
return retval; return retval;
} }
public void SaveFile(string filename, ITerrainChannel map) public void SaveFile(string filename, ITerrainChannel map)
{ {
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write);
SaveStream(s, map);
s.Close(); using (FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write))
SaveStream(s, map);
} }
public void SaveStream(Stream s, ITerrainChannel map) public void SaveStream(Stream s, ITerrainChannel map)
{ {
BinaryWriter binStream = new BinaryWriter(s); using (BinaryWriter binStream = new BinaryWriter(s))
// Output the calculated raw
for (int y = 0; y < map.Height; y++)
{ {
for (int x = 0; x < map.Width; x++) // Output the calculated raw
for (int y = 0; y < map.Height; y++)
{ {
double t = map[x, (map.Height - 1) - y]; for (int x = 0; x < map.Width; x++)
//if height is less than 0, set it to 0 as
//can't save -ve values in a LLRAW file
if (t < 0d)
{ {
t = 0d; double t = map[x, (map.Height - 1) - y];
//if height is less than 0, set it to 0 as
//can't save -ve values in a LLRAW file
if (t < 0d)
{
t = 0d;
}
int index = 0;
// The lookup table is pre-sorted, so we either find an exact match or
// the next closest (smaller) match with a binary search
index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t));
if (index < 0)
index = ~index - 1;
index = LookupHeightTable[index].Index;
byte red = (byte) (index & 0xFF);
byte green = (byte) ((index >> 8) & 0xFF);
const byte blue = 20;
const byte alpha1 = 0;
const byte alpha2 = 0;
const byte alpha3 = 0;
const byte alpha4 = 0;
const byte alpha5 = 255;
const byte alpha6 = 255;
const byte alpha7 = 255;
const byte alpha8 = 255;
byte alpha9 = red;
byte alpha10 = green;
binStream.Write(red);
binStream.Write(green);
binStream.Write(blue);
binStream.Write(alpha1);
binStream.Write(alpha2);
binStream.Write(alpha3);
binStream.Write(alpha4);
binStream.Write(alpha5);
binStream.Write(alpha6);
binStream.Write(alpha7);
binStream.Write(alpha8);
binStream.Write(alpha9);
binStream.Write(alpha10);
} }
int index = 0;
// The lookup table is pre-sorted, so we either find an exact match or
// the next closest (smaller) match with a binary search
index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t));
if (index < 0)
index = ~index - 1;
index = LookupHeightTable[index].Index;
byte red = (byte) (index & 0xFF);
byte green = (byte) ((index >> 8) & 0xFF);
const byte blue = 20;
const byte alpha1 = 0;
const byte alpha2 = 0;
const byte alpha3 = 0;
const byte alpha4 = 0;
const byte alpha5 = 255;
const byte alpha6 = 255;
const byte alpha7 = 255;
const byte alpha8 = 255;
byte alpha9 = red;
byte alpha10 = green;
binStream.Write(red);
binStream.Write(green);
binStream.Write(blue);
binStream.Write(alpha1);
binStream.Write(alpha2);
binStream.Write(alpha3);
binStream.Write(alpha4);
binStream.Write(alpha5);
binStream.Write(alpha6);
binStream.Write(alpha7);
binStream.Write(alpha8);
binStream.Write(alpha9);
binStream.Write(alpha10);
} }
} }
binStream.Close();
} }
public string FileExtension public string FileExtension
@ -268,7 +267,6 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
public bool SupportsTileSave() public bool SupportsTileSave()
{ {
return false; return false;
} }
} }
} }