Example #1
0
        public const int MAX_UGC_RESULTS = 50; // according to 

        public static ulong[] GetSubscribedItems()
        {
            SteamAPIWrapper.Init();

            var num = SteamUGC.GetNumSubscribedItems();
            var ids = new PublishedFileId_t[num];
            SteamUGC.GetSubscribedItems(ids, num);

            return ids.Select(t => t.m_PublishedFileId).ToArray();
        }
        public static IEnumerable <ulong> GetAllModIds()
        {
            if (Init())
            {
                var amount = SteamUGC.GetNumSubscribedItems();
                var ids    = new PublishedFileId_t[amount];
                SteamUGC.GetSubscribedItems(ids, amount);

                return(ids.Select(t => t.m_PublishedFileId));
            }
            return(default);
        //Starts the Steam API and retrives the subscribed mods of the currently logged in user
        private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
        {
            if (!SteamAPI.Init())
            {
                if (checkBoxStartWithSteam.IsChecked == true)
                {
                    Process.Start("steam://rungameid/" + File.ReadAllText("steam_appid.txt"));
                }
                else
                {
                    MessageBox.Show("Steam must be running in order to use this program!");
                }
                Closed -= MainWindow1_Closed;
                Close();
            }
            else
            {
                //Links the visibility of the tree view range bar to its scrollbar
                ScrollViewer sv = FindChild <ScrollViewer>(treeViewModFiles);
                DependencyPropertyDescriptor.FromProperty(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty, typeof(ScrollViewer)).AddValueChanged(sv, (s, _e) =>
                {
                    rangeBarTreeView.Visibility = sv.ComputedVerticalScrollBarVisibility;
                });
                Title += " - Logged in as " + SteamFriends.GetPersonaName();
                //Gets an array of all the subscribed mods by the user
                uint subItemCount = SteamUGC.GetNumSubscribedItems();
                PublishedFileId_t[] subItemList = new PublishedFileId_t[subItemCount];
                SteamUGC.GetSubscribedItems(subItemList, subItemCount);
                //For each mod, makes a query to the steam server for all the mod info
                foreach (var item in subItemList)
                {
                    PublishedFileId_t[] itemID = new PublishedFileId_t[1] {
                        item
                    };
                    UGCQueryHandle_t itemQuery = SteamUGC.CreateQueryUGCDetailsRequest(itemID, 1);
                    SteamAPICall_t   itemCall  = SteamUGC.SendQueryUGCRequest(itemQuery);
                    itemNameQueryResult = CallResult <SteamUGCQueryCompleted_t> .Create(OnSendQueryUGCRequest);

                    itemNameQueryResult.Set(itemCall, OnSendQueryUGCRequest);
                }
                //Keeps running all the callbacks in background
                runCallbacksTimer = new Timer((argument) => SteamAPI.RunCallbacks(), null, 0, 30);
                //Monitors for changes in the subscribed items
                monitorSubscribtionsTimer = new Timer((argument) =>
                {
                    uint newSubItemCount = SteamUGC.GetNumSubscribedItems();
                    if (modList.Count != newSubItemCount)
                    {
                        PublishedFileId_t[] newSubItemList = new PublishedFileId_t[newSubItemCount];
                        SteamUGC.GetSubscribedItems(newSubItemList, newSubItemCount);
                        if (modList.Count > newSubItemCount) //Removed item
                        {
                            List <ulong> subItemIDList = newSubItemList.Select(x => x.m_PublishedFileId).ToList();
                            for (int i = modList.Count - 1; i >= 0; i--)    //Iterates in reverse to allow removing an item during the loop
                            {
                                if (!subItemIDList.Contains(modList[i].ID)) //If the item has been removed
                                {
                                    Dispatcher.Invoke(() => modList.RemoveAt(i));
                                }
                            }
                        }
                        else //Added item
                        {
                            foreach (var item in newSubItemList)
                            {
                                if (!modList.Contains(item.m_PublishedFileId)) //If the item is new
                                {
                                    PublishedFileId_t[] itemID = new PublishedFileId_t[1] {
                                        item
                                    };
                                    UGCQueryHandle_t itemQuery = SteamUGC.CreateQueryUGCDetailsRequest(itemID, 1);
                                    SteamAPICall_t itemCall    = SteamUGC.SendQueryUGCRequest(itemQuery);
                                    itemNameQueryResult        = CallResult <SteamUGCQueryCompleted_t> .Create(OnSendQueryUGCRequest);
                                    itemNameQueryResult.Set(itemCall, OnSendQueryUGCRequest);
                                    break;
                                }
                            }
                        }
                    }
                }, null, Timeout.Infinite, 1000);
                //Monitors for changes in the user votes
                monitorVotesTimer = new Timer((argument) =>
                {
                    foreach (var mod in modList)
                    {
                        SteamAPICall_t itemVoteCall = SteamUGC.GetUserItemVote(new PublishedFileId_t(mod.ID));
                        var voteQueryResult         = CallResult <GetUserItemVoteResult_t> .Create(OnSendVoteQueryUGCRequest);
                        voteQueryResult.Set(itemVoteCall, OnSendVoteQueryUGCRequest);
                    }
                }, null, 0, 1000);
                //Waits for all the mods info to be obtained, then loads the current settings
                taskFactory = new TaskFactory();
                taskFactory.StartNew(() =>
                {
                    while (modList.Count != SteamUGC.GetNumSubscribedItems()) //Waits for the complete mod list to be obtained
                    {
                        Thread.Sleep(30);
                    }
                    Application.Current.Dispatcher.InvokeAsync(() =>
                    {
                        //Sets the current profile to the last used one; this counts as a "profile change", so it will load all the profile settings (this invokes "OnModListObtained")
                        comboBoxProfile.SelectedIndex = Settings.CurrentProfileIndex + 1;
                        //If every mod is enabled, checks the "Enable All" checkbox
                        bool allChecked = true;
                        foreach (Mod item in modList)
                        {
                            if (!item.Enabled)
                            {
                                allChecked = false;
                            }
                        }
                        if (allChecked)
                        {
                            checkBoxEnableAll.Checked  -= checkBoxEnableAll_Checked;
                            checkBoxEnableAll.IsChecked = true;
                            checkBoxEnableAll.Checked  += checkBoxEnableAll_Checked;
                        }
                        modList.CollectionChanged += ModList_CollectionChanged;
                        //Starts monitoring for new subscribed items
                        monitorSubscribtionsTimer.Change(0, 1000);
                    }, DispatcherPriority.Background);
                });
            }

            T FindChild <T>(DependencyObject parent) where T : DependencyObject
            {
                T   foundChild    = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

                for (int i = 0; i < childrenCount; i++)
                {
                    var child     = VisualTreeHelper.GetChild(parent, i);
                    T   childType = child as T;
                    if (childType == null)
                    {
                        foundChild = FindChild <T>(child);
                        if (foundChild != null)
                        {
                            break;
                        }
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
                return(foundChild);
            }
        }