/// <summary>Initializes the program. A matching call to deinitialize must be made when the program is terminated.</summary> /// <returns>Whether the initialization was successful.</returns> private static bool Initialize() { if (!Plugins.LoadPlugins()) { return(false); } if (!Screen.Initialize()) { MessageBox.Show("SDL failed to initialize the video subsystem.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } Joysticks.RefreshJoysticks(); // begin HACK // //One degree in radians const double degrees = 0.0174532925199433; World.VerticalViewingAngle = 45.0 * degrees; World.HorizontalViewingAngle = 2.0 * Math.Atan(Math.Tan(0.5 * World.VerticalViewingAngle) * World.AspectRatio); World.OriginalVerticalViewingAngle = World.VerticalViewingAngle; World.ExtraViewingDistance = 50.0; World.ForwardViewingDistance = (double)Interface.CurrentOptions.ViewingDistance; World.BackwardViewingDistance = 0.0; World.BackgroundImageDistance = (double)Interface.CurrentOptions.ViewingDistance; // end HACK // ClearLogFile(); return(true); }
/******************** * MENU C'TOR *********************/ public SingleMenu(MenuType menuType, int data = 0) { int i, menuItem; int jump = 0; Size size; Align = Renderer.TextAlignment.TopMiddle; Height = Width = 0; Selection = 0; // defaults to first menu item switch (menuType) { case MenuType.Top: // top level menu for (i = 0; i < Game.Stations.Length; i++) { if (Game.PlayerStopsAtStation(i) & Game.Stations[i].Stops.Length > 0) { jump = 1; break; } } Items = new MenuEntry[4 + jump]; Items[0] = new MenuCommand(Interface.GetInterfaceString("menu_resume"), MenuTag.BackToSim, 0); if (jump > 0) { Items[1] = new MenuCommand(Interface.GetInterfaceString("menu_jump"), MenuTag.MenuJumpToStation, 0); } Items[1 + jump] = new MenuCommand(Interface.GetInterfaceString("menu_exit"), MenuTag.MenuExitToMainMenu, 0); Items[2 + jump] = new MenuCommand(Interface.GetInterfaceString("menu_customize_controls"), MenuTag.MenuControls, 0); Items[3 + jump] = new MenuCommand(Interface.GetInterfaceString("menu_quit"), MenuTag.MenuQuit, 0); break; case MenuType.JumpToStation: // list of stations to jump to // count the number of available stations menuItem = 0; for (i = 0; i < Game.Stations.Length; i++) { if (Game.PlayerStopsAtStation(i) & Game.Stations [i].Stops.Length > 0) { menuItem++; } } // list available stations, selecting the next station as predefined choice jump = 0; // no jump found yet Items = new MenuEntry[menuItem + 1]; Items[0] = new MenuCommand(Interface.GetInterfaceString("menu_back"), MenuTag.MenuBack, 0); menuItem = 1; for (i = 0; i < Game.Stations.Length; i++) { if (Game.PlayerStopsAtStation(i) & Game.Stations[i].Stops.Length > 0) { Items[menuItem] = new MenuCommand(Game.Stations[i].Name, MenuTag.JumpToStation, i); // if no preferred jump-to-station found yet and this station is // after the last station the user stopped at, select this item if (jump == 0 && i > TrainManager.PlayerTrain.LastStation) { jump = i; Selection = menuItem; } menuItem++; } } Align = Renderer.TextAlignment.TopLeft; break; case MenuType.ExitToMainMenu: Items = new MenuEntry[3]; Items[0] = new MenuCaption(Interface.GetInterfaceString("menu_exit_question")); Items[1] = new MenuCommand(Interface.GetInterfaceString("menu_exit_no"), MenuTag.MenuBack, 0); Items[2] = new MenuCommand(Interface.GetInterfaceString("menu_exit_yes"), MenuTag.ExitToMainMenu, 0); Selection = 1; break; case MenuType.Quit: // ask for quit confirmation Items = new MenuEntry[3]; Items[0] = new MenuCaption(Interface.GetInterfaceString("menu_quit_question")); Items[1] = new MenuCommand(Interface.GetInterfaceString("menu_quit_no"), MenuTag.MenuBack, 0); Items[2] = new MenuCommand(Interface.GetInterfaceString("menu_quit_yes"), MenuTag.Quit, 0); Selection = 1; break; case MenuType.Controls: //Refresh the joystick list Joysticks.RefreshJoysticks(); Items = new MenuEntry[Interface.CurrentControls.Length + 1]; Items[0] = new MenuCommand(Interface.GetInterfaceString("menu_back"), MenuTag.MenuBack, 0); for (i = 0; i < Interface.CurrentControls.Length; i++) { Items[i + 1] = new MenuCommand(Interface.CurrentControls[i].Command.ToString(), MenuTag.Control, i); } Align = Renderer.TextAlignment.TopLeft; break; case MenuType.Control: //Refresh the joystick list Joysticks.RefreshJoysticks(); Selection = SelectionNone; Items = new MenuEntry[4]; // get code name and description Interface.Control loadedControl = Interface.CurrentControls[data]; for (int h = 0; h < Interface.CommandInfos.Length; h++) { if (Interface.CommandInfos[h].Command == loadedControl.Command) { Items[0] = new MenuCommand(loadedControl.Command.ToString() + " - " + Interface.CommandInfos[h].Description, MenuTag.None, 0); break; } } // get assignment String str = ""; switch (loadedControl.Method) { case Interface.ControlMethod.Keyboard: if (loadedControl.Modifier != Interface.KeyboardModifier.None) { str = Interface.GetInterfaceString("menu_keyboard") + " [" + loadedControl.Modifier + "-" + loadedControl.Key + "]"; } else { str = Interface.GetInterfaceString("menu_keyboard") + " [" + loadedControl.Key + "]"; } break; case Interface.ControlMethod.Joystick: str = Interface.GetInterfaceString("menu_joystick") + " " + loadedControl.Device + " [" + loadedControl.Component + " " + loadedControl.Element + "]"; switch (loadedControl.Component) { case Interface.JoystickComponent.FullAxis: case Interface.JoystickComponent.Axis: str += " " + (loadedControl.Direction == 1 ? Interface.GetInterfaceString("menu_joystickdirection_positive") : Interface.GetInterfaceString("menu_joystickdirection_negative")); break; // case Interface.JoystickComponent.Button: // NOTHING TO DO FOR THIS CASE! // str = str; // break; case Interface.JoystickComponent.Hat: str += " " + (OpenTK.Input.HatPosition)loadedControl.Direction; break; case Interface.JoystickComponent.Invalid: str = Interface.GetInterfaceString("menu_joystick_notavailable"); break; } break; case Interface.ControlMethod.Invalid: str = Interface.GetInterfaceString("menu_joystick_notavailable"); break; } Items[1] = new MenuCommand(Interface.GetInterfaceString("menu_assignment_current") + " " + str, MenuTag.None, 0); Items[2] = new MenuCommand(" ", MenuTag.None, 0); Items[3] = new MenuCommand(Interface.GetInterfaceString("menu_assign"), MenuTag.None, 0); break; } // compute menu extent for (i = 0; i < Items.Length; i++) { size = Renderer.MeasureString(Game.Menu.MenuFont, Items [i].Text); if (size.Width > Width) { Width = size.Width; } if (!(Items[i] is MenuCaption) && size.Width > ItemWidth) { ItemWidth = size.Width; } } Height = Items.Length * Game.Menu.LineHeight; TopItem = 0; }
private static void Main(string[] args) { #if !DEBUG // Add handler for UI thread exceptions Application.ThreadException += new ThreadExceptionEventHandler(CrashHandler.UIThreadException); // Force all WinForms errors to go through handler Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // This handler is for catching non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CrashHandler.CurrentDomain_UnhandledException); #endif //Determine the current CPU architecture- //ARM will generally only support OpenGL-ES PortableExecutableKinds peKind; typeof(object).Module.GetPEKind(out peKind, out CurrentCPUArchitecture); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //--- determine the running environment --- //I wonder if disabling this hack will stop the craashing on Linux.... CurrentlyRunningOnMono = Type.GetType("Mono.Runtime") != null; //Doesn't appear to, but Mono have fixed the button appearance bug 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(Interface.GetInterfaceString("errors_filesystem_invalid") + Environment.NewLine + Environment.NewLine + ex.Message, Interface.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } //Platform specific startup checks if (CurrentlyRunningOnMono) { // --- Check if we're running as root, and prompt not to --- if (getuid() == 0) { MessageBox.Show( "You are currently running as the root user." + System.Environment.NewLine + "This is a bad idea, please dont!", Interface.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand); } } else { if (!System.IO.File.Exists(System.IO.Path.Combine(Environment.SystemDirectory, "OpenAL32.dll"))) { MessageBox.Show( "OpenAL was not found on your system, and will now be installed." + System.Environment.NewLine + System.Environment.NewLine + "Please follow the install prompts.", Interface.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand); ProcessStartInfo info = new ProcessStartInfo(System.IO.Path.Combine(FileSystem.DataFolder, "Dependencies\\Win32\\oalinst.exe")); info.UseShellExecute = true; if (Environment.OSVersion.Version.Major >= 6) { info.Verb = "runas"; } try { System.Diagnostics.Process p = System.Diagnostics.Process.Start(info); p.WaitForExit(); } catch (Win32Exception) { MessageBox.Show( "An error occured during OpenAL installation....", Interface.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand); } } } // --- load options and controls --- Interface.LoadOptions(); //Switch between SDL2 and native backends; use native backend by default var options = new ToolkitOptions(); if (Interface.CurrentOptions.PreferNativeBackend) { options.Backend = PlatformBackend.PreferNative; } Toolkit.Init(options); // --- load language --- { string folder = Program.FileSystem.GetDataFolder("Languages"); try { string[] LanguageFiles = Directory.GetFiles(folder, "*.cfg"); foreach (var File in LanguageFiles) { Interface.AddLanguage(File); } } catch { MessageBox.Show(@"An error occured whilst attempting to load the default language files."); //Environment.Exit(0); } } 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); } // --- 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 (!string.IsNullOrEmpty(Game.TrainName)) { 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; } if (folder == null) { continue; } 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) { Joysticks.RefreshJoysticks(); // 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; //Thread.Sleep(20); 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) { CrashHandler.LoadingCrash(ex.Message, true); 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; RestartArguments = ""; break; } } } if (!found) { MessageBox.Show("The route and train loader encountered the following critical error: " + Environment.NewLine + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); CrashHandler.LoadingCrash(ex.Message, false); RestartArguments = ""; } } #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); } } }