Example #1
0
 /// <summary>
 /// Gets a list of located account ids. Uses settings for the steam path.
 /// </summary>
 /// <returns>An array of located IDs</returns>
 private string[] GetSteamIds()
 {
     try {
         DirectoryInfo dir = new DirectoryInfo(Settings.Instance().SteamPath + "\\userdata");
         if (dir.Exists)
         {
             DirectoryInfo[] userDirs = dir.GetDirectories();
             string[]        result   = new string[userDirs.Length];
             for (int i = 0; i < userDirs.Length; i++)
             {
                 result[i] = userDirs[i].Name;
             }
             return(result);
         }
         return(new string[0]);
     } catch {
         return(new string[0]);
     }
 }
        protected override void RunProcess()
        {
            Added   = 0;
            Fetched = 0;
            switch (Settings.Instance().ListSource)
            {
            case GameListSource.XmlPreferred:
                FetchXmlPref();
                break;

            case GameListSource.XmlOnly:
                FetchXml();
                break;

            case GameListSource.WebsiteOnly:
                FetchHtml();
                break;
            }

            OnThreadCompletion();
        }
        private void FillFieldsFromSettings()
        {
            Settings settings = Settings.Instance();

            txtSteamPath.Text      = settings.SteamPath;
            txtDefaultProfile.Text = settings.ProfileToLoad;
            switch (settings.StartupAction)
            {
            case StartupAction.Load:
                radLoad.Checked = true;
                break;

            case StartupAction.Create:
                radCreate.Checked = true;
                break;

            default:
                radNone.Checked = true;
                break;
            }
            switch (settings.ListSource)
            {
            case GameListSource.XmlPreferred:
                cmbDatSrc.SelectedIndex = 0;
                break;

            case GameListSource.XmlOnly:
                cmbDatSrc.SelectedIndex = 1;
                break;

            case GameListSource.WebsiteOnly:
                cmbDatSrc.SelectedIndex = 2;
                break;
            }

            chkRemoveExtraEntries.Checked = settings.RemoveExtraEntries;
            chkIgnoreDlc.Checked          = settings.IgnoreDlc;
            chkFullAutocat.Checked        = settings.FullAutocat;

            //jpodadera. Non-Steam games
            chkIgnoreExternal.Checked = settings.IgnoreExternal;

            cmbLogLevel.SelectedIndex = (int)settings.LogLevel;
            numLogSize.Value          = settings.LogSize;
            numLogBackup.Value        = settings.LogBackups;

            switch (settings.UserLang)
            {
            case UserLanguage.en:
                cmbLanguage.SelectedIndex = 1;
                break;

            case UserLanguage.es:
                cmbLanguage.SelectedIndex = 2;
                break;

            default:
                cmbLanguage.SelectedIndex = 0;
                break;
            }
        }
        private void SaveFieldsToSettings()
        {
            Settings settings = Settings.Instance();

            settings.SteamPath = txtSteamPath.Text;
            if (radLoad.Checked)
            {
                settings.StartupAction = StartupAction.Load;
            }
            else if (radCreate.Checked)
            {
                settings.StartupAction = StartupAction.Create;
            }
            else
            {
                settings.StartupAction = StartupAction.None;
            }

            switch (cmbDatSrc.SelectedIndex)
            {
            case 0:
                settings.ListSource = GameListSource.XmlPreferred;
                break;

            case 1:
                settings.ListSource = GameListSource.XmlOnly;
                break;

            case 2:
                settings.ListSource = GameListSource.WebsiteOnly;
                break;
            }

            settings.ProfileToLoad      = txtDefaultProfile.Text;
            settings.RemoveExtraEntries = chkRemoveExtraEntries.Checked;
            settings.IgnoreDlc          = chkIgnoreDlc.Checked;
            settings.FullAutocat        = chkFullAutocat.Checked;
            //jpodadera. Non-Steam games
            settings.IgnoreExternal = chkIgnoreExternal.Checked;

            settings.LogLevel   = (LoggerLevel)cmbLogLevel.SelectedIndex;
            settings.LogSize    = (int)numLogSize.Value;
            settings.LogBackups = (int)numLogBackup.Value;

            switch (cmbLanguage.SelectedIndex)
            {
            case 0:
                settings.UserLang = UserLanguage.windows;
                break;

            case 1:
                settings.UserLang = UserLanguage.en;
                break;

            case 2:
                settings.UserLang = UserLanguage.es;
                break;
            }

            try {
                settings.Save();
            } catch (Exception e) {
                MessageBox.Show(GlobalStrings.DlgOptions_ErrorSavingSettingsFile + e.Message, GlobalStrings.DBEditDlg_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Integrate external games defined by Steam user. Only external games with identifier in screenshot.vdf file are included in game DB.
        /// </summary>
        /// <param name="SteamId">Identifier of Steam user</param>
        /// <param name="overWrite">Overwrite actual contents of game DB</param>
        /// <param name="ignore">List of identifiers of games to be ignored</param>
        /// <param name="newItems">Returns number of new games integrated</param>
        /// <returns>Returns number of external games located</returns>
        public int IntegrateNonSteamGameList(long SteamId, bool overWrite, SortedSet <int> ignore, out int newItems, out int removedItems)
        {
            newItems     = 0;
            removedItems = 0;
            if (SteamId <= 0)
            {
                return(0);
            }
            int loadedGames = 0;

            Dictionary <string, int> shortcutgames;

            if (LoadShortcutGames(SteamId, out shortcutgames))
            {
                string       filePath  = string.Format(Properties.Resources.ShortCutsFilePath, Settings.Instance().SteamPath, Profile.ID64toDirName(SteamId));
                FileStream   fStream   = null;
                BinaryReader binReader = null;
                try
                {
                    fStream   = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    binReader = new BinaryReader(fStream);

                    BinaryVdfFileNode dataRoot = BinaryVdfFileNode.Load(binReader);

                    VdfFileNode shortcutsNode = dataRoot.GetNodeAt(new string[] { "shortcuts" }, false);

                    if (shortcutsNode != null)
                    {
                        foreach (KeyValuePair <string, VdfFileNode> shortcutPair in shortcutsNode.NodeArray)
                        {
                            //string indexGame = shortcutPair.Key;

                            VdfFileNode attrGame = shortcutPair.Value;
                            VdfFileNode appGame  = attrGame.GetNodeAt(new string[] { "appname" }, false);

                            string gameName = appGame.NodeString;

                            // Check if external game has identifier in screenshots.vdf file (this happens only if game has been launched before from Steam client)
                            if (shortcutgames.ContainsKey(gameName))
                            {
                                bool isNew;
                                if (IntegrateGame(shortcutgames[gameName], gameName, overWrite, ignore, false, out isNew))
                                {
                                    loadedGames++;
                                    if (isNew)
                                    {
                                        newItems++;
                                    }
                                }
                                shortcutgames.Remove(gameName);
                            }
                        }
                    }
                    // Remove external games which have been deleted from Steam client
                    foreach (KeyValuePair <string, int> shortcutpair in shortcutgames)
                    {
                        if (RemoveGame(shortcutpair.Value))
                        {
                            removedItems++;
                        }
                    }
                }
                catch (FileNotFoundException e)
                {
                    Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorOpeningConfigFileParam, e.ToString());
                    //throw new ApplicationException(string.Format(GlobalStrings.GameData_ErrorOpeningConfigFileParam, filePath) + e.Message, e);
                }
                catch (IOException e)
                {
                    Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_LoadingErrorSteamConfig, e.ToString());
                }
                catch (ParseException e)
                {
                    Program.Logger.Write(LoggerLevel.Error, e.ToString());
                }
                finally
                {
                    if (binReader != null)
                    {
                        binReader.Close();
                    }
                    if (fStream != null)
                    {
                        fStream.Close();
                    }
                }
            }

            Program.Logger.Write(LoggerLevel.Info, GlobalStrings.GameData_IntegratedShortCuts, loadedGames, newItems, removedItems);

            return(loadedGames);
        }
        /// <summary>
        /// Load identifiers for external games from screenshots.vdf
        /// </summary>
        /// <param name="SteamId">Steam user identifier</param>
        /// <param name="shortcutGames">Found games listed as pairs of {gameName, gameId} </param>
        /// <returns></returns>
        private bool LoadShortcutGames(long SteamId, out Dictionary <string, int> shortcutGames)
        {
            bool   result   = false;
            string filePath = string.Format(Properties.Resources.ScreenshotsFilePath, Settings.Instance().SteamPath, Profile.ID64toDirName(SteamId));

            shortcutGames = new Dictionary <string, int>();

            StreamReader reader = null;

            try
            {
                reader = new StreamReader(filePath, false);
                TextVdfFileNode dataRoot = TextVdfFileNode.Load(reader, true);

                VdfFileNode appsNode = dataRoot.GetNodeAt(new string[] { "shortcutnames" }, false);

                foreach (KeyValuePair <string, VdfFileNode> shortcutPair in appsNode.NodeArray)
                {
                    string strId = shortcutPair.Key;

                    ulong ulongId;
                    if (ulong.TryParse(strId, out ulongId))
                    {
                        int    gameId   = (int)(ulongId >> 32);
                        string gameName = (string)shortcutPair.Value.NodeData;
                        shortcutGames.Add(gameName, gameId);
                    }
                    else
                    {
                        Program.Logger.Write(LoggerLevel.Warning, GlobalStrings.GameData_ErrorParsingScreenshots, (string)shortcutPair.Value.NodeData);
                    }
                }
                result = true;
            }
            catch (FileNotFoundException e)
            {
                Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorOpeningConfigFileParam, e.ToString());
            }
            catch (IOException e)
            {
                Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_LoadingErrorSteamConfig, e.ToString());
            }

            if (reader != null)
            {
                reader.Close();
            }

            return(result);
        }
        /// <summary>
        /// Writes category info for shortcut games to shortcuts.vdf config file for specified Steam user.
        /// </summary>
        /// <param name="SteamId">Identifier of Steam user to save information</param>
        /// <param name="discardMissing">If true, category information in shortcuts.vdf file is removed if game is not in Game list</param>
        private void SaveShortcutGames(long SteamId, bool discardMissing)
        {
            string screenshotsFilePath = string.Format(Properties.Resources.ScreenshotsFilePath, Settings.Instance().SteamPath, Profile.ID64toDirName(SteamId));

            Program.Logger.Write(LoggerLevel.Info, GlobalStrings.GameData_SavingSteamConfigFile, screenshotsFilePath);

            Dictionary <string, int> shortcutgames;

            if (LoadShortcutGames(SteamId, out shortcutgames))
            {
                string            filePath  = string.Format(Properties.Resources.ShortCutsFilePath, Settings.Instance().SteamPath, Profile.ID64toDirName(SteamId));
                FileStream        fStream   = null;
                BinaryReader      binReader = null;
                BinaryVdfFileNode dataRoot  = null;
                try
                {
                    fStream   = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    binReader = new BinaryReader(fStream);

                    dataRoot = BinaryVdfFileNode.Load(binReader);
                }
                catch (FileNotFoundException e)
                {
                    Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorOpeningConfigFileParam, e.ToString());
                }
                catch (IOException e)
                {
                    Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_LoadingErrorSteamConfig, e.ToString());
                }
                if (binReader != null)
                {
                    binReader.Close();
                }
                if (fStream != null)
                {
                    fStream.Close();
                }
                if (dataRoot != null)
                {
                    List <KeyValuePair <string, int>?> listShortCutGames = new List <KeyValuePair <string, int>?>();
                    VdfFileNode appsNode = dataRoot.GetNodeAt(new string[] { "shortcuts" }, false);
                    foreach (KeyValuePair <string, VdfFileNode> shortcutPair in appsNode.NodeArray)
                    {
                        VdfFileNode attrGame = shortcutPair.Value;
                        VdfFileNode appGame  = attrGame.GetNodeAt(new string[] { "appname" }, false);

                        string gameName = appGame.NodeString;
                        // Check if external game has identifier in screenshots.vdf file (this happens only if game has been launched before from Steam client)
                        if (shortcutgames.ContainsKey(gameName))
                        {
                            VdfFileNode tagsNode = attrGame.GetNodeAt(new string[] { "tags" }, false);
                            int         idGame   = shortcutgames[gameName];
                            if (Games.ContainsKey(idGame))
                            {
                                Program.Logger.Write(LoggerLevel.Verbose, GlobalStrings.GameData_AddingGameToConfigFile, idGame);
                                tagsNode.NodeArray.Clear();
                                Game game = Games[idGame];
                                if ((game.Category != null) || (game.Favorite))
                                {
                                    int index = 0;
                                    if (game.Category != null)
                                    {
                                        tagsNode.NodeArray.Add(index.ToString(), new BinaryVdfFileNode(game.Category.Name));
                                        index++;
                                    }
                                    if (game.Favorite)
                                    {
                                        tagsNode.NodeArray.Add(index.ToString(), new BinaryVdfFileNode("favorite"));
                                    }
                                }
                            }
                            else if (discardMissing)
                            {
                                Program.Logger.Write(LoggerLevel.Verbose, GlobalStrings.GameData_RemovingGameCategoryFromSteamConfig, idGame);
                                tagsNode.NodeArray.Clear();
                            }
                        }
                    }
                    if (dataRoot.NodeType == ValueType.Array)
                    {
                        Program.Logger.Write(LoggerLevel.Info, GlobalStrings.GameData_SavingShortcutConfigFile, filePath);
                        BinaryWriter binWriter;
                        try
                        {
                            fStream   = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
                            binWriter = new BinaryWriter(fStream);
                            dataRoot.Save(binWriter);
                            binWriter.Close();
                            fStream.Close();
                        }
                        catch (ArgumentException e)
                        {
                            Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorSavingSteamConfigFile, e.ToString());
                            throw new ApplicationException(GlobalStrings.GameData_FailedToSaveSteamConfigBadPath, e);
                        }
                        catch (IOException e)
                        {
                            Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorSavingSteamConfigFile, e.ToString());
                            throw new ApplicationException(GlobalStrings.GameData_FailedToSaveSteamConfigFile + e.Message, e);
                        }
                        catch (UnauthorizedAccessException e)
                        {
                            Program.Logger.Write(LoggerLevel.Error, GlobalStrings.GameData_ErrorSavingSteamConfigFile, e.ToString());
                            throw new ApplicationException(GlobalStrings.GameData_AccessDeniedSteamConfigFile + e.Message, e);
                        }
                    }
                }
            }
        }
        private int ImportNonSteamGames(long SteamId, SortedSet <int> ignore)
        {
            int    result   = 0;
            string filePath = string.Format(Properties.Resources.ShortCutsFilePath, Settings.Instance().SteamPath, Profile.ID64toDirName(SteamId));

            Program.Logger.Write(LoggerLevel.Info, GlobalStrings.GameData_OpeningSteamConfigFile, filePath);

            Dictionary <string, int> shortcutgames;

            if (LoadShortcutGames(SteamId, out shortcutgames))
            {
                FileStream        fStream   = null;
                BinaryReader      binReader = null;
                BinaryVdfFileNode dataRoot  = null;
                try
                {
                    fStream   = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    binReader = new BinaryReader(fStream);
                    dataRoot  = BinaryVdfFileNode.Load(binReader);
                }
                finally
                {
                    if (binReader != null)
                    {
                        binReader.Close();
                    }
                    if (fStream != null)
                    {
                        fStream.Close();
                    }
                }
                if (dataRoot != null)
                {
                    VdfFileNode shortcutsNode = dataRoot.GetNodeAt(new string[] { "shortcuts" }, false);
                    foreach (KeyValuePair <string, VdfFileNode> shortcutPair in shortcutsNode.NodeArray)
                    {
                        string indexGame = shortcutPair.Key;

                        VdfFileNode attrGame = shortcutPair.Value;
                        VdfFileNode appGame  = attrGame.GetNodeAt(new string[] { "appname" }, false);

                        string gameName = appGame.NodeString;

                        // Check if external game has identifier in screenshots.vdf file (this happens only if game has been launched before from Steam client)
                        if (shortcutgames.ContainsKey(gameName))
                        {
                            int gameIdInDB = shortcutgames[gameName];
                            if (!ignore.Contains(gameIdInDB))
                            {
                                Game gameDB;
                                if (Games.ContainsKey(gameIdInDB))
                                {
                                    gameDB = Games[gameIdInDB];
                                }
                                else
                                {
                                    gameDB = new Game(gameIdInDB, gameName);
                                }

                                string      cat0 = null, cat1 = null;
                                VdfFileNode tagsGame = attrGame.GetNodeAt(new string[] { "tags" }, false);
                                if ((tagsGame != null) && (tagsGame.NodeType == ValueType.Array) &&
                                    (tagsGame.NodeArray.Count > 0) && (tagsGame.NodeArray.ContainsKey("0")))
                                {
                                    VdfFileNode vdfCat = tagsGame.NodeArray["0"];
                                    if (vdfCat.NodeType == ValueType.Value)
                                    {
                                        cat0 = vdfCat.NodeData.ToString();
                                    }
                                    if (tagsGame.NodeArray.ContainsKey("1"))
                                    {
                                        vdfCat = tagsGame.NodeArray["1"];
                                        if (vdfCat.NodeType == ValueType.Value)
                                        {
                                            cat1 = vdfCat.NodeData.ToString();
                                        }
                                    }
                                }
                                gameDB.Favorite = ((cat0 == "favorite") || (cat1 == "favorite"));
                                if (cat0 != "favorite")
                                {
                                    gameDB.Category = GetCategory(cat0);
                                }
                                else
                                {
                                    gameDB.Category = GetCategory(cat1);
                                }
                                result++;
                            }
                        }
                    }
                }
            }
            return(result);
        }