Skip to content

Commit 6c3df12

Browse files
committed
style: Format environment file reader code
1 parent 1cc4a74 commit 6c3df12

File tree

3 files changed

+185
-82
lines changed

3 files changed

+185
-82
lines changed
Lines changed: 172 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
1-
// COPYRIGHT 2009, 2010 by the Open Rails project.
2-
//
1+
// COPYRIGHT 2009 - 2023 by the Open Rails project.
2+
//
33
// This file is part of Open Rails.
4-
//
4+
//
55
// Open Rails is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU General Public License as published by
77
// the Free Software Foundation, either version 3 of the License, or
88
// (at your option) any later version.
9-
//
9+
//
1010
// Open Rails is distributed in the hope that it will be useful,
1111
// but WITHOUT ANY WARRANTY; without even the implied warranty of
1212
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
// GNU General Public License for more details.
14-
//
14+
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

1818
using System.Collections.Generic;
19-
using System.Diagnostics;
2019
using Orts.Parsers.Msts;
2120

22-
/*
23-
stf.ParseBlock(new STFReader.TokenProcessor[] {
24-
new STFReader.TokenProcessor("", ()=>{ }),
25-
});
26-
*/
27-
2821
namespace Orts.Formats.Msts
2922
{
3023
public class EnvironmentFile
@@ -39,56 +32,89 @@ public class EnvironmentFile
3932
public EnvironmentFile(string filePath)
4033
{
4134
using (STFReader stf = new STFReader(filePath, false))
42-
stf.ParseFile(new STFReader.TokenProcessor[] {
43-
new STFReader.TokenProcessor("world", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
44-
new STFReader.TokenProcessor("world_water", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
45-
new STFReader.TokenProcessor("world_water_wave_height", ()=>{ WaterWaveHeight = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
46-
new STFReader.TokenProcessor("world_water_wave_speed", ()=>{ WaterWaveSpeed = stf.ReadFloatBlock(STFReader.UNITS.Speed, null); }),
47-
new STFReader.TokenProcessor("world_water_layers", ()=>{ ParseWaterLayers(stf); }),
48-
});}),
49-
});}),
50-
});
35+
{
36+
stf.ParseFile(new STFReader.TokenProcessor[]
37+
{
38+
new STFReader.TokenProcessor("world", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
39+
{
40+
new STFReader.TokenProcessor("world_water", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
41+
{
42+
new STFReader.TokenProcessor("world_water_wave_height", () => { WaterWaveHeight = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
43+
new STFReader.TokenProcessor("world_water_wave_speed", () => { WaterWaveSpeed = stf.ReadFloatBlock(STFReader.UNITS.Speed, null); }),
44+
new STFReader.TokenProcessor("world_water_layers", () => { ParseWaterLayers(stf); }),
45+
})),
46+
})),
47+
});
48+
}
5149

5250
using (STFReader stf = new STFReader(filePath, false))
53-
stf.ParseFile(new STFReader.TokenProcessor[] {
54-
new STFReader.TokenProcessor("world", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
55-
new STFReader.TokenProcessor("world_sky", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
56-
new STFReader.TokenProcessor("worldskynlayers_behind_satellites", ()=>{ WorldSkynLayers = stf.ReadFloatBlock( STFReader.UNITS.Any, null ); }),
57-
new STFReader.TokenProcessor("world_sky_layers", ()=>{ ParseSkyLayers(stf); }),
58-
new STFReader.TokenProcessor("world_sky_satellites", ()=>{ ParseWorldSkySatellites(stf); }),
59-
});}),
60-
});}),
61-
});
51+
{
52+
stf.ParseFile(new STFReader.TokenProcessor[]
53+
{
54+
new STFReader.TokenProcessor("world", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
55+
{
56+
new STFReader.TokenProcessor("world_sky", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
57+
{
58+
new STFReader.TokenProcessor("worldskynlayers_behind_satellites", () => { WorldSkynLayers = stf.ReadFloatBlock(STFReader.UNITS.Any, null); }),
59+
new STFReader.TokenProcessor("world_sky_layers", () => { ParseSkyLayers(stf); }),
60+
new STFReader.TokenProcessor("world_sky_satellites", () => { ParseWorldSkySatellites(stf); }),
61+
})),
62+
})),
63+
});
64+
}
6265
}
66+
6367
private void ParseWaterLayers(STFReader stf)
6468
{
6569
stf.MustMatch("(");
6670
int texturelayers = stf.ReadInt(null);
6771
WaterLayers = new List<ENVFileWaterLayer>(texturelayers);
68-
stf.ParseBlock(new STFReader.TokenProcessor[] {
69-
new STFReader.TokenProcessor("world_water_layer", ()=>{ if(texturelayers-- > 0) WaterLayers.Add(new ENVFileWaterLayer(stf)); })
72+
stf.ParseBlock(new STFReader.TokenProcessor[]
73+
{
74+
new STFReader.TokenProcessor("world_water_layer", () =>
75+
{
76+
if (texturelayers-- > 0)
77+
{
78+
WaterLayers.Add(new ENVFileWaterLayer(stf));
79+
}
80+
}),
7081
});
7182
}
83+
7284
private void ParseSkyLayers(STFReader stf)
7385
{
7486
stf.MustMatch("(");
7587
int skylayers = stf.ReadInt(null);
7688
SkyLayers = new List<ENVFileSkyLayer>(skylayers);
7789

78-
stf.ParseBlock(new STFReader.TokenProcessor[]
90+
stf.ParseBlock(new STFReader.TokenProcessor[]
7991
{
80-
new STFReader.TokenProcessor("world_sky_layer", ()=>{ if(skylayers-- > 0) SkyLayers.Add(new ENVFileSkyLayer(stf)); })});
81-
92+
new STFReader.TokenProcessor("world_sky_layer", () =>
93+
{
94+
if (skylayers-- > 0)
95+
{
96+
SkyLayers.Add(new ENVFileSkyLayer(stf));
97+
}
98+
}),
99+
});
82100
}
101+
83102
private void ParseWorldSkySatellites(STFReader stf)
84103
{
85104
stf.MustMatch("(");
86105
int skysatellite = stf.ReadInt(null);
87106
SkySatellite = new List<ENVFileSkySatellite>(skysatellite);
88107

89-
stf.ParseBlock(new STFReader.TokenProcessor[]
90-
{
91-
new STFReader.TokenProcessor("world_sky_satellite", () => { if (skysatellite-- > 0) SkySatellite.Add(new ENVFileSkySatellite(stf)); })});
108+
stf.ParseBlock(new STFReader.TokenProcessor[]
109+
{
110+
new STFReader.TokenProcessor("world_sky_satellite", () =>
111+
{
112+
if (skysatellite-- > 0)
113+
{
114+
SkySatellite.Add(new ENVFileSkySatellite(stf));
115+
}
116+
}),
117+
});
92118
}
93119

94120
public class ENVFileWaterLayer
@@ -98,72 +124,138 @@ public class ENVFileWaterLayer
98124

99125
public ENVFileWaterLayer(STFReader stf)
100126
{
101-
stf.MustMatch("(");
102-
stf.ParseBlock(new STFReader.TokenProcessor[] {
103-
new STFReader.TokenProcessor("world_water_layer_height", ()=>{ Height = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
104-
new STFReader.TokenProcessor("world_anim_shader", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
105-
new STFReader.TokenProcessor("world_shader", ()=>{ stf.MustMatch("("); stf.ReadString()/*TextureMode*/; stf.ParseBlock(new STFReader.TokenProcessor[] {
106-
new STFReader.TokenProcessor("terrain_texslots", ()=>{ stf.MustMatch("("); stf.ReadInt(null)/*Count*/; stf.ParseBlock(new STFReader.TokenProcessor[] {
107-
new STFReader.TokenProcessor("terrain_texslot", ()=>{ stf.MustMatch("("); TextureName = stf.ReadString(); stf.SkipRestOfBlock(); }),
108-
});}),
109-
});}),
110-
});}),
127+
stf.ParseWholeBlock(new STFReader.TokenProcessor[]
128+
{
129+
new STFReader.TokenProcessor("world_water_layer_height", () => { Height = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
130+
new STFReader.TokenProcessor("world_anim_shader", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
131+
{
132+
new STFReader.TokenProcessor("world_shader", () =>
133+
{
134+
stf.MustMatch("(");
135+
stf.ReadString() /*TextureMode*/;
136+
stf.ParseBlock(new STFReader.TokenProcessor[]
137+
{
138+
new STFReader.TokenProcessor("terrain_texslots", () =>
139+
{
140+
stf.MustMatch("(");
141+
stf.ReadInt(null) /*Count*/;
142+
stf.ParseBlock(new STFReader.TokenProcessor[]
143+
{
144+
new STFReader.TokenProcessor("terrain_texslot", () =>
145+
{
146+
stf.MustMatch("(");
147+
TextureName = stf.ReadString();
148+
stf.SkipRestOfBlock();
149+
}),
150+
});
151+
}),
152+
});
153+
}),
154+
})),
111155
});
112156
}
113157
}
114-
115158
}
116159

117160
public class ENVFileSkyLayer
118161
{
119-
public string Fadein_Begin_Time;
120-
public string Fadein_End_Time;
162+
public string FadeinBeginTime;
163+
public string FadeinEndTime;
121164
public string TextureName;
122165
public string TextureMode;
123166
public float TileX;
124167
public float TileY;
125168

126-
127169
public ENVFileSkyLayer(STFReader stf)
128170
{
129-
stf.MustMatch("(");
130-
stf.ParseBlock(new STFReader.TokenProcessor[] {
131-
new STFReader.TokenProcessor("world_sky_layer_fadein", ()=>{ stf.MustMatch("("); Fadein_Begin_Time = stf.ReadString(); Fadein_End_Time = stf.ReadString(); stf.SkipRestOfBlock();}),
132-
new STFReader.TokenProcessor("world_anim_shader", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
133-
new STFReader.TokenProcessor("world_anim_shader_frames", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
134-
new STFReader.TokenProcessor("world_anim_shader_frame", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
135-
new STFReader.TokenProcessor("world_anim_shader_frame_uvtiles", ()=>{ stf.MustMatch("("); TileX = stf.ReadFloat(STFReader.UNITS.Any, 1.0f); TileY = stf.ReadFloat(STFReader.UNITS.Any, 1.0f); stf.ParseBlock(new STFReader.TokenProcessor[] {
136-
});}),
137-
});}),
138-
});}),
139-
new STFReader.TokenProcessor("world_shader", ()=>{ stf.MustMatch("("); TextureMode = stf.ReadString(); stf.ParseBlock(new STFReader.TokenProcessor[] {
140-
new STFReader.TokenProcessor("terrain_texslots", ()=>{ stf.MustMatch("("); stf.ReadInt(null)/*Count*/; stf.ParseBlock(new STFReader.TokenProcessor[] {
141-
new STFReader.TokenProcessor("terrain_texslot", ()=>{ stf.MustMatch("("); TextureName = stf.ReadString(); stf.SkipRestOfBlock(); }),
142-
});}),
143-
});}),
144-
});}),
171+
stf.ParseWholeBlock(new STFReader.TokenProcessor[]
172+
{
173+
new STFReader.TokenProcessor("world_sky_layer_fadein", () =>
174+
{
175+
stf.MustMatch("(");
176+
FadeinBeginTime = stf.ReadString();
177+
FadeinEndTime = stf.ReadString();
178+
stf.SkipRestOfBlock();
179+
}),
180+
new STFReader.TokenProcessor("world_anim_shader", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
181+
{
182+
new STFReader.TokenProcessor("world_anim_shader_frames", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
183+
{
184+
new STFReader.TokenProcessor("world_anim_shader_frame", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
185+
{
186+
new STFReader.TokenProcessor("world_anim_shader_frame_uvtiles", () =>
187+
{
188+
stf.MustMatch("(");
189+
TileX = stf.ReadFloat(STFReader.UNITS.Any, 1.0f);
190+
TileY = stf.ReadFloat(STFReader.UNITS.Any, 1.0f);
191+
stf.ParseBlock(new STFReader.TokenProcessor[]
192+
{
193+
});
194+
}),
195+
})),
196+
})),
197+
})),
198+
new STFReader.TokenProcessor("world_shader", () =>
199+
{
200+
stf.MustMatch("(");
201+
TextureMode = stf.ReadString();
202+
stf.ParseBlock(new STFReader.TokenProcessor[]
203+
{
204+
new STFReader.TokenProcessor("terrain_texslots", () =>
205+
{
206+
stf.MustMatch("(");
207+
stf.ReadInt(null) /*Count*/;
208+
stf.ParseBlock(new STFReader.TokenProcessor[]
209+
{
210+
new STFReader.TokenProcessor("terrain_texslot", () =>
211+
{
212+
stf.MustMatch("(");
213+
TextureName = stf.ReadString();
214+
stf.SkipRestOfBlock();
215+
}),
216+
});
217+
}),
218+
});
219+
}),
145220
});
146-
147221
}
148222
}
223+
149224
public class ENVFileSkySatellite
150225
{
151-
152226
public string TextureName;
153227
public string TextureMode;
154228

155229
public ENVFileSkySatellite(STFReader stf)
156230
{
157-
stf.MustMatch("(");
158-
stf.ParseBlock(new STFReader.TokenProcessor[] {
159-
new STFReader.TokenProcessor("world_anim_shader", ()=>{ stf.MustMatch("("); stf.ParseBlock(new STFReader.TokenProcessor[] {
160-
new STFReader.TokenProcessor("world_shader", ()=>{ stf.MustMatch("("); TextureMode = stf.ReadString(); stf.ParseBlock(new STFReader.TokenProcessor[] {
161-
new STFReader.TokenProcessor("terrain_texslots", ()=>{ stf.MustMatch("("); stf.ReadInt(null)/*Count*/; stf.ParseBlock(new STFReader.TokenProcessor[] {
162-
new STFReader.TokenProcessor("terrain_texslot", ()=>{ stf.MustMatch("("); TextureName = stf.ReadString(); stf.SkipRestOfBlock(); }),
163-
});}),
164-
});}),
165-
});}),
166-
});
231+
stf.ParseWholeBlock(new STFReader.TokenProcessor[]
232+
{
233+
new STFReader.TokenProcessor("world_anim_shader", () => stf.ParseWholeBlock(new STFReader.TokenProcessor[]
234+
{
235+
new STFReader.TokenProcessor("world_shader", () =>
236+
{
237+
stf.MustMatch("(");
238+
TextureMode = stf.ReadString();
239+
stf.ParseBlock(new STFReader.TokenProcessor[]
240+
{
241+
new STFReader.TokenProcessor("terrain_texslots", () =>
242+
{
243+
stf.MustMatch("(");
244+
stf.ReadInt(null) /*Count*/;
245+
stf.ParseBlock(new STFReader.TokenProcessor[]
246+
{
247+
new STFReader.TokenProcessor("terrain_texslot", () =>
248+
{
249+
stf.MustMatch("(");
250+
TextureName = stf.ReadString();
251+
stf.SkipRestOfBlock();
252+
}),
253+
});
254+
}),
255+
});
256+
}),
257+
})),
258+
});
167259
}
168260
}
169261
}

Source/Orts.Parsers.Msts/STFReader.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ public void ParseFile(ParsingBreak breakout, TokenProcessor[] processors)
15141514
tp.processor(); // Press F11 'Step Into' to debug the Processor delegate
15151515
} // Press F10 'Step Over' to jump to the next token
15161516
}
1517+
15171518
/// <summary>Parse an STF file until the end of block ')' marker, using the array of lower case tokens, with a processor delegate/lambda
15181519
/// </summary>
15191520
/// <param name="processors">Array of lower case token, and the delegate/lambda to call when matched.</param>
@@ -1530,6 +1531,7 @@ public void ParseBlock(IEnumerable<TokenProcessor> processors)
15301531
tp.processor(); // Press F11 'Step Into' to debug the Processor delegate
15311532
} // Press F10 'Step Over' to jump to the next token
15321533
}
1534+
15331535
/// <summary>Parse an STF file until the end of block ')' marker, using the array of lower case tokens, with a processor delegate/lambda
15341536
/// </summary>
15351537
/// <param name="breakout">A delegate that returns true, if the processing should be halted prematurely</param>
@@ -1553,6 +1555,15 @@ public void ParseBlock(ParsingBreak breakout, TokenProcessor[] processors)
15531555
tp.processor(); // Press F11 'Step Into' to debug the Processor delegate
15541556
} // Press F10 'Step Over' to jump to the next token
15551557
}
1558+
1559+
/// <summary>Parse an entire STF block from a '(' until the end of block ')' marker, using the array of lower case tokens, with a processor delegate/lambda</summary>
1560+
/// <param name="processors">Array of lower case token, and the delegate/lambda to call when matched.</param>
1561+
public void ParseWholeBlock(TokenProcessor[] processors)
1562+
{
1563+
VerifyStartOfBlock();
1564+
ParseBlock(processors);
1565+
}
1566+
15561567
#region *** Delegate and Structure definitions used by the Parse...() methods.
15571568
/// <summary>This delegate definition is used by the ParseFile and ParseBlock methods, and is called when an associated matching token is found.
15581569
/// </summary>
@@ -3874,4 +3885,4 @@ public STFException(string fileName, int lineNumber, string message)
38743885

38753886
}
38763887
#endif
3877-
#endregion
3888+
#endregion

Source/RunActivity/Viewer3D/MSTSSky.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ public MSTSSkyMaterial(Viewer viewer)
512512
mstsskytexturey = mstsskytexture[i].TileY;
513513

514514
}
515-
else if(mstsskytexture[i].Fadein_Begin_Time != null)
515+
else if(mstsskytexture[i].FadeinBeginTime != null)
516516
{
517517
MSTSSkyStarTexture = MSTSSkyTexture[i];
518518
mstsskytexturex = mstsskytexture[i].TileX;

0 commit comments

Comments
 (0)