Ejemplo n.º 1
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);
                }
            }
        }