Allow loading settings from external file
This commit is contained in:
parent
09fa50602a
commit
e6d75aa2f0
6 changed files with 225 additions and 6 deletions
197
src/Managers/ConfigManager.cs
Normal file
197
src/Managers/ConfigManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue