diff --git a/src/Managers/ConfigManager.cs b/src/Managers/ConfigManager.cs new file mode 100644 index 0000000..b5ef9d8 --- /dev/null +++ b/src/Managers/ConfigManager.cs @@ -0,0 +1,197 @@ +using System; + +namespace OpenbveFcmbTrainPlugin +{ + /// A class for loading plugin settings from a file. + internal static class ConfigManager + { + /// Represents a collection of plugin settings. + 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; + } + + /// Represents the plugin settings. + internal static SettingsCollection PluginSettings = new SettingsCollection(); + + /// Loads a config file. + 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; + } + } + } + } + } + } + + } +} diff --git a/src/Managers/MessageManager.cs b/src/Managers/MessageManager.cs index ee716a3..1954580 100644 --- a/src/Managers/MessageManager.cs +++ b/src/Managers/MessageManager.cs @@ -2,6 +2,7 @@ namespace OpenbveFcmbTrainPlugin { + /// A class for displaying messages to the user. internal static class MessageManager { /// The callback function to show an interface message. diff --git a/src/Managers/SoundManager.cs b/src/Managers/SoundManager.cs index f1a3a42..20298d0 100644 --- a/src/Managers/SoundManager.cs +++ b/src/Managers/SoundManager.cs @@ -2,6 +2,7 @@ namespace OpenbveFcmbTrainPlugin { + /// A class for playing sounds in the simulation. internal static class SoundManager { /// An array storing the sound handles. diff --git a/src/OpenbveFcmbTrainPlugin.cs b/src/OpenbveFcmbTrainPlugin.cs index 218f572..ad3d4f5 100644 --- a/src/OpenbveFcmbTrainPlugin.cs +++ b/src/OpenbveFcmbTrainPlugin.cs @@ -23,6 +23,7 @@ namespace OpenbveFcmbTrainPlugin /// Whether the plugin was loaded successfully. 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); diff --git a/src/OpenbveFcmbTrainPlugin.csproj b/src/OpenbveFcmbTrainPlugin.csproj index b9f9dea..164a12d 100644 --- a/src/OpenbveFcmbTrainPlugin.csproj +++ b/src/OpenbveFcmbTrainPlugin.csproj @@ -46,6 +46,7 @@ + diff --git a/src/Train/Train.cs b/src/Train/Train.cs index 08f4560..f928b1a 100644 --- a/src/Train/Train.cs +++ b/src/Train/Train.cs @@ -58,19 +58,37 @@ namespace OpenbveFcmbTrainPlugin private Speed AccelerationSpeed = new Speed(0); /// Creates a new train with the device configuration provided. - /// The array of panel variables.The array of panel variables. 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(); - Devices.Add(new Doors(false, 5, 15, 10)); - Devices.Add(new Deadman(5.0, false)); - Devices.Add(new TrainStop()); - //Devices.Add(new AtcBombardier(30, 60, 20, 500)); - Devices.Add(new AtcDimetronic(25)); + 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()); + } + if (settings.AtcBombardierDeviceEnabled) + { + Devices.Add(new AtcBombardier(settings.AtcBombardierInitializationTime, settings.AtcBombardierYardIdleTime, settings.AtcBombardierYardSpeedLimit, settings.AtcBombardierBlinkTime)); + } + if (settings.AtcDimetronicDeviceEnabled) + { + Devices.Add(new AtcDimetronic(settings.AtcDimetronicYardSpeedLimit)); + } } /// Is called when the train should initialize.