Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve train loading progress bar #1130

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/OpenBVE/System/GameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public void LoadingScreenLoop()
if (Program.TrainManager.Trains.Count != 0)
{
//Trains are not loaded until after the route
finalTrainProgress = (Loading.CurrentTrain * trainProgressWeight) + trainProgressWeight * trainProgress;
finalTrainProgress = (Loading.LoadedTrain * trainProgressWeight) + trainProgressWeight * trainProgress;
}
else
{
Expand Down
13 changes: 5 additions & 8 deletions source/OpenBVE/System/Loading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
namespace OpenBve {
internal static class Loading
{
/// <summary>The current train loading index</summary>
internal static int CurrentTrain;
/// <summary>The number of trains that have been loaded so far</summary>
internal static int LoadedTrain;
/// <summary>Set this member to true to cancel loading</summary>
internal static bool Cancel
{
Expand Down Expand Up @@ -404,7 +404,8 @@ private static void LoadEverythingThreaded() {
{
Program.TrainManager.Trains.Add(new TrainBase(TrainState.Bogus, TrainType.PreTrain));
}


LoadedTrain = 0;
// load trains
for (int k = 0; k < Program.TrainManager.Trains.Count; k++) {

Expand All @@ -415,6 +416,7 @@ private static void LoadEverythingThreaded() {
{

Program.CurrentHost.Plugins[i].Train.LoadTrain(CurrentTrainEncoding, CurrentTrainFolder, ref currentTrain, ref Interface.CurrentControls);
LoadedTrain++;
break;
}
}
Expand Down Expand Up @@ -445,9 +447,6 @@ private static void LoadEverythingThreaded() {
Program.CurrentRoute.UpdateAllSections();
}
// load plugin


CurrentTrain = 0;
for (int i = 0; i < Program.TrainManager.Trains.Count; i++) {
if ( Program.TrainManager.Trains[i].State != TrainState.Bogus) {
if ( Program.TrainManager.Trains[i].IsPlayerTrain) {
Expand All @@ -468,8 +467,6 @@ private static void LoadEverythingThreaded() {
}
}
}
CurrentTrain++;

}
}

Expand Down
2 changes: 2 additions & 0 deletions source/Plugins/Train.OpenBve/Panel/PanelAnimatedXmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ private void ParsePanelAnimatedNode(XElement Element, string FileName, string Tr
}
break;
}

currentSectionElement++;
}
}

Expand Down
10 changes: 9 additions & 1 deletion source/Plugins/Train.OpenBve/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
// add panel section
if (currentTrain.IsPlayerTrain) {
ParsePanelConfig(currentTrain, encoding);
LastProgress = 0.6;
Thread.Sleep(1);
if (Cancel)
{
Expand All @@ -266,6 +265,10 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
}
FileSystem.AppendToLogFile("Train panel loaded sucessfully.");
}

CurrentProgress = 0.5;
LastProgress = 0.5;

// add exterior section
if (currentTrain.State != TrainState.Bogus)
{
Expand Down Expand Up @@ -304,6 +307,10 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
IsLoading = false;
return false;
}

CurrentProgress = 0.75;
LastProgress = 0.75;

//Stores the current array index of the bogie object to add
//Required as there are two bogies per car, and we're using a simple linear array....
int currentBogieObject = 0;
Expand Down Expand Up @@ -398,6 +405,7 @@ public override bool LoadTrain(Encoding encoding, string trainPath, ref Abstract
currentTrain.Cars[0].Sounds.Motor = new BVEMotorSound(currentTrain.Cars[0], 18.0, MotorSoundTables);
}
}
CurrentProgress = 1;
// place cars
currentTrain.PlaceCars(0.0);
currentControls = CurrentControls;
Expand Down
6 changes: 6 additions & 0 deletions source/Plugins/Train.OpenBve/Train/BVE/ExtensionsCfgParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ internal void ParseExtensionsConfig(string TrainPath, Encoding Encoding, ref Uni
return;
}
ConfigFile<ExtensionCfgSection, ExtensionCfgKey> cfg = new ConfigFile<ExtensionCfgSection, ExtensionCfgKey>(Lines, Plugin.CurrentHost);

double perBlockProgress = cfg.RemainingSubBlocks == 0 ? 0.25 : 0.25 / cfg.RemainingSubBlocks;
int readBlocks = 0;
while (cfg.RemainingSubBlocks > 0)
{
Plugin.CurrentProgress = Plugin.LastProgress + perBlockProgress * readBlocks;
Block<ExtensionCfgSection, ExtensionCfgKey> block = cfg.ReadNextBlock();
switch (block.Key)
{
Expand Down Expand Up @@ -201,6 +205,8 @@ internal void ParseExtensionsConfig(string TrainPath, Encoding Encoding, ref Uni
}
break;
}

readBlocks++;
}

// check for car objects and reverse if necessary
Expand Down
3 changes: 3 additions & 0 deletions source/Plugins/Train.OpenBve/Train/XML/TrainXmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
}

int carIndex = 0;

double perCarProgress = 0.25 / DocumentNodes.Count;
//Use the index here for easy access to the car count
for (int i = 0; i < DocumentNodes.Count; i++)
{
Plugin.CurrentProgress = Plugin.LastProgress + perCarProgress * i;
if (carIndex > Train.Cars.Length - 1)
{
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.");
Expand Down