Ejemplo n.º 1
0
        // Check game settings against folder.
        public GameRefreshStatus GetDllAndIniStatus(x360ce.Engine.Data.UserGame game, bool fix = false)
        {
            var status = GameRefreshStatus.OK;
            var fi     = new FileInfo(game.FullPath);

            // Check if game file exists.
            if (!fi.Exists)
            {
                return(GameRefreshStatus.ExeNotExist);
            }
            // Check if game is not enabled.
            else if (!game.IsEnabled)
            {
                return(status);
            }
            else
            {
                var content      = GetIniContent(game);
                var bytes        = SettingsHelper.GetFileConentBytes(content, Encoding.Unicode);
                var iniDirectory = Path.GetDirectoryName(game.FullPath);
                var iniFullName  = Path.Combine(iniDirectory, IniFileName);
                var isDifferent  = SettingsHelper.IsDifferent(iniFullName, bytes);
                if (isDifferent)
                {
                    if (fix)
                    {
                        File.WriteAllBytes(iniFullName, bytes);
                    }
                    else
                    {
                        var iniExists = File.Exists(iniFullName);
                        status |= (iniExists ? GameRefreshStatus.IniDifferent : GameRefreshStatus.IniNotExist);
                    }
                }
                var xiValues = ((XInputMask[])Enum.GetValues(typeof(XInputMask))).Where(x => x != XInputMask.None).ToArray();
                // Create dictionary from XInput type and XInput file name.
                var dic = new Dictionary <XInputMask, string>();
                foreach (var value in xiValues)
                {
                    dic.Add(value, Attributes.GetDescription(value));
                }
                // Get names of files: xinput9_1_0.dll, xinput1_1.dll, xinput1_2.dll, xinput1_3.dll, xinput1_4.dll
                var xiFileNames = dic.Values.Distinct();
                // Loop through XInput DLL files.
                foreach (var xiFileName in xiFileNames)
                {
                    // Get enum linked to 64-bit version of the file.
                    var x64Value = dic.First(x => x.Value == xiFileName && x.ToString().Contains("x64")).Key;
                    // Get enum linked to 32-bit version of the file.
                    var x86Value = dic.First(x => x.Value == xiFileName && x.ToString().Contains("x86")).Key;
                    // Get information about XInput DLL file.
                    var xiFullPath = System.IO.Path.Combine(fi.Directory.FullName, xiFileName);
                    var xiFileInfo = new System.IO.FileInfo(xiFullPath);
                    // Determine required architecture.
                    var requiredArchitecture = ProcessorArchitecture.None;
                    var x64Enabled           = ((XInputMask)game.XInputMask).HasFlag(x64Value);
                    var x86Enabled           = ((XInputMask)game.XInputMask).HasFlag(x86Value);
                    if (x86Enabled && x64Enabled)
                    {
                        requiredArchitecture = ProcessorArchitecture.MSIL;
                    }
                    else if (x86Enabled)
                    {
                        requiredArchitecture = ProcessorArchitecture.X86;
                    }
                    else if (x64Enabled)
                    {
                        requiredArchitecture = ProcessorArchitecture.Amd64;
                    }
                    // Get embeded resource name for current xinput file.
                    var resourceName = EngineHelper.GetXInputResoureceName(requiredArchitecture);
                    // If both XInput file CheckBoxes are disabled then...
                    if (requiredArchitecture == ProcessorArchitecture.None)
                    {
                        // If XInput file exists then...
                        if (xiFileInfo.Exists)
                        {
                            if (fix)
                            {
                                // Delete unnecessary XInput file.
                                xiFileInfo.Delete();
                            }
                            else
                            {
                                status |= GameRefreshStatus.XInputFilesUnnecessary;
                            }
                        }
                    }
                    // If XInput file doesn't exists then...
                    else if (!xiFileInfo.Exists)
                    {
                        // Create XInput file.
                        if (fix)
                        {
                            AppHelper.WriteFile(resourceName, xiFileInfo.FullName);
                        }
                        else
                        {
                            status |= GameRefreshStatus.XInputFilesNotExist;
                        }
                    }
                    else
                    {
                        // Get current architecture of XInput DLL.
                        var xiCurrentArchitecture = Engine.Win32.PEReader.GetProcessorArchitecture(xiFullPath);
                        // If processor architectures doesn't match then...
                        if (requiredArchitecture != xiCurrentArchitecture)
                        {
                            // Create XInput file.
                            if (fix)
                            {
                                AppHelper.WriteFile(resourceName, xiFileInfo.FullName);
                            }
                            else
                            {
                                status |= GameRefreshStatus.XInputFilesWrongPlatform;
                            }
                        }
                        else
                        {
                            // Determine if file was created by Microsoft.
                            bool byMicrosoft;
                            // Get version of XInput DLL on the disk.
                            var dllVersion = EngineHelper.GetDllVersion(xiFullPath, out byMicrosoft);
                            // Get version of embeded XInput DLL.
                            var embededVersion = EngineHelper.GetEmbeddedDllVersion(xiCurrentArchitecture);
                            // If file on disk is older then...
                            if (dllVersion < embededVersion)
                            {
                                // Overwrite XInput file.
                                if (fix)
                                {
                                    AppHelper.WriteFile(resourceName, xiFileInfo.FullName);
                                }
                                else
                                {
                                    status |= GameRefreshStatus.XInputFilesOlderVersion;
                                }
                            }
                            else if (dllVersion > embededVersion)
                            {
                                // Allow new version.
                                // return GameRefreshStatus.XInputFileNewerVersion;
                            }
                        }
                    }
                }
            }
            return(status);
        }
Ejemplo n.º 2
0
        private void OnCloseAction(CancelEventArgs e)
        {
            // Disable force feedback effect before closing application.
            if (UpdateTimer != null)
            {
                UpdateTimer.Stop();
            }
            lock (Controller.XInputLock)
            {
                for (var i = 0; i < 4; i++)
                {
                    //if (PadControls[i].LeftMotorTestTrackBar.Value > 0 || PadControls[i].RightMotorTestTrackBar.Value > 0)
                    //{
                    var gamePad    = Global.DHelper.LiveXiControllers[i];
                    var isConected = Global.DHelper.LiveXiConnected[i];
                    if (Controller.IsLoaded && isConected)
                    {
                        // Stop vibration.
                        gamePad.SetVibration(new Vibration());
                    }
                    //}
                }
                //BeginInvoke((Action)delegate()
                //{
                //	XInput.FreeLibrary();
                //});
            }
            // Logical delay without blocking the current thread.
            System.Threading.Tasks.Task.Delay(100).Wait();
            var tmp = new FileInfo(SettingsManager.TmpFileName);
            var ini = new FileInfo(SettingsManager.IniFileName);

            if (tmp.Exists && ini.Exists)
            {
                // Before renaming file check for changes.
                var changed = false;
                if (tmp.Length != ini.Length)
                {
                    changed = true;
                }
                else
                {
                    var tmpChecksum = EngineHelper.GetFileChecksum(tmp.FullName);
                    var iniChecksum = EngineHelper.GetFileChecksum(ini.FullName);
                    changed = !tmpChecksum.Equals(iniChecksum);
                }
                if (changed)
                {
                    var form   = new MessageBoxWindow();
                    var result = form.ShowDialog(
                        "Do you want to save changes you made to configuration?",
                        "Save Changes?",
                        System.Windows.MessageBoxButton.YesNoCancel, System.Windows.MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.Yes);
                    if (result == System.Windows.MessageBoxResult.Yes)
                    {
                        // Do nothing since INI contains latest updates.
                    }
                    else if (result == System.Windows.MessageBoxResult.No)
                    {
                        // Rename temp to INI.
                        tmp.CopyTo(SettingsManager.IniFileName, true);
                    }
                    else if (result == System.Windows.MessageBoxResult.Cancel)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                // delete temp.
                tmp.Delete();
            }
            SettingsManager.SaveAll();
            AppHelper.UnInitializeHidGuardian();
        }
Ejemplo n.º 3
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (ControlsHelper.IsDesignMode(this))
            {
                return;
            }
            StartHelper.OnClose += (sender1, e1)
                                   => Close();
            StartHelper.OnRestore += (sender1, e1)
                                     => Global._TrayManager.RestoreFromTray(true);
            AppHelper.InitializeHidGuardian();
            if (string.IsNullOrEmpty(System.Threading.Thread.CurrentThread.Name))
            {
                System.Threading.Thread.CurrentThread.Name = "MainFormThread";
            }
            Global.DHelper.DevicesUpdated                         += DHelper_DevicesUpdated;
            Global.DHelper.UpdateCompleted                        += DHelper_UpdateCompleted;
            Global.DHelper.FrequencyUpdated                       += DHelper_FrequencyUpdated;
            Global.DHelper.StatesRetrieved                        += DHelper_StatesRetrieved;
            Global.DHelper.XInputReloaded                         += DHelper_XInputReloaded;
            MainBodyPanel.SettingsPanel._ParentControl             = MainPanel._bwm;
            MainBodyPanel.SettingsPanel.MainDataGrid.SelectionMode = System.Windows.Controls.DataGridSelectionMode.Extended;
            MainBodyPanel.SettingsPanel.InitPanel();
            // NotifySettingsChange will be called on event suspension and resume.
            SettingsManager.Current.NotifySettingsStatus = NotifySettingsStatus;
            // NotifySettingsChange will be called on setting changes.
            SettingsManager.Current.SettingChanged      += Current_SettingChanged;
            SettingsManager.Summaries.Items.ListChanged += Summaries_ListChanged;
            XInputMaskScanner.FileInfoCache.Load();
            UpdateTimer = new System.Timers.Timer
            {
                AutoReset = false,
                Interval  = DefaultPoolingInterval
            };
            UpdateTimer.Elapsed += UpdateTimer_Elapsed;
            SettingsTimer        = new System.Timers.Timer
            {
                AutoReset = false,
                Interval  = 500
            };
            SettingsTimer.Elapsed += SettingsTimer_Elapsed;
            CleanStatusTimer       = new System.Timers.Timer
            {
                AutoReset = false,
                Interval  = 3000
            };
            CleanStatusTimer.Elapsed += CleanStatusTimer_Elapsed;
            Title = EngineHelper.GetProductFullName();
            MainBodyPanel.ShowProgramsTab(SettingsManager.Options.ShowProgramsTab);
            MainBodyPanel.ShowSettingsTab(SettingsManager.Options.ShowSettingsTab);
            MainBodyPanel.ShowDevicesTab(SettingsManager.Options.ShowDevicesTab);
            // Start Timers.
            UpdateTimer.Start();
            JocysCom.ClassLibrary.Win32.NativeMethods.CleanSystemTray();
            // If enabling first time and application version changed then...
            ErrorsHelper.InitErrorsHelper(AppVersionChanged, MainPanel.StatusErrorsLabel, MainPanel.StatusErrorsIcon, MainPanel);
            var game = SettingsManager.CurrentGame;

            if (SettingsManager.Options.HidGuardianConfigureAutomatically)
            {
                // Enable Reconfigure HID Guardian.
                var changed             = SettingsManager.AutoHideShowMappedDevices(game);
                var mappedInstanceGuids = SettingsManager.GetMappedDevices(game?.FileName, true)
                                          .Select(x => x.InstanceGuid).ToArray();
                AppHelper.SynchronizeToHidGuardian(mappedInstanceGuids);
            }
        }
Ejemplo n.º 4
0
        bool CheckFiles(bool createIfNotExist)
        {
            if (createIfNotExist)
            {
                // If ini file doesn't exists.
                if (!System.IO.File.Exists(SettingManager.IniFileName))
                {
                    if (!CreateFile(this.GetType().Namespace + ".Presets." + SettingManager.IniFileName, SettingManager.IniFileName))
                    {
                        return(false);
                    }
                }
                // If xinput file doesn't exists.
                var architecture       = Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture;
                var embeddedDllVersion = EngineHelper.GetEmbeddedDllVersion(architecture);
                var file = EngineHelper.GetDefaultDll();
                // If XInput dll was not found then...
                if (file == null)
                {
                    var xFile = JocysCom.ClassLibrary.ClassTools.EnumTools.GetDescription(XInputMask.XInput13_x86);
                    if (!CreateFile(EngineHelper.GetXInputResoureceName(), xFile))
                    {
                        return(false);
                    }
                }
                else
                {
                    bool byMicrosoft;
                    var  dllVersion = EngineHelper.GetDllVersion(file.Name, out byMicrosoft);
                    // If file on the disk is older then...
                    if (dllVersion < embeddedDllVersion)
                    {
                        // Offer upgrade.
                        CreateFile(EngineHelper.GetXInputResoureceName(), file.Name, dllVersion, embeddedDllVersion);
                    }
                    var xiCurrentArchitecture = Engine.Win32.PEReader.GetProcessorArchitecture(file.Name);
                    if (architecture != xiCurrentArchitecture)
                    {
                        // Offer upgrade.
                        CreateFile(EngineHelper.GetXInputResoureceName(), file.Name, xiCurrentArchitecture, architecture);
                        return(true);
                    }
                }
            }
            // Can't run witout ini.
            if (!File.Exists(SettingManager.IniFileName))
            {
                var form = new MessageBoxForm();
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowForm(
                    string.Format("Configuration file '{0}' is required for application to run!", SettingManager.IniFileName),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return(false);
            }
            // If temp file exist then.
            FileInfo iniTmp = new FileInfo(SettingManager.TmpFileName);

            if (iniTmp.Exists)
            {
                // It means that application crashed. Restore ini from temp.
                if (!AppHelper.CopyFile(iniTmp.FullName, SettingManager.IniFileName))
                {
                    return(false);
                }
            }
            else
            {
                // Create temp file to store original settings.
                if (!AppHelper.CopyFile(SettingManager.IniFileName, SettingManager.TmpFileName))
                {
                    return(false);
                }
            }
            // Set status labels.
            StatusIsAdminLabel.Text = WinAPI.IsVista
                                ? string.Format("Elevated: {0}", WinAPI.IsElevated())
                                : "";
            StatusIniLabel.Text = SettingManager.IniFileName;
            return(true);
        }