Skip to content

Commit 6c1eb75

Browse files
committed
adds cache for SD files
1 parent 3643573 commit 6c1eb75

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

Source/Orts.Formats.Msts/ShapeDescriptorFile.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

1818
using System;
19+
using System.Collections.Generic;
20+
1921
using Orts.Parsers.Msts;
2022

2123
namespace Orts.Formats.Msts
@@ -29,16 +31,26 @@ public class ShapeDescriptorFile
2931
shape = new SDShape();
3032
}
3133

34+
public static Dictionary<string, SDShape> Cache = new Dictionary<string, SDShape>();
35+
3236
public ShapeDescriptorFile(string filename)
3337
{
34-
using (STFReader stf = new STFReader(filename, false))
38+
var shapeDescriptorPath = filename.ToLowerInvariant();
39+
if (Cache.ContainsKey(shapeDescriptorPath))
3540
{
36-
stf.ParseFile(new STFReader.TokenProcessor[] {
37-
new STFReader.TokenProcessor("shape", ()=>{ shape = new SDShape(stf); }),
38-
});
39-
//TODO This should be changed to STFException.TraceError() with defaults values created
40-
if (shape == null)
41-
throw new STFException(stf, "Missing shape statement");
41+
shape = Cache[shapeDescriptorPath];
42+
}
43+
else
44+
{
45+
using (STFReader stf = new STFReader(filename, false))
46+
{
47+
stf.ParseFile(new STFReader.TokenProcessor[] {
48+
new STFReader.TokenProcessor("shape", ()=>{ shape = new SDShape(stf); }),
49+
});
50+
//TODO This should be changed to STFException.TraceError() with defaults values created
51+
if (shape == null) throw new STFException(stf, "Missing shape statement");
52+
}
53+
Cache.Add(shapeDescriptorPath, shape);
4254
}
4355
}
4456

Source/RunActivity/Viewer3D/Scenery.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,23 @@ public WorldFile(Viewer viewer, int tileX, int tileZ, bool visible)
341341
}
342342
}
343343

344-
if (shapeFilePath != null && File.Exists(shapeFilePath + "d"))
344+
var shapeDescriptorPath = shapeFilePath + "d";
345+
if (shapeFilePath != null)
345346
{
346-
var shape = new ShapeDescriptorFile(shapeFilePath + "d");
347-
if (shape.shape.ESD_Bounding_Box != null)
347+
var shape = new ShapeDescriptorFile(shapeDescriptorPath);
348+
if (shape != null)
348349
{
349-
var min = shape.shape.ESD_Bounding_Box.Min;
350-
var max = shape.shape.ESD_Bounding_Box.Max;
351-
var transform = Matrix.Invert(worldMatrix.XNAMatrix);
352-
// Not sure if this is needed, but it is to correct for center-of-gravity being not the center of the box.
353-
//transform.M41 += (max.X + min.X) / 2;
354-
//transform.M42 += (max.Y + min.Y) / 2;
355-
//transform.M43 += (max.Z + min.Z) / 2;
356-
BoundingBoxes.Add(new BoundingBox(transform, new Vector3((max.X - min.X) / 2, (max.Y - min.Y) / 2, (max.Z - min.Z) / 2), worldMatrix.XNAMatrix.Translation.Y));
350+
if (shape.shape.ESD_Bounding_Box != null)
351+
{
352+
var min = shape.shape.ESD_Bounding_Box.Min;
353+
var max = shape.shape.ESD_Bounding_Box.Max;
354+
var transform = Matrix.Invert(worldMatrix.XNAMatrix);
355+
// Not sure if this is needed, but it is to correct for center-of-gravity being not the center of the box.
356+
//transform.M41 += (max.X + min.X) / 2;
357+
//transform.M42 += (max.Y + min.Y) / 2;
358+
//transform.M43 += (max.Z + min.Z) / 2;
359+
BoundingBoxes.Add(new BoundingBox(transform, new Vector3((max.X - min.X) / 2, (max.Y - min.Y) / 2, (max.Z - min.Z) / 2), worldMatrix.XNAMatrix.Translation.Y));
360+
}
357361
}
358362
}
359363

0 commit comments

Comments
 (0)