<commit_before>﻿using HamstarHelpers.Components.Errors;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Helpers.DotNetHelpers.Reflection;
using HamstarHelpers.Helpers.TmlHelpers.Menus;
using HamstarHelpers.Internals.WebRequests;
using HamstarHelpers.Services.Promises;
using System;
using System.Collections.Generic;
using Terraria.ModLoader.UI;
using Terraria.UI;


namespace HamstarHelpers.Internals.Menus.ModTags {
	partial class ModBrowserTagsMenuContext : TagsMenuContextBase {
		internal void FilterMods() {
			IList<string> modNames = new List<string>();
			UIState myUI = this.MyUI;

			object items;
			if( !ReflectionHelpers.Get( myUI, "items", out items ) ) {
				throw new HamstarException( "No 'items' field in ui " + myUI );
			}

			var itemsArr = (Array)items.GetType()
				.GetMethod( "ToArray" )
				.Invoke( items, new object[] { } );

			for( int i = 0; i < itemsArr.Length; i++ ) {
				object item = itemsArr.GetValue( i );
				string modName;

				if( ReflectionHelpers.Get( item, "mod", out modName ) ) {
					modNames.Add( modName );
				}
			}

			this.FilterModsAsync( modNames, ( isFiltered, filteredModNameList, onTagCount, offTagCount ) => {
				string filterName = "Tags";
				if( onTagCount > 0 || offTagCount > 0 ) {
					filterName += " ";

					if( onTagCount > 0 ) {
						filterName += "+" + onTagCount;
						if( offTagCount > 0 ) {
							filterName += " ";
						}
					}
					if( offTagCount > 0 ) {
						filterName += "-" + offTagCount;
					}
				}

				ReflectionHelpers.Set( myUI, "updateFilterMode", (UpdateFilter)0 );
				ModMenuHelpers.ApplyModBrowserFilter( filterName, isFiltered, (List<string>)filteredModNameList );
			} );
		}


		////////////////

		public void FilterModsAsync( IList<string> modNames, Action<bool, IList<string>, int, int> callback ) {
			Promises.AddValidatedPromise<ModTagsPromiseArguments>( GetModTags.TagsReceivedPromiseValidator, ( args ) => {
				if( !args.Found ) {
					this.InfoDisplay?.SetText( "Could not acquire mod data." );
					callback( false, new List<string>(), 0, 0 );
					return false;
				}
				
				IList<string> filteredModNameList = new List<string>();
				ISet<string> onTags = this.GetTagsOfState( 1 );
				ISet<string> offTags = this.GetTagsOfState( -1 );
				bool isFiltered = onTags.Count > 0 || offTags.Count > 0;

				if( isFiltered ) {
					foreach( string modName in modNames ) {
						if( onTags.Count > 0 && !args.ModTags.ContainsKey( modName ) ) { continue; }

						ISet<string> modHasTags = args.ModTags[modName];

						if( modHasTags.Overlaps( offTags ) ) { continue; }
						if( onTags.Count > 0 && !modHasTags.IsSupersetOf( onTags ) ) { continue; }

						filteredModNameList.Add( modName );
					}
				}

				if( ModHelpersMod.Instance.Config.DebugModeHelpersInfo ) {
					LogHelpers.Log( "Filtered to "+filteredModNameList.Count+" mods."
						+(onTags.Count > 0 ? "\nWith tags: "+string.Join(", ",onTags) : "")
						+(offTags.Count > 0 ? "\nWithout tags: "+string.Join(", ",offTags) : "")
					);
				}

				callback( isFiltered, filteredModNameList, onTags.Count, offTags.Count );

				return false;
			} );
		}
	}
}
<commit_msg>v4.4.0 * Fixed mod browser unfilters-only fix<commit_after>﻿using HamstarHelpers.Components.Errors;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Helpers.DotNetHelpers.Reflection;
using HamstarHelpers.Helpers.TmlHelpers.Menus;
using HamstarHelpers.Internals.WebRequests;
using HamstarHelpers.Services.Promises;
using System;
using System.Collections.Generic;
using Terraria.ModLoader.UI;
using Terraria.UI;


namespace HamstarHelpers.Internals.Menus.ModTags {
	partial class ModBrowserTagsMenuContext : TagsMenuContextBase {
		internal void FilterMods() {
			IList<string> modNames = new List<string>();
			UIState myUI = this.MyUI;

			object items;
			if( !ReflectionHelpers.Get( myUI, "items", out items ) ) {
				throw new HamstarException( "No 'items' field in ui " + myUI );
			}

			var itemsArr = (Array)items.GetType()
				.GetMethod( "ToArray" )
				.Invoke( items, new object[] { } );

			for( int i = 0; i < itemsArr.Length; i++ ) {
				object item = itemsArr.GetValue( i );
				string modName;

				if( ReflectionHelpers.Get( item, "mod", out modName ) ) {
					modNames.Add( modName );
				}
			}

			this.FilterModsAsync( modNames, ( isFiltered, filteredModNameList, onTagCount, offTagCount ) => {
				string filterName = "Tags";
				if( onTagCount > 0 || offTagCount > 0 ) {
					filterName += " ";

					if( onTagCount > 0 ) {
						filterName += "+" + onTagCount;
						if( offTagCount > 0 ) {
							filterName += " ";
						}
					}
					if( offTagCount > 0 ) {
						filterName += "-" + offTagCount;
					}
				}

				ReflectionHelpers.Set( myUI, "updateFilterMode", (UpdateFilter)0 );
				ModMenuHelpers.ApplyModBrowserFilter( filterName, isFiltered, (List<string>)filteredModNameList );
			} );
		}


		////////////////

		public void FilterModsAsync( IList<string> modNames, Action<bool, IList<string>, int, int> callback ) {
			Promises.AddValidatedPromise<ModTagsPromiseArguments>( GetModTags.TagsReceivedPromiseValidator, ( args ) => {
				if( !args.Found ) {
					this.InfoDisplay?.SetText( "Could not acquire mod data." );
					callback( false, new List<string>(), 0, 0 );
					return false;
				}

				this.FilterMods_Promised( modNames, callback, args.ModTags );
				return false;
			} );
		}


		private void FilterMods_Promised( IList<string> modNames, Action<bool, IList<string>, int, int> callback,
				IDictionary<string, ISet<string>> modTags ) {
			IList<string> filteredModNameList = new List<string>();
			ISet<string> onTags = this.GetTagsOfState( 1 );
			ISet<string> offTags = this.GetTagsOfState( -1 );
			bool isFiltered = onTags.Count > 0 || offTags.Count > 0;

			if( isFiltered ) {
				foreach( string modName in modNames ) {
					if( modTags.ContainsKey( modName ) ) {
						ISet<string> modHasTags = modTags[modName];

						if( modHasTags.Overlaps( offTags ) ) { continue; }
						if( onTags.Count > 0 && !modHasTags.IsSupersetOf( onTags ) ) { continue; }
					} else if( onTags.Count > 0 ) {
						continue;
					}

					filteredModNameList.Add( modName );
				}
			}

			if( ModHelpersMod.Instance.Config.DebugModeHelpersInfo ) {
				LogHelpers.Log( "Filtered to " + filteredModNameList.Count + " mods."
					+ ( onTags.Count > 0 ? "\nWith tags: " + string.Join( ", ", onTags ) : "" )
					+ ( offTags.Count > 0 ? "\nWithout tags: " + string.Join( ", ", offTags ) : "" )
				);
			}

			callback( isFiltered, filteredModNameList, onTags.Count, offTags.Count );
		}
	}
}

{'fraction_non_alphanumeric': 0.13745080230093853, 'fraction_numerical': 0.005600968816227672, 'mean_word_length': 3.2625806451612904, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1784085', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2172, 'n_words': 466}