diff options
author | Justin Clark-Casey (justincc) | 2015-02-28 00:41:11 +0000 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2015-02-28 00:41:11 +0000 |
commit | 4717132b827aeed7a6e423749c5a7157aa3cc268 (patch) | |
tree | 818de90cb90630aba7812923a5f52fe09a995f20 /OpenSim/Region/CoreModules | |
parent | Add Magnuz Binder to CONTRIBUTORS (diff) | |
download | opensim-SC-4717132b827aeed7a6e423749c5a7157aa3cc268.zip opensim-SC-4717132b827aeed7a6e423749c5a7157aa3cc268.tar.gz opensim-SC-4717132b827aeed7a6e423749c5a7157aa3cc268.tar.bz2 opensim-SC-4717132b827aeed7a6e423749c5a7157aa3cc268.tar.xz |
Use using constructs on disposable io objects in LLRaw to ensure they are always closed even if an exception is thrown.
Diffstat (limited to 'OpenSim/Region/CoreModules')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs | 224 |
1 files changed, 111 insertions, 113 deletions
diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs index 959ac5a..be1fb24 100644 --- a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs | |||
@@ -74,12 +74,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
74 | public ITerrainChannel LoadFile(string filename) | 74 | public ITerrainChannel LoadFile(string filename) |
75 | { | 75 | { |
76 | FileInfo file = new FileInfo(filename); | 76 | FileInfo file = new FileInfo(filename); |
77 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); | ||
78 | ITerrainChannel retval = LoadStream(s); | ||
79 | 77 | ||
80 | s.Close(); | 78 | ITerrainChannel channel; |
81 | 79 | ||
82 | return retval; | 80 | using (FileStream s = file.Open(FileMode.Open, FileAccess.Read)) |
81 | channel = LoadStream(s); | ||
82 | |||
83 | return channel; | ||
83 | } | 84 | } |
84 | 85 | ||
85 | public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) | 86 | 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 | |||
87 | TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); | 88 | TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); |
88 | 89 | ||
89 | FileInfo file = new FileInfo(filename); | 90 | FileInfo file = new FileInfo(filename); |
90 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); | ||
91 | BinaryReader bs = new BinaryReader(s); | ||
92 | 91 | ||
93 | int currFileYOffset = fileHeight - 1; | 92 | using (FileStream s = file.Open(FileMode.Open, FileAccess.Read)) |
94 | 93 | using (BinaryReader bs = new BinaryReader(s)) | |
95 | // if our region isn't on the first Y section of the areas to be landscaped, then | ||
96 | // advance to our section of the file | ||
97 | while (currFileYOffset > offsetY) | ||
98 | { | 94 | { |
99 | // read a whole strip of regions | 95 | int currFileYOffset = fileHeight - 1; |
100 | int heightsToRead = sectionHeight * (fileWidth * sectionWidth); | ||
101 | bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels | ||
102 | currFileYOffset--; | ||
103 | } | ||
104 | 96 | ||
105 | // got to the Y start offset within the file of our region | 97 | // if our region isn't on the first Y section of the areas to be landscaped, then |
106 | // so read the file bits associated with our region | 98 | // advance to our section of the file |
107 | int y; | 99 | while (currFileYOffset > offsetY) |
108 | // for each Y within our Y offset | ||
109 | for (y = sectionHeight - 1; y >= 0; y--) | ||
110 | { | ||
111 | int currFileXOffset = 0; | ||
112 | |||
113 | // if our region isn't the first X section of the areas to be landscaped, then | ||
114 | // advance the stream to the X start pos of our section in the file | ||
115 | // i.e. eat X upto where we start | ||
116 | while (currFileXOffset < offsetX) | ||
117 | { | 100 | { |
118 | bs.ReadBytes(sectionWidth * 13); | 101 | // read a whole strip of regions |
119 | currFileXOffset++; | 102 | int heightsToRead = sectionHeight * (fileWidth * sectionWidth); |
103 | bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels | ||
104 | currFileYOffset--; | ||
120 | } | 105 | } |
121 | 106 | ||
122 | // got to our X offset, so write our regions X line | 107 | // got to the Y start offset within the file of our region |
123 | int x; | 108 | // so read the file bits associated with our region |
124 | for (x = 0; x < sectionWidth; x++) | 109 | int y; |
125 | { | ||
126 | // Read a strip and continue | ||
127 | retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
128 | bs.ReadBytes(11); | ||
129 | } | ||
130 | // record that we wrote it | ||
131 | currFileXOffset++; | ||
132 | 110 | ||
133 | // if our region isn't the last X section of the areas to be landscaped, then | 111 | // for each Y within our Y offset |
134 | // advance the stream to the end of this Y column | 112 | for (y = sectionHeight - 1; y >= 0; y--) |
135 | while (currFileXOffset < fileWidth) | ||
136 | { | 113 | { |
137 | // eat the next regions x line | 114 | int currFileXOffset = 0; |
138 | bs.ReadBytes(sectionWidth * 13); //The 13 channels again | 115 | |
116 | // if our region isn't the first X section of the areas to be landscaped, then | ||
117 | // advance the stream to the X start pos of our section in the file | ||
118 | // i.e. eat X upto where we start | ||
119 | while (currFileXOffset < offsetX) | ||
120 | { | ||
121 | bs.ReadBytes(sectionWidth * 13); | ||
122 | currFileXOffset++; | ||
123 | } | ||
124 | |||
125 | // got to our X offset, so write our regions X line | ||
126 | int x; | ||
127 | for (x = 0; x < sectionWidth; x++) | ||
128 | { | ||
129 | // Read a strip and continue | ||
130 | retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
131 | bs.ReadBytes(11); | ||
132 | } | ||
133 | // record that we wrote it | ||
139 | currFileXOffset++; | 134 | currFileXOffset++; |
135 | |||
136 | // if our region isn't the last X section of the areas to be landscaped, then | ||
137 | // advance the stream to the end of this Y column | ||
138 | while (currFileXOffset < fileWidth) | ||
139 | { | ||
140 | // eat the next regions x line | ||
141 | bs.ReadBytes(sectionWidth * 13); //The 13 channels again | ||
142 | currFileXOffset++; | ||
143 | } | ||
140 | } | 144 | } |
141 | } | 145 | } |
142 | 146 | ||
143 | bs.Close(); | ||
144 | s.Close(); | ||
145 | |||
146 | return retval; | 147 | return retval; |
147 | } | 148 | } |
148 | 149 | ||
@@ -158,90 +159,88 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
158 | 159 | ||
159 | TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension); | 160 | TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension); |
160 | 161 | ||
161 | BinaryReader bs = new BinaryReader(s); | 162 | using (BinaryReader bs = new BinaryReader(s)) |
162 | int y; | ||
163 | for (y = 0; y < retval.Height; y++) | ||
164 | { | 163 | { |
165 | int x; | 164 | int y; |
166 | for (x = 0; x < retval.Width; x++) | 165 | for (y = 0; y < retval.Height; y++) |
167 | { | 166 | { |
168 | retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | 167 | int x; |
169 | bs.ReadBytes(11); // Advance the stream to next bytes. | 168 | for (x = 0; x < retval.Width; x++) |
169 | { | ||
170 | retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
171 | bs.ReadBytes(11); // Advance the stream to next bytes. | ||
172 | } | ||
170 | } | 173 | } |
171 | } | 174 | } |
172 | 175 | ||
173 | bs.Close(); | ||
174 | |||
175 | return retval; | 176 | return retval; |
176 | } | 177 | } |
177 | 178 | ||
178 | public void SaveFile(string filename, ITerrainChannel map) | 179 | public void SaveFile(string filename, ITerrainChannel map) |
179 | { | 180 | { |
180 | FileInfo file = new FileInfo(filename); | 181 | FileInfo file = new FileInfo(filename); |
181 | FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); | ||
182 | SaveStream(s, map); | ||
183 | 182 | ||
184 | s.Close(); | 183 | using (FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write)) |
184 | SaveStream(s, map); | ||
185 | } | 185 | } |
186 | 186 | ||
187 | public void SaveStream(Stream s, ITerrainChannel map) | 187 | public void SaveStream(Stream s, ITerrainChannel map) |
188 | { | 188 | { |
189 | BinaryWriter binStream = new BinaryWriter(s); | 189 | using (BinaryWriter binStream = new BinaryWriter(s)) |
190 | |||
191 | // Output the calculated raw | ||
192 | for (int y = 0; y < map.Height; y++) | ||
193 | { | 190 | { |
194 | for (int x = 0; x < map.Width; x++) | 191 | // Output the calculated raw |
192 | for (int y = 0; y < map.Height; y++) | ||
195 | { | 193 | { |
196 | double t = map[x, (map.Height - 1) - y]; | 194 | for (int x = 0; x < map.Width; x++) |
197 | //if height is less than 0, set it to 0 as | ||
198 | //can't save -ve values in a LLRAW file | ||
199 | if (t < 0d) | ||
200 | { | 195 | { |
201 | t = 0d; | 196 | double t = map[x, (map.Height - 1) - y]; |
197 | //if height is less than 0, set it to 0 as | ||
198 | //can't save -ve values in a LLRAW file | ||
199 | if (t < 0d) | ||
200 | { | ||
201 | t = 0d; | ||
202 | } | ||
203 | |||
204 | int index = 0; | ||
205 | |||
206 | // The lookup table is pre-sorted, so we either find an exact match or | ||
207 | // the next closest (smaller) match with a binary search | ||
208 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t)); | ||
209 | if (index < 0) | ||
210 | index = ~index - 1; | ||
211 | |||
212 | index = LookupHeightTable[index].Index; | ||
213 | |||
214 | byte red = (byte) (index & 0xFF); | ||
215 | byte green = (byte) ((index >> 8) & 0xFF); | ||
216 | const byte blue = 20; | ||
217 | const byte alpha1 = 0; | ||
218 | const byte alpha2 = 0; | ||
219 | const byte alpha3 = 0; | ||
220 | const byte alpha4 = 0; | ||
221 | const byte alpha5 = 255; | ||
222 | const byte alpha6 = 255; | ||
223 | const byte alpha7 = 255; | ||
224 | const byte alpha8 = 255; | ||
225 | byte alpha9 = red; | ||
226 | byte alpha10 = green; | ||
227 | |||
228 | binStream.Write(red); | ||
229 | binStream.Write(green); | ||
230 | binStream.Write(blue); | ||
231 | binStream.Write(alpha1); | ||
232 | binStream.Write(alpha2); | ||
233 | binStream.Write(alpha3); | ||
234 | binStream.Write(alpha4); | ||
235 | binStream.Write(alpha5); | ||
236 | binStream.Write(alpha6); | ||
237 | binStream.Write(alpha7); | ||
238 | binStream.Write(alpha8); | ||
239 | binStream.Write(alpha9); | ||
240 | binStream.Write(alpha10); | ||
202 | } | 241 | } |
203 | |||
204 | int index = 0; | ||
205 | |||
206 | // The lookup table is pre-sorted, so we either find an exact match or | ||
207 | // the next closest (smaller) match with a binary search | ||
208 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t)); | ||
209 | if (index < 0) | ||
210 | index = ~index - 1; | ||
211 | |||
212 | index = LookupHeightTable[index].Index; | ||
213 | |||
214 | byte red = (byte) (index & 0xFF); | ||
215 | byte green = (byte) ((index >> 8) & 0xFF); | ||
216 | const byte blue = 20; | ||
217 | const byte alpha1 = 0; | ||
218 | const byte alpha2 = 0; | ||
219 | const byte alpha3 = 0; | ||
220 | const byte alpha4 = 0; | ||
221 | const byte alpha5 = 255; | ||
222 | const byte alpha6 = 255; | ||
223 | const byte alpha7 = 255; | ||
224 | const byte alpha8 = 255; | ||
225 | byte alpha9 = red; | ||
226 | byte alpha10 = green; | ||
227 | |||
228 | binStream.Write(red); | ||
229 | binStream.Write(green); | ||
230 | binStream.Write(blue); | ||
231 | binStream.Write(alpha1); | ||
232 | binStream.Write(alpha2); | ||
233 | binStream.Write(alpha3); | ||
234 | binStream.Write(alpha4); | ||
235 | binStream.Write(alpha5); | ||
236 | binStream.Write(alpha6); | ||
237 | binStream.Write(alpha7); | ||
238 | binStream.Write(alpha8); | ||
239 | binStream.Write(alpha9); | ||
240 | binStream.Write(alpha10); | ||
241 | } | 242 | } |
242 | } | 243 | } |
243 | |||
244 | binStream.Close(); | ||
245 | } | 244 | } |
246 | 245 | ||
247 | public string FileExtension | 246 | public string FileExtension |
@@ -268,7 +267,6 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
268 | public bool SupportsTileSave() | 267 | public bool SupportsTileSave() |
269 | { | 268 | { |
270 | return false; | 269 | return false; |
271 | } | 270 | } |
272 | |||
273 | } | 271 | } |
274 | } | 272 | } \ No newline at end of file |