Skip to content

Commit 3532035

Browse files
authored
Merge pull request #1130 from Kenny-Hui/fix-train-progress
Improve train loading progress bar
2 parents 106b7ee + 2d2cd5a commit 3532035

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

source/OpenBVE/System/GameWindow.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ public void LoadingScreenLoop()
11301130
if (Program.TrainManager.Trains.Count != 0)
11311131
{
11321132
//Trains are not loaded until after the route
1133-
finalTrainProgress = (Loading.CurrentTrain * trainProgressWeight) + trainProgressWeight * trainProgress;
1133+
finalTrainProgress = (Loading.LoadedTrain * trainProgressWeight) + trainProgressWeight * trainProgress;
11341134
}
11351135
else
11361136
{

source/OpenBVE/System/Loading.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
namespace OpenBve {
2020
internal static class Loading
2121
{
22-
/// <summary>The current train loading index</summary>
23-
internal static int CurrentTrain;
22+
/// <summary>The number of trains that have been loaded so far</summary>
23+
internal static int LoadedTrain;
2424
/// <summary>Set this member to true to cancel loading</summary>
2525
internal static bool Cancel
2626
{
@@ -404,7 +404,8 @@ private static void LoadEverythingThreaded() {
404404
{
405405
Program.TrainManager.Trains.Add(new TrainBase(TrainState.Bogus, TrainType.PreTrain));
406406
}
407-
407+
408+
LoadedTrain = 0;
408409
// load trains
409410
for (int k = 0; k < Program.TrainManager.Trains.Count; k++) {
410411

@@ -415,6 +416,7 @@ private static void LoadEverythingThreaded() {
415416
{
416417

417418
Program.CurrentHost.Plugins[i].Train.LoadTrain(CurrentTrainEncoding, CurrentTrainFolder, ref currentTrain, ref Interface.CurrentControls);
419+
LoadedTrain++;
418420
break;
419421
}
420422
}
@@ -445,9 +447,6 @@ private static void LoadEverythingThreaded() {
445447
Program.CurrentRoute.UpdateAllSections();
446448
}
447449
// load plugin
448-
449-
450-
CurrentTrain = 0;
451450
for (int i = 0; i < Program.TrainManager.Trains.Count; i++) {
452451
if ( Program.TrainManager.Trains[i].State != TrainState.Bogus) {
453452
if ( Program.TrainManager.Trains[i].IsPlayerTrain) {
@@ -468,8 +467,6 @@ private static void LoadEverythingThreaded() {
468467
}
469468
}
470469
}
471-
CurrentTrain++;
472-
473470
}
474471
}
475472

source/Plugins/Train.OpenBve/Panel/PanelAnimatedXmlParser.cs

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ private void ParsePanelAnimatedNode(XElement Element, string FileName, string Tr
268268
}
269269
break;
270270
}
271+
272+
currentSectionElement++;
271273
}
272274
}
273275

source/Plugins/Train.OpenBve/Plugin.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
257257
// add panel section
258258
if (currentTrain.IsPlayerTrain) {
259259
ParsePanelConfig(currentTrain, encoding);
260-
LastProgress = 0.6;
261260
Thread.Sleep(1);
262261
if (Cancel)
263262
{
@@ -266,6 +265,10 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
266265
}
267266
FileSystem.AppendToLogFile("Train panel loaded sucessfully.");
268267
}
268+
269+
CurrentProgress = 0.5;
270+
LastProgress = 0.5;
271+
269272
// add exterior section
270273
if (currentTrain.State != TrainState.Bogus)
271274
{
@@ -304,6 +307,10 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
304307
IsLoading = false;
305308
return false;
306309
}
310+
311+
CurrentProgress = 0.75;
312+
LastProgress = 0.75;
313+
307314
//Stores the current array index of the bogie object to add
308315
//Required as there are two bogies per car, and we're using a simple linear array....
309316
int currentBogieObject = 0;
@@ -398,6 +405,7 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
398405
currentTrain.Cars[0].Sounds.Motor = new BVEMotorSound(currentTrain.Cars[0], 18.0, MotorSoundTables);
399406
}
400407
}
408+
CurrentProgress = 1;
401409
// place cars
402410
currentTrain.PlaceCars(0.0);
403411
currentControls = CurrentControls;

source/Plugins/Train.OpenBve/Train/BVE/ExtensionsCfgParser.cs

+6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ internal void ParseExtensionsConfig(string TrainPath, Encoding Encoding, ref Uni
5151
return;
5252
}
5353
ConfigFile<ExtensionCfgSection, ExtensionCfgKey> cfg = new ConfigFile<ExtensionCfgSection, ExtensionCfgKey>(Lines, Plugin.CurrentHost);
54+
55+
double perBlockProgress = cfg.RemainingSubBlocks == 0 ? 0.25 : 0.25 / cfg.RemainingSubBlocks;
56+
int readBlocks = 0;
5457
while (cfg.RemainingSubBlocks > 0)
5558
{
59+
Plugin.CurrentProgress = Plugin.LastProgress + perBlockProgress * readBlocks;
5660
Block<ExtensionCfgSection, ExtensionCfgKey> block = cfg.ReadNextBlock();
5761
switch (block.Key)
5862
{
@@ -201,6 +205,8 @@ internal void ParseExtensionsConfig(string TrainPath, Encoding Encoding, ref Uni
201205
}
202206
break;
203207
}
208+
209+
readBlocks++;
204210
}
205211

206212
// check for car objects and reverse if necessary

source/Plugins/Train.OpenBve/Train/XML/TrainXmlParser.cs

+3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
6969
}
7070

7171
int carIndex = 0;
72+
73+
double perCarProgress = 0.25 / DocumentNodes.Count;
7274
//Use the index here for easy access to the car count
7375
for (int i = 0; i < DocumentNodes.Count; i++)
7476
{
77+
Plugin.CurrentProgress = Plugin.LastProgress + perCarProgress * i;
7578
if (carIndex > Train.Cars.Length - 1)
7679
{
7780
Plugin.CurrentHost.AddMessage(MessageType.Warning, false, "WARNING: A total of " + DocumentNodes.Count + " cars were specified in XML file " + fileName + " whilst only " + Train.Cars.Length + " were specified in the train.dat file.");

0 commit comments

Comments
 (0)