Exemple #1
0
        public static void GetModProfiles(IEnumerable <int> modIds,
                                          Action <List <ModProfile> > onSuccess,
                                          Action <WebRequestError> onError)
        {
            List <int>        missingModIds = new List <int>(modIds);
            List <ModProfile> modProfiles   = new List <ModProfile>(missingModIds.Count);

            foreach (ModProfile profile in CacheClient.AllModProfiles())
            {
                if (missingModIds.Contains(profile.id))
                {
                    missingModIds.Remove(profile.id);
                    modProfiles.Add(profile);
                }
            }

            if (missingModIds.Count == 0)
            {
                if (onSuccess != null)
                {
                    onSuccess(modProfiles);
                }
            }
            else
            {
                // - Filter -
                RequestFilter modFilter = new RequestFilter();
                modFilter.sortFieldName = GetAllModsFilterFields.id;
                modFilter.fieldFilters[GetAllModsFilterFields.id]
                    = new InArrayFilter <int>()
                    {
                    filterArray = missingModIds.ToArray()
                    };

                Action <List <ModProfile> > onGetMods = (profiles) =>
                {
                    modProfiles.AddRange(profiles);

                    CacheClient.SaveModProfiles(profiles);

                    if (onSuccess != null)
                    {
                        onSuccess(modProfiles);
                    }
                };

                // - Get All Events -
                ModManager.FetchAllResultsForQuery <ModProfile>((p, s, e) => APIClient.GetAllMods(modFilter, p, s, e),
                                                                onGetMods,
                                                                onError);
            }
        }
Exemple #2
0
        public static void GetAuthenticatedUserMods(Action <List <ModProfile> > onSuccess,
                                                    Action <WebRequestError> onError)
        {
            List <int> cachedModIds = CacheClient.LoadAuthenticatedUserMods();

            if (cachedModIds != null)
            {
                ModManager.GetModProfiles(cachedModIds,
                                          onSuccess,
                                          onError);
            }
            else
            {
                RequestFilter userModsFilter = new RequestFilter();
                userModsFilter.fieldFilters[GetUserModFilterFields.gameId]
                    = new EqualToFilter <int>()
                    {
                    filterValue = APIClient.gameId
                    };

                Action <List <ModProfile> > onGetMods = (modProfiles) =>
                {
                    CacheClient.SaveModProfiles(modProfiles);

                    List <int> modIds = new List <int>(modProfiles.Count);
                    foreach (ModProfile profile in modProfiles)
                    {
                        modIds.Add(profile.id);
                    }
                    CacheClient.SaveAuthenticatedUserMods(modIds);

                    if (onSuccess != null)
                    {
                        onSuccess(modProfiles);
                    }
                };

                // - Get All Events -
                ModManager.FetchAllResultsForQuery <ModProfile>((p, s, e) => APIClient.GetUserMods(userModsFilter, p, s, e),
                                                                onGetMods,
                                                                onError);
            }
        }
Exemple #3
0
        public static void FetchAllUserEvents(int fromTimeStamp,
                                              int untilTimeStamp,
                                              Action <List <UserEvent> > onSuccess,
                                              Action <WebRequestError> onError)
        {
            // - Filter -
            RequestFilter userEventFilter = new RequestFilter();

            userEventFilter.sortFieldName = GetUserEventsFilterFields.dateAdded;
            userEventFilter.fieldFilters[GetUserEventsFilterFields.dateAdded]
                = new RangeFilter <int>()
                {
                min            = fromTimeStamp,
                isMinInclusive = false,
                max            = untilTimeStamp,
                isMaxInclusive = true,
                };

            // - Get All Events -
            ModManager.FetchAllResultsForQuery <UserEvent>((p, s, e) => APIClient.GetUserEvents(userEventFilter, p, s, e),
                                                           onSuccess,
                                                           onError);
        }
Exemple #4
0
        public static void ApplyModEventsToCache(IEnumerable <ModEvent> modEvents,
                                                 Action <List <ModProfile> > profilesAvailableCallback     = null,
                                                 Action <List <ModProfile> > profilesEditedCallback        = null,
                                                 Action <List <int> > profilesUnavailableCallback          = null,
                                                 Action <List <int> > profilesDeletedCallback              = null,
                                                 Action <List <ModfileStub> > profileBuildsUpdatedCallback = null,
                                                 Action onSuccess = null,
                                                 Action <WebRequestError> onError = null)
        {
            List <int> addedIds          = new List <int>();
            List <int> editedIds         = new List <int>();
            List <int> modfileChangedIds = new List <int>();
            List <int> removedIds        = new List <int>();
            List <int> deletedIds        = new List <int>();

            // Sort by event type
            foreach (ModEvent modEvent in modEvents)
            {
                switch (modEvent.eventType)
                {
                case ModEventType.ModAvailable:
                {
                    addedIds.Add(modEvent.modId);
                }
                break;

                case ModEventType.ModEdited:
                {
                    editedIds.Add(modEvent.modId);
                }
                break;

                case ModEventType.ModfileChanged:
                {
                    modfileChangedIds.Add(modEvent.modId);
                }
                break;

                case ModEventType.ModUnavailable:
                {
                    removedIds.Add(modEvent.modId);
                }
                break;

                case ModEventType.ModDeleted:
                {
                    deletedIds.Add(modEvent.modId);
                }
                break;
                }
            }

            // --- Process Add/Edit/ModfileChanged ---
            List <int> modsToFetch = new List <int>(addedIds.Count + editedIds.Count + modfileChangedIds.Count);

            modsToFetch.AddRange(addedIds);
            modsToFetch.AddRange(editedIds);
            modsToFetch.AddRange(modfileChangedIds);

            if (modsToFetch.Count > 0)
            {
                // - Filter & Pagination -
                RequestFilter modsFilter = new RequestFilter();
                modsFilter.fieldFilters[GetAllModsFilterFields.id]
                    = new InArrayFilter <int>()
                    {
                    filterArray = modsToFetch.ToArray(),
                    };
                // - Get Mods -
                Action <List <ModProfile> > onGetMods = (updatedProfiles) =>
                {
                    // - Create Update Lists -
                    List <ModProfile>  addedProfiles       = new List <ModProfile>(addedIds.Count);
                    List <ModProfile>  editedProfiles      = new List <ModProfile>(editedIds.Count);
                    List <ModfileStub> modfileChangedStubs = new List <ModfileStub>(modfileChangedIds.Count);

                    foreach (ModProfile profile in updatedProfiles)
                    {
                        int idIndex;
                        // NOTE(@jackson): If added, ignore everything else
                        if ((idIndex = addedIds.IndexOf(profile.id)) >= 0)
                        {
                            addedIds.RemoveAt(idIndex);
                            addedProfiles.Add(profile);
                        }
                        else
                        {
                            if ((idIndex = editedIds.IndexOf(profile.id)) >= 0)
                            {
                                editedIds.RemoveAt(idIndex);
                                editedProfiles.Add(profile);
                            }
                            if ((idIndex = modfileChangedIds.IndexOf(profile.id)) >= 0)
                            {
                                modfileChangedIds.RemoveAt(idIndex);
                                modfileChangedStubs.Add(profile.activeBuild);
                            }
                        }
                    }

                    // - Save changed to cache -
                    CacheClient.SaveModProfiles(updatedProfiles);

                    // --- Notifications ---
                    if (profilesAvailableCallback != null &&
                        addedProfiles.Count > 0)
                    {
                        profilesAvailableCallback(addedProfiles);
                    }

                    if (profilesEditedCallback != null &&
                        editedProfiles.Count > 0)
                    {
                        profilesEditedCallback(editedProfiles);
                    }

                    if (profileBuildsUpdatedCallback != null &&
                        modfileChangedStubs.Count > 0)
                    {
                        profileBuildsUpdatedCallback(modfileChangedStubs);
                    }
                };

                ModManager.FetchAllResultsForQuery <ModProfile>((p, s, e) => APIClient.GetAllMods(modsFilter, p, s, e),
                                                                onGetMods,
                                                                null);
            }

            // --- Process Removed ---
            if (removedIds.Count > 0)
            {
                // TODO(@jackson): Compare with subscriptions
                foreach (int modId in removedIds)
                {
                    CacheClient.DeleteMod(modId);
                }

                if (profilesUnavailableCallback != null)
                {
                    profilesUnavailableCallback(removedIds);
                }
            }

            if (deletedIds.Count > 0)
            {
                // TODO(@jackson): Check install state
                foreach (int modId in deletedIds)
                {
                    CacheClient.DeleteMod(modId);
                }

                if (profilesDeletedCallback != null)
                {
                    profilesDeletedCallback(deletedIds);
                }
            }
        }
        /// <summary>Pulls the subscriptions from the server and stores the changes.</summary>
        public static void PullSubscriptionChanges(Action <List <ModProfile> > onSuccess,
                                                   Action <WebRequestError> onError)
        {
            // early out
            if (LocalUser.AuthenticationState == AuthenticationState.NoToken)
            {
                if (onSuccess != null)
                {
                    onSuccess(new List <ModProfile>(0));
                }
                return;
            }

            // holding vars
            string            userToken = LocalUser.OAuthToken;
            List <ModProfile> remoteOnlySubscriptions = new List <ModProfile>();

            // set filter and initial pagination
            RequestFilter subscriptionFilter = new RequestFilter();

            subscriptionFilter.AddFieldFilter(ModIO.API.GetUserSubscriptionsFilterFields.gameId,
                                              new EqualToFilter <int>(PluginSettings.GAME_ID));

            APIPaginationParameters pagination = new APIPaginationParameters()
            {
                limit  = APIPaginationParameters.LIMIT_MAX,
                offset = 0,
            };

            // define actions
            Action getNextPage = null;
            Action <RequestPage <ModProfile> > onPageReceived = null;
            Action onAllPagesReceived = null;

            getNextPage = () =>
            {
                APIClient.GetUserSubscriptions(subscriptionFilter, pagination,
                                               (response) =>
                {
                    onPageReceived(response);

                    // check if all pages received
                    if (response != null &&
                        response.items != null &&
                        response.items.Length > 0 &&
                        response.resultTotal > response.size + response.resultOffset)
                    {
                        pagination.offset = response.resultOffset + response.size;

                        getNextPage();
                    }
                    else
                    {
                        onAllPagesReceived();

                        if (onSuccess != null)
                        {
                            onSuccess(remoteOnlySubscriptions);
                        }
                    }
                },
                                               (e) =>
                {
                    if (onError != null)
                    {
                        onError(e);
                    }
                });
            };


            onPageReceived = (r) =>
            {
                foreach (ModProfile profile in r.items)
                {
                    if (profile != null)
                    {
                        remoteOnlySubscriptions.Add(profile);
                    }
                }
            };

            onAllPagesReceived = () =>
            {
                if (userToken != LocalUser.OAuthToken)
                {
                    return;
                }

                List <int> localOnlySubs
                    = new List <int>(LocalUser.SubscribedModIds);

                // NOTE(@jackson): Unsub actions *should not* be found in activeUser.subscribedModIds
                foreach (int modId in LocalUser.QueuedUnsubscribes)
                {
                    #if DEBUG
                    if (localOnlySubs.Contains(modId))
                    {
                        Debug.LogWarning("[mod.io] A locally subscribed mod was found in the"
                                         + " queuedUnsubscribes. This should not occur - please"
                                         + " ensure that any mod ids added to"
                                         + " activeUser.queuedUnsubscribes are removed from"
                                         + " activeUser.subscribedModIds or use"
                                         + " UserAccountManagement.UnsubscribeFromMod() to handle"
                                         + " this automatically.");
                    }
                    #endif

                    localOnlySubs.Remove(modId);
                }

                List <int> newSubs = new List <int>();

                // build new subs list
                for (int i = 0; i < remoteOnlySubscriptions.Count; ++i)
                {
                    ModProfile profile = remoteOnlySubscriptions[i];

                    // remove if in queued subs
                    LocalUser.QueuedSubscribes.Remove(profile.id);

                    // if in unsub queue
                    if (LocalUser.QueuedUnsubscribes.Contains(profile.id))
                    {
                        remoteOnlySubscriptions.RemoveAt(i);
                        --i;
                    }
                    // if locally subbed
                    else if (localOnlySubs.Remove(profile.id))
                    {
                        remoteOnlySubscriptions.RemoveAt(i);
                        --i;
                    }
                    // if not locally subbed && if not in unsub queue
                    else
                    {
                        newSubs.Add(profile.id);
                    }
                }

                // -- update locally --
                // remove new unsubs
                foreach (int modId in localOnlySubs)
                {
                    // if not in sub queue
                    if (!LocalUser.QueuedSubscribes.Contains(modId))
                    {
                        LocalUser.SubscribedModIds.Remove(modId);
                    }
                }

                LocalUser.SubscribedModIds.AddRange(newSubs);

                // save
                LocalUser.Save();
            };

            // get pages
            getNextPage();
        }