Example #1
0
        internal static void ProcessKeyboard()
        {
            if (Interface.CurrentOptions.UseJoysticks)
            {
                for (int k = 0; k < JoystickManager.AttachedJoysticks.Length; k++)
                {
                    JoystickManager.AttachedJoysticks[k].Poll();
                }
            }
            if (Game.CurrentInterface == Game.InterfaceType.Menu && Game.Menu.IsCustomizingControl())
            {
                if (Interface.CurrentOptions.UseJoysticks)
                {
                    for (int k = 0; k < JoystickManager.AttachedJoysticks.Length; k++)
                    {
                        int axes = JoystickManager.AttachedJoysticks[k].AxisCount();
                        for (int i = 0; i < axes; i++)
                        {
                            double aa = JoystickManager.AttachedJoysticks[k].GetAxis(i);
                            if (aa < -0.75)
                            {
                                Game.Menu.SetControlJoyCustomData(k, Interface.JoystickComponent.Axis, i, -1);
                                return;
                            }
                            if (aa > 0.75)
                            {
                                Game.Menu.SetControlJoyCustomData(k, Interface.JoystickComponent.Axis, i, 1);
                                return;
                            }
                        }
                        int buttons = JoystickManager.AttachedJoysticks[k].ButtonCount();
                        for (int i = 0; i < buttons; i++)
                        {
                            if (JoystickManager.AttachedJoysticks[k].GetButton(i) == ButtonState.Pressed)
                            {
                                Game.Menu.SetControlJoyCustomData(k, Interface.JoystickComponent.Button, i, 1);
                                return;
                            }
                        }
                        int hats = JoystickManager.AttachedJoysticks[k].HatCount();
                        for (int i = 0; i < hats; i++)
                        {
                            JoystickHatState hat = JoystickManager.AttachedJoysticks[k].GetHat(i);
                            if (hat.Position != HatPosition.Centered)
                            {
                                Game.Menu.SetControlJoyCustomData(k, Interface.JoystickComponent.Hat, i, (int)hat.Position);
                                return;
                            }
                        }
                    }
                }
                return;
            }
            if (World.MouseGrabEnabled)
            {
                previousMouseState = currentMouseState;
                currentMouseState  = Mouse.GetState();
                if (previousMouseState != currentMouseState)
                {
                    if (World.MouseGrabIgnoreOnce)
                    {
                        World.MouseGrabIgnoreOnce = false;
                    }
                    else if (World.MouseGrabEnabled)
                    {
                        World.MouseGrabTarget = new OpenBveApi.Math.Vector2(currentMouseState.X - previousMouseState.X, currentMouseState.Y - previousMouseState.Y);
                    }
                }
            }

            //Traverse the controls array
            for (int i = 0; i < Interface.CurrentControls.Length; i++)
            {
                int currentDevice = Interface.CurrentControls[i].Device;
                //Check to see if our device is currently available
                switch (Interface.CurrentControls[i].Method)
                {
                case Interface.ControlMethod.Joystick:
                    if (JoystickManager.AttachedJoysticks.Length == 0 || !Joystick.GetCapabilities(Interface.CurrentControls[i].Device).IsConnected)
                    {
                        //Not currently connected
                        continue;
                    }
                    break;

                case Interface.ControlMethod.RailDriver:
                    if (JoystickManager.RailDriverIndex == -1)
                    {
                        //Not currently connected
                        continue;
                    }
                    currentDevice = JoystickManager.RailDriverIndex;
                    break;

                default:
                    //Not a joystick / RD
                    continue;
                }


                switch (Interface.CurrentControls[i].Component)
                {
                case Interface.JoystickComponent.Axis:
                    var axisState = JoystickManager.GetAxis(currentDevice, Interface.CurrentControls[i].Element);
                    if (axisState.ToString(CultureInfo.InvariantCulture) != Interface.CurrentControls[i].LastState)
                    {
                        Interface.CurrentControls[i].LastState = axisState.ToString(CultureInfo.InvariantCulture);
                        if (Interface.CurrentControls[i].InheritedType == Interface.CommandType.AnalogHalf)
                        {
                            if (Math.Sign(axisState) == Math.Sign(Interface.CurrentControls[i].Direction))
                            {
                                axisState = Math.Abs(axisState);
                                if (axisState < Interface.CurrentOptions.JoystickAxisThreshold)
                                {
                                    Interface.CurrentControls[i].AnalogState = 0.0;
                                }
                                else if (Interface.CurrentOptions.JoystickAxisThreshold != 1.0)
                                {
                                    Interface.CurrentControls[i].AnalogState = (axisState - Interface.CurrentOptions.JoystickAxisThreshold) / (1.0 - Interface.CurrentOptions.JoystickAxisThreshold);
                                }
                                else
                                {
                                    Interface.CurrentControls[i].AnalogState = 1.0;
                                }
                            }
                        }
                        else if (Interface.CurrentControls[i].InheritedType == Interface.CommandType.AnalogFull)
                        {
                            axisState *= (float)Interface.CurrentControls[i].Direction;
                            if (axisState > -Interface.CurrentOptions.JoystickAxisThreshold & axisState < Interface.CurrentOptions.JoystickAxisThreshold)
                            {
                                Interface.CurrentControls[i].AnalogState = 0.0;
                            }
                            else if (Interface.CurrentOptions.JoystickAxisThreshold != 1.0)
                            {
                                if (axisState < 0.0)
                                {
                                    Interface.CurrentControls[i].AnalogState = (axisState + Interface.CurrentOptions.JoystickAxisThreshold) / (1.0 - Interface.CurrentOptions.JoystickAxisThreshold);
                                }
                                else if (axisState > 0.0)
                                {
                                    Interface.CurrentControls[i].AnalogState = (axisState - Interface.CurrentOptions.JoystickAxisThreshold) / (1.0 - Interface.CurrentOptions.JoystickAxisThreshold);
                                }
                                else
                                {
                                    Interface.CurrentControls[i].AnalogState = 0.0;
                                }
                            }
                            else
                            {
                                Interface.CurrentControls[i].AnalogState = (double)Math.Sign(axisState);
                            }
                        }
                        else
                        {
                            if (Math.Sign(axisState) == Math.Sign(Interface.CurrentControls[i].Direction))
                            {
                                axisState = Math.Abs(axisState);
                                if (axisState < Interface.CurrentOptions.JoystickAxisThreshold)
                                {
                                    axisState = 0.0f;
                                }
                                else if (Interface.CurrentOptions.JoystickAxisThreshold != 1.0)
                                {
                                    axisState = (float)((axisState - Interface.CurrentOptions.JoystickAxisThreshold) / (1.0 - Interface.CurrentOptions.JoystickAxisThreshold));
                                }
                                else
                                {
                                    axisState = 1.0f;
                                }
                                if (Interface.CurrentControls[i].DigitalState == Interface.DigitalControlState.Released | Interface.CurrentControls[i].DigitalState == Interface.DigitalControlState.ReleasedAcknowledged)
                                {
                                    if (axisState > 0.67)
                                    {
                                        Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Pressed;
                                    }
                                }
                                else
                                {
                                    if (axisState < 0.33)
                                    {
                                        Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Released;
                                    }
                                }
                            }
                        }
                    }
                    break;

                case Interface.JoystickComponent.Button:
                    //Load the current state
                    var buttonState = JoystickManager.GetButton(currentDevice, Interface.CurrentControls[i].Element);
                    //Test whether the state is the same as the last frame
                    if (buttonState.ToString() != Interface.CurrentControls[i].LastState)
                    {
                        if (buttonState == ButtonState.Pressed)
                        {
                            Interface.CurrentControls[i].AnalogState  = 1.0;
                            Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Pressed;
                            AddControlRepeat(i);
                        }
                        else
                        {
                            Interface.CurrentControls[i].AnalogState  = 0.0;
                            Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Released;
                            RemoveControlRepeat(i);
                        }
                        //Store the state
                        Interface.CurrentControls[i].LastState = buttonState.ToString();
                    }
                    break;

                case Interface.JoystickComponent.Hat:
                    //Load the current state
                    var hatState = JoystickManager.GetHat(currentDevice, Interface.CurrentControls[i].Element).Position;
                    //Test if the state is the same as last frame
                    if (hatState.ToString() != Interface.CurrentControls[i].LastState)
                    {
                        if ((int)hatState == Interface.CurrentControls[i].Direction)
                        {
                            Interface.CurrentControls[i].AnalogState  = 1.0;
                            Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Pressed;
                            AddControlRepeat(i);
                        }
                        else
                        {
                            Interface.CurrentControls[i].AnalogState  = 0.0;
                            Interface.CurrentControls[i].DigitalState = Interface.DigitalControlState.Released;
                            RemoveControlRepeat(i);
                        }
                        //Store the state
                        Interface.CurrentControls[i].LastState = hatState.ToString();
                    }
                    break;
                }
            }
        }
Example #2
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;
            Joysticks   = new JoystickManager();
            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 && !CurrentlyRunningOnWindows)
            {
                // --- 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();
            CommandLine.ParseArguments(args, ref result);
            // --- 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;
                //Apply translations
                Interface.SetInGameLanguage(Interface.CurrentLanguageCode);
            }
            // --- 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.ToString(), 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(System.IO.File.Exists(FileSystem.RestartProcess) ? FileSystem.RestartProcess : Application.ExecutablePath, arguments);
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message + "\n\nProcess = " + FileSystem.RestartProcess + "\nArguments = " + arguments, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Example #3
0
        private static void Main(string[] args)
        {
            // Add handler for UI thread exceptions
            Application.ThreadException += (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 += (CrashHandler.CurrentDomain_UnhandledException);


            //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;
            Joysticks   = new JoystickManager();
            CurrentHost = new Host();
            try {
                FileSystem = FileSystem.FromCommandLineArgs(args);
                FileSystem.CreateFileSystem();
            } catch (Exception ex) {
                MessageBox.Show(Translations.GetInterfaceString("errors_filesystem_invalid") + Environment.NewLine + Environment.NewLine + ex.Message, Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            Renderer     = new NewRenderer();
            Sounds       = new Sounds();
            CurrentRoute = new CurrentRoute(Renderer);

            //Platform specific startup checks
            if (CurrentlyRunningOnMono && !CurrentlyRunningOnWindows)
            {
                // --- 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!", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }

                if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
                {
                    //Mono's platform detection doesn't reliably differentiate between OS-X and Unix
                    CurrentlyRunningOnMacOS = true;
                }
            }
            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.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);

                    ProcessStartInfo info = new ProcessStartInfo(Path.Combine(FileSystem.DataFolder, "Dependencies\\Win32\\oalinst.exe"));
                    info.UseShellExecute = true;
                    if (Environment.OSVersion.Version.Major >= 6)
                    {
                        info.Verb = "runas";
                    }
                    try
                    {
                        Process p = Process.Start(info);
                        if (p != null)
                        {
                            p.WaitForExit();
                        }
                        else
                        {
                            //For unknown reasons, the process failed to trigger, but did not raise an exception itself
                            //Throw one
                            throw new Win32Exception();
                        }
                    }
                    catch (Win32Exception)
                    {
                        MessageBox.Show(
                            "An error occured during OpenAL installation....", Translations.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");

            Translations.LoadLanguageFiles(folder);

            folder = Program.FileSystem.GetDataFolder("Cursors");
            Cursors.LoadCursorImages(folder);

            Interface.LoadControls(null, out Interface.CurrentControls);
            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);

            InputDevicePlugin.LoadPlugins(Program.FileSystem);

            // --- check the command-line arguments for route and train ---
            formMain.MainDialogResult result = new formMain.MainDialogResult();
            CommandLine.ParseArguments(args, ref result);
            // --- 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)
            {
                if (!Plugins.LoadPlugins())
                {
                    throw new Exception("Unable to load the required plugins- Please reinstall OpenBVE");
                }
                Game.Reset(false);
                bool loaded = false;
                for (int i = 0; i < Program.CurrentHost.Plugins.Length; i++)
                {
                    if (Program.CurrentHost.Plugins[i].Route != null && Program.CurrentHost.Plugins[i].Route.CanLoadRoute(result.RouteFile))
                    {
                        object Route = (object)Program.CurrentRoute;                         //must cast to allow us to use the ref keyword.
                        Program.CurrentHost.Plugins[i].Route.LoadRoute(result.RouteFile, result.RouteEncoding, null, null, null, true, ref Route);
                        Program.CurrentRoute = (CurrentRoute)Route;
                        Program.Renderer.Lighting.OptionAmbientColor  = CurrentRoute.Atmosphere.AmbientLightColor;
                        Program.Renderer.Lighting.OptionDiffuseColor  = CurrentRoute.Atmosphere.DiffuseLightColor;
                        Program.Renderer.Lighting.OptionLightPosition = CurrentRoute.Atmosphere.LightPosition;
                        loaded = true;
                        break;
                    }
                }
                Plugins.UnloadPlugins();
                if (!loaded)
                {
                    throw new Exception("No plugins capable of loading routefile " + result.RouteFile + " were found.");
                }
                if (!string.IsNullOrEmpty(Interface.CurrentOptions.TrainName))
                {
                    folder = System.IO.Path.GetDirectoryName(result.RouteFile);
                    while (true)
                    {
                        string trainFolder = OpenBveApi.Path.CombineDirectory(folder, "Train");
                        if (System.IO.Directory.Exists(trainFolder))
                        {
                            try
                            {
                                folder = OpenBveApi.Path.CombineDirectory(trainFolder, Interface.CurrentOptions.TrainName);
                            }
                            catch (Exception ex)
                            {
                                if (ex is ArgumentException)
                                {
                                    break;
                                }
                            }
                            if (System.IO.Directory.Exists(folder))
                            {
                                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;
                //Apply translations
                Translations.SetInGameLanguage(Translations.CurrentLanguageCode);
            }
            // --- 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)
                            {
                                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)
                    {
                        if (ex is System.DllNotFoundException)
                        {
                            Interface.AddMessage(MessageType.Critical, false, "The required system library " + ex.Message + " was not found on the system.");
                            switch (ex.Message)
                            {
                            case "libopenal.so.1":
                                MessageBox.Show("openAL was not found on this system. \n Please install libopenal1 via your distribtion's package management system.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                break;

                            default:
                                MessageBox.Show("The required system library " + ex.Message + " was not found on this system.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                break;
                            }
                        }
                        else
                        {
                            Interface.AddMessage(MessageType.Critical, false, "The route and train loader encountered the following critical error: " + ex.Message);
                            CrashHandler.LoadingCrash(ex + Environment.StackTrace, 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(System.IO.File.Exists(FileSystem.RestartProcess) ? FileSystem.RestartProcess : Application.ExecutablePath, arguments);
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message + "\n\nProcess = " + FileSystem.RestartProcess + "\nArguments = " + arguments, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Example #4
0
        private static void Main(string[] args)
        {
            // --- load options and controls ---
            try
            {
                FileSystem = FileSystem.FromCommandLineArgs(args, CurrentHost);
                FileSystem.CreateFileSystem();
                Interface.LoadOptions();
            }
            catch
            {
                // ignored
            }
            //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);

            // Add handler for UI thread exceptions
            Application.ThreadException += (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 += (CrashHandler.CurrentDomain_UnhandledException);


            //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);
            CurrentHost = new Host();
            if (IntPtr.Size == 4)
            {
                Joysticks = new JoystickManager32();
            }
            else
            {
                Joysticks = new JoystickManager64();
            }
            try {
                FileSystem = FileSystem.FromCommandLineArgs(args, CurrentHost);
                FileSystem.CreateFileSystem();
            } catch (Exception ex) {
                MessageBox.Show(Translations.GetInterfaceString("errors_filesystem_invalid") + Environment.NewLine + Environment.NewLine + ex.Message, Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            Renderer     = new NewRenderer(CurrentHost, Interface.CurrentOptions, FileSystem);
            Sounds       = new Sounds();
            CurrentRoute = new CurrentRoute(CurrentHost, Renderer);

            //Platform specific startup checks
            // --- Check if we're running as root, and prompt not to ---
            if (CurrentHost.Platform == HostPlatform.GNULinux && (getuid() == 0 || geteuid() == 0))
            {
                MessageBox.Show(
                    "You are currently running as the root user, or via the sudo command." + System.Environment.NewLine +
                    "This is a bad idea, please dont!", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }



            TrainManager = new TrainManager(CurrentHost, Renderer, Interface.CurrentOptions, FileSystem);

            // --- load language ---
            string folder = Program.FileSystem.GetDataFolder("Languages");

            Translations.LoadLanguageFiles(folder);

            folder = Program.FileSystem.GetDataFolder("Cursors");
            Cursors.LoadCursorImages(folder);

            Interface.LoadControls(null, out Interface.CurrentControls);
            folder = Program.FileSystem.GetDataFolder("Controls");
            string file = OpenBveApi.Path.CombineFile(folder, "Default keyboard assignment.controls");

            Control[] controls;
            Interface.LoadControls(file, out controls);
            Interface.AddControls(ref Interface.CurrentControls, controls);

            InputDevicePlugin.LoadPlugins(Program.FileSystem);

            // --- check the command-line arguments for route and train ---
            formMain.MainDialogResult result = new formMain.MainDialogResult();
            CommandLine.ParseArguments(args, ref result);
            // --- 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)
            {
                string error;
                if (!CurrentHost.LoadPlugins(FileSystem, Interface.CurrentOptions, out error, TrainManager, Renderer))
                {
                    MessageBox.Show(error, @"OpenBVE", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw new Exception("Unable to load the required plugins- Please reinstall OpenBVE");
                }
                Game.Reset(false);
                bool loaded = false;
                for (int i = 0; i < Program.CurrentHost.Plugins.Length; i++)
                {
                    if (Program.CurrentHost.Plugins[i].Route != null && Program.CurrentHost.Plugins[i].Route.CanLoadRoute(result.RouteFile))
                    {
                        object Route = (object)Program.CurrentRoute;                         //must cast to allow us to use the ref keyword.
                        Program.CurrentHost.Plugins[i].Route.LoadRoute(result.RouteFile, result.RouteEncoding, null, null, null, true, ref Route);
                        Program.CurrentRoute = (CurrentRoute)Route;
                        Program.Renderer.Lighting.OptionAmbientColor  = CurrentRoute.Atmosphere.AmbientLightColor;
                        Program.Renderer.Lighting.OptionDiffuseColor  = CurrentRoute.Atmosphere.DiffuseLightColor;
                        Program.Renderer.Lighting.OptionLightPosition = CurrentRoute.Atmosphere.LightPosition;
                        loaded = true;
                        break;
                    }
                }

                if (!CurrentHost.UnloadPlugins(out error))
                {
                    MessageBox.Show(error, @"OpenBVE", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (!loaded)
                {
                    throw new Exception("No plugins capable of loading routefile " + result.RouteFile + " were found.");
                }
                if (!string.IsNullOrEmpty(Interface.CurrentOptions.TrainName))
                {
                    folder = System.IO.Path.GetDirectoryName(result.RouteFile);
                    while (true)
                    {
                        string trainFolder = OpenBveApi.Path.CombineDirectory(folder, "Train");
                        if (System.IO.Directory.Exists(trainFolder))
                        {
                            try
                            {
                                folder = OpenBveApi.Path.CombineDirectory(trainFolder, Interface.CurrentOptions.TrainName);
                            }
                            catch (Exception ex)
                            {
                                if (ex is ArgumentException)
                                {
                                    break;
                                }
                            }
                            if (System.IO.Directory.Exists(folder))
                            {
                                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();

                if (CurrentHost.Platform == HostPlatform.AppleOSX && IntPtr.Size != 4)
                {
                    //WinForms are not supported on 64-bit Apple, so show the experimental GL menu
                    result.ExperimentalGLMenu = true;
                }
                else
                {
                    if (!result.ExperimentalGLMenu)
                    {
                        result = formMain.ShowMainDialog(result);
                    }
                }
            }
            else
            {
                result.Start = true;
                //Apply translations
                Translations.SetInGameLanguage(Translations.CurrentLanguageCode);
            }

            if (result.ExperimentalGLMenu)
            {
                result.Start       = true;
                result.RouteFile   = null;
                result.TrainFolder = null;
            }

            // --- 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)
                            {
                                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)
                    {
                        if (ex is System.DllNotFoundException)
                        {
                            Interface.AddMessage(MessageType.Critical, false, "The required system library " + ex.Message + " was not found on the system.");
                            switch (ex.Message)
                            {
                            case "libopenal.so.1":
                                MessageBox.Show("openAL was not found on this system. \n Please install libopenal1 via your distribtion's package management system.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                break;

                            default:
                                MessageBox.Show("The required system library " + ex.Message + " was not found on this system.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                break;
                            }
                        }
                        else
                        {
                            Interface.AddMessage(MessageType.Critical, false, "The route and train loader encountered the following critical error: " + ex.Message);
                            CrashHandler.LoadingCrash(ex + Environment.StackTrace, 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(System.IO.File.Exists(FileSystem.RestartProcess) ? FileSystem.RestartProcess : Application.ExecutablePath, arguments);
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message + "\n\nProcess = " + FileSystem.RestartProcess + "\nArguments = " + arguments, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }