// ---------[ INITIALIZATION ]--------- protected virtual void Awake() { if (ModProfileRequestManager._instance == null) { ModProfileRequestManager._instance = this; } #if DEBUG else if (ModProfileRequestManager._instance != this) { Debug.LogWarning("[mod.io] Second instance of a ModProfileRequestManager" + " component enabled simultaneously." + " Only one instance of a ModProfileRequestManager" + " component should be active at a time."); this.enabled = false; } #endif }
// ---------[ 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); }