Allow loading settings from external file

This commit is contained in:
Marc Riera 2024-12-01 18:47:08 +01:00
parent 09fa50602a
commit e6d75aa2f0
6 changed files with 225 additions and 6 deletions

View file

@ -0,0 +1,197 @@
using System;
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A class for loading plugin settings from a file.</summary>
internal static class ConfigManager
{
/// <summary>Represents a collection of plugin settings.</summary>
internal class SettingsCollection
{
internal bool DoorsDeviceEnabled;
internal bool DeadmanDeviceEnabled;
internal bool TrainStopDeviceEnabled;
internal bool AtcBombardierDeviceEnabled;
internal bool AtcDimetronicDeviceEnabled;
internal bool DoorsRequireClosingSound;
internal int DoorsClosingSoundDuration = 5;
internal int DoorsClosingSoundTimeout = 15;
internal int DoorsClosingSoundIndex = 10;
internal int DeadmanBrakeDelay = 5;
internal bool DeadmanRequireFullStop;
internal int AtcBombardierInitializationTime = 30;
internal int AtcBombardierYardIdleTime = 60;
internal int AtcBombardierYardSpeedLimit = 20;
internal int AtcBombardierBlinkTime = 500;
internal int AtcDimetronicYardSpeedLimit = 25;
}
/// <summary>Represents the plugin settings.</summary>
internal static SettingsCollection PluginSettings = new SettingsCollection();
/// <summary>Loads a config file.</summary>
internal static void LoadConfig(string folder, string file)
{
string configFile = OpenBveApi.Path.CombineFile(folder, file);
if (System.IO.File.Exists(configFile))
{
// load options
string[] Lines = System.IO.File.ReadAllLines(configFile, new System.Text.UTF8Encoding());
string Section = "";
for (int i = 0; i < Lines.Length; i++)
{
Lines[i] = Lines[i].Trim(new char[] { });
if (Lines[i].Length != 0 && !Lines[i].StartsWith(";", StringComparison.OrdinalIgnoreCase))
{
if (Lines[i].StartsWith("[", StringComparison.Ordinal) &
Lines[i].EndsWith("]", StringComparison.Ordinal))
{
Section = Lines[i].Substring(1, Lines[i].Length - 2).Trim(new char[] { }).ToLowerInvariant();
}
else
{
int j = Lines[i].IndexOf("=", StringComparison.OrdinalIgnoreCase);
string Key, Value;
if (j >= 0)
{
Key = Lines[i].Substring(0, j).TrimEnd().ToLowerInvariant();
Value = Lines[i].Substring(j + 1).TrimStart();
}
else
{
Key = "";
Value = Lines[i];
}
switch (Section)
{
case "doors":
switch (Key)
{
case "enabled":
PluginSettings.DoorsDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
case "brakedelay":
PluginSettings.DoorsRequireClosingSound = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
case "closingsoundduration":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.DoorsClosingSoundDuration = a;
}
}
break;
case "closingsoundtimeout":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.DoorsClosingSoundTimeout = a;
}
}
break;
case "closingsoundindex":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.DoorsClosingSoundIndex = a;
}
}
break;
}
break;
case "deadman":
switch (Key)
{
case "enabled":
PluginSettings.DeadmanDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
case "brakedelay":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.DeadmanBrakeDelay = a;
}
}
break;
case "requirefullstop":
PluginSettings.DeadmanRequireFullStop = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
}
break;
case "trainstop":
switch (Key)
{
case "enabled":
PluginSettings.TrainStopDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
}
break;
case "atcbombardier":
switch (Key)
{
case "enabled":
PluginSettings.AtcBombardierDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
case "initializationtime":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.AtcBombardierInitializationTime = a;
}
}
break;
case "yardidletime":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.AtcBombardierYardIdleTime = a;
}
}
break;
case "yardspeedlimit":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.AtcBombardierYardSpeedLimit = a;
}
}
break;
case "blinktime":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.AtcBombardierBlinkTime = a;
}
}
break;
}
break;
case "atcdimetronic":
switch (Key)
{
case "enabled":
PluginSettings.AtcDimetronicDeviceEnabled = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
break;
case "yardspeedlimit":
{
if (int.TryParse(Value, out int a))
{
PluginSettings.AtcDimetronicYardSpeedLimit = a;
}
}
break;
}
break;
}
}
}
}
}
}
}
}

View file

@ -2,6 +2,7 @@
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A class for displaying messages to the user.</summary>
internal static class MessageManager
{
/// <summary>The callback function to show an interface message.</summary>

View file

@ -2,6 +2,7 @@
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A class for playing sounds in the simulation.</summary>
internal static class SoundManager
{
/// <summary>An array storing the sound handles.</summary>

View file

@ -23,6 +23,7 @@ namespace OpenbveFcmbTrainPlugin
/// <returns>Whether the plugin was loaded successfully.</returns>
public bool Load(LoadProperties properties)
{
ConfigManager.LoadConfig(properties.TrainFolder, "OpenbveFcmbTrainPlugin.cfg");
properties.AISupport = AISupport.Basic;
properties.Panel = new int[256];
Train = new Train(properties.Panel);

View file

@ -46,6 +46,7 @@
<Compile Include="Managers\SoundManager.cs" />
<Compile Include="Managers\MessageManager.cs" />
<Compile Include="Devices\AtcDimetronic.cs" />
<Compile Include="Managers\ConfigManager.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Devices\" />

View file

@ -58,19 +58,37 @@ namespace OpenbveFcmbTrainPlugin
private Speed AccelerationSpeed = new Speed(0);
/// <summary>Creates a new train with the device configuration provided.</summary>
/// <param name="panel">The array of panel variables.</pa
/// <param name="panel">The array of panel variables.</param>
internal Train(int[] panel)
{
Specs = new VehicleSpecs(0, BrakeTypes.ElectromagneticStraightAirBrake, 0, false, 0);
State = new VehicleState(0.0, new Speed(0.0), 0.0, 0.0, 0.0, 0.0, 0.0);
Panel = panel;
PhysicalHandles = new DriverHandles();
// Create list of devices and populate it according to settings
Devices = new List<Device>();
Devices.Add(new Doors(false, 5, 15, 10));
Devices.Add(new Deadman(5.0, false));
ConfigManager.SettingsCollection settings = ConfigManager.PluginSettings;
if (settings.DoorsDeviceEnabled)
{
Devices.Add(new Doors(settings.DoorsRequireClosingSound, settings.DoorsClosingSoundDuration, settings.DoorsClosingSoundTimeout, settings.DoorsClosingSoundIndex));
}
if (settings.DeadmanDeviceEnabled)
{
Devices.Add(new Deadman(settings.DeadmanBrakeDelay, settings.DeadmanRequireFullStop));
}
if (settings.TrainStopDeviceEnabled)
{
Devices.Add(new TrainStop());
//Devices.Add(new AtcBombardier(30, 60, 20, 500));
Devices.Add(new AtcDimetronic(25));
}
if (settings.AtcBombardierDeviceEnabled)
{
Devices.Add(new AtcBombardier(settings.AtcBombardierInitializationTime, settings.AtcBombardierYardIdleTime, settings.AtcBombardierYardSpeedLimit, settings.AtcBombardierBlinkTime));
}
if (settings.AtcDimetronicDeviceEnabled)
{
Devices.Add(new AtcDimetronic(settings.AtcDimetronicYardSpeedLimit));
}
}
/// <summary>Is called when the train should initialize.</summary>