public static async Task ReFetchFavorites() { ImportRunning = true; ImportStatusOuter = "Re-fetch running..."; var database = FavCatMod.Database; ImportStatusOuter = "Fetching worlds..."; var worldFavs = database.WorldFavorites.myStoredFavorites.FindAll().ToList(); for (var i = 0; i < worldFavs.Count; i++) { ImportStatusInner = i + "/" + worldFavs.Count; var storedFavorite = worldFavs[i]; if (database.myStoredWorlds.FindById(storedFavorite.ObjectId) != null) { continue; } await FavCatMod.YieldToMainThread(); new ApiWorld { id = storedFavorite.ObjectId }.Fetch(null, new Action <ApiContainer>(c => { if (c.Code == 404) { database.CompletelyDeleteWorld(storedFavorite.ObjectId); } })); await Task.Delay(TimeSpan.FromSeconds(5f + Random.Range(0f, 5f))).ConfigureAwait(false); } var canShowFavorites = DateTime.Now < FavCatMod.NoMoreVisibleAvatarFavoritesAfter; if (canShowFavorites) { ImportStatusOuter = "Fetching avatars..."; var avatarFavs = database.AvatarFavorites.myStoredFavorites.FindAll().ToList(); for (var i = 0; i < avatarFavs.Count; i++) { ImportStatusInner = i + "/" + avatarFavs.Count; var storedFavorite = avatarFavs[i]; if (database.myStoredAvatars.FindById(storedFavorite.ObjectId) != null) { continue; } await FavCatMod.YieldToMainThread(); new ApiAvatar { id = storedFavorite.ObjectId }.Fetch(null, new Action <ApiContainer>(c => { if (c.Code == 404) { database.CompletelyDeleteAvatar(storedFavorite.ObjectId); } })); await Task.Delay(TimeSpan.FromSeconds(5f + Random.Range(0f, 5f))).ConfigureAwait(false); } } ImportStatusOuter = "Fetching players..."; var playerFavs = database.PlayerFavorites.myStoredFavorites.FindAll().ToList(); for (var i = 0; i < playerFavs.Count; i++) { ImportStatusInner = i + "/" + playerFavs.Count; var storedFavorite = playerFavs[i]; if (database.myStoredPlayers.FindById(storedFavorite.ObjectId) != null) { continue; } await FavCatMod.YieldToMainThread(); new APIUser { id = storedFavorite.ObjectId }.Fetch(new Action <ApiContainer>(c => { if (c.Code == 404) { database.CompletelyDeletePlayer(storedFavorite.ObjectId); } })); await Task.Delay(TimeSpan.FromSeconds(5f + Random.Range(0f, 5f))).ConfigureAwait(false); } ImportRunning = false; }
internal static async Task ProcessTextFile(string filePath) { var fileName = Path.GetFileName(filePath); MelonLogger.Log($"Started avatar import process for file {fileName}"); var toAdd = new List <string>(); { // file access block using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read); using var reader = new StreamReader(file); string line; while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null) { var matches = AvatarIdRegex.Matches(line); foreach (Match match in matches) { var avatarId = match.Value; toAdd.Add(avatarId); } } } for (var i = 0; i < toAdd.Count; i++) { ImportStatusInner = $"Fetching avatar {i + 1}/{toAdd.Count}"; var avatarId = toAdd[i]; if (FavCatMod.Database.myStoredAvatars.FindById(avatarId) == null) { await FavCatMod.YieldToMainThread(); new ApiAvatar { id = avatarId }.Fetch(); // it will get intercepted and stored await Task.Delay(TimeSpan.FromSeconds(5f + Random.Range(0f, 5f))).ConfigureAwait(false); } } ImportStatusInner = "Creating favorites list"; await FavCatMod.YieldToMainThread(); var userId = APIUser.CurrentUser.id; var categoryName = $"Imported from {fileName}"; var existingCategory = FavCatMod.Database.AvatarFavorites.GetCategory(categoryName); foreach (var avatarId in toAdd) { if (FavCatMod.Database.AvatarFavorites.IsFavorite(avatarId, categoryName)) { continue; } var storedAvatar = FavCatMod.Database.myStoredAvatars.FindById(avatarId); if (storedAvatar == null || storedAvatar.ReleaseStatus != "public" && storedAvatar.AuthorId != userId) { continue; } FavCatMod.Database.AvatarFavorites.AddFavorite(avatarId, categoryName); } if (existingCategory == null) { existingCategory = new StoredCategory { CategoryName = categoryName, SortType = "!added" }; FavCatMod.Database.AvatarFavorites.UpdateCategory(existingCategory); var avatarModule = FavCatMod.Instance.AvatarModule !; avatarModule.CreateList(existingCategory); avatarModule.ReorderLists(); avatarModule.RefreshFavButtons(); } MelonLogger.Log($"Done importing {fileName}"); File.Delete(filePath); }