OpenBve.Interface.LoadOptions C# (CSharp) Method

LoadOptions() static private method

Loads the options file from disk
static private LoadOptions ( ) : void
return void
		internal static void LoadOptions()
		{
			CurrentOptions = new Options();
			CultureInfo Culture = CultureInfo.InvariantCulture;
			string File = OpenBveApi.Path.CombineFile(Program.FileSystem.SettingsFolder, "options.cfg");
			if (System.IO.File.Exists(File))
			{
				// load options
				string[] Lines = System.IO.File.ReadAllLines(File, new System.Text.UTF8Encoding());
				string Section = "";
				for (int i = 0; i < Lines.Length; i++)
				{
					Lines[i] = Lines[i].Trim();
					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().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 "language":
									switch (Key)
									{
										case "code":
											Interface.CurrentOptions.LanguageCode = Value.Length != 0 ? Value : "en-US";
											break;
									} break;
								case "interface":
									switch (Key)
									{
										case "folder":
											Interface.CurrentOptions.UserInterfaceFolder = Value.Length != 0 ? Value : "Default";
											break;
										case "timetablemode":
											switch (Value.ToLowerInvariant())
											{
												case "none":
													Interface.CurrentOptions.TimeTableStyle = TimeTableMode.None;
													break;
												case "default":
													Interface.CurrentOptions.TimeTableStyle = TimeTableMode.Default;
													break;
												case "autogenerated":
													Interface.CurrentOptions.TimeTableStyle = TimeTableMode.AutoGenerated;
													break;
												case "prefercustom":
													Interface.CurrentOptions.TimeTableStyle = TimeTableMode.PreferCustom;
													break;
											}
											break;
									} break;
								case "display":
									switch (Key)
									{
										case "prefernativebackend":
											Interface.CurrentOptions.PreferNativeBackend = string.Compare(Value, "true", StringComparison.OrdinalIgnoreCase) == 0;
											break;
										case "mode":
											Interface.CurrentOptions.FullscreenMode = string.Compare(Value, "fullscreen", StringComparison.OrdinalIgnoreCase) == 0;
											break;
										case "vsync":
											Interface.CurrentOptions.VerticalSynchronization = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "windowwidth":
											{
												int a;
												if (!int.TryParse(Value, NumberStyles.Integer, Culture, out a))
												{
													a = 960;
												}
												Interface.CurrentOptions.WindowWidth = a;
											} break;
										case "windowheight":
											{
												int a;
												if (!int.TryParse(Value, NumberStyles.Integer, Culture, out a))
												{
													a = 600;
												}
												Interface.CurrentOptions.WindowHeight = a;
											} break;
										case "fullscreenwidth":
											{
												int a;
												if (!int.TryParse(Value, NumberStyles.Integer, Culture, out a))
												{
													a = 1024;
												}
												Interface.CurrentOptions.FullscreenWidth = a;
											} break;
										case "fullscreenheight":
											{
												int a;
												if (!int.TryParse(Value, NumberStyles.Integer, Culture, out a))
												{
													a = 768;
												}
												Interface.CurrentOptions.FullscreenHeight = a;
											} break;
										case "fullscreenbits":
											{
												int a;
												if (!int.TryParse(Value, NumberStyles.Integer, Culture, out a))
												{
													a = 32;
												}
												Interface.CurrentOptions.FullscreenBits = a;
											} break;
										case "mainmenuwidth":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.MainMenuWidth = a;
											} break;
										case "mainmenuheight":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.MainMenuHeight = a;
											} break;
										case "disabledisplaylists":
											Interface.CurrentOptions.DisableDisplayLists = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "loadinadvance":
											Interface.CurrentOptions.LoadInAdvance = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "unloadtextures":
											Interface.CurrentOptions.UnloadUnusedTextures = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "notextureresize":
											Interface.CurrentOptions.NoTextureResize = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
									} break;
								case "quality":
									switch (Key)
									{
										case "interpolation":
											switch (Value.ToLowerInvariant())
											{
												case "nearestneighbor": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.NearestNeighbor; break;
												case "bilinear": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.Bilinear; break;
												case "nearestneighbormipmapped": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.NearestNeighborMipmapped; break;
												case "bilinearmipmapped": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.BilinearMipmapped; break;
												case "trilinearmipmapped": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.TrilinearMipmapped; break;
												case "anisotropicfiltering": Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.AnisotropicFiltering; break;
												default: Interface.CurrentOptions.Interpolation = Interface.InterpolationMode.BilinearMipmapped; break;
											} break;
										case "anisotropicfilteringlevel":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.AnisotropicFilteringLevel = a;
											} break;
										case "anisotropicfilteringmaximum":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.AnisotropicFilteringMaximum = a;
											} break;
										case "antialiasinglevel":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.AntiAliasingLevel = a;
											} break;
										case "transparencymode":
											switch (Value.ToLowerInvariant())
											{
												case "sharp": Interface.CurrentOptions.TransparencyMode = Renderer.TransparencyMode.Performance; break;
												case "smooth": Interface.CurrentOptions.TransparencyMode = Renderer.TransparencyMode.Quality; break;
												default:
													{
														int a;
														if (int.TryParse(Value, NumberStyles.Integer, Culture, out a))
														{
															Interface.CurrentOptions.TransparencyMode = (Renderer.TransparencyMode)a;
														}
														else
														{
															Interface.CurrentOptions.TransparencyMode = Renderer.TransparencyMode.Quality;
														}
														break;
													}
											} break;
										case "viewingdistance":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.ViewingDistance = a;
											} break;
										case "motionblur":
											switch (Value.ToLowerInvariant())
											{
												case "low": Interface.CurrentOptions.MotionBlur = MotionBlurMode.Low; break;
												case "medium": Interface.CurrentOptions.MotionBlur = MotionBlurMode.Medium; break;
												case "high": Interface.CurrentOptions.MotionBlur = MotionBlurMode.High; break;
												default: Interface.CurrentOptions.MotionBlur = MotionBlurMode.None; break;
											} break;
									} break;
								case "objectoptimization":
									switch (Key)
									{
										case "basicthreshold":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.ObjectOptimizationBasicThreshold = a;
											} break;
										case "fullthreshold":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.ObjectOptimizationFullThreshold = a;
											} break;
									} break;
								case "simulation":
									switch (Key)
									{
										case "toppling":
											Interface.CurrentOptions.Toppling = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "collisions":
											Interface.CurrentOptions.Collisions = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "derailments":
											Interface.CurrentOptions.Derailments = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "blackbox":
											Interface.CurrentOptions.BlackBox = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "mode":
											switch (Value.ToLowerInvariant())
											{
												case "arcade": Interface.CurrentOptions.GameMode = Interface.GameMode.Arcade; break;
												case "normal": Interface.CurrentOptions.GameMode = Interface.GameMode.Normal; break;
												case "expert": Interface.CurrentOptions.GameMode = Interface.GameMode.Expert; break;
												default: Interface.CurrentOptions.GameMode = Interface.GameMode.Normal; break;
											} break;
										case "acceleratedtimefactor":
											int tf;
											int.TryParse(Value, NumberStyles.Integer, Culture, out tf);
											if (tf <= 0)
											{
												tf = 5;
											}
											Interface.CurrentOptions.TimeAccelerationFactor = tf;
											break;
									} break;
								case "controls":
									switch (Key)
									{
										case "usejoysticks":
											Interface.CurrentOptions.UseJoysticks = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "joystickaxiseb":
											Interface.CurrentOptions.AllowAxisEB = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "joystickaxisthreshold":
											{
												double a;
												double.TryParse(Value, NumberStyles.Float, Culture, out a);
												Interface.CurrentOptions.JoystickAxisThreshold = a;
											} break;
										case "keyrepeatdelay":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												if (a <= 0) a = 500;
												Interface.CurrentOptions.KeyRepeatDelay = 0.001 * (double)a;
											} break;
										case "keyrepeatinterval":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												if (a <= 0) a = 100;
												Interface.CurrentOptions.KeyRepeatInterval = 0.001 * (double)a;
											} break;
									} break;
								case "sound":
									switch (Key)
									{
										case "model":
											switch (Value.ToLowerInvariant())
											{
												case "linear": Interface.CurrentOptions.SoundModel = Sounds.SoundModels.Linear; break;
												default: Interface.CurrentOptions.SoundModel = Sounds.SoundModels.Inverse; break;
											}
											break;
										case "range":
											switch (Value.ToLowerInvariant())
											{
												case "low": Interface.CurrentOptions.SoundRange = SoundRange.Low; break;
												case "medium": Interface.CurrentOptions.SoundRange = SoundRange.Medium; break;
												case "high": Interface.CurrentOptions.SoundRange = SoundRange.High; break;
												default: Interface.CurrentOptions.SoundRange = SoundRange.Low; break;
											}
											break;
										case "number":
											{
												int a;
												int.TryParse(Value, NumberStyles.Integer, Culture, out a);
												Interface.CurrentOptions.SoundNumber = a < 16 ? 16 : a;
											} break;
									} break;
								case "verbosity":
									switch (Key)
									{
										case "showwarningmessages":
											Interface.CurrentOptions.ShowWarningMessages = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "showerrormessages":
											Interface.CurrentOptions.ShowErrorMessages = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
										case "debuglog":
											Program.GenerateDebugLogging = string.Compare(Value, "false", StringComparison.OrdinalIgnoreCase) != 0;
											break;
									} break;
								case "folders":
									switch (Key)
									{
										case "route":
											Interface.CurrentOptions.RouteFolder = Value;
											break;
										case "train":
											Interface.CurrentOptions.TrainFolder = Value;
											break;
									} break;
								case "proxy":
									switch (Key)
									{
										case "url":
											Interface.CurrentOptions.ProxyUrl = Value;
											break;
										case "username":
											Interface.CurrentOptions.ProxyUserName = Value;
											break;
										case "password":
											Interface.CurrentOptions.ProxyPassword = Value;
											break;
									} break;
								case "packages":
									switch (Key)
									{
										case "compression":
											switch (Value.ToLowerInvariant())
											{
												case "zip":
													Interface.CurrentOptions.packageCompressionType = CompressionType.Zip;
													break;
												case "bzip":
													Interface.CurrentOptions.packageCompressionType = CompressionType.BZ2;
													break;
												case "gzip":
													Interface.CurrentOptions.packageCompressionType = CompressionType.TarGZ;
													break;
											}
											break;
									} break;
								case "recentlyusedroutes":
									{
										int n = Interface.CurrentOptions.RecentlyUsedRoutes.Length;
										Array.Resize<string>(ref Interface.CurrentOptions.RecentlyUsedRoutes, n + 1);
										Interface.CurrentOptions.RecentlyUsedRoutes[n] = Value;
									} break;
								case "recentlyusedtrains":
									{
										int n = Interface.CurrentOptions.RecentlyUsedTrains.Length;
										Array.Resize<string>(ref Interface.CurrentOptions.RecentlyUsedTrains, n + 1);
										Interface.CurrentOptions.RecentlyUsedTrains[n] = Value;
									} break;
								case "routeencodings":
									{
										int a = System.Text.Encoding.UTF8.CodePage;
										int.TryParse(Key, NumberStyles.Integer, Culture, out a);
										int n = Interface.CurrentOptions.RouteEncodings.Length;
										Array.Resize<TextEncoding.EncodingValue>(ref Interface.CurrentOptions.RouteEncodings, n + 1);
										Interface.CurrentOptions.RouteEncodings[n].Codepage = a;
										Interface.CurrentOptions.RouteEncodings[n].Value = Value;
									} break;
								case "trainencodings":
									{
										int a = System.Text.Encoding.UTF8.CodePage;
										int.TryParse(Key, NumberStyles.Integer, Culture, out a);
										int n = Interface.CurrentOptions.TrainEncodings.Length;
										Array.Resize<TextEncoding.EncodingValue>(ref Interface.CurrentOptions.TrainEncodings, n + 1);
										Interface.CurrentOptions.TrainEncodings[n].Codepage = a;
										Interface.CurrentOptions.TrainEncodings[n].Value = Value;
									} break;
							}
						}
					}
				}
			}
			else
			{
				// file not found
				string Code = CultureInfo.CurrentUICulture.Name;
				if (string.IsNullOrEmpty(Code)) Code = "en-US";
				File = OpenBveApi.Path.CombineFile(Program.FileSystem.GetDataFolder("Languages"), Code + ".cfg");
				if (System.IO.File.Exists(File))
				{
					CurrentOptions.LanguageCode = Code;
				}
				else
				{
					try
					{
						int i = Code.IndexOf("-", StringComparison.Ordinal);
						if (i > 0)
						{
							Code = Code.Substring(0, i);
							File = OpenBveApi.Path.CombineFile(Program.FileSystem.GetDataFolder("Languages"), Code + ".cfg");
							if (System.IO.File.Exists(File))
							{
								CurrentOptions.LanguageCode = Code;
							}
						}
					}
					catch
					{
						CurrentOptions.LanguageCode = "en-US";
					}
				}
			}
		}
		internal static void SaveOptions()

Usage Example

Example #1
0
 private static void Main(string[] args)
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     // --- determine the running environment ---
     CurrentlyRunningOnMono    = Type.GetType("Mono.Runtime") != null;
     CurrentlyRunningOnWindows = Environment.OSVersion.Platform == PlatformID.Win32S | Environment.OSVersion.Platform == PlatformID.Win32Windows | Environment.OSVersion.Platform == PlatformID.Win32NT;
     CurrentHost = new Host();
     try {
         FileSystem = FileSystem.FromCommandLineArgs(args);
         FileSystem.CreateFileSystem();
     } catch (Exception ex) {
         MessageBox.Show("The file system configuration could not be accessed or is invalid due to the following reason:\n\n" + ex.Message, "openBVE", MessageBoxButtons.OK, MessageBoxIcon.Hand);
         return;
     }
     // --- set up packages ---
     SetPackageLookupDirectories();
     // --- load options and controls ---
     Interface.LoadOptions();
     Interface.LoadControls(null, out Interface.CurrentControls);
     {
         string folder = Program.FileSystem.GetDataFolder("Controls");
         string file   = OpenBveApi.Path.CombineFile(folder, "Default keyboard assignment.controls");
         Interface.Control[] controls;
         Interface.LoadControls(file, out controls);
         Interface.AddControls(ref Interface.CurrentControls, controls);
     }
     // --- load language ---
     {
         string folder = Program.FileSystem.GetDataFolder("Languages");
         string file   = OpenBveApi.Path.CombineFile(folder, Interface.CurrentOptions.LanguageCode + ".cfg");
         if (!System.IO.File.Exists(file))
         {
             file = OpenBveApi.Path.CombineFile(folder, "en-US.cfg");
         }
         Interface.LoadLanguage(file);
     }
     // --- check the command-line arguments for route and train ---
     formMain.MainDialogResult result = new formMain.MainDialogResult();
     for (int i = 0; i < args.Length; i++)
     {
         if (args[i].StartsWith("/route=", StringComparison.OrdinalIgnoreCase))
         {
             result.RouteFile     = args[i].Substring(7);
             result.RouteEncoding = System.Text.Encoding.UTF8;
             for (int j = 0; j < Interface.CurrentOptions.RouteEncodings.Length; j++)
             {
                 if (string.Compare(Interface.CurrentOptions.RouteEncodings[j].Value, result.RouteFile, StringComparison.InvariantCultureIgnoreCase) == 0)
                 {
                     result.RouteEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.RouteEncodings[j].Codepage);
                     break;
                 }
             }
         }
         else if (args[i].StartsWith("/train=", StringComparison.OrdinalIgnoreCase))
         {
             result.TrainFolder   = args[i].Substring(7);
             result.TrainEncoding = System.Text.Encoding.UTF8;
             for (int j = 0; j < Interface.CurrentOptions.TrainEncodings.Length; j++)
             {
                 if (string.Compare(Interface.CurrentOptions.TrainEncodings[j].Value, result.TrainFolder, StringComparison.InvariantCultureIgnoreCase) == 0)
                 {
                     result.TrainEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.TrainEncodings[j].Codepage);
                     break;
                 }
             }
         }
     }
     // --- check whether route and train exist ---
     if (result.RouteFile != null)
     {
         if (!System.IO.File.Exists(result.RouteFile))
         {
             result.RouteFile = null;
         }
     }
     if (result.TrainFolder != null)
     {
         if (!System.IO.Directory.Exists(result.TrainFolder))
         {
             result.TrainFolder = null;
         }
     }
     // --- if a route was provided but no train, try to use the route default ---
     if (result.RouteFile != null & result.TrainFolder == null)
     {
         bool isRW = string.Equals(System.IO.Path.GetExtension(result.RouteFile), ".rw", StringComparison.OrdinalIgnoreCase);
         CsvRwRouteParser.ParseRoute(result.RouteFile, isRW, result.RouteEncoding, null, null, null, true);
         if (Game.TrainName != null && Game.TrainName.Length != 0)
         {
             string folder = System.IO.Path.GetDirectoryName(result.RouteFile);
             while (true)
             {
                 string trainFolder = OpenBveApi.Path.CombineDirectory(folder, "Train");
                 if (System.IO.Directory.Exists(trainFolder))
                 {
                     folder = OpenBveApi.Path.CombineDirectory(trainFolder, Game.TrainName);
                     if (System.IO.Directory.Exists(folder))
                     {
                         string file = OpenBveApi.Path.CombineFile(folder, "train.dat");
                         if (System.IO.File.Exists(file))
                         {
                             result.TrainFolder   = folder;
                             result.TrainEncoding = System.Text.Encoding.UTF8;
                             for (int j = 0; j < Interface.CurrentOptions.TrainEncodings.Length; j++)
                             {
                                 if (string.Compare(Interface.CurrentOptions.TrainEncodings[j].Value, result.TrainFolder, StringComparison.InvariantCultureIgnoreCase) == 0)
                                 {
                                     result.TrainEncoding = System.Text.Encoding.GetEncoding(Interface.CurrentOptions.TrainEncodings[j].Codepage);
                                     break;
                                 }
                             }
                         }
                     }
                     break;
                 }
                 else
                 {
                     System.IO.DirectoryInfo info = System.IO.Directory.GetParent(folder);
                     if (info != null)
                     {
                         folder = info.FullName;
                     }
                     else
                     {
                         break;
                     }
                 }
             }
         }
         Game.Reset(false);
     }
     // --- show the main menu if necessary ---
     if (result.RouteFile == null | result.TrainFolder == null)
     {
         // begin HACK //
         if (!Joysticks.Initialize())
         {
             MessageBox.Show("SDL failed to initialize the joystick subsystem.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
             return;
         }
         // end HACK //
         result = formMain.ShowMainDialog(result);
     }
     else
     {
         result.Start = true;
     }
     // --- start the actual program ---
     if (result.Start)
     {
         if (Initialize())
         {
                                 #if !DEBUG
             try {
                                         #endif
             MainLoop.StartLoopEx(result);
                                         #if !DEBUG
         }
         catch (Exception ex) {
             bool found = false;
             for (int i = 0; i < TrainManager.Trains.Length; i++)
             {
                 if (TrainManager.Trains[i] != null && TrainManager.Trains[i].Plugin != null)
                 {
                     if (TrainManager.Trains[i].Plugin.LastException != null)
                     {
                         MessageBox.Show("The train plugin " + TrainManager.Trains[i].Plugin.PluginTitle + " caused a runtime exception: " + TrainManager.Trains[i].Plugin.LastException.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                         found = true;
                         break;
                     }
                 }
             }
             if (!found)
             {
                 MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
             }
         }
                                 #endif
         }
         Deinitialize();
     }
     // --- restart the program if necessary ---
     if (RestartArguments != null)
     {
         string arguments;
         if (FileSystem.RestartArguments.Length != 0 & RestartArguments.Length != 0)
         {
             arguments = FileSystem.RestartArguments + " " + RestartArguments;
         }
         else
         {
             arguments = FileSystem.RestartArguments + RestartArguments;
         }
         try {
             System.Diagnostics.Process.Start(FileSystem.RestartProcess, arguments);
         } catch (Exception ex) {
             MessageBox.Show(ex.Message + "\n\nProcess = " + FileSystem.RestartProcess + "\nArguments = " + arguments, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
All Usage Examples Of OpenBve.Interface::LoadOptions