Beispiel #1
0
        /// <summary>Requests the release history for a mod.</summary>
        public void RequestReleaseHistory(int modId)
        {
            this.m_modId = modId;

            if (this.isActiveAndEnabled &&
                modId != this.m_requestedModId)
            {
                this.m_requestedModId = modId;

                // set item count
                int itemCount = this.container.itemLimit;
                if (itemCount < 0)
                {
                    itemCount = APIPaginationParameters.LIMIT_MAX;
                }

                container.DisplayModfiles(null);

                // pagination
                var pagination = new APIPaginationParameters()
                {
                    offset = 0,
                    limit  = itemCount,
                };

                // filter
                RequestFilter filter = new RequestFilter()
                {
                    sortFieldName   = ModIO.API.GetAllModfilesFilterFields.dateAdded,
                    isSortAscending = !this.reverseChronological,
                };

                // fetch
                APIClient.GetAllModfiles(modId, filter, pagination,
                                         (r) =>
                {
                    if (this != null &&
                        modId == this.m_modId)
                    {
                        this.container.DisplayModfiles(r.items);
                    }
                },
                                         null);
            }
        }
        // ---------[ UTILITY ]---------
        /// <summary>Recursively fetches all of the mod profiles in the array.</summary>
        protected System.Collections.IEnumerator FetchAllModProfiles(int[] modIds,
                                                                     Action <List <ModProfile> > onSuccess,
                                                                     Action <WebRequestError> onError)
        {
            List <ModProfile> modProfiles = new List <ModProfile>();

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

            // filter
            RequestFilter filter = new RequestFilter();

            filter.AddFieldFilter(API.GetAllModsFilterFields.id,
                                  new InArrayFilter <int>()
            {
                filterArray = modIds,
            });

            bool isDone = false;

            while (!isDone)
            {
                RequestPage <ModProfile> page  = null;
                WebRequestError          error = null;

                APIClient.GetAllMods(filter, pagination,
                                     (r) => page  = r,
                                     (e) => error = e);

                while (page == null && error == null)
                {
                    yield return(null);
                }

                if (error != null)
                {
                    if (onError != null)
                    {
                        onError(error);
                    }

                    modProfiles = null;
                    isDone      = true;
                }
                else
                {
                    modProfiles.AddRange(page.items);

                    if (page.resultTotal <= (page.resultOffset + page.size))
                    {
                        isDone = true;
                    }
                    else
                    {
                        pagination.offset = page.resultOffset + page.size;
                    }
                }
            }

            if (isDone && modProfiles != null)
            {
                onSuccess(modProfiles);
            }
        }
        // ---------[ FUNCTIONALITY ]---------
        /// <summary>Fetches page of ModProfiles grabbing from the cache where possible.</summary>
        public virtual void FetchModProfilePage(RequestFilter filter, int resultOffset, int profileCount,
                                                Action <RequestPage <ModProfile> > onSuccess,
                                                Action <WebRequestError> onError)
        {
            Debug.Assert(onSuccess != null);
            Debug.Assert(this.minimumFetchSize <= APIPaginationParameters.LIMIT_MAX);

            if (profileCount > APIPaginationParameters.LIMIT_MAX)
            {
                Debug.LogWarning("[mod.io] FetchModProfilePage has been called with a profileCount"
                                 + " larger than the APIPaginationParameters.LIMIT_MAX."
                                 + "\nAs such, results may not be as expected.");

                profileCount = APIPaginationParameters.LIMIT_MAX;
            }

            // ensure indicies are positive
            if (resultOffset < 0)
            {
                resultOffset = 0;
            }
            if (profileCount < 0)
            {
                profileCount = 0;
            }

            // check if results already cached
            string          filterString = filter.GenerateFilterString();
            RequestPageData cachedPage;

            if (this.requestCache.TryGetValue(filterString, out cachedPage))
            {
                List <int> requestModIds = new List <int>(profileCount);

                int cachedPageOffset = resultOffset - cachedPage.resultOffset;

                if (cachedPageOffset >= 0)
                {
                    int expectedIdCount = profileCount;
                    if (profileCount + resultOffset > cachedPage.resultTotal)
                    {
                        expectedIdCount = cachedPage.resultTotal - resultOffset;
                    }

                    for (int i = 0;
                         i < profileCount &&
                         i + cachedPageOffset < cachedPage.modIds.Length;
                         ++i)
                    {
                        requestModIds.Add(cachedPage.modIds[i + cachedPageOffset]);
                    }

                    if (expectedIdCount == requestModIds.Count)
                    {
                        RequestPage <ModProfile> requestPage = new RequestPage <ModProfile>()
                        {
                            size         = profileCount,
                            resultOffset = resultOffset,
                            resultTotal  = cachedPage.resultTotal,
                            items        = this.PullProfilesFromCache(requestModIds),
                        };

                        bool isPageComplete = true;
                        for (int i = 0;
                             i < requestPage.items.Length &&
                             isPageComplete;
                             ++i)
                        {
                            isPageComplete = (requestPage.items[i] != null);
                        }


                        if (isPageComplete)
                        {
                            onSuccess(requestPage);
                            return;
                        }
                    }
                }
            }

            // PaginationParameters
            APIPaginationParameters pagination = new APIPaginationParameters();

            pagination.offset = resultOffset;
            pagination.limit  = profileCount;
            if (profileCount < this.minimumFetchSize)
            {
                pagination.limit = this.minimumFetchSize;
            }

            // Send Request
            APIClient.GetAllMods(filter, pagination,
                                 (r) =>
            {
                if (this != null)
                {
                    this.CacheRequestPage(filter, r);
                }

                if (onSuccess != null)
                {
                    if (pagination.limit != profileCount)
                    {
                        var subPage = ModProfileRequestManager.CreatePageSubset(r,
                                                                                resultOffset,
                                                                                profileCount);
                        onSuccess(subPage);
                    }
                    else
                    {
                        onSuccess(r);
                    }
                }
            }, onError);
        }
Beispiel #4
0
        // ---------[ FUNCTIONALITY ]---------
        /// <summary>Fetches page of ModProfiles grabbing from the cache where possible.</summary>
        public virtual void FetchModProfilePage(RequestFilter filter, int resultOffset, int profileCount,
                                                Action <RequestPage <ModProfile> > onSuccess,
                                                Action <WebRequestError> onError)
        {
            Debug.Assert(this.minimumFetchSize <= APIPaginationParameters.LIMIT_MAX);

            // early out if onSuccess == null
            if (onSuccess == null && onError == null)
            {
                return;
            }

            if (profileCount > APIPaginationParameters.LIMIT_MAX)
            {
                Debug.LogWarning("[mod.io] FetchModProfilePage has been called with a profileCount"
                                 + " larger than the APIPaginationParameters.LIMIT_MAX."
                                 + "\nAs such, results may not be as expected.");

                profileCount = APIPaginationParameters.LIMIT_MAX;
            }

            // ensure indicies are positive
            if (resultOffset < 0)
            {
                resultOffset = 0;
            }
            if (profileCount < 0)
            {
                profileCount = 0;
            }

            // setup request structures
            List <ModProfile> results = new List <ModProfile>(profileCount);

            // PaginationParameters
            APIPaginationParameters pagination = new APIPaginationParameters();

            int pageIndex = resultOffset / this.minimumFetchSize;

            pagination.offset = pageIndex * this.minimumFetchSize;
            pagination.limit  = this.minimumFetchSize;

            APIClient.GetAllMods(filter, pagination,
                                 (r01) =>
            {
                int pageOffset = resultOffset % this.minimumFetchSize;

                for (int i = pageOffset;
                     i < r01.items.Length &&
                     i < pageOffset + profileCount;
                     ++i)
                {
                    results.Add(r01.items[i]);
                }

                if (pageOffset + profileCount > r01.size &&
                    r01.items.Length == r01.size)
                {
                    pagination.offset += pagination.limit;
                    APIClient.GetAllMods(filter, pagination,
                                         (r02) =>
                    {
                        for (int i = 0;
                             i < r02.items.Length &&
                             i < pageOffset + profileCount - r02.size;
                             ++i)
                        {
                            results.Add(r02.items[i]);
                            OnModsReceived(resultOffset, profileCount, r02.resultTotal,
                                           results, onSuccess);
                        }
                    }, onError);
                }
                else
                {
                    OnModsReceived(resultOffset, profileCount, r01.resultTotal,
                                   results, onSuccess);
                }
            }, onError);
        }