Example #1
0
        // Constructor
        public GameLauncher(int gameId)
        {
            db = new MyDbContext();

            GameId = gameId;

            // get Game object
            Game game = (from g in db.Game
                         where g.gameId == gameId
                         select g).SingleOrDefault();

            ConfigId = game.configId;
            RomPath  = game.gamePath;
            RomName  = game.gameName;

            // get globals
            Global = (from g in db.GlobalSettings
                      where g.settingsId == 1
                      select g).SingleOrDefault();

            SystemId = game.systemId;

            // do PSX sbi check and check whether game file actually exists (as it might have been renamed)
            if (SystemId == 9 && File.Exists(game.gamePath))
            {
                // get all implied files from othe cue/m3u that is in the database
                string       cuePath     = game.gamePath; // this is never relative with disc-based games
                DiscGameFile originalCue = new DiscGameFile(cuePath, 9);

                List <DiscGameFile> imageFiles = new List <DiscGameFile>(); // DiscScan.ParseTrackSheetForImageFiles(new DiscGameFile(cuePath, 9), 9);

                // check whether m3u
                if (originalCue.Extension.ToLower() == ".m3u")
                {
                    // get all cue files
                    var allc = DiscScan.ParseTrackSheet(originalCue, CueType.m3u, SystemId);
                    foreach (var g in allc)
                    {
                        imageFiles.Add(g);
                    }
                }
                else
                {
                    // standard cue file
                    imageFiles.Add(originalCue);
                }

                // iterate through each image and check for serial number
                for (int i = 0; i < imageFiles.Count; i++)
                {
                    string serial = MedDiscUtils.GetPSXSerial(imageFiles[i].FullPath);

                    if (serial == null || serial == "")
                    {
                        continue;
                    }

                    // add serial to imageFiles
                    imageFiles[i].ExtraInfo = serial;
                }

                // if imageFile has only one entry, then this matches originalCue
                if (imageFiles.Count == 1)
                {
                    if (PsxSBI.IsSbiAvailable(imageFiles.First().ExtraInfo) == true)
                    {
                        // sbi is available - check whether sbi already exists
                        string sbipath = imageFiles.First().FullPath.Replace(imageFiles.First().Extension, ".sbi");

                        //if (!File.Exists(imageFiles.First().FolderPath + "\\" + imageFiles.First().FileName.Replace(imageFiles.First().Extension, "") + ".sbi"))

                        if (!File.Exists(sbipath))
                        {
                            var result = MessagePopper.ShowMessageDialog("MedLaunch has determined that you need an available SBI patch file to play this game properly.\n\nDo you wish to copy this file to your disc directory?\n",
                                                                         "SBI Patch Needed - " + imageFiles.First().FileName, MessagePopper.DialogButtonOptions.YESNO);

                            //MessageBoxResult result = MessageBox.Show("MedLaunch has determined that you need an available SBI patch file to play this game properly.\n\nDo you wish to copy this file to your disc directory?\n",
                            //    "SBI Patch Needed - " + imageFiles.First().FileName, MessageBoxButton.YesNo, MessageBoxImage.Question);

                            if (result == MessagePopper.ReturnResult.Affirmative)
                            {
                                // copy sbi file to folder (named the same as the cue file)
                                originalCue.ExtraInfo = imageFiles.First().ExtraInfo;

                                //PsxSBI.InstallSBIFile(originalCue);
                                PsxSBI.InstallSBIFile(imageFiles.First());
                            }
                        }
                    }
                }

                // if imageFiles has multiple entries - it will have come from an m3u file
                if (imageFiles.Count > 1)
                {
                    // create an array of m3u cue files
                    string[] cues = File.ReadAllLines(originalCue.FullPath);

                    // loop through
                    for (int image = 0; image < imageFiles.Count; image++)
                    {
                        if (PsxSBI.IsSbiAvailable(imageFiles[image].ExtraInfo) == true)
                        {
                            // sbi is available - prompt user
                            if (!File.Exists(imageFiles[image].FolderPath + "\\" + imageFiles[image].FileName.Replace(imageFiles[image].Extension, "") + ".sbi"))
                            {
                                var result = MessagePopper.ShowMessageDialog("MedLaunch has determined that you need an available SBI patch file to play this game properly.\n\nDo you wish to copy this file to your disc directory?\n",
                                                                             "SBI Patch Needed - " + imageFiles.First().FileName, MessagePopper.DialogButtonOptions.YESNO);

                                //MessageBoxResult result = MessageBox.Show("MedLaunch has determined that you need an available SBI patch file to play this game properly.\n\nDo you wish to copy this file to your disc directory?\n",
                                //"SBI Patch Needed - " + imageFiles[image].FileName + imageFiles[image].Extension, MessageBoxButton.YesNo, MessageBoxImage.Question);

                                if (result == MessagePopper.ReturnResult.Affirmative)
                                {
                                    // copy sbi file to folder (named the same as the cue file)
                                    DiscGameFile d = new DiscGameFile(cues[image], 9);
                                    d.ExtraInfo = imageFiles[image].ExtraInfo;

                                    PsxSBI.InstallSBIFile(d);
                                }
                            }
                        }
                    }
                }
            }

            // logic for faust & fast
            if (game.systemId == 12)
            {
                if (Global.enableSnes_faust == true)
                {
                    SystemId = 16;
                    //MessageBoxResult result = MessageBox.Show("FAUST DETECTED");
                }
                else
                {
                    SystemId = game.systemId;
                }
            }
            if (game.systemId == 7 || game.systemId == 18)
            {
                if (Global.enablePce_fast == true)
                {
                    SystemId = 17;
                }
                else
                {
                    SystemId = 7;
                }
            }

            gSystem    = GSystem.GetSystems().Where(a => a.systemId == SystemId).Single();
            SystemCode = gSystem.systemCode;



            RomFolder = GetRomFolder(SystemId, db);

            MednafenFolder = (from m in db.Paths
                              select m.mednafenExe).SingleOrDefault();

            // set the config id
            int actualConfigId = SystemId + 2000000000;

            // take general settings from base config (2000000000) and system specific settings from actual config



            ConfigBaseSettings _config = (from c in db.ConfigBaseSettings
                                          where (c.ConfigId == actualConfigId)
                                          select c).SingleOrDefault();

            List <ConfigObject> sysConfigObject = ListFromType(_config).Where(a => !a.Key.StartsWith("__")).ToList();

            SysConfigObject = new List <ConfigObject>();

            foreach (var x in sysConfigObject)
            {
                var  systems = GSystem.GetSystems().Where(a => a.systemCode != SystemCode);
                bool isValid = true;
                foreach (var sc in systems)
                {
                    if (x.Key.StartsWith(sc.systemCode + "__"))
                    {
                        isValid = false;
                        break;
                    }
                }
                if (isValid == true)
                {
                    SysConfigObject.Add(x);
                }
            }


            // if option is enabled save system specific config for this system
            if (Global.saveSystemConfigs == true)
            {
                if (SystemCode == "pcecd")
                {
                    SystemCode = "pce";
                }

                SaveSystemConfigToDisk(SystemCode, SysConfigObject);
            }


            // build actual config list
            //ConfObject = new List<ConfigObject>();
            //ConfObject.AddRange(GenConfigObject);
            //ConfObject.AddRange(SysConfigObject);

            /*
             * if (_config.isEnabled == true)
             * {
             *  Config = _config;
             * }
             * else
             * {
             *  Config = (from c in db.ConfigBaseSettings
             *            where c.ConfigId == 2000000000
             *            select c).SingleOrDefault();
             * }
             */


            // get netplay
            Netplay = (from n in db.ConfigNetplaySettings
                       where n.ConfigNPId == 1
                       select n).SingleOrDefault();



            // get server
            Server = (from s in db.ConfigServerSettings
                      where s.ConfigServerId == Global.serverSelected
                      select s).SingleOrDefault();

            // get overide server settings (password and gamekey from custom
            ServerOveride = (from s in db.ConfigServerSettings
                             where s.ConfigServerId == 100
                             select s).SingleOrDefault();
        }
Example #2
0
        public void RunGame(string cmdArguments, int systemId)
        {
            /*
             * // check mednafen.exe instruction set
             * InstructionSet medInst = InstructionSetDetector.GetExeInstructionSet(BuildMednafenPath(MednafenFolder));
             * // get operating system type
             * InstructionSet osInst = InstructionSetDetector.GetOperatingSystemInstructionSet();
             *
             * if (osInst == InstructionSet.x86)
             * {
             *  if (medInst == InstructionSet.x64)
             *  {
             *      MessageBox.Show("You are targetting a 64-bit version of Mednafen on a 32-bit operating system. This will not work.\n\nPlease target a 32-bit (x86) version of Mednafen", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
             *      return;
             *  }
             *
             *  if (systemId == 13 && medInst == InstructionSet.x86)
             *  {
             *      MessageBox.Show("You are trying to emulate a Sega Saturn game using a 32-bit Mednafen build on a 32-bit operating system. This will not work.\n\nYou are unable to emulate Saturn games on this machine", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
             *      return;
             *  }
             *
             *  if (systemId == 13)
             *  {
             *      MessageBox.Show("You are trying to emulate a Sega Saturn game using a 32-bit operating system. This will not work.\n\nYou are unable to emulate Saturn games on this machine", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
             *      return;
             *  }
             * }
             *
             * if (osInst == InstructionSet.x64)
             * {
             *  if (systemId == 13 && medInst == InstructionSet.x86)
             *  {
             *      MessageBox.Show("You are trying to emulate a Sega Saturn game using a 32-bit Mednafen build. This will not work.\n\nPlease target a 64-bit (x64) version of Mednafen", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
             *      return;
             *  }
             * }
             *
             */

            int procId;

            bool rememberWinPos = GlobalSettings.GetGlobals().rememberSysWinPositions.Value;

            using (System.Diagnostics.Process gProcess = new System.Diagnostics.Process())
            {
                gProcess.StartInfo.UseShellExecute        = true;
                gProcess.StartInfo.RedirectStandardOutput = false;
                gProcess.StartInfo.WorkingDirectory       = "\"" + MednafenFolder + "\"";
                gProcess.StartInfo.FileName       = "\"" + BuildMednafenPath(MednafenFolder) + "\"";
                gProcess.StartInfo.CreateNoWindow = false;
                // Build command line config arguments
                gProcess.StartInfo.Arguments = cmdArguments;
                //gProcess.StartInfo.UseShellExecute = false;
                //gProcess.StartInfo.EnvironmentVariables["MEDNAFEN_NOPOPUPS"] = "1";
                gProcess.Start();

                gProcess.WaitForExit();

                procId = gProcess.Id;
                IntPtr hwnd = new IntPtr();
                // set windows position
                System.Drawing.Point pnt = new System.Drawing.Point();

                // get process window handle
                try
                {
                    hwnd = gProcess.MainWindowHandle;
                    if (rememberWinPos == true)
                    {
                        // get windows position from database
                        pnt = GlobalSettings.GetWindowPosBySystem(systemId);
                        // set windows position
                        HwndInterface.SetHwndPos(hwnd, pnt.X, pnt.Y);
                    }

                    bool isClosed = false;
                    while (isClosed == false)
                    {
                        try
                        {
                            // get process id
                            Process p = Process.GetProcessById(procId);

                            if (rememberWinPos == true)
                            {
                                // get window top left x y coords
                                pnt = HwndInterface.GetHwndPos(hwnd);
                            }
                        }
                        catch
                        {
                            isClosed = true;
                        }

                        Thread.Sleep(1000);
                    }

                    if (rememberWinPos == true)
                    {
                        // save coords to database
                        GlobalSettings.SaveWindowPosBySystem(systemId, pnt);
                    }
                }
                catch
                {
                    // catch exception if mednafen doesnt launch correctly

                    if (VersionChecker.Instance.CurrentMedVerDesc.MajorINT > 0)
                    {
                        // new mednafen already pops up error messages - so do nothing
                    }
                    else
                    {
                        // old mednafen does not pop messages - interogate stdout.txt and display the last error
                        string res = LogParser.Instance.GetErrors();
                        if (res != string.Empty)
                        {
                            string end = "\n--------------------------------------------\n";
                            string beg = "It looks like mednafen did NOT launch correctly.\nThe following error log may help in troubleshooting:" + end;

                            MessagePopper.ShowMessageDialog((beg + res + end).Replace("\n\n", "\n").TrimEnd('\n'), "MEDNAFEN ERROR PARSER");
                        }
                    }

                    return;
                }
            }
        }
        /// <summary>
        /// Entry point for the application to get mednafen version and
        /// display compatibility info if neccessary
        /// </summary>
        /// <param name="showDialog"></param>
        /// <returns></returns>
        public static bool MednafenVersionCheck(bool showDialog)
        {
            // mednafen version check
            Paths  pa            = Paths.GetPaths();
            string medFolderPath = pa.mednafenExe;
            string medPath       = medFolderPath + @"\mednafen.exe";

            if (!File.Exists(medPath))
            {
                if (showDialog)
                {
                    MessagePopper.ShowMessageDialog("Path to Mednafen is NOT valid\nPlease set this on the Settings tab",
                                                    "ERROR");
                }
                //MessageBox.Show("Path to Mednafen is NOT valid\nPlease set this on the Settings tab", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                return(false);
            }

            // detect current version
            var currDesc = Instance.CurrentMedVerDesc;

            if (currDesc == null)
            {
                if (showDialog)
                {
                    MessagePopper.ShowMessageDialog("There was a problem retreiving the Mednafen version.\nPlease check your paths",
                                                    "ERROR");
                }
                //MessageBox.Show("There was a problem retreiving the Mednafen version.\nPlease check your paths", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            if (!currDesc.IsValid)
            {
                if (showDialog)
                {
                    MessagePopper.ShowMessageDialog("There was a problem parsing the current Mednafen version.\nPlease check your paths",
                                                    "ERROR");
                }
                //MessageBox.Show("There was a problem parsing the current Mednafen version.\nPlease check your paths", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            // get the min and max support mednafen versions
            var latestDesc = Instance.LatestCompatMedVerDesc;
            var oldestDesc = MednafenVersionDescriptor.ReturnVersionDescriptor(GetMednafenCompatibilityMatrix().Last().Version);

            // check whether the current mednafen version is within the min and max supported constraints
            // just looking at the first 3 digits of the version
            string currStr = currDesc.MajorINT.ToString() + "." +
                             currDesc.MinorINT.ToString() + "." +
                             currDesc.BuildINT.ToString();

            var loookup = GetMednafenCompatibilityMatrix()
                          .Where(a => a.Version.StartsWith(currStr)).ToList();

            bool isCompat;

            if (loookup.Count() > 0)
            {
                isCompat = true;
            }
            else
            {
                isCompat = false;
            }

            if (isCompat)
            {
                // is compatible
                return(true);
            }
            else
            {
                // is not compatible
                if (showDialog)
                {
                    // version doesnt match
                    StringBuilder sb = new StringBuilder();
                    sb.Append("The version of Mednafen you are trying to launch is potentially NOT compatible with this version of MedLaunch.\n\n");
                    sb.Append("Mednafen version installed:\t");
                    sb.Append(currDesc.FullVersionString);
                    sb.Append("\nMednafen version required: \t");
                    sb.Append(oldestDesc.FullVersionString + " - " + latestDesc.FullVersionString);
                    sb.Append("\n\nPlease ensure you are targeting a MedLaunch supported version of Mednafen.\n");
                    sb.Append("\nPress LAUNCH ANYWAY to try your luck");
                    sb.Append("\nPress CANCEL to return to the Games Library");

                    var result = MessagePopper.ShowMessageDialog(sb.ToString(),
                                                                 "POSSIBLE VERSION MISMATCH", MessagePopper.DialogButtonOptions.YESNO, new MahApps.Metro.Controls.Dialogs.MetroDialogSettings
                    {
                        AnimateHide = false,
                        AnimateShow = false,
                        //ColorScheme = MahApps.Metro.Controls.Dialogs.MetroDialogColorScheme.Accented,
                        AffirmativeButtonText = "LAUNCH ANYWAY",
                        NegativeButtonText    = "CANCEL",
                    });

                    //MessageBoxResult result = MessageBox.Show(sb.ToString(), "Mednafen Version Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                    if (result == MessagePopper.ReturnResult.Affirmative)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
        }